ghassmo 5 роки тому
батько
коміт
b836fbc8b4
4 змінених файлів з 25 додано та 26 видалено
  1. 2 2
      src/bin/demoservices.rs
  2. 2 5
      src/bin/demowallet.rs
  3. 19 19
      src/service/gateway.rs
  4. 2 0
      src/service/mod.rs

+ 2 - 2
src/bin/demoservices.rs

@@ -4,10 +4,10 @@ use std::sync::Arc;
 
 use sapvi::Result;
 
-use sapvi::service::gateway;
+use sapvi::service::GatewayService;
 
 async fn start(executor: Arc<Executor<'_>>) -> Result<()> {
-    let gateway = gateway::GatewayService::new(
+    let gateway = GatewayService::new(
         String::from("tcp://127.0.0.1:3333"),
         String::from("tcp://127.0.0.1:4444"),
     );

+ 2 - 5
src/bin/demowallet.rs

@@ -2,7 +2,7 @@ use async_executor::Executor;
 use async_std::sync::{Arc, Mutex};
 use easy_parallel::Parallel;
 
-use sapvi::service::gateway::GatewayClient;
+use sapvi::service::{fetch_slabs_loop, GatewayClient};
 use sapvi::Result;
 
 async fn start(executor: Arc<Executor<'_>>) -> Result<()> {
@@ -19,10 +19,7 @@ async fn start(executor: Arc<Executor<'_>>) -> Result<()> {
 
     println!("subscription ready");
 
-    let fetch_loop_task = executor.spawn(GatewayClient::fetch_slabs_loop(
-        subscriber.clone(),
-        slabs.clone(),
-    ));
+    let fetch_loop_task = executor.spawn(fetch_slabs_loop(subscriber.clone(), slabs.clone()));
 
     client.put_slab(vec![0, 0, 0, 0]).await?;
     client.put_slab(vec![0, 0, 0, 0]).await?;

+ 19 - 19
src/service/gateway.rs

@@ -8,6 +8,13 @@ use async_executor::Executor;
 
 pub type Slabs = Vec<Vec<u8>>;
 
+#[repr(u8)]
+enum GatewayCommand {
+    PutSlab,
+    GetSlab,
+    GetLastIndex,
+}
+
 pub struct GatewayService {
     slabs: Mutex<Slabs>,
     addr: String,
@@ -125,26 +132,19 @@ impl GatewayClient {
         let rep: [u8; 4] = rep.try_into().unwrap();
         Ok(u32::from_be_bytes(rep))
     }
+}
 
-    pub async fn fetch_slabs_loop(
-        subscriber: Arc<Mutex<Subscriber>>,
-        slabs: Arc<Mutex<Slabs>>,
-    ) -> Result<()> {
-        loop {
-            let slab: Vec<u8>;
-            {
-                let mut subscriber = subscriber.lock().await;
-                slab = subscriber.fetch().await?;
-            }
-            println!("received new slab from subscriber");
-            slabs.lock().await.push(slab);
+pub async fn fetch_slabs_loop(
+    subscriber: Arc<Mutex<Subscriber>>,
+    slabs: Arc<Mutex<Slabs>>,
+) -> Result<()> {
+    loop {
+        let slab: Vec<u8>;
+        {
+            let mut subscriber = subscriber.lock().await;
+            slab = subscriber.fetch().await?;
         }
+        println!("received new slab from subscriber");
+        slabs.lock().await.push(slab);
     }
 }
-
-#[repr(u8)]
-enum GatewayCommand {
-    PutSlab,
-    GetSlab,
-    GetLastIndex,
-}

+ 2 - 0
src/service/mod.rs

@@ -1,2 +1,4 @@
 pub mod gateway;
 pub mod reqrep;
+
+pub use gateway::{fetch_slabs_loop, GatewayClient, GatewayService};