-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
168 lines (144 loc) · 4.91 KB
/
lib.rs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use dartfrog_lib::{default_load_service, default_save_service, provider_handle_message, update_subscriber, update_subscribers, AppServiceState, ChatRequest, ChatServiceState, ChatUpdate, DefaultAppClientState, DefaultAppProcessState, ProviderState, Service};
use kinode_process_lib::{call_init, http, Address, Request};
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
mod constants;
wit_bindgen::generate!({
path: "target/wit",
world: "process-v0",
});
type AppProviderState = ProviderState<AppService, DefaultAppClientState, DefaultAppProcessState>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppService {
pub chat: ChatServiceState,
pub clicker: ClickerServiceState,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AppUpdate {
Chat(ChatUpdate),
Clicker(ClickerUpdate),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AppRequest {
Chat(ChatRequest),
Clicker(ClickerRequest),
}
#[derive(Debug, Clone)]
pub struct AppState {
pub provider: AppProviderState,
}
impl AppState {
pub fn new(our:&Address) -> Self {
AppState {
provider: AppProviderState::new(our),
}
}
}
impl AppServiceState for AppService {
fn new() -> Self {
AppService {
chat: ChatServiceState::new(),
clicker: ClickerServiceState::new(),
}
}
fn init(&mut self, our: &Address, service: &Service) -> anyhow::Result<()> {
default_load_service::<Self>(our, &service.id.to_string(), self)
}
fn save(&mut self, our: &Address, service: &Service) -> anyhow::Result<()> {
default_save_service::<Self>(our, &service.id.to_string(), self)
}
fn handle_subscribe(&mut self, subscriber_node: String, our: &Address, service: &Service) -> anyhow::Result<()> {
self.chat.handle_subscribe(subscriber_node.clone(), our, service)?;
self.clicker.handle_subscribe(subscriber_node, our, service)?;
self.save(our, service)?;
Ok(())
}
fn handle_request(&mut self, from: String, req: String, our: &Address, service: &Service) -> anyhow::Result<()> {
let request = serde_json::from_str::<AppRequest>(&req)?;
match request {
AppRequest::Chat(chat_request) => {
self.chat.handle_request(from, chat_request, our, service)?;
}
AppRequest::Clicker(clicker_request) => {
self.clicker.handle_request(from, clicker_request, our, service)?;
}
}
self.save(our, service)?;
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClickerUpdate {
ClickMap(HashMap<String, u32>),
ClickMapNode(String, u32),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClickerRequest {
Click,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClickerServiceState {
pub click_map: HashMap<String, u32>,
}
impl ClickerServiceState {
fn new() -> Self {
ClickerServiceState {
click_map: HashMap::new(),
}
}
fn handle_subscribe(&mut self, subscriber_node: String, our: &Address, service: &Service) -> anyhow::Result<()> {
let upd = ClickerUpdate::ClickMap(self.click_map.clone());
update_subscriber(AppUpdate::Clicker(upd), &subscriber_node, our, service)?;
Ok(())
}
fn handle_request(&mut self, from: String, req: ClickerRequest, our: &Address, service: &Service) -> anyhow::Result<()> {
match req {
ClickerRequest::Click => {
let count = self.click_map.entry(from.clone()).or_insert(0);
*count += 1;
// Use ClickMapNode update instead of ClickMap
let upd = ClickerUpdate::ClickMapNode(from, *count);
update_subscribers(AppUpdate::Clicker(upd), our, service)?;
}
}
Ok(())
}
}
call_init!(init);
fn init(our: Address) {
println!("init clicker");
let mut state = AppState::new(&our);
let loaded_provider = AppProviderState::load(&our);
state.provider = loaded_provider;
let try_ui = http::secure_serve_ui(&our, "df-clicker-ui", vec!["/", "*"]);
http::secure_bind_ws_path("/", true).unwrap();
match try_ui {
Ok(()) => {}
Err(e) => {
println!("error starting ui: {:?}", e);
}
};
Request::to(("our", "homepage", "homepage", "sys"))
.body(
serde_json::json!({
"Add": {
"label": "df-clicker",
"icon": constants::HOMEPAGE_IMAGE,
"path": "/",
}
})
.to_string()
.as_bytes()
.to_vec(),
)
.send()
.unwrap();
loop {
match provider_handle_message(&our, &mut state.provider) {
Ok(()) => {}
Err(e) => {
println!("error handling message: {:?}", e);
}
};
}
}