Skip to content

Commit

Permalink
Use RingsBackend in InnerSwarmCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 committed Oct 10, 2023
1 parent d2ff028 commit 096642a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
10 changes: 8 additions & 2 deletions core/src/swarm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,17 @@ impl SwarmBuilder {
let transport_event_channel = Channel::new();

let transport = Box::new(Transport::new(&self.ice_servers, self.external_address));
let backend = Arc::new(RingsBackend::new(dht, transport));

let transport_callback = Arc::new(
InnerSwarmCallback::new(transport_event_channel.sender(), swarm_callback).boxed(),
InnerSwarmCallback::new(
transport_event_channel.sender(),
backend.clone(),
swarm_callback,
)
.boxed(),
);

let backend = Arc::new(RingsBackend::new(dht, transport));
Swarm {
transport_event_channel,
measure: self.measure,
Expand Down
24 changes: 21 additions & 3 deletions core/src/swarm/callback.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::str::FromStr;
use std::sync::Arc;

use async_trait::async_trait;
use rings_transport::core::callback::TransportCallback;
use rings_transport::core::transport::WebrtcConnectionState;

use crate::backend::RingsBackend;
use crate::channels::Channel;
use crate::dht::Did;
use crate::message::Message;
Expand Down Expand Up @@ -56,13 +58,19 @@ pub type BoxedSwarmCallback = Box<dyn SwarmCallback>;

pub struct InnerSwarmCallback {
transport_event_sender: TransportEventSender,
backend: Arc<RingsBackend>,
callback: BoxedSwarmCallback,
}

impl InnerSwarmCallback {
pub fn new(transport_event_sender: TransportEventSender, callback: BoxedSwarmCallback) -> Self {
pub fn new(
transport_event_sender: TransportEventSender,
backend: Arc<RingsBackend>,
callback: BoxedSwarmCallback,
) -> Self {
Self {
transport_event_sender,
backend,
callback,
}
}
Expand All @@ -72,12 +80,22 @@ impl InnerSwarmCallback {
#[cfg_attr(not(feature = "wasm"), async_trait)]
impl TransportCallback for InnerSwarmCallback {
async fn on_message(&self, _cid: &str, msg: &[u8]) -> Result<(), CallbackError> {
let payload = MessagePayload::from_bincode(msg)?;

if !payload.verify() {
tracing::error!("Cannot verify msg or it's expired: {:?}", payload);
return Err("Cannot verify msg or it's expired".into());
}

self.callback.on_validate(&payload).await?;

Channel::send(
&self.transport_event_sender,
TransportEvent::DataChannelMessage(msg.into()),
)
.await
.map_err(|e| e.into())
.await?;

self.callback.on_payload(&payload).await
}

async fn on_peer_connection_state_change(
Expand Down

0 comments on commit 096642a

Please sign in to comment.