瀏覽代碼

client: Initialize synchronized directories from the database.

All the backend code is still missing.
Mathias Gottschlag 5 年之前
父節點
當前提交
efdfb2e123
共有 3 個檔案被更改,包括 55 行新增28 行删除
  1. 44
    27
      src/bin/client.rs
  2. 3
    1
      src/database.rs
  3. 8
    0
      src/lib.rs

+ 44
- 27
src/bin/client.rs 查看文件

@@ -1,12 +1,13 @@
1 1
 use std::fs::create_dir_all;
2
-use std::io;
3
-use std::sync::{Arc, Mutex};
2
+use std::sync::Arc;
3
+use std::ffi::OsStr;
4 4
 
5 5
 use tokio::io::{AsyncReadExt, AsyncWriteExt};
6 6
 use tokio::net::{UnixListener, UnixStream};
7 7
 use tokio::stream::StreamExt;
8
+use tokio::sync::{mpsc, Mutex};
8 9
 
9
-use twfss::Database;
10
+use twfss::{Database, FileTree, ClientSideSync, FileSystemWatcher, Error, SynchronizationError};
10 11
 
11 12
 mod cli;
12 13
 
@@ -28,6 +29,33 @@ fn db_path() -> String {
28 29
     format!("{}/client.db", data_dir())
29 30
 }
30 31
 
32
+struct SynchronizedDirectory {
33
+    client_side_sync: ClientSideSync,
34
+    file_system_watcher: FileSystemWatcher,
35
+}
36
+
37
+impl SynchronizedDirectory {
38
+    async fn open(database: Arc<Mutex<Database>>, directory: &OsStr, errors: mpsc::Sender<SynchronizationError>) -> Result<SynchronizedDirectory, Error> {
39
+        let file_tree = Arc::new(Mutex::new(FileTree::open(database, directory)?));
40
+        let client_side_sync = ClientSideSync::new(file_tree.clone(), errors.clone()).await?;
41
+        let file_system_watcher = FileSystemWatcher::new(file_tree, errors).await?;
42
+        Ok(SynchronizedDirectory{
43
+            client_side_sync,
44
+            file_system_watcher,
45
+        })
46
+    }
47
+
48
+    async fn unpause(&mut self) {
49
+        self.client_side_sync.unpause().await;
50
+        self.file_system_watcher.unpause().await;
51
+    }
52
+
53
+    async fn pause(&mut self) {
54
+        self.client_side_sync.pause().await;
55
+        self.file_system_watcher.pause().await;
56
+    }
57
+}
58
+
31 59
 #[tokio::main]
32 60
 async fn main() {
33 61
     // Create the data directories if necessary.
@@ -39,14 +67,20 @@ async fn main() {
39 67
 
40 68
     // TODO: Register signal handler for graceful shutdown.
41 69
 
70
+    // TODO: Error handling. If a directory was removed, remove it from the
71
+    // list of synchronized directories (or mark it for the user).
72
+    let (errors_send, errors) = mpsc::channel(32);
73
+
42 74
     // Initialize the existing synchronized directories.
43
-    //let mut directories = Vec::new();
44
-    let _db = Arc::new(Mutex::new(Database::create_or_open(&db_path()).unwrap()));
45
-    /*for directory in db.lock().unwrap().synchronized_directories() {
46
-        // TODO: Error handling. If the directory was removed, remove it from the
47
-        // list of synchronized directories.
48
-        directories.push(SynchronizedDirectory::open(db.clone(), &directory, false).unwrap());
49
-    }*/
75
+    let mut directories = Vec::new();
76
+    let db = Arc::new(Mutex::new(Database::create_or_open(&db_path()).unwrap()));
77
+    for directory in db.lock().await.synchronized_directories() {
78
+        // TODO: Error handling - remove the synchronized directory instead (or mark it for the
79
+        // user).
80
+        let mut sync_dir = SynchronizedDirectory::open(db.clone(), &directory, errors_send.clone()).await.unwrap();
81
+        sync_dir.unpause().await;
82
+        directories.push(sync_dir);
83
+    }
50 84
 
51 85
     // Listen for CLI commands.
52 86
     // TODO: Graceful shutdown on errors.
@@ -98,20 +132,3 @@ async fn handle_cli_client(stream: &mut UnixStream) -> Result<(), Error> {
98 132
     Ok(())
99 133
 }
100 134
 
101
-#[derive(Debug)]
102
-enum Error {
103
-    Json(serde_json::error::Error),
104
-    Io(std::io::Error),
105
-}
106
-
107
-impl From<serde_json::error::Error> for Error {
108
-    fn from(e: serde_json::error::Error) -> Error {
109
-        Error::Json(e)
110
-    }
111
-}
112
-
113
-impl From<io::Error> for Error {
114
-    fn from(e: io::Error) -> Error {
115
-        Error::Io(e)
116
-    }
117
-}

+ 3
- 1
src/database.rs 查看文件

@@ -1,3 +1,5 @@
1
+use std::ffi::OsString;
2
+
1 3
 pub struct Database {
2 4
     // TODO
3 5
 }
@@ -8,7 +10,7 @@ impl Database {
8 10
         Ok(Database {})
9 11
     }
10 12
 
11
-    pub fn synchronized_directories(&self) -> Vec<String> {
13
+    pub fn synchronized_directories(&self) -> Vec<OsString> {
12 14
         // TODO
13 15
         Vec::new()
14 16
     }

+ 8
- 0
src/lib.rs 查看文件

@@ -1,3 +1,4 @@
1
+//! Library for file-system synchronization.
1 2
 extern crate rmp_serde as rmps;
2 3
 
3 4
 use std::io;
@@ -147,9 +148,16 @@ where
147 148
 #[derive(Debug)]
148 149
 pub enum Error {
149 150
     Io(std::io::Error),
151
+    Json(serde_json::error::Error),
150 152
     DirectoryRemoved,
151 153
 }
152 154
 
155
+impl From<serde_json::error::Error> for Error {
156
+    fn from(e: serde_json::error::Error) -> Error {
157
+        Error::Json(e)
158
+    }
159
+}
160
+
153 161
 impl From<io::Error> for Error {
154 162
     fn from(e: io::Error) -> Error {
155 163
         Error::Io(e)

Loading…
取消
儲存