Skip to content

Commit

Permalink
Implement run_network_node. (#1063)
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n authored Apr 24, 2024
1 parent b60a0ec commit a82a610
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
17 changes: 14 additions & 3 deletions crates/core/src/bin/freenet.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use clap::Parser;
use freenet::local_node::{Executor, OperationMode, PeerCliConfig};
use freenet::{
local_node::{Executor, OperationMode, PeerCliConfig},
server::{local_node::run_local_node, network_node::run_network_node},
};
use std::net::SocketAddr;

type DynError = Box<dyn std::error::Error + Send + Sync + 'static>;

async fn run(config: PeerCliConfig) -> Result<(), DynError> {
match config.mode {
OperationMode::Local => run_local(config).await,
OperationMode::Network => Err("network mode not yet enabled".into()),
OperationMode::Network => run_network(config).await,
}
}

Expand All @@ -17,7 +20,15 @@ async fn run_local(config: PeerCliConfig) -> Result<(), DynError> {
freenet::config::Config::set_op_mode(OperationMode::Local);
let executor = Executor::from_config(config, None).await?;
let socket: SocketAddr = (ip, port).into();
freenet::server::local_node::run_local_node(executor, socket).await
run_local_node(executor, socket).await
}

async fn run_network(config: PeerCliConfig) -> Result<(), DynError> {
let port = config.port;
let ip = config.address;
freenet::config::Config::set_op_mode(OperationMode::Network);

run_network_node(config, (ip, port).into()).await
}

fn main() -> Result<(), DynError> {
Expand Down
77 changes: 63 additions & 14 deletions crates/core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub(crate) mod errors;
mod http_gateway;
pub(crate) mod path_handlers;

use std::net::SocketAddr;

use freenet_stdlib::{
client_api::{ClientError, ClientRequest, HostResponse},
prelude::*,
Expand Down Expand Up @@ -42,11 +44,19 @@ pub(crate) enum HostCallbackResult {
},
}

pub mod local_node {
use std::net::{IpAddr, SocketAddr};
fn serve(socket: SocketAddr, router: axum::Router) {
tokio::spawn(async move {
tracing::info!("listening on {}", socket);
let listener = tokio::net::TcpListener::bind(socket).await.unwrap();
axum::serve(listener, router).await.map_err(|e| {
tracing::error!("Error while running HTTP gateway server: {e}");
})
});
}

use axum::Router;
pub mod local_node {
use freenet_stdlib::client_api::{ClientRequest, ErrorKind};
use std::net::{IpAddr, SocketAddr};
use tower_http::trace::TraceLayer;

use crate::{
Expand All @@ -55,17 +65,7 @@ pub mod local_node {
DynError,
};

use super::http_gateway::HttpGateway;

fn serve(socket: SocketAddr, router: Router) {
tokio::spawn(async move {
tracing::info!("listening on {}", socket);
let listener = tokio::net::TcpListener::bind(socket).await.unwrap();
axum::serve(listener, router).await.map_err(|e| {
tracing::error!("Error while running HTTP gateway server: {e}");
})
});
}
use super::{http_gateway::HttpGateway, serve};

pub async fn run_local_node(
mut executor: Executor,
Expand Down Expand Up @@ -178,3 +178,52 @@ pub mod local_node {
}
}
}

pub mod network_node {
use std::net::SocketAddr;

use libp2p_identity::Keypair;
use tower_http::trace::TraceLayer;

use crate::{
client_events::websocket::WebSocketProxy, dev_tool::NodeConfig, local_node::PeerCliConfig,
DynError,
};

use super::{http_gateway::HttpGateway, serve};

pub async fn run_network_node(
config: PeerCliConfig,
socket: SocketAddr,
) -> Result<(), DynError> {
let (gw, gw_router) = HttpGateway::as_router(&socket);
let (ws_proxy, ws_router) = WebSocketProxy::as_router(gw_router);
serve(socket, ws_router.layer(TraceLayer::new_for_http()));

let mut node_config = NodeConfig::new();
node_config.with_ip(socket.ip()).with_port(socket.port());

let private_key = Keypair::generate_ed25519();

let is_gateway = node_config.is_gateway();
let node = node_config
.build(config, [Box::new(gw), Box::new(ws_proxy)], private_key)
.await?;

match node.run().await {
Ok(_) => {
if is_gateway {
tracing::info!("Gateway finished");
} else {
tracing::info!("Node finished");
}

Ok(())
}
Err(e) => {
tracing::error!("{e}");
Err(e)
}
}
}
}

0 comments on commit a82a610

Please sign in to comment.