Sfoglia il codice sorgente

create gateway daemon

ghassmo 5 anni fa
parent
commit
ef56e87322
2 ha cambiato i file con 83 aggiunte e 2 eliminazioni
  1. 2 2
      Cargo.toml
  2. 81 0
      src/bin/gatewayd.rs

+ 2 - 2
Cargo.toml

@@ -129,8 +129,8 @@ name = "compile-shaders"
 path = "src/bin/compile-shaders.rs"
 
 [[bin]]
-name = "demoservices"
-path = "src/bin/demoservices.rs"
+name = "gatewayd"
+path = "src/bin/gatewayd.rs"
 
 [[bin]]
 name = "demowallet"

+ 81 - 0
src/bin/gatewayd.rs

@@ -0,0 +1,81 @@
+use std::sync::Arc;
+use std::net::SocketAddr;
+
+extern crate clap;
+use async_executor::Executor;
+use easy_parallel::Parallel;
+
+use sapvi::Result;
+
+use sapvi::service::{GatewayService, ProgramOptions};
+
+fn setup_addr(address: Option<SocketAddr>, default: SocketAddr ) -> SocketAddr{
+    match address {
+        Some(addr) => {
+            addr
+        },
+        None => default 
+    }
+}
+
+async fn start(executor: Arc<Executor<'_>>, options: ProgramOptions) -> Result<()> {
+
+    let accept_addr: SocketAddr  = setup_addr(options.accept_addr, "127.0.0.1:3333".parse()?);
+    let pub_addr: SocketAddr = setup_addr(options.pub_addr, "127.0.0.1:4444".parse()?);
+
+    let gateway = GatewayService::new(
+        accept_addr,
+        pub_addr,
+    );
+
+    gateway.start(executor.clone()).await?;
+    Ok(())
+}
+
+fn main() -> Result<()> {
+    use simplelog::*;
+
+    let ex = Arc::new(Executor::new());
+    let (signal, shutdown) = async_channel::unbounded::<()>();
+
+
+    let options = ProgramOptions::load()?;
+
+    let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
+
+    let debug_level = if options.verbose {
+        LevelFilter::Debug
+    } else {
+        LevelFilter::Off
+    };
+
+    CombinedLogger::init(vec![
+        TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
+        WriteLogger::new(
+            LevelFilter::Debug,
+            Config::default(),
+            std::fs::File::create(options.log_path.as_path()).unwrap(),
+        ),
+    ])
+        .unwrap();
+
+
+
+    let ex2 = ex.clone();
+
+
+
+    let (_, result) = Parallel::new()
+        // Run four executor threads.
+        .each(0..3, |_| smol::future::block_on(ex.run(shutdown.recv())))
+        // Run the main future on the current thread.
+        .finish(|| {
+            smol::future::block_on(async move {
+                start(ex2, options).await?;
+                drop(signal);
+                Ok::<(), sapvi::Error>(())
+            })
+        });
+
+    result
+}