Add send rpc in message handler
This commit is contained in:
parent
495348f934
commit
31333e8f8e
@ -17,6 +17,15 @@ impl From<u16> for RPCMethod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Into<u16> for RPCMethod {
|
||||||
|
fn into(self) -> u16 {
|
||||||
|
match self {
|
||||||
|
RPCMethod::Hello => 0,
|
||||||
|
_ => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum RPCRequest {
|
pub enum RPCRequest {
|
||||||
Hello(HelloMessage),
|
Hello(HelloMessage),
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
use crate::beacon_chain::BeaconChain;
|
use crate::beacon_chain::BeaconChain;
|
||||||
use crate::error;
|
use crate::error;
|
||||||
use crate::messages::NodeMessage;
|
use crate::messages::NodeMessage;
|
||||||
use crate::service::NetworkMessage;
|
use crate::service::{NetworkMessage, OutgoingMessage};
|
||||||
use crate::sync::SimpleSync;
|
use crate::sync::SimpleSync;
|
||||||
use crossbeam_channel::{unbounded as channel, Sender};
|
use crossbeam_channel::{unbounded as channel, Sender};
|
||||||
use futures::future;
|
use futures::future;
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
rpc::{RPCRequest, RPCResponse},
|
rpc::{RPCMethod, RPCRequest, RPCResponse},
|
||||||
PeerId, RPCEvent,
|
PeerId, RPCEvent,
|
||||||
};
|
};
|
||||||
use slog::debug;
|
use slog::debug;
|
||||||
|
use slog::warn;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
@ -30,7 +31,7 @@ pub struct MessageHandler {
|
|||||||
/// A mapping of peers we have sent an RPC request to.
|
/// A mapping of peers we have sent an RPC request to.
|
||||||
requests: HashMap<PeerId, Vec<RPCRequestInfo>>,
|
requests: HashMap<PeerId, Vec<RPCRequestInfo>>,
|
||||||
/// A counter of request id for each peer.
|
/// A counter of request id for each peer.
|
||||||
request_ids: HashMap<PeerId, u16>,
|
request_ids: HashMap<PeerId, u64>,
|
||||||
/// The `MessageHandler` logger.
|
/// The `MessageHandler` logger.
|
||||||
log: slog::Logger,
|
log: slog::Logger,
|
||||||
}
|
}
|
||||||
@ -38,7 +39,7 @@ pub struct MessageHandler {
|
|||||||
/// RPC request information
|
/// RPC request information
|
||||||
pub struct RPCRequestInfo {
|
pub struct RPCRequestInfo {
|
||||||
/// The id of the request
|
/// The id of the request
|
||||||
id: u16,
|
id: u64,
|
||||||
/// The time the request was sent, to check ttl.
|
/// The time the request was sent, to check ttl.
|
||||||
request_time: Instant,
|
request_time: Instant,
|
||||||
}
|
}
|
||||||
@ -98,7 +99,7 @@ impl MessageHandler {
|
|||||||
fn handle_message(&mut self, message: HandlerMessage) {
|
fn handle_message(&mut self, message: HandlerMessage) {
|
||||||
match message {
|
match message {
|
||||||
HandlerMessage::PeerDialed(peer_id) => {
|
HandlerMessage::PeerDialed(peer_id) => {
|
||||||
self.send_hello(peer_id);
|
self.send_hello_request(peer_id);
|
||||||
}
|
}
|
||||||
//TODO: Handle all messages
|
//TODO: Handle all messages
|
||||||
_ => {}
|
_ => {}
|
||||||
@ -106,7 +107,7 @@ impl MessageHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sends a HELLO RPC request to a newly connected peer.
|
/// Sends a HELLO RPC request to a newly connected peer.
|
||||||
fn send_hello(&mut self, peer_id: PeerId) {
|
fn send_hello_request(&mut self, peer_id: PeerId) {
|
||||||
// generate a unique id for the peer
|
// generate a unique id for the peer
|
||||||
let id = {
|
let id = {
|
||||||
let borrowed_id = self.request_ids.entry(peer_id.clone()).or_insert_with(|| 0);
|
let borrowed_id = self.request_ids.entry(peer_id.clone()).or_insert_with(|| 0);
|
||||||
@ -117,16 +118,39 @@ impl MessageHandler {
|
|||||||
};
|
};
|
||||||
// register RPC request
|
// register RPC request
|
||||||
{
|
{
|
||||||
let requests = self.requests.entry(peer_id).or_insert_with(|| vec![]);
|
let requests = self
|
||||||
|
.requests
|
||||||
|
.entry(peer_id.clone())
|
||||||
|
.or_insert_with(|| vec![]);
|
||||||
requests.push(RPCRequestInfo {
|
requests.push(RPCRequestInfo {
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
request_time: Instant::now(),
|
request_time: Instant::now(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// build the rpc request
|
||||||
|
let rpc_event = RPCEvent::Request {
|
||||||
|
id,
|
||||||
|
method_id: RPCMethod::Hello.into(),
|
||||||
|
body: RPCRequest::Hello(self.sync.generate_hello()),
|
||||||
|
};
|
||||||
|
|
||||||
// send the hello request to the network
|
// send the hello request to the network
|
||||||
self.send_rpc_request(id, RPCResponse::Hello(self.sync.generate_hello()));
|
self.send_rpc(peer_id, rpc_event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends and RPC response
|
/// Sends and RPC response
|
||||||
fn send_rpc_request(&self, request_id: u16, response: RPCResponse) {}
|
fn send_rpc(&self, peer_id: PeerId, rpc_event: RPCEvent) {
|
||||||
|
self.network_send
|
||||||
|
.send(NetworkMessage::Send(
|
||||||
|
peer_id,
|
||||||
|
OutgoingMessage::RPC(rpc_event),
|
||||||
|
))
|
||||||
|
.unwrap_or_else(|_| {
|
||||||
|
warn!(
|
||||||
|
self.log,
|
||||||
|
"Could not send RPC message to the network service"
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user