Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Name When Listing Peers #41

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions citadel-internal-service-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,18 @@ pub struct LocalDBClearAllKVSuccess {
pub request_id: Option<Uuid>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PeerInformation {
pub cid: u64,
pub online_status: bool,
pub name: Option<String>,
pub username: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListAllPeersResponse {
pub cid: u64,
pub online_status: HashMap<u64, bool>,
pub peer_information: HashMap<u64, PeerInformation>,
pub request_id: Option<Uuid>,
}

Expand All @@ -522,8 +530,7 @@ pub struct ListRegisteredPeersFailure {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListRegisteredPeersResponse {
pub cid: u64,
pub peers: HashMap<u64, PeerSessionInformation>,
pub online_status: HashMap<u64, bool>,
pub peers: HashMap<u64, PeerInformation>,
pub request_id: Option<Uuid>,
}

Expand Down
7 changes: 5 additions & 2 deletions citadel-internal-service/src/kernel/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ mod local_db;
mod peer;

#[async_recursion]
pub async fn handle_request<T: IOInterface>(
pub async fn handle_request<T>(
this: &CitadelWorkspaceService<T>,
uuid: Uuid,
command: InternalServiceRequest,
) -> Option<HandledRequestResult> {
) -> Option<HandledRequestResult>
where
T: IOInterface,
{
match &command {
InternalServiceRequest::GetAccountInformation { .. } => {
return get_account_information::handle(this, uuid, command).await
Expand Down
15 changes: 13 additions & 2 deletions citadel-internal-service/src/kernel/requests/peer/list_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::kernel::CitadelWorkspaceService;
use citadel_internal_service_connector::io_interface::IOInterface;
use citadel_internal_service_types::{
InternalServiceRequest, InternalServiceResponse, ListAllPeersFailure, ListAllPeersResponse,
PeerInformation,
};
use citadel_sdk::prelude::ProtocolRemoteExt;
use uuid::Uuid;
Expand All @@ -21,10 +22,20 @@ pub async fn handle<T: IOInterface>(
Ok(peers) => {
let peers = ListAllPeersResponse {
cid,
online_status: peers
peer_information: peers
.into_iter()
.filter(|peer| peer.cid != cid)
.map(|peer| (peer.cid, peer.is_online))
.map(|peer| {
(
peer.cid,
PeerInformation {
cid: peer.cid,
online_status: peer.is_online,
name: peer.full_name,
username: peer.username,
},
)
})
.collect(),
request_id: Some(request_id),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use crate::kernel::CitadelWorkspaceService;
use citadel_internal_service_connector::io_interface::IOInterface;
use citadel_internal_service_types::{
InternalServiceRequest, InternalServiceResponse, ListRegisteredPeersFailure,
ListRegisteredPeersResponse, PeerSessionInformation,
ListRegisteredPeersResponse, PeerInformation,
};
use citadel_sdk::prelude::{ProtocolRemoteExt, TargetLockedRemote};
use std::collections::HashMap;
use citadel_sdk::prelude::ProtocolRemoteExt;
use uuid::Uuid;

pub async fn handle<T: IOInterface>(
Expand All @@ -21,32 +20,23 @@ pub async fn handle<T: IOInterface>(

match remote.get_local_group_mutual_peers(cid).await {
Ok(peers) => {
let mut accounts = HashMap::new();
for peer in &peers {
// TOOD: Do not unwrap below
let peer_username = remote
.find_target(cid, peer.cid)
.await
.unwrap()
.target_username()
.cloned()
.unwrap_or_default();
accounts.insert(
peer.cid,
PeerSessionInformation {
cid,
peer_cid: peer.cid,
peer_username,
},
);
}

let peers = ListRegisteredPeersResponse {
cid,
peers: accounts,
online_status: peers
.iter()
.map(|peer| (peer.cid, peer.is_online))
peers: peers
.clone()
.into_iter()
.filter(|peer| peer.cid != cid)
.map(|peer| {
(
peer.cid,
PeerInformation {
cid,
online_status: peer.is_online,
name: peer.full_name,
username: peer.username,
},
)
})
.collect(),
request_id: Some(request_id),
};
Expand Down
6 changes: 3 additions & 3 deletions citadel-internal-service/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,13 @@ pub fn server_info_skip_cert_verification_with_password<'a>(
server_test_node_skip_cert_verification_with_password(EmptyKernel, server_password, |_| {})
}

pub fn server_info_reactive_skip_cert_verification<'a, F: 'a, Fut: 'a>(
pub fn server_info_reactive_skip_cert_verification<'a, F, Fut>(
f: F,
opts: impl FnOnce(&mut NodeBuilder),
) -> (NodeFuture<'a, Box<dyn NetKernel + 'a>>, SocketAddr)
where
F: Fn(ConnectionSuccess, ClientServerRemote) -> Fut + Send + Sync,
Fut: Future<Output = Result<(), NetworkError>> + Send + Sync,
F: Fn(ConnectionSuccess, ClientServerRemote) -> Fut + Send + Sync + 'a,
Fut: Future<Output = Result<(), NetworkError>> + Send + Sync + 'a,
{
server_test_node_skip_cert_verification(
Box::new(ClientConnectListenerKernel::new(f)) as Box<dyn NetKernel>,
Expand Down
8 changes: 4 additions & 4 deletions citadel-internal-service/tests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ mod tests {

let resp = from_service.recv().await.unwrap();
if let InternalServiceResponse::ListAllPeersResponse(list) = resp {
assert_eq!(list.online_status.len(), 1);
assert!(list.online_status.contains_key(&peer_cid))
assert_eq!(list.peer_information.len(), 1);
assert!(list.peer_information.contains_key(&peer_cid))
} else {
panic!("Invalid ListAllPeers response")
}
Expand All @@ -517,8 +517,8 @@ mod tests {

let resp = from_service.recv().await.unwrap();
if let InternalServiceResponse::ListRegisteredPeersResponse(list) = resp {
assert_eq!(list.online_status.len(), 1);
assert!(list.online_status.contains_key(&peer_cid))
assert_eq!(list.peers.len(), 1);
assert!(list.peers.contains_key(&peer_cid))
} else {
panic!("Invalid ListAllPeers response")
}
Expand Down
Loading