Skip to content
Snippets Groups Projects

Refactor command to make is shorter

Merged DISSOUBRAY Nathan requested to merge refactor_command into master
+ 175
170
@@ -40,105 +40,107 @@ use crate::to_serialize::{ConvertSer, JsonWrapper};
// - is_connected
//
// - behaviour
type Sender<T> = oneshot::Sender<Result<T, Box<dyn Error + Send>>>;
#[derive(Debug)]
pub(crate) enum DragoonCommand {
Listen {
multiaddr: String,
sender: oneshot::Sender<Result<u64, Box<dyn Error + Send>>>,
},
GetListeners {
sender: oneshot::Sender<Result<Vec<Multiaddr>, Box<dyn Error + Send>>>,
},
GetPeerId {
sender: oneshot::Sender<Result<PeerId, Box<dyn Error + Send>>>,
},
GetNetworkInfo {
sender: oneshot::Sender<Result<NetworkInfo, Box<dyn Error + Send>>>,
#[cfg(feature = "file-sharing")]
AddFile {
file: Vec<u8>,
channel: ResponseChannel<FileResponse>,
},
RemoveListener {
listener_id: u64,
sender: oneshot::Sender<Result<bool, Box<dyn Error + Send>>>,
AddPeer {
multiaddr: String,
sender: Sender<()>,
},
GetConnectedPeers {
sender: oneshot::Sender<Result<Vec<PeerId>, Box<dyn Error + Send>>>,
Bootstrap {
sender: Sender<()>,
},
Dial {
multiaddr: String,
sender: oneshot::Sender<Result<(), Box<dyn Error + Send>>>,
sender: Sender<()>,
},
AddPeer {
multiaddr: String,
sender: oneshot::Sender<Result<(), Box<dyn Error + Send>>>,
},
StartProvide {
DragoonGet {
peerid: String,
key: String,
sender: oneshot::Sender<Result<(), Box<dyn Error + Send>>>,
sender: Sender<Vec<u8>>,
},
GetProviders {
key: String,
sender: oneshot::Sender<Result<HashSet<PeerId>, Box<dyn Error + Send>>>,
DragoonPeers {
sender: Sender<HashSet<PeerId>>,
},
Bootstrap {
sender: oneshot::Sender<Result<(), Box<dyn Error + Send>>>,
DragoonSend {
data: String,
peerid: String,
sender: Sender<()>,
},
GetConnectedPeers {
sender: Sender<Vec<PeerId>>,
},
#[cfg(feature = "file-sharing")]
GetFile {
key: String,
peer: PeerId,
sender: oneshot::Sender<Result<Vec<u8>, Box<dyn Error + Send>>>,
sender: Sender<Vec<u8>>,
},
#[cfg(feature = "file-sharing")]
AddFile {
file: Vec<u8>,
channel: ResponseChannel<FileResponse>,
GetListeners {
sender: Sender<Vec<Multiaddr>>,
},
PutRecord {
GetNetworkInfo {
sender: Sender<NetworkInfo>,
},
GetPeerId {
sender: Sender<PeerId>,
},
GetProviders {
key: String,
value: Vec<u8>,
sender: oneshot::Sender<Result<(), Box<dyn Error + Send>>>,
sender: Sender<HashSet<PeerId>>,
},
GetRecord {
key: String,
sender: oneshot::Sender<Result<Vec<u8>, Box<dyn Error + Send>>>,
sender: Sender<Vec<u8>>,
},
DragoonPeers {
sender: oneshot::Sender<Result<HashSet<PeerId>, Box<dyn Error + Send>>>,
Listen {
multiaddr: String,
sender: Sender<u64>,
},
DragoonSend {
data: String,
peerid: String,
sender: oneshot::Sender<Result<(), Box<dyn Error + Send>>>,
PutRecord {
key: String,
value: Vec<u8>,
sender: Sender<()>,
},
DragoonGet {
peerid: String,
RemoveListener {
listener_id: u64,
sender: Sender<bool>,
},
StartProvide {
key: String,
sender: oneshot::Sender<Result<Vec<u8>, Box<dyn Error + Send>>>,
sender: Sender<()>,
},
}
impl std::fmt::Display for DragoonCommand {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
DragoonCommand::Listen { .. } => write!(f, "listen"),
DragoonCommand::GetListeners { .. } => write!(f, "get-listener"),
DragoonCommand::GetPeerId { .. } => write!(f, "get-peer-id"),
DragoonCommand::GetNetworkInfo { .. } => write!(f, "get-network-info"),
DragoonCommand::RemoveListener { .. } => write!(f, "remove-listener"),
DragoonCommand::GetConnectedPeers { .. } => write!(f, "get-connected-peers"),
DragoonCommand::Dial { .. } => write!(f, "dial"),
#[cfg(feature = "file-sharing")]
DragoonCommand::AddFile { .. } => write!(f, "add-file"),
DragoonCommand::AddPeer { .. } => write!(f, "add-peer"),
DragoonCommand::StartProvide { .. } => write!(f, "start-provide"),
DragoonCommand::GetProviders { .. } => write!(f, "get-providers"),
DragoonCommand::Bootstrap { .. } => write!(f, "bootstrap"),
DragoonCommand::Dial { .. } => write!(f, "dial"),
DragoonCommand::DragoonGet { .. } => write!(f, "dragoon-get"),
DragoonCommand::DragoonPeers { .. } => write!(f, "dragoon-peers"),
DragoonCommand::DragoonSend { .. } => write!(f, "dragoon-send"),
DragoonCommand::GetConnectedPeers { .. } => write!(f, "get-connected-peers"),
#[cfg(feature = "file-sharing")]
DragoonCommand::GetFile { .. } => write!(f, "get-file"),
#[cfg(feature = "file-sharing")]
DragoonCommand::AddFile { .. } => write!(f, "add-file"),
DragoonCommand::PutRecord { .. } => write!(f, "put-record"),
DragoonCommand::GetListeners { .. } => write!(f, "get-listener"),
DragoonCommand::GetPeerId { .. } => write!(f, "get-peer-id"),
DragoonCommand::GetNetworkInfo { .. } => write!(f, "get-network-info"),
DragoonCommand::GetProviders { .. } => write!(f, "get-providers"),
DragoonCommand::GetRecord { .. } => write!(f, "get-record"),
DragoonCommand::DragoonPeers { .. } => write!(f, "dragoon-peers"),
DragoonCommand::DragoonSend { .. } => write!(f, "dragoon-send"),
DragoonCommand::DragoonGet { .. } => write!(f, "dragoon-get"),
DragoonCommand::Listen { .. } => write!(f, "listen"),
DragoonCommand::PutRecord { .. } => write!(f, "put-record"),
DragoonCommand::RemoveListener { .. } => write!(f, "remove-listener"),
DragoonCommand::StartProvide { .. } => write!(f, "start-provide"),
}
}
}
@@ -190,70 +192,48 @@ macro_rules! dragoon_command {
}
// dragoon_command(state, DragoonCommand::Something, peerid, data)
// Implementation of dragoon commands
pub(crate) async fn listen(
Path(multiaddr): Path<String>,
#[cfg(feature = "file-sharing")]
pub(crate) async fn add_file(
Path((key, content)): Path<(String, String)>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `listen`");
dragoon_command!(state, Listen, multiaddr)
}
pub(crate) async fn get_listeners(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_listeners`");
dragoon_command!(state, GetListeners)
}
pub(crate) async fn get_peer_id(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_peer_id`");
dragoon_command!(state, GetPeerId)
}
#[derive(Serialize, Deserialize)]
pub(crate) struct SerNetworkInfo {
peers: usize,
pending: u32,
connections: u32,
established: u32,
pending_incoming: u32,
pending_outgoing: u32,
established_incoming: u32,
established_outgoing: u32,
}
// as each field of SerNetworkInfo is Serialize, SerNetworkInfo becomes Serialize by extension
info!(
"running command `add_file`: key = {}, content = {}",
key, content
);
let mut event_receiver = state.event_receiver.lock().await;
impl SerNetworkInfo {
pub(crate) fn new(network_info: &NetworkInfo) -> Self {
let connections = network_info.connection_counters();
SerNetworkInfo {
peers: network_info.num_peers(),
pending: connections.num_pending(),
connections: connections.num_connections(),
established: connections.num_established(),
pending_incoming: connections.num_pending_incoming(),
pending_outgoing: connections.num_pending_outgoing(),
established_incoming: connections.num_established_incoming(),
established_outgoing: connections.num_established_outgoing(),
loop {
match event_receiver.next().await {
Some(DragoonEvent::InboundRequest { channel, request }) => {
debug!("add_file: request '{}'", request);
if request == key {
debug!("add_file: request accepted");
let cmd = DragoonCommand::AddFile {
file: content.as_bytes().to_vec(),
channel,
};
send_command(cmd, state.clone()).await;
}
}
e => todo!("{:?}", e),
}
}
}
pub(crate) async fn get_network_info(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_network_info`");
dragoon_command!(state, GetNetworkInfo)
}
pub(crate) async fn remove_listener(
Path(listener_id): Path<u64>,
pub(crate) async fn add_peer(
Path(multiaddr): Path<String>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `remove_listener`");
dragoon_command!(state, RemoveListener, listener_id)
info!("running command `add_peer`");
dragoon_command!(state, AddPeer, multiaddr)
}
pub(crate) async fn get_connected_peers(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_connected_peers`");
dragoon_command!(state, GetConnectedPeers)
pub(crate) async fn bootstrap(State(state): State<Arc<AppState>>) -> Response {
info!("running command `bootstrap`");
dragoon_command!(state, Bootstrap)
}
pub(crate) async fn dial(
@@ -264,20 +244,12 @@ pub(crate) async fn dial(
dragoon_command!(state, Dial, multiaddr)
}
pub(crate) async fn add_peer(
Path(multiaddr): Path<String>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `add_peer`");
dragoon_command!(state, AddPeer, multiaddr)
}
pub(crate) async fn start_provide(
Path(key): Path<String>,
pub(crate) async fn dragoon_get(
Path((peerid, key)): Path<(String, String)>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `start_provide`");
dragoon_command!(state, StartProvide, key)
info!("running command `dragoon_get`");
dragoon_command!(state, DragoonGet, peerid, key)
}
pub(crate) async fn dragoon_peers(State(state): State<Arc<AppState>>) -> Response {
@@ -293,25 +265,9 @@ pub(crate) async fn dragoon_send(
dragoon_command!(state, DragoonSend, data, peerid)
}
pub(crate) async fn dragoon_get(
Path((peerid, key)): Path<(String, String)>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `dragoon_get`");
dragoon_command!(state, DragoonGet, peerid, key)
}
pub(crate) async fn get_providers(
Path(key): Path<String>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `get_providers`");
dragoon_command!(state, GetProviders, key)
}
pub(crate) async fn bootstrap(State(state): State<Arc<AppState>>) -> Response {
info!("running command `bootstrap`");
dragoon_command!(state, Bootstrap)
pub(crate) async fn get_connected_peers(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_connected_peers`");
dragoon_command!(state, GetConnectedPeers)
}
#[cfg(feature = "file-sharing")]
@@ -355,35 +311,74 @@ pub(crate) async fn get_file(
command_res_match(receiver, cmd_name).await
}
#[cfg(feature = "file-sharing")]
pub(crate) async fn add_file(
Path((key, content)): Path<(String, String)>,
pub(crate) async fn get_listeners(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_listeners`");
dragoon_command!(state, GetListeners)
}
pub(crate) async fn get_peer_id(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_peer_id`");
dragoon_command!(state, GetPeerId)
}
pub(crate) async fn get_providers(
Path(key): Path<String>,
State(state): State<Arc<AppState>>,
) -> Response {
info!(
"running command `add_file`: key = {}, content = {}",
key, content
);
let mut event_receiver = state.event_receiver.lock().await;
info!("running command `get_providers`");
dragoon_command!(state, GetProviders, key)
}
loop {
match event_receiver.next().await {
Some(DragoonEvent::InboundRequest { channel, request }) => {
debug!("add_file: request '{}'", request);
if request == key {
debug!("add_file: request accepted");
let cmd = DragoonCommand::AddFile {
file: content.as_bytes().to_vec(),
channel,
};
send_command(cmd, state.clone()).await;
}
}
e => todo!("{:?}", e),
#[derive(Serialize, Deserialize)]
pub(crate) struct SerNetworkInfo {
peers: usize,
pending: u32,
connections: u32,
established: u32,
pending_incoming: u32,
pending_outgoing: u32,
established_incoming: u32,
established_outgoing: u32,
}
// as each field of SerNetworkInfo is Serialize, SerNetworkInfo becomes Serialize by extension
impl SerNetworkInfo {
pub(crate) fn new(network_info: &NetworkInfo) -> Self {
let connections = network_info.connection_counters();
SerNetworkInfo {
peers: network_info.num_peers(),
pending: connections.num_pending(),
connections: connections.num_connections(),
established: connections.num_established(),
pending_incoming: connections.num_pending_incoming(),
pending_outgoing: connections.num_pending_outgoing(),
established_incoming: connections.num_established_incoming(),
established_outgoing: connections.num_established_outgoing(),
}
}
}
pub(crate) async fn get_network_info(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_network_info`");
dragoon_command!(state, GetNetworkInfo)
}
pub(crate) async fn get_record(
Path(key): Path<String>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `get_record`");
dragoon_command!(state, GetRecord, key)
}
pub(crate) async fn listen(
Path(multiaddr): Path<String>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `listen`");
dragoon_command!(state, Listen, multiaddr)
}
pub(crate) async fn put_record(
Path((key, value)): Path<(String, String)>,
State(state): State<Arc<AppState>>,
@@ -393,14 +388,24 @@ pub(crate) async fn put_record(
dragoon_command!(state, PutRecord, key, value)
}
pub(crate) async fn get_record(
pub(crate) async fn remove_listener(
Path(listener_id): Path<u64>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `remove_listener`");
dragoon_command!(state, RemoveListener, listener_id)
}
pub(crate) async fn start_provide(
Path(key): Path<String>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `get_record`");
dragoon_command!(state, GetRecord, key)
info!("running command `start_provide`");
dragoon_command!(state, StartProvide, key)
}
// End of dragoon command implementation
fn handle_dragoon_error(err: Box<dyn Error + Send>, command: &str) -> Response {
if let Ok(dragoon_error) = err.downcast::<DragoonError>() {
dragoon_error.into_response()
Loading