-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apis): adding client and node APIs, as well as safenode RPC serv…
…ices to pub/sub to gossipsub topics
- Loading branch information
Showing
18 changed files
with
279 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2023 MaidSafe.net limited. | ||
// | ||
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3. | ||
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed | ||
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. Please review the Licences for the specific language governing | ||
// permissions and limitations relating to use of the SAFE Network Software. | ||
|
||
use clap::Subcommand; | ||
use color_eyre::Result; | ||
use sn_client::{Client, ClientEvent}; | ||
|
||
#[derive(Subcommand, Debug)] | ||
pub enum GossipsubCmds { | ||
/// Subscribe to a topic and listen for messages published on it | ||
Subscribe { | ||
/// The name of the topic. | ||
#[clap(name = "topic")] | ||
topic: String, | ||
}, | ||
/// Publish a message on a given topic | ||
Publish { | ||
/// The name of the topic. | ||
#[clap(name = "topic")] | ||
topic: String, | ||
/// The message to publish. | ||
#[clap(name = "msg")] | ||
msg: String, | ||
}, | ||
} | ||
|
||
pub(crate) async fn gossipsub_cmds(cmds: GossipsubCmds, client: &Client) -> Result<()> { | ||
match cmds { | ||
GossipsubCmds::Subscribe { topic } => { | ||
client.subscribe_to_topic(topic.clone())?; | ||
println!("Subscribed to topic '{topic}'. Listening for messages published on it..."); | ||
let mut events_channel = client.events_channel(); | ||
while let Ok(event) = events_channel.recv().await { | ||
if let ClientEvent::GossipsubMsg { msg, .. } = event { | ||
let msg = String::from_utf8(msg)?; | ||
println!("New message published: {msg}"); | ||
} | ||
} | ||
} | ||
GossipsubCmds::Publish { topic, msg } => { | ||
client.publish_on_topic(topic.clone(), msg.into())?; | ||
println!("Message published on topic '{topic}'."); | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.