浏览代码

client: Initialize synchronized directories from the database.

All the backend code is still missing.
父节点
当前提交
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
 use std::fs::create_dir_all;
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
 use tokio::io::{AsyncReadExt, AsyncWriteExt};
5
 use tokio::io::{AsyncReadExt, AsyncWriteExt};
6
 use tokio::net::{UnixListener, UnixStream};
6
 use tokio::net::{UnixListener, UnixStream};
7
 use tokio::stream::StreamExt;
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
 mod cli;
12
 mod cli;
12
 
13
 
28
     format!("{}/client.db", data_dir())
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
 #[tokio::main]
59
 #[tokio::main]
32
 async fn main() {
60
 async fn main() {
33
     // Create the data directories if necessary.
61
     // Create the data directories if necessary.
39
 
67
 
40
     // TODO: Register signal handler for graceful shutdown.
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
     // Initialize the existing synchronized directories.
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
     // Listen for CLI commands.
85
     // Listen for CLI commands.
52
     // TODO: Graceful shutdown on errors.
86
     // TODO: Graceful shutdown on errors.
98
     Ok(())
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
+use std::ffi::OsString;
2
+
1
 pub struct Database {
3
 pub struct Database {
2
     // TODO
4
     // TODO
3
 }
5
 }
8
         Ok(Database {})
10
         Ok(Database {})
9
     }
11
     }
10
 
12
 
11
-    pub fn synchronized_directories(&self) -> Vec<String> {
13
+    pub fn synchronized_directories(&self) -> Vec<OsString> {
12
         // TODO
14
         // TODO
13
         Vec::new()
15
         Vec::new()
14
     }
16
     }

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

1
+//! Library for file-system synchronization.
1
 extern crate rmp_serde as rmps;
2
 extern crate rmp_serde as rmps;
2
 
3
 
3
 use std::io;
4
 use std::io;
147
 #[derive(Debug)]
148
 #[derive(Debug)]
148
 pub enum Error {
149
 pub enum Error {
149
     Io(std::io::Error),
150
     Io(std::io::Error),
151
+    Json(serde_json::error::Error),
150
     DirectoryRemoved,
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
 impl From<io::Error> for Error {
161
 impl From<io::Error> for Error {
154
     fn from(e: io::Error) -> Error {
162
     fn from(e: io::Error) -> Error {
155
         Error::Io(e)
163
         Error::Io(e)

正在加载...
取消
保存