-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmain.rs
More file actions
98 lines (90 loc) · 2.61 KB
/
main.rs
File metadata and controls
98 lines (90 loc) · 2.61 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (C) 2021-2022 The Aero Project Developers.
*
* This file is part of The Aero Project.
*
* Aero is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Aero is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
*/
use core::sync::atomic::{AtomicUsize, Ordering, AtomicBool};
use aero_syscall::{sys_fork, sys_exit};
use aipc::async_runtime::Listener;
#[aipc::def("TestObj")]
trait TestObj {
async fn create() -> TestObj;
async fn foo(&self);
async fn kill(&self);
}
#[aipc::def("ServerControlObj")]
trait ServerControlObj {
async fn create() -> ServerControlObj;
async fn can_control(&self) -> bool;
async fn quit(&self);
}
static COUNTER: AtomicUsize = AtomicUsize::new(1);
struct NoData {
data: usize,
}
#[aipc::object(TestObjSrv as TestObj)]
impl NoData {
fn create() -> NoData {
return NoData {
data: COUNTER.fetch_add(1, Ordering::SeqCst),
};
}
fn foo(&self) {
println!("Hello from obj {}!", self.data);
}
}
static SCS_PERMIT_ONCE: AtomicBool = AtomicBool::new(true);
pub struct ServerControlState {
permitted: bool
}
#[aipc::object(ServerControlSrv as ServerControlObj)]
impl ServerControlState {
pub fn create() -> ServerControlState {
return ServerControlState {
permitted: SCS_PERMIT_ONCE.swap(false, Ordering::SeqCst)
};
}
pub fn can_control(&self) -> bool {
self.permitted
}
pub fn quit(&self) {
if self.permitted {
println!("[ServerControlSrv] exiting!");
aipc::async_runtime::spawn(async {
sys_exit(0);
});
}
}
}
fn main() {
let mut rt = aipc::async_runtime::AsyncRuntime::new();
rt.spawn(async {
let pid = sys_fork().unwrap();
if pid == 0 {
// server
ServerControlSrv::listen();
TestObjSrv::listen();
} else {
// client
let ctl = ServerControlObj::create(pid).await;
let o = TestObj::create(pid).await;
o.foo().await;
ctl.quit().await;
sys_exit(0);
}
});
rt.run();
}