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

request_id attribute for generic response #42

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ members = [
"service",
]

exclude = [
"./target/*"
]
exclude = ["./target/*"]

[workspace.dependencies]

Expand All @@ -33,13 +31,13 @@ tokio-util = { version = "0.7.8", default-features = false }
bincode2 = { version = "2.0.1", default-features = false }
futures = { version = "0.3.28", default-features = false }
bytes = { version = "1.4.0", default-features = false }
uuid = { version="1.3.3", features = [
uuid = { version = "1.3.3", features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]}
] }
anyhow = "1.0.71"
async-recursion = { version = "1.0.4" }
parking_lot = { version = "0.12.1" }
structopt = { version = "0.3.26" }
lazy_static = "1.4.0"
lazy_static = "1.4.0"
106 changes: 105 additions & 1 deletion citadel-internal-service-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub struct RegisterFailure {
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ServiceConnectionAccepted;
pub struct ServiceConnectionAccepted {
pub request_id: Option<Uuid>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MessageSendSuccess {
Expand Down Expand Up @@ -553,6 +555,7 @@ pub struct FileTransferRequestNotification {
pub cid: u64,
pub peer_cid: u64,
pub metadata: VirtualObjectMetadata,
pub request_id: Option<Uuid>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there are a few occurrences in tests that didn't get updated to reflect the addition of request_id

}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -570,6 +573,11 @@ pub struct FileTransferTickNotification {
pub cid: u64,
pub peer_cid: Option<u64>,
pub status: ObjectTransferStatus,
pub request_id: Option<Uuid>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially the same as with the other struct. Take a look at the clippy workflow to see the test it shows an error for.

}

pub trait ResponseId {
fn response_id() -> String;
}

#[derive(Serialize, Deserialize, Debug, Clone, IsError, IsNotification)]
Expand Down Expand Up @@ -653,6 +661,102 @@ pub enum InternalServiceResponse {
ListRegisteredPeersFailure(ListRegisteredPeersFailure),
}

/// Shortcut for getting `.request_id` attribute on all members
macro_rules! match_request_id {
($val:expr, $($variant:ident),+) => {
match $val {
$(
InternalServiceResponse::$variant(x) => x.request_id,
)+
}
}
}

impl InternalServiceResponse {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could use a proc macro to simplify this. Take a look at the answer on this.

pub fn request_id(&self) -> Option<Uuid> {
match_request_id!(
self,
ConnectSuccess,
ConnectFailure,
RegisterSuccess,
RegisterFailure,
ServiceConnectionAccepted,
MessageSendSuccess,
MessageSendFailure,
MessageNotification,
DisconnectNotification,
DisconnectFailure,
SendFileRequestSuccess,
SendFileRequestFailure,
FileTransferRequestNotification,
FileTransferStatusNotification,
FileTransferTickNotification,
DownloadFileSuccess,
DownloadFileFailure,
DeleteVirtualFileSuccess,
DeleteVirtualFileFailure,
PeerConnectSuccess,
PeerConnectFailure,
PeerConnectNotification,
PeerRegisterNotification,
PeerDisconnectSuccess,
PeerDisconnectFailure,
PeerRegisterSuccess,
PeerRegisterFailure,
GroupChannelCreateSuccess,
GroupChannelCreateFailure,
GroupBroadcastHandleFailure,
GroupCreateSuccess,
GroupCreateFailure,
GroupLeaveSuccess,
GroupLeaveFailure,
GroupLeaveNotification,
GroupEndSuccess,
GroupEndFailure,
GroupEndNotification,
GroupMessageNotification,
GroupMessageResponse,
GroupMessageSuccess,
GroupMessageFailure,
GroupInviteNotification,
GroupInviteSuccess,
GroupInviteFailure,
GroupRespondRequestSuccess,
GroupRespondRequestFailure,
GroupMembershipResponse,
GroupRequestJoinPendingNotification,
GroupDisconnectNotification,
GroupKickSuccess,
GroupKickFailure,
GroupListGroupsSuccess,
GroupListGroupsFailure,
GroupListGroupsResponse,
GroupJoinRequestNotification,
GroupRequestJoinAcceptResponse,
GroupRequestJoinDeclineResponse,
GroupRequestJoinSuccess,
GroupRequestJoinFailure,
GroupMemberStateChangeNotification,
LocalDBGetKVSuccess,
LocalDBGetKVFailure,
LocalDBSetKVSuccess,
LocalDBSetKVFailure,
LocalDBDeleteKVSuccess,
LocalDBDeleteKVFailure,
LocalDBGetAllKVSuccess,
LocalDBGetAllKVFailure,
LocalDBClearAllKVSuccess,
LocalDBClearAllKVFailure,
GetSessionsResponse,
GetAccountInformationResponse,
ListAllPeersResponse,
ListAllPeersFailure,
ListRegisteredPeersResponse,
ListRegisteredPeersFailure
)
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum InternalServiceRequest {
Connect {
Expand Down
4 changes: 3 additions & 1 deletion citadel-internal-service/src/kernel/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ pub trait IOInterfaceExt: IOInterface {
tokio::task::spawn(async move {
let write_task = async move {
let response =
InternalServiceResponse::ServiceConnectionAccepted(ServiceConnectionAccepted);
InternalServiceResponse::ServiceConnectionAccepted(ServiceConnectionAccepted {
request_id: None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this request_id matches that of the initiated connection so we know which request this belongs to

});

if let Err(err) = sink_send_payload::<Self>(response, &mut sink).await {
error!(target: "citadel", "Failed to send to client: {err:?}");
Expand Down
1 change: 1 addition & 0 deletions citadel-internal-service/src/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ fn spawn_tick_updater(
cid: implicated_cid,
peer_cid,
status: status_message,
request_id: None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would ideally want to make this the same as the request_id as the initial file transfer request that initiated this.

},
);
match entry.send(message.clone()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub async fn handle<T: IOInterface>(
cid: implicated_cid,
peer_cid,
metadata,
request_id: None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

},
);

Expand Down
Loading