Implements RPC call functionality

This commit is contained in:
Age Manning
2019-03-18 23:34:44 +11:00
parent 0625bb6b03
commit 8ec0688cb9
3 changed files with 34 additions and 26 deletions

View File

@@ -72,14 +72,16 @@ impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
}
}
/// Implements the combined behaviour for the libp2p service.
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
/// Subscribes to a gossipsub topic.
pub fn subscribe(&mut self, topic: Topic) -> bool {
self.gossipsub.subscribe(topic)
}
pub fn send_message(&self, message: String) {
// TODO: Encode and send via gossipsub
/// Sends an RPC Request/Response via the RPC protocol.
pub fn send_rpc(&mut self, peer_id: PeerId, rpc_event: RPCEvent) {
self.serenity_rpc.send_rpc(peer_id, rpc_event);
}
}

View File

@@ -13,7 +13,7 @@ use libp2p::core::swarm::{
use libp2p::{Multiaddr, PeerId};
pub use methods::{HelloMessage, RPCMethod, RPCRequest, RPCResponse};
pub use protocol::{RPCEvent, RPCProtocol};
use slog::{debug, o, Logger};
use slog::{debug, o};
use std::marker::PhantomData;
use tokio::io::{AsyncRead, AsyncWrite};
@@ -40,15 +40,10 @@ impl<TSubstream> Rpc<TSubstream> {
}
/// Submits and RPC request.
pub fn send_request(&mut self, peer_id: PeerId, id: u64, method_id: u16, body: RPCRequest) {
let request = RPCEvent::Request {
id,
method_id,
body,
};
pub fn send_rpc(&mut self, peer_id: PeerId, rpc_event: RPCEvent) {
self.events.push(NetworkBehaviourAction::SendEvent {
peer_id,
event: request,
event: rpc_event,
});
}
}