Skip to content

Commit

Permalink
chore: change to torii-relay
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Jan 24, 2024
1 parent 9a9f3d3 commit eb84c61
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ torii-core = { path = "crates/torii/core" }
torii-graphql = { path = "crates/torii/graphql" }
torii-grpc = { path = "crates/torii/grpc" }
torii-server = { path = "crates/torii/server" }
torii-relay = { path = "crates/torii/libp2p" }

# sozo
sozo-signers = { path = "crates/sozo/signers" }
Expand Down
2 changes: 1 addition & 1 deletion bin/torii/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tower-http = "0.4.4"
tracing-subscriber.workspace = true
tracing.workspace = true
url.workspace = true
torii-libp2p = { version = "0.5.1-alpha.0", path = "../../crates/torii/libp2p" }
torii-relay.workspace = true

[dev-dependencies]
camino.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ struct Args {

/// Port to serve Libp2p TCP & UDP Quic transports
#[arg(long, value_name = "PORT", default_value = "9090")]
port: u16,
relay_port: u16,

Check warning on line 69 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L69

Added line #L69 was not covered by tests

/// Port to serve Libp2p WebRTC transport
#[arg(long, value_name = "PORT", default_value = "9091")]
port_webrtc: u16,
relay_webrtc_port: u16,

Check warning on line 73 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L73

Added line #L73 was not covered by tests

/// Path to a local identity key file. If not specified, a new identity will be generated
#[arg(long, value_name = "PATH")]
Expand Down Expand Up @@ -180,9 +180,9 @@ async fn main() -> anyhow::Result<()> {
proxy_server.clone(),
);

let mut libp2p_relay_server = torii_libp2p::server::Libp2pRelay::new(
args.port,
args.port_webrtc,
let mut libp2p_relay_server = torii_relay::server::Relay::new(
args.relay_port,
args.relay_webrtc_port,
args.local_key_path,
args.cert_path,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ starknet.workspace = true
thiserror.workspace = true
tokio = { version = "1.32.0", features = [ "sync" ], default-features = false }
torii-grpc = { path = "../grpc", features = [ "client" ] }
torii-libp2p = { path = "../libp2p" }
torii-relay = { path = "../libp2p" }
url.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/client/src/client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum Error {
#[error(transparent)]
GrpcClient(#[from] torii_grpc::client::Error),
#[error(transparent)]
Libp2pClient(#[from] torii_libp2p::errors::Error),
RelayClient(#[from] torii_relay::errors::Error),
#[error(transparent)]
Model(#[from] ModelError),
#[error("Unsupported query")]
Expand Down
48 changes: 29 additions & 19 deletions crates/torii/client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use dojo_types::packing::unpack;
use dojo_types::schema::Ty;
use dojo_types::WorldMetadata;
use dojo_world::contracts::WorldContractReader;
use futures::channel::mpsc::UnboundedSender;
use futures::channel::mpsc::{SendError, UnboundedSender};
use futures_util::{SinkExt, TryFutureExt};
use parking_lot::{RwLock, RwLockReadGuard};
use starknet::core::utils::cairo_short_string_to_felt;
use starknet::providers::jsonrpc::HttpTransport;
Expand All @@ -21,8 +22,8 @@ use torii_grpc::client::{EntityUpdateStreaming, ModelDiffsStreaming};
use torii_grpc::proto::world::RetrieveEntitiesResponse;
use torii_grpc::types::schema::Entity;
use torii_grpc::types::{KeysClause, Query};
use torii_libp2p::client::Message;
use torii_libp2p::types::ClientMessage;
use torii_relay::client::Message;
use torii_relay::types::ClientMessage;

use crate::client::error::{Error, ParseError};
use crate::client::storage::ModelStorage;
Expand All @@ -38,7 +39,7 @@ pub struct Client {
/// The grpc client.
inner: AsyncRwLock<torii_grpc::client::WorldClient>,
/// Libp2p client.
libp2p_client: torii_libp2p::client::Libp2pClient,
libp2p_client: torii_relay::client::RelayClient,
/// Model storage
storage: Arc<ModelStorage>,
/// Models the client are subscribed to.
Expand All @@ -60,7 +61,7 @@ impl Client {
) -> Result<Self, Error> {
let mut grpc_client = torii_grpc::client::WorldClient::new(torii_url, world).await?;

let libp2p_client = torii_libp2p::client::Libp2pClient::new(libp2p_relay_url)?;
let libp2p_client = torii_relay::client::RelayClient::new(libp2p_relay_url)?;

Check warning on line 64 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L64

Added line #L64 was not covered by tests

let metadata = grpc_client.metadata().await?;

Expand Down Expand Up @@ -102,36 +103,45 @@ impl Client {
}

/// Returns all of the subscribed topics of the libp2p client.
pub fn subscribed_topics(&self) -> HashSet<String> {
self.libp2p_client.topics.keys().cloned().collect()
}
// pub fn subscribed_topics(&self) -> HashSet<String> {
// self.libp2p_client.topics.keys().cloned().collect()
// }

/// Subscribes to a topic.
/// Returns true if the topic was subscribed to.
/// Returns false if the topic was already subscribed to.
pub fn subscribe_topic(&mut self, topic: &str) -> Result<bool, Error> {
self.libp2p_client.subscribe(topic).map_err(Error::Libp2pClient)
pub async fn subscribe_topic(&mut self, topic: &str) -> Result<(), SendError> {
self.libp2p_client
.command_sender
.send(torii_relay::client::Command::Subscribe(topic.to_string()))
.await
}

Check warning on line 118 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L113-L118

Added lines #L113 - L118 were not covered by tests

/// Unsubscribes from a topic.
/// Returns true if the topic was subscribed to.
pub fn unsubscribe_topic(&mut self, topic: &str) -> Result<bool, Error> {
self.libp2p_client.unsubscribe(topic).map_err(Error::Libp2pClient)
pub async fn unsubscribe_topic(&mut self, topic: &str) -> Result<(), SendError> {
self.libp2p_client
.command_sender
.send(torii_relay::client::Command::Unsubscribe(topic.to_string()))
.await
}

Check warning on line 127 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L122-L127

Added lines #L122 - L127 were not covered by tests

/// Publishes a message to a topic.
/// Returns the message id.
pub fn publish_message(&mut self, topic: &str, message: &[u8]) -> Result<Vec<u8>, Error> {
pub async fn publish_message(&mut self, topic: &str, message: &[u8]) -> Result<(), SendError> {
self.libp2p_client
.publish(&ClientMessage { topic: topic.to_string(), data: message.to_vec() })
.map(|id| id.0)
.map_err(Error::Libp2pClient)
.command_sender
.send(torii_relay::client::Command::Publish(ClientMessage {
topic: topic.to_string(),
data: message.to_vec(),
}))
.await
}

Check warning on line 139 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L131-L139

Added lines #L131 - L139 were not covered by tests

/// Runs the libp2p event listener which processes incoming messages.
/// Runs the libp2p event loop which processes incoming messages and commands.
/// And sends events in the channel
pub async fn run_libp2p(&mut self, sender: &UnboundedSender<Message>) {
self.libp2p_client.run(sender).await;
pub async fn run_libp2p(&mut self) {
self.libp2p_client.event_loop.run().await;
}

Check warning on line 145 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L143-L145

Added lines #L143 - L145 were not covered by tests

/// Returns a read lock on the World metadata that the client is connected to.
Expand Down

0 comments on commit eb84c61

Please sign in to comment.