@@ -122,12 +122,14 @@ pub struct RuntimeStartConfig {
122
122
}
123
123
124
124
pub struct Runtime {
125
- pub rx : RustOpaque < Mutex < Option < mpsc:: Receiver < Action > > > > ,
125
+ pub arx : RustOpaque < Mutex < Option < mpsc:: Receiver < Action > > > > ,
126
+ pub urx : RustOpaque < Mutex < Option < mpsc:: Receiver < UpdateMsg > > > > ,
126
127
pub model : RustOpaque < Mutex < Model > > ,
127
128
pub handle : RustOpaque < Mutex < Option < Handle > > > ,
128
129
}
129
130
130
131
impl Runtime {
132
+ #[ frb( sync) ]
131
133
pub fn new ( ) -> Result < Runtime > {
132
134
#[ cfg( target_os = "android" ) ]
133
135
android_logger:: init_once (
@@ -144,18 +146,21 @@ impl Runtime {
144
146
. level_filter ( log:: LevelFilter :: Trace )
145
147
. init ( ) ?;
146
148
147
- let ( tx, rx) = mpsc:: channel ( 32 ) ;
148
- let model = Model :: new ( tx) ?;
149
+ let ( atx, arx) = mpsc:: channel ( 32 ) ;
150
+ let ( utx, urx) = mpsc:: channel ( 32 ) ;
151
+ let model = Model :: new ( atx, utx) ?;
149
152
Ok ( Self {
150
- rx : RustOpaque :: new ( Mutex :: new ( Some ( rx) ) ) ,
153
+ arx : RustOpaque :: new ( Mutex :: new ( Some ( arx) ) ) ,
154
+ urx : RustOpaque :: new ( Mutex :: new ( Some ( urx) ) ) ,
151
155
model : RustOpaque :: new ( Mutex :: new ( model) ) ,
152
156
handle : RustOpaque :: new ( Mutex :: new ( None ) ) ,
153
157
} )
154
158
}
155
159
156
160
pub fn start ( & self , sink : StreamSink < UpdateMsgWrap > , config : RuntimeStartConfig ) {
157
161
let model = self . model . clone ( ) ;
158
- let mut rx = self . rx . lock ( ) . unwrap ( ) . take ( ) . unwrap ( ) ;
162
+ let mut arx = self . arx . lock ( ) . unwrap ( ) . take ( ) . unwrap ( ) ;
163
+ let mut urx = self . urx . lock ( ) . unwrap ( ) . take ( ) . unwrap ( ) ;
159
164
let runtime = tokio:: runtime:: Builder :: new_multi_thread ( )
160
165
. worker_threads ( 1 )
161
166
. enable_all ( )
@@ -166,66 +171,81 @@ impl Runtime {
166
171
std:: thread:: spawn ( move || {
167
172
runtime. block_on ( async {
168
173
{
169
- let mut model = model. lock ( ) . unwrap ( ) ;
170
- model. update = Some ( Box :: new ( move |model, msg| {
171
- let msg = match msg {
172
- UpdateMsg :: Credential => {
173
- UpdateMsgWrap :: Credential ( model. username . clone ( ) )
174
- }
175
- UpdateMsg :: State => UpdateMsgWrap :: State ( model. state ) ,
176
- UpdateMsg :: Status => UpdateMsgWrap :: Status ( model. status . to_string ( ) ) ,
177
- UpdateMsg :: Log => UpdateMsgWrap :: Log ( model. log . to_string ( ) ) ,
178
- UpdateMsg :: Flux => UpdateMsgWrap :: Flux ( model. flux . clone ( ) ) ,
179
- UpdateMsg :: Online => UpdateMsgWrap :: Online (
180
- model
181
- . users
182
- . iter ( )
183
- . map ( |u| NetUserWrap {
184
- address : u. address . into ( ) ,
185
- address_v6 : u. address_v6 . into ( ) ,
186
- login_time : u. login_time ,
187
- mac_address : u
188
- . mac_address
189
- . map ( |addr| addr. to_string ( ) )
190
- . unwrap_or_default ( ) ,
191
- flux : u. flux ,
192
- is_local : model
193
- . mac_addrs
174
+ let model = model. clone ( ) ;
175
+ tokio:: spawn ( async move {
176
+ while let Some ( msg) = urx. recv ( ) . await {
177
+ let msg = {
178
+ let model = model. lock ( ) . unwrap ( ) ;
179
+ match msg {
180
+ UpdateMsg :: Credential => {
181
+ UpdateMsgWrap :: Credential ( model. username . clone ( ) )
182
+ }
183
+ UpdateMsg :: State => UpdateMsgWrap :: State ( model. state ) ,
184
+ UpdateMsg :: Status => {
185
+ UpdateMsgWrap :: Status ( model. status . to_string ( ) )
186
+ }
187
+ UpdateMsg :: Log => UpdateMsgWrap :: Log ( model. log . to_string ( ) ) ,
188
+ UpdateMsg :: Flux => UpdateMsgWrap :: Flux ( model. flux . clone ( ) ) ,
189
+ UpdateMsg :: Online => UpdateMsgWrap :: Online (
190
+ model
191
+ . users
194
192
. iter ( )
195
- . any ( |it| Some ( it) == u. mac_address . as_ref ( ) ) ,
196
- } )
197
- . collect ( ) ,
198
- ) ,
199
- UpdateMsg :: Details => UpdateMsgWrap :: Details ( model. details . clone ( ) , {
200
- let data = DetailDaily :: new ( & model. details ) ;
201
- DetailDailyWrap {
202
- details : data
203
- . details
204
- . into_iter ( )
205
- . map ( |( date, flux) | DetailDailyPoint {
206
- day : date. day ( ) ,
207
- flux,
193
+ . map ( |u| NetUserWrap {
194
+ address : u. address . into ( ) ,
195
+ address_v6 : u. address_v6 . into ( ) ,
196
+ login_time : u. login_time ,
197
+ mac_address : u
198
+ . mac_address
199
+ . map ( |addr| addr. to_string ( ) )
200
+ . unwrap_or_default ( ) ,
201
+ flux : u. flux ,
202
+ is_local : model
203
+ . mac_addrs
204
+ . iter ( )
205
+ . any ( |it| Some ( it) == u. mac_address . as_ref ( ) ) ,
206
+ } )
207
+ . collect ( ) ,
208
+ ) ,
209
+ UpdateMsg :: Details => {
210
+ UpdateMsgWrap :: Details ( model. details . clone ( ) , {
211
+ let data = DetailDaily :: new ( & model. details ) ;
212
+ DetailDailyWrap {
213
+ details : data
214
+ . details
215
+ . into_iter ( )
216
+ . map ( |( date, flux) | DetailDailyPoint {
217
+ day : date. day ( ) ,
218
+ flux,
219
+ } )
220
+ . collect ( ) ,
221
+ now_month : data. now . month ( ) ,
222
+ now_day : data. now . day ( ) ,
223
+ max_flux : data. max_flux ,
224
+ }
208
225
} )
209
- . collect ( ) ,
210
- now_month : data. now . month ( ) ,
211
- now_day : data. now . day ( ) ,
212
- max_flux : data. max_flux ,
226
+ }
227
+ UpdateMsg :: LogBusy => UpdateMsgWrap :: LogBusy ( model. log_busy ( ) ) ,
228
+ UpdateMsg :: OnlineBusy => {
229
+ UpdateMsgWrap :: OnlineBusy ( model. online_busy ( ) )
230
+ }
231
+ UpdateMsg :: DetailBusy => {
232
+ UpdateMsgWrap :: DetailBusy ( model. detail_busy ( ) )
233
+ }
213
234
}
214
- } ) ,
215
- UpdateMsg :: LogBusy => UpdateMsgWrap :: LogBusy ( model. log_busy ( ) ) ,
216
- UpdateMsg :: OnlineBusy => UpdateMsgWrap :: OnlineBusy ( model. online_busy ( ) ) ,
217
- UpdateMsg :: DetailBusy => UpdateMsgWrap :: DetailBusy ( model. detail_busy ( ) ) ,
218
- } ;
219
- sink. add ( msg) . unwrap ( ) ;
220
- } ) ) ;
221
-
235
+ } ;
236
+ sink. add ( msg) . unwrap ( ) ;
237
+ }
238
+ } ) ;
239
+ }
240
+ {
241
+ let model = model. lock ( ) . unwrap ( ) ;
222
242
if ( !config. username . is_empty ( ) ) && ( !config. password . is_empty ( ) ) {
223
243
model. queue ( Action :: Credential ( config. username , config. password ) ) ;
224
244
}
225
245
model. queue ( Action :: Status ( Some ( config. status ) ) ) ;
226
246
model. queue ( Action :: Timer ) ;
227
247
}
228
- while let Some ( action) = rx . recv ( ) . await {
248
+ while let Some ( action) = arx . recv ( ) . await {
229
249
log:: debug!( "received action: {:?}" , action) ;
230
250
model. lock ( ) . unwrap ( ) . handle ( action) ;
231
251
}
0 commit comments