Skip to content

Commit

Permalink
Update remaining functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Apr 25, 2023
1 parent d80a7b9 commit 9494725
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
3 changes: 3 additions & 0 deletions base_layer/chat_ffi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ fn main() {
include: Some(vec![
"tari_core".to_string(),
"tari_common_types".to_string(),
"tari_crypto".to_string(),
"tari_p2p".to_string(),
"tari_wallet".to_string(),
"tari_contacts".to_string(),
]),
..Default::default()
},
Expand Down
23 changes: 22 additions & 1 deletion base_layer/chat_ffi/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

struct ClientFFI;

struct Message;

/**
* Configuration for a comms node
*/
Expand All @@ -23,13 +25,17 @@ extern "C" {

/**
* Creates a Chat Client
* TODO: This function take a ptr to a collection of seed peers and this works fine in cucumber, or native rust but
* isn't at all ideal for a real FFI. We need to work with the mobile teams and come up with a better interface
* for supplying seed peers.
*
* ## Arguments
* `config` - The P2PConfig pointer
* `identity_file_path` - The path to the node identity file
* `db_path` - The path to the db file
* `seed_peers` - A ptr to a collection of seed peers
* `network_str` - The network to connect to
*
* ## Returns
* `*mut ChatClient` - Returns a pointer to a ChatClient, note that it returns ptr::null_mut()
* if config is null, an error was encountered or if the runtime could not be created
Expand Down Expand Up @@ -117,8 +123,23 @@ int check_online_status(struct ClientFFI *client, struct TariAddress *receiver);
*
* # Safety
* The ```address``` should be destroyed after use
* The returned pointer to ```*mut *mut Message``` should be destroyed after use
*/
struct Message **get_all_messages(struct ClientFFI *client, struct TariAddress *address);

/**
* Frees memory for messages
*
* ## Arguments
* `messages_ptr` - The pointer of a Vec<Message>
*
* ## Returns
* `()` - Does not return a value, equivalent to void in C
*
* # Safety
* None
*/
Message **get_all_messages(struct ClientFFI *client, struct TariAddress *address);
void destroy_messages(struct Message **messages_ptr);

/**
* Creates a TariAddress and returns a ptr
Expand Down
11 changes: 7 additions & 4 deletions base_layer/chat_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ pub struct ClientFFI {
}

/// Creates a Chat Client
/// TODO: This function take a ptr to a collection of seed peers and this works fine in cucumber, or native rust but
/// isn't at all ideal for a real FFI. We need to work with the mobile teams and come up with a better interface
/// for supplying seed peers.
///
/// ## Arguments
/// `config` - The P2PConfig pointer
/// `identity_file_path` - The path to the node identity file
/// `db_path` - The path to the db file
/// `seed_peers` - A ptr to a collection of seed peers
/// `network_str` - The network to connect to
///
/// ## Returns
/// `*mut ChatClient` - Returns a pointer to a ChatClient, note that it returns ptr::null_mut()
/// if config is null, an error was encountered or if the runtime could not be created
Expand Down Expand Up @@ -178,11 +182,10 @@ pub unsafe extern "C" fn add_contact(client: *mut ClientFFI, receiver: *mut Tari
/// The ```address``` should be destroyed after use
#[no_mangle]
pub unsafe extern "C" fn check_online_status(client: *mut ClientFFI, receiver: *mut TariAddress) -> c_int {
let status = (client)
.runtime
.block_on((client).client.check_online_status(&*receiver));
let rec = (*receiver).clone();
let status = (*client).runtime.block_on((*client).client.check_online_status(&rec));

status.as_u8().into()
status.clone().as_u8().into()
}

/// Get a ptr to all messages from or to address
Expand Down
1 change: 0 additions & 1 deletion base_layer/contacts/src/contacts_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ impl Display for ContactMessageType {
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum ContactOnlineStatus {
Online,
Offline,
Expand Down
5 changes: 4 additions & 1 deletion integration_tests/src/chat_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#![recursion_limit = "1024"]

use std::{
ffi::{c_void, CString},
fs,
Expand All @@ -33,6 +35,7 @@ use async_trait::async_trait;
type ClientFFI = c_void;

use libc::{c_char, c_int};
use log::warn;
use rand::rngs::OsRng;
use tari_chat_client::{database, ChatClient};
use tari_common::configuration::{MultiaddrList, Network};
Expand Down Expand Up @@ -88,7 +91,7 @@ impl ChatClient for ChatFFI {
async fn check_online_status(&self, address: &TariAddress) -> ContactOnlineStatus {
let client = self.ptr.lock().unwrap();

let address_ptr = Box::into_raw(Box::new(address.to_owned()));
let address_ptr = Box::into_raw(Box::new(address.clone()));

let result;
unsafe { result = check_online_status(client.0, address_ptr) }
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/tests/features/ChatFFI.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Feature: Chat FFI messaging
When I have a chat FFI client CHAT_A connected to seed node SEED_A
When I have a chat FFI client CHAT_B connected to seed node SEED_A
When CHAT_A adds CHAT_B as a contact
When I stop node SEED_A
When CHAT_B adds CHAT_A as a contact
When CHAT_A waits for contact CHAT_B to be online
When I use CHAT_A to send a message 'Hey there' to CHAT_B
Then CHAT_B will have 1 message with CHAT_A
12 changes: 9 additions & 3 deletions integration_tests/tests/steps/chat_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,20 @@ async fn wait_for_contact_to_be_online(world: &mut TariWorld, client: String, co
let contact = world.chat_clients.get(&contact).unwrap();

let address = TariAddress::from_public_key(contact.identity().public_key(), Network::LocalNet);
let mut last_status = ContactOnlineStatus::Banned("No result came back".to_string());

for _ in 0..(TWO_MINUTES_WITH_HALF_SECOND_SLEEP) {
if ContactOnlineStatus::Online == client.check_online_status(&address).await {
for _ in 0..(TWO_MINUTES_WITH_HALF_SECOND_SLEEP / 4) {
last_status = client.check_online_status(&address).await;
if ContactOnlineStatus::Online == last_status {
return;
}

tokio::time::sleep(Duration::from_millis(HALF_SECOND)).await;
}

panic!("Contact {} never came online", contact.identity().node_id())
panic!(
"Contact {} never came online, status is: {}",
contact.identity().node_id(),
last_status
)
}

0 comments on commit 9494725

Please sign in to comment.