This commit is contained in:
tezlm 2023-08-16 17:47:04 -07:00
parent 2adb0ae8b1
commit a6c8cbf96a
Signed by: tezlm
GPG key ID: 649733FCD94AFBBA

View file

@ -6,7 +6,7 @@ use axum::{extract::State, routing, Json, Router};
use reqwest::StatusCode; use reqwest::StatusCode;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
use tracing::debug; use tracing::{trace, debug};
use super::things::Error; use super::things::Error;
@ -31,13 +31,16 @@ async fn message(
Json(request): Json<Request>, Json(request): Json<Request>,
) -> Response<Json<RPCResponse>> { ) -> Response<Json<RPCResponse>> {
// FIXME (security): hostname must be validated! // FIXME (security): hostname must be validated!
trace!("receive message from {}", request.contact.host);
let response = match request.info { let response = match request.info {
RPCRequest::Ping => { RPCRequest::Ping => {
trace!("receive Ping");
let mut router = state.p2p.router.lock().await; let mut router = state.p2p.router.lock().await;
router.update(request.contact); router.update(request.contact);
RPCResponse::Ok RPCResponse::Ok
} }
RPCRequest::Announce(node_ids) => { RPCRequest::Announce(node_ids) => {
trace!("receive Announce (ids={:?})", node_ids);
let mut map = state.p2p.map.lock().await; let mut map = state.p2p.map.lock().await;
for node_id in node_ids { for node_id in node_ids {
map.entry(node_id) map.entry(node_id)
@ -47,11 +50,13 @@ async fn message(
RPCResponse::Ok RPCResponse::Ok
} }
RPCRequest::FindNode(node_id) => { RPCRequest::FindNode(node_id) => {
trace!("receive FindNode (id={})", node_id);
let router = state.p2p.router.lock().await; let router = state.p2p.router.lock().await;
let contacts = router.find_closest(&node_id, 20); let contacts = router.find_closest(&node_id, 20);
RPCResponse::FindNode(contacts) RPCResponse::FindNode(contacts)
} }
RPCRequest::FindValue(node_id) => { RPCRequest::FindValue(node_id) => {
trace!("receive FindValue (id={})", node_id);
let map = state.p2p.map.lock().await; let map = state.p2p.map.lock().await;
if let Some(value) = map.get(&node_id) { if let Some(value) = map.get(&node_id) {
RPCResponse::FindValue(value.clone()) RPCResponse::FindValue(value.clone())
@ -61,7 +66,8 @@ async fn message(
RPCResponse::FindNode(contacts) RPCResponse::FindNode(contacts)
} }
} }
RPCRequest::Subscribe(_node_id) => { RPCRequest::Subscribe(node_ids) => {
trace!("receive Subscribe (id={:?})", node_ids);
todo!() todo!()
} }
}; };