Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from vimmerru/feature/api-proposl
Browse files Browse the repository at this point in the history
Feature/api proposl
  • Loading branch information
Vyacheslav authored Apr 13, 2017
2 parents 549b210 + 548c3e6 commit 920eb1d
Show file tree
Hide file tree
Showing 30 changed files with 496 additions and 191 deletions.
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ services:
dockerfile: ci/ubuntu.dockerfile
command: cargo test --color=always
volumes:
- "./src:/home/sorvin-client-rust/src"
- "./Cargo.toml:/home/sorvin-client-rust/Cargo.toml"
- "./target:/home/sorvin-client-rust/target"
- ".:/home/sovrin/sovrin-client-rust"
working_dir: /home/sovrin/sovrin-client-rust
2 changes: 1 addition & 1 deletion src/api/anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate libc;

use api::ErrorCode;

use self::libc::{c_char, c_uchar};
use self::libc::c_char;

/// Create keys (both primary and revocation) for the given schema and stores the keys
/// in a secure wallet. The public key in the wallet is identifying by a returned unique key.
Expand Down
2 changes: 1 addition & 1 deletion src/api/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate libc;

use api::ErrorCode;

use self::libc::{c_char, c_uchar};
use self::libc::c_char;

/// Signs and submits request message to validator pool.
///
Expand Down
27 changes: 16 additions & 11 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ pub mod ledger;
pub mod pool;
pub mod wallet;


use self::libc::{c_char};

