Skip to content

Commit

Permalink
Split connection state machines files (#115)
Browse files Browse the repository at this point in the history
* Restructure connection state machine files

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Split connection state machines (#120)

* Split connection state machines files

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Inviter conn should also check bootstrap agent for messages. Refactor.

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Connection.connect returns invitation for inviter. Refactor

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Update access modifiers, removed redundant comments

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove unused imports in v3/**

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Optimize imports, reformat code

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Error in connection update state should not be ignored

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove outdated test, fix warnings in connection.rs

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Oct 1, 2020
1 parent 3bf6f91 commit e8ce4b9
Show file tree
Hide file tree
Showing 66 changed files with 3,150 additions and 2,538 deletions.
2 changes: 2 additions & 0 deletions agents/node/vcxagent-core/vcx-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ async function createVcxAgent ({ agentName, protocolType, agencyUrl, seed, webho
const connection = await Connection.createWithInvite({ id, invite })
logger.debug(`InviteeConnectionSM after created from invitation:\n${JSON.stringify(await connection.serialize())}`)
await connection.connect({ data: '{}' })
logger.debug(`InviteeConnectionSM created connection agent`)
await connection.updateState()
logger.debug(`InviteeConnectionSM updated state`)
return connection
}

Expand Down
59 changes: 33 additions & 26 deletions libvcx/src/api/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,38 +308,30 @@ pub extern fn vcx_connection_connect(command_handle: CommandHandle,
return VcxError::from(VcxErrorKind::InvalidConnectionHandle).into();
}

let options = if !connection_options.is_null() {
let _options = if !connection_options.is_null() {
check_useful_opt_c_str!(connection_options, VcxErrorKind::InvalidOption);
connection_options.to_owned()
} else {
None
};

let source_id = get_source_id(connection_handle).unwrap_or_default();
trace!("vcx_connection_connect(command_handle: {}, connection_handle: {}, connection_options: {:?}), source_id: {:?}",
command_handle, connection_handle, options, source_id);
trace!("vcx_connection_connect(command_handle: {}, connection_handle: {}, source_id: {:?}",
command_handle, connection_handle, source_id);

spawn(move || {
match connect(connection_handle, options) {
Ok(_) => {
match get_invite_details(connection_handle, true) {
Ok(x) => {
trace!("vcx_connection_connect_cb(command_handle: {}, connection_handle: {}, rc: {}, details: {}), source_id: {:?}",
command_handle, connection_handle, error::SUCCESS.message, x, source_id);
let msg = CStringUtils::string_to_cstring(x);
cb(command_handle, error::SUCCESS.code_num, msg.as_ptr());
}
Err(_) => {
warn!("vcx_connection_connect_cb(command_handle: {}, connection_handle: {}, rc: {}, details: {}), source_id: {:?}",
command_handle, connection_handle, error::SUCCESS.message, "null", source_id); // TODO: why Success?????
cb(command_handle, error::SUCCESS.code_num, ptr::null_mut());
}
}
match connect(connection_handle) {
Ok(invitation) => {
let invitation = invitation.unwrap_or(String::from("{}"));
trace!("vcx_connection_connect_cb(command_handle: {}, connection_handle: {}, rc: {}, details: {}), source_id: {}",
command_handle, connection_handle, error::SUCCESS.message, invitation, source_id);
let invitation = CStringUtils::string_to_cstring(invitation);
cb(command_handle, error::SUCCESS.code_num, invitation.as_ptr());
}
Err(x) => {
Err(err) => {
warn!("vcx_connection_connect_cb(command_handle: {}, connection_handle: {}, rc: {}, details: {}, source_id: {})",
command_handle, connection_handle, x, "null", source_id);
cb(command_handle, x.into(), ptr::null_mut());
command_handle, connection_handle, err, "null", source_id);
cb(command_handle, err.into(), ptr::null_mut());
}
};

Expand Down Expand Up @@ -536,7 +528,7 @@ pub extern fn vcx_connection_update_state(command_handle: CommandHandle,
}

spawn(move || {
let rc = match update_state(connection_handle, None) {
let rc = match update_state(connection_handle) {
Ok(x) => {
trace!("vcx_connection_update_state_cb(command_handle: {}, rc: {}, connection_handle: {}, state: {}), source_id: {:?}",
command_handle, error::SUCCESS.message, connection_handle, get_state(connection_handle), source_id);
Expand Down Expand Up @@ -1283,7 +1275,7 @@ mod tests {
use std::ptr;

use api::{return_types_u32, VcxStateType};
use connection::tests::{build_test_connection_inviter_requested, build_test_connection_inviter_invited};
use connection::tests::{build_test_connection_inviter_invited, build_test_connection_inviter_null, build_test_connection_inviter_requested};
use utils::constants::{DELETE_CONNECTION_DECRYPTED_RESPONSE, GET_MESSAGES_DECRYPTED_RESPONSE};
use utils::devsetup::*;
use utils::error;
Expand All @@ -1293,6 +1285,7 @@ mod tests {
use utils::timeout::TimeoutUtils;

use super::*;
use serde_json::Value;

#[test]
#[cfg(feature = "general_test")]
Expand Down Expand Up @@ -1331,7 +1324,8 @@ mod tests {
let cb = return_types_u32::Return_U32_STR::new().unwrap();
let rc = vcx_connection_connect(cb.command_handle, 0, CString::new("{}").unwrap().into_raw(), Some(cb.get_callback()));
assert_eq!(rc, error::INVALID_CONNECTION_HANDLE.code_num);
let handle = build_test_connection_inviter_requested();

let handle = build_test_connection_inviter_null();
assert!(handle > 0);

let cb = return_types_u32::Return_U32_STR::new().unwrap();
Expand All @@ -1341,14 +1335,28 @@ mod tests {
assert!(invite_details.is_some());
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_connection_connect_returns_invitation() {
let _setup = SetupAriesMocks::init();

let handle = build_test_connection_inviter_null();
let invitation = connect(handle).unwrap().unwrap();
let invitation: Value = serde_json::from_str(&invitation).unwrap();
assert!(invitation["serviceEndpoint"].is_string());
assert!(invitation["recipientKeys"].is_array());
assert!(invitation["routingKeys"].is_array());
assert!(invitation["@type"].is_string());
assert!(invitation["@id"].is_string());
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_connection_update_state() {
let _setup = SetupAriesMocks::init();

let handle = build_test_connection_inviter_invited();
assert!(handle > 0);
connect(handle, None).unwrap();

AgencyMockDecrypted::set_next_decrypted_response(GET_MESSAGES_DECRYPTED_RESPONSE);
AgencyMockDecrypted::set_next_decrypted_message(ARIES_CONNECTION_REQUEST);
Expand All @@ -1374,7 +1382,6 @@ mod tests {

let handle = build_test_connection_inviter_requested();
assert!(handle > 0);
connect(handle, None).unwrap();

let cb = return_types_u32::Return_U32_U32::new().unwrap();
let rc = vcx_connection_update_state_with_message(cb.command_handle, handle, CString::new(ARIES_CONNECTION_REQUEST).unwrap().into_raw(), Some(cb.get_callback()));
Expand Down
2 changes: 1 addition & 1 deletion libvcx/src/api/vcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ mod tests {
assert_eq!(_vcx_init_minimal_c_closure(&config), error::SUCCESS.code_num);

let connection_handle = ::connection::create_connection("test_create_fails").unwrap();
::connection::connect(connection_handle, None).unwrap_err();
::connection::connect(connection_handle).unwrap_err();

settings::set_defaults();
}
Expand Down
Loading

0 comments on commit e8ce4b9

Please sign in to comment.