chiark / gitweb /
server: reorganise setup, pass Global to route
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 16 Aug 2021 23:49:05 +0000 (00:49 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 16 Aug 2021 23:49:05 +0000 (00:49 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/server.rs

index 77a9617599b5e6495343093d92a54d358f29c678..1cc0660414a101f0caa6a2a4e657e2380f4ddc9c 100644 (file)
@@ -16,13 +16,13 @@ pub struct Opts {
 const METADATA_MAX_LEN: usize = MAX_OVERHEAD;
 
 #[derive(Debug)]
-struct Global {
+pub struct Global {
   config: config::InstanceConfigGlobal,
   all_clients: HashMap<ClientName, Client>,
 }
 
 #[derive(Debug)]
-struct Client {
+pub struct Client {
   ic: Arc<InstanceConfig>,
   web: tokio::sync::mpsc::Sender<WebRequest>,
 }
@@ -55,7 +55,8 @@ struct WebResponse {
 type WebResponseData = Vec<u8>;
 
 #[throws(PacketError)]
-pub fn route_packet(conn: &str, link: &dyn Display,
+pub fn route_packet(_global: &Global,
+                    conn: &str, link: &dyn Display,
                     packet: Box<[u8]>, daddr: IpAddr)
 {
   // xxx
@@ -259,7 +260,8 @@ async fn handle(
 
 #[allow(unused_variables)] // xxx
 #[allow(unused_mut)] // xxx
-async fn run_client(ic: Arc<InstanceConfig>,
+async fn run_client(global: Arc<Global>,
+                    ic: Arc<InstanceConfig>,
                     mut web: mpsc::Receiver<WebRequest>)
                     -> Result<Void, AE>
 {
@@ -384,7 +386,7 @@ async fn run_client(ic: Arc<InstanceConfig>,
               let daddr = ip_packet_addr::<true>(header)?;
               Ok(daddr)
             }, |(daddr,packet)| route_packet(
-              &conn, &ic.link.client, daddr,packet
+              &global, &conn, &ic.link.client, daddr,packet
             ),
               |e| Ok::<_,SlipFramesError<_>>({ warnings.add(&e)?; })
             )?;
@@ -443,21 +445,22 @@ async fn main() {
        })
     }).collect();
 
+    let global = Arc::new(Global {
+      config: global_config,
+      all_clients,
+    });
+
     for (ic, web_recv) in izip!(
       ics,
       client_handles_recv,
     ) {
+      let global_ = global.clone();
       let ic_ = ic.clone();
       tasks.push((tokio::spawn(async move {
-        run_client(ic_, web_recv).await.void_unwrap_err()
+        run_client(global_, ic_, web_recv).await.void_unwrap_err()
       }), format!("client {}", &ic)));
     }
 
-    let global = Arc::new(Global {
-      config: global_config,
-      all_clients,
-    });
-
     for addr in &global.config.addrs {
       let global_ = global.clone();
       let make_service = hyper::service::make_service_fn(