#[derive(Debug, PartialEq, Copy, Clone)]
#[repr(i32)]
pub enum ErrorCode {
Success = 0,

// Common errors
// Caller passed invalid pool ledger handle
CommonInvalidPoolLedgerHandle = 100,

// Caller passed invalid wallet handle
CommonInvalidWalletHandle,

// Caller passed invalid value as param 1 (null, invalid json and etc..)
CommonInvalidParam1,
CommonInvalidParam1 = 100,

// Caller passed invalid value as param 2 (null, invalid json and etc..)
CommonInvalidParam2,
Expand All @@ -39,6 +32,9 @@ pub enum ErrorCode {
CommonInvalidState,

// Wallet errors
// Caller passed invalid wallet handle
WalletInvalidHandle,

// Unknown type of wallet was passed on create_wallet
WalletUnknownTypeError = 200,

Expand All @@ -58,11 +54,20 @@ pub enum ErrorCode {
WalletIncompatiblePoolError,

// Ledger errors
// Trying to open pool ledger that wasn't created before
PoolLedgerNotCreatedError = 300,

// Invalid pool ledger configuration was passed to open_pool_ledger or create_pool_ledger
PoolLedgerInvalidConfiguration,

// Pool ledger files referenced in open_pool_ledger have invalid data format
PoolLedgerInvalidDataFormat = 300,
PoolLedgerInvalidDataFormat,

// Caller passed invalid pool ledger handle
PoolLedgerInvalidPoolHandle,

// IO error during access pool ledger files
PoolILedgerOError,
PoolLedgerIOError,

// No concensus during ledger operation
LedgerNoConsensusError,
Expand Down
94 changes: 83 additions & 11 deletions src/api/pool.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
extern crate libc;

use api::ErrorCode;
use commands::{Command, CommandExecutor};
use commands::pool::PoolCommand;
use errors::ToErrorCode;
use utils::cstring::CStringUtils;

use self::libc::{c_char, c_uchar};
use self::libc::c_char;

/// Creates a new local pool ledger that can be used later to connect pool nodes.
///
Expand All @@ -24,8 +28,22 @@ use self::libc::{c_char, c_uchar};
pub extern fn sovrin_create_pool_ledger(command_handle: i32,
name: *const c_char,
config: *const c_char,
cb: extern fn(xcommand_handle: i32, err: ErrorCode)) -> ErrorCode {
unimplemented!();
cb: Option<extern fn(xcommand_handle: i32, err: ErrorCode)>) -> ErrorCode {
check_useful_c_str!(name, ErrorCode::CommonInvalidParam2);
check_useful_opt_c_str!(config, ErrorCode::CommonInvalidParam3);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam4);

let result = CommandExecutor::instance()
.send(Command::Pool(PoolCommand::Create(
name,
config,
Box::new(move |result| {
let err = result_to_err_code!(result);
cb(command_handle, err)
})
)));

result_to_err_code!(result)
}

/// Opens pool ledger and performs connecting to pool nodes.
Expand Down Expand Up @@ -55,8 +73,28 @@ pub extern fn sovrin_create_pool_ledger(command_handle: i32,
pub extern fn sovrin_open_pool_ledger(command_handle: i32,
name: *const c_char,
config: *const c_char,
cb: extern fn(xcommand_handle: i32, err: ErrorCode, pool_handle: i32)) -> ErrorCode {
unimplemented!();
cb: Option<extern fn(xcommand_handle: i32, err: ErrorCode, pool_handle: i32)>) -> ErrorCode {
check_useful_c_str!(name, ErrorCode::CommonInvalidParam2);
check_useful_opt_c_str!(config, ErrorCode::CommonInvalidParam3);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam4);

let result = CommandExecutor::instance()
.send(Command::Pool(PoolCommand::Open(
name,
config,
Box::new(move |result| {
let err = result_to_err_code!(result);

let pool_handle = match result {
Ok(pool_handle) => pool_handle,
Err(err) => 0
};

cb(command_handle, err, pool_handle)
})
)));

result_to_err_code!(result)
}

/// Refreshes a local copy of a pool ledger and updates pool nodes connections.
Expand All @@ -73,8 +111,19 @@ pub extern fn sovrin_open_pool_ledger(command_handle: i32,
#[no_mangle]
pub extern fn sovrin_refresh_pool_ledger(command_handle: i32,
handle: i32,
cb: extern fn(xcommand_handle: i32, err: ErrorCode)) -> ErrorCode {
unimplemented!();
cb: Option<extern fn(xcommand_handle: i32, err: ErrorCode)>) -> ErrorCode {
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam3);

let result = CommandExecutor::instance()
.send(Command::Pool(PoolCommand::Refresh(
handle,
Box::new(move |result| {
let err = result_to_err_code!(result);
cb(command_handle, err)
})
)));

result_to_err_code!(result)
}

/// Closes opened pool ledger, opened nodes connections and frees allocated resources.
Expand All @@ -91,8 +140,19 @@ pub extern fn sovrin_refresh_pool_ledger(command_handle: i32,
#[no_mangle]
pub extern fn sovrin_close_pool_ledger(command_handle: i32,
handle: i32,
cb: extern fn(xcommand_handle: i32, err: ErrorCode)) -> ErrorCode {
unimplemented!();
cb: Option<extern fn(xcommand_handle: i32, err: ErrorCode)>) -> ErrorCode {
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam3);

let result = CommandExecutor::instance()
.send(Command::Pool(PoolCommand::Close(
handle,
Box::new(move |result| {
let err = result_to_err_code!(result);
cb(command_handle, err)
})
)));

result_to_err_code!(result)
}

/// Deletes created pool ledger.
Expand All @@ -109,6 +169,18 @@ pub extern fn sovrin_close_pool_ledger(command_handle: i32,
#[no_mangle]
pub extern fn sovrin_delete_pool_ledger(command_handle: i32,
name: *const c_char,
cb: extern fn(xcommand_handle: i32, err: ErrorCode)) -> ErrorCode {
unimplemented!();
cb: Option<extern fn(xcommand_handle: i32, err: ErrorCode)>) -> ErrorCode {
check_useful_c_str!(name, ErrorCode::CommonInvalidParam2);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam3);

let result = CommandExecutor::instance()
.send(Command::Pool(PoolCommand::Delete(
name,
Box::new(move |result| {
let err = result_to_err_code!(result);
cb(command_handle, err)
})
)));

result_to_err_code!(result)
}
2 changes: 1 addition & 1 deletion src/api/signus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate libc;

use api::ErrorCode;

use self::libc::{c_char, c_uchar};
use self::libc::c_char;

/// Creates keys (signing and encryption keys) for a new
/// DID (owned by the caller of the library).
Expand Down
2 changes: 1 addition & 1 deletion src/api/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate libc;

use api::ErrorCode;

use self::libc::{c_char, c_uchar};
use self::libc::c_char;

/// Registers custom wallet implementation.
///
Expand Down
2 changes: 0 additions & 2 deletions src/commands/anoncreds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ mod issuer;

use commands::anoncreds::issuer::{IssuerCommand, IssuerCommandExecutor};

use errors::anoncreds::AnoncredsError;

use services::crypto::CryptoService;
use services::pool::PoolService;
use services::wallet::WalletService;
Expand Down
26 changes: 11 additions & 15 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use commands::pool::{PoolCommand, PoolCommandExecutor};
use commands::signus::{SignusCommand, SignusCommandExecutor};
use commands::wallet::{WalletCommand, WalletCommandExecutor};

use errors::common::CommonError;

use services::crypto::CryptoService;
use services::pool::PoolService;
use services::wallet::WalletService;

use std::error;
use std::error::Error;
use std::sync::mpsc::{Sender, channel};
use std::rc::Rc;
use std::thread;
Expand All @@ -34,16 +36,7 @@ pub struct CommandExecutor {
sender: Sender<Command>
}

/// Global (lazy inited) instance of CommandExecutor
///
/// Sample:
///
/// {
/// ...
/// let ref ce: CommandExecutor = *CommandExecutor::instance(); <- lock +
/// ce.send(Command::Exit); |
/// ... |
/// } <- unlock +
// Global (lazy inited) instance of CommandExecutor
lazy_static! {
static ref COMMAND_EXECUTOR: Mutex<CommandExecutor> = Mutex::new(CommandExecutor::new());
}
Expand Down Expand Up @@ -107,17 +100,20 @@ impl CommandExecutor {
}
}

pub fn send(&self, cmd: Command) {
self.sender.send(cmd);
pub fn send(&self, cmd: Command) -> Result<(), CommonError> {
match self.sender.send(cmd) {
Ok(val) => Ok(()),
Err(ref err) => Err(CommonError::InvalidState(err.description().to_string()))
}
}
}

impl Drop for CommandExecutor {
fn drop(&mut self) {
info!(target: "command_executor", "Drop started");
self.send(Command::Exit);
self.send(Command::Exit).unwrap();
// Option worker type and this kludge is workaround for rust
self.worker.take().unwrap().join();
self.worker.take().unwrap().join().unwrap();
info!(target: "command_executor", "Drop finished");
}
}
Expand Down
27 changes: 16 additions & 11 deletions src/commands/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use std::rc::Rc;

pub enum PoolCommand {
Create(String, // name
String, // config
Option<String>, // config
Box<Fn(Result<(), PoolError>) + Send>),
Delete(String, // name
Box<Fn(Result<(), PoolError>) + Send>),
Open(String, // name
String, // config
Option<String>, // config
Box<Fn(Result<i32, PoolError>) + Send>),
Close(i32, // pool handle
Box<Fn(Result<(), PoolError>) + Send>),
Expand All @@ -35,15 +35,15 @@ impl PoolCommandExecutor {
match command {
PoolCommand::Create(name, config, cb) => {
info!(target: "pool_command_executor", "Create command received");
self.create(&name, &config, cb);
self.create(&name, config.as_ref().map(String::as_str), cb);
},
PoolCommand::Delete(name, cb) => {
info!(target: "pool_command_executor", "Delete command received");
self.delete(&name, cb);
},
PoolCommand::Open(name, config, cb) => {
info!(target: "pool_command_executor", "Open command received");
self.open(&name, &config, cb);
self.open(&name, config.as_ref().map(String::as_str), cb);
},
PoolCommand::Close(handle, cb) => {
info!(target: "pool_command_executor", "Close command received");
Expand All @@ -56,23 +56,28 @@ impl PoolCommandExecutor {
};
}

fn create(&self, name: &str, config: &str, cb: Box<Fn(Result<(), PoolError>) + Send>) {
unimplemented!()
fn create(&self, name: &str, config: Option<&str>, cb: Box<Fn(Result<(), PoolError>) + Send>) {
// TODO: FIXME: Implement me!!!
cb(Ok(()));
}

fn delete(&self, name: &str, cb: Box<Fn(Result<(), PoolError>) + Send>) {
unimplemented!()
// TODO: FIXME: Implement me!!!
cb(Ok(()));
}

fn open(&self, name: &str, config: &str, cb: Box<Fn(Result<i32, PoolError>) + Send>) {
unimplemented!()
fn open(&self, name: &str, config: Option<&str>, cb: Box<Fn(Result<i32, PoolError>) + Send>) {
// TODO: FIXME: Implement me!!!
cb(Ok((1000)));
}

fn close(&self, handle: i32, cb: Box<Fn(Result<(), PoolError>) + Send>) {
unimplemented!()
// TODO: FIXME: Implement me!!!
cb(Ok(()));
}

fn refresh(&self, handle: i32, cb: Box<Fn(Result<(), PoolError>) + Send>) {
unimplemented!()
// TODO: FIXME: Implement me!!!
cb(Ok(()));
}
}
Loading

0 comments on commit 920eb1d

Please sign in to comment.