-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathregister.rs
More file actions
58 lines (49 loc) · 1.7 KB
/
register.rs
File metadata and controls
58 lines (49 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use super::*;
use crate::runtime::ProjectRuntime;
use crate::*;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, time::Duration};
/// Register a project root
#[derive(Debug, Serialize, Deserialize, TypeDef)]
pub struct RegisterRequest {
pub id: u32,
pub root: PathBuf,
}
#[async_trait]
impl RequestHandler<PathBuf> for RegisterRequest {
async fn handle(self) -> Result<PathBuf> {
let RegisterRequest { id, root } = self;
let mut runtimes = runtimes().await;
tracing::trace!("{:#?}", runtimes);
if let Some(runtime) = runtimes.get_mut(&root) {
if runtime.contains(&id) {
return Err(Error::Unexpected(
"Trying to adding a connected client!".into(),
));
}
let address = runtime.broadcaster_adderss().clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
runtimes.get_mut(&root).unwrap().connect(id);
});
return Ok(address);
}
let (rloop, mut runtime) = match ProjectRuntime::new(root.clone()).await {
Ok(v) => v,
Err(err) => {
let name = root.as_path().name().unwrap();
return Err(Error::Setup(name, err.to_string()));
}
};
let address = runtime.broadcaster_adderss().clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
runtime.insert(id);
runtimes.insert(root, runtime);
drop(runtimes);
rloop.start(id).await;
});
Ok(address)
}
}