@@ -19,6 +19,7 @@ use std::os::unix::fs as unix_fs;
19
19
use std:: path:: Path ;
20
20
use tedge_actors:: fan_in_message_type;
21
21
use tedge_actors:: Actor ;
22
+ use tedge_actors:: ClientMessageBox ;
22
23
use tedge_actors:: DynSender ;
23
24
use tedge_actors:: LoggingReceiver ;
24
25
use tedge_actors:: MessageReceiver ;
@@ -54,13 +55,11 @@ pub type OperationTimeout = Timeout<OperationKey>;
54
55
pub type IdDownloadResult = ( String , DownloadResult ) ;
55
56
pub type IdDownloadRequest = ( String , DownloadRequest ) ;
56
57
57
- fan_in_message_type ! ( FirmwareInput [ MqttMessage , OperationTimeout , IdDownloadResult ] : Debug ) ;
58
- fan_in_message_type ! ( FirmwareOutput [ MqttMessage , OperationSetTimeout , IdDownloadRequest ] : Debug ) ;
58
+ fan_in_message_type ! ( FirmwareInput [ MqttMessage , OperationTimeout ] : Debug ) ;
59
59
60
60
pub struct FirmwareManagerActor {
61
61
config : FirmwareManagerConfig ,
62
62
active_child_ops : HashMap < OperationKey , ActiveOperationState > ,
63
- reqs_pending_download : HashMap < String , SmartRestFirmwareRequest > ,
64
63
message_box : FirmwareManagerMessageBox ,
65
64
}
66
65
@@ -90,9 +89,6 @@ impl Actor for FirmwareManagerActor {
90
89
FirmwareInput :: OperationTimeout ( timeout) => {
91
90
self . process_operation_timeout ( timeout) . await ?;
92
91
}
93
- FirmwareInput :: IdDownloadResult ( ( id, result) ) => {
94
- self . process_downloaded_firmware ( & id, result) . await ?
95
- }
96
92
}
97
93
}
98
94
Ok ( ( ) )
@@ -104,7 +100,6 @@ impl FirmwareManagerActor {
104
100
Self {
105
101
config,
106
102
active_child_ops : HashMap :: new ( ) ,
107
- reqs_pending_download : HashMap :: new ( ) ,
108
103
message_box,
109
104
}
110
105
}
@@ -262,12 +257,13 @@ impl FirmwareManagerActor {
262
257
DownloadRequest :: new ( firmware_url, cache_file_path. as_std_path ( ) )
263
258
} ;
264
259
265
- self . message_box
260
+ let ( _, download_result) = self
261
+ . message_box
266
262
. download_sender
267
- . send ( ( operation_id. to_string ( ) , download_request) )
263
+ . await_response ( ( operation_id. to_string ( ) , download_request) )
264
+ . await ?;
265
+ self . process_downloaded_firmware ( operation_id, smartrest_request, download_result)
268
266
. await ?;
269
- self . reqs_pending_download
270
- . insert ( operation_id. to_string ( ) , smartrest_request) ;
271
267
}
272
268
Ok ( ( ) )
273
269
}
@@ -278,39 +274,32 @@ impl FirmwareManagerActor {
278
274
async fn process_downloaded_firmware (
279
275
& mut self ,
280
276
operation_id : & str ,
277
+ smartrest_request : SmartRestFirmwareRequest ,
281
278
download_result : DownloadResult ,
282
279
) -> Result < ( ) , FirmwareManagementError > {
283
- if let Some ( smartrest_request) = self . reqs_pending_download . remove ( operation_id) {
284
- let child_id = smartrest_request. device . clone ( ) ;
285
- match download_result {
286
- Ok ( response) => {
287
- if let Err ( err) =
288
- // Publish a firmware update request to child device.
289
- self
290
- . handle_firmware_update_request_with_downloaded_file (
291
- smartrest_request,
292
- operation_id,
293
- & response. file_path ,
294
- )
295
- . await
296
- {
297
- self . fail_operation_in_cloud (
298
- & child_id,
299
- Some ( operation_id) ,
300
- & err. to_string ( ) ,
280
+ let child_id = smartrest_request. device . clone ( ) ;
281
+ match download_result {
282
+ Ok ( response) => {
283
+ if let Err ( err) =
284
+ // Publish a firmware update request to child device.
285
+ self
286
+ . handle_firmware_update_request_with_downloaded_file (
287
+ smartrest_request,
288
+ operation_id,
289
+ & response. file_path ,
301
290
)
302
- . await ?;
303
- }
304
- }
305
- Err ( err) => {
306
- let firmware_url = smartrest_request. url ;
307
- let failure_reason = format ! ( "Download from {firmware_url} failed with {err}" ) ;
308
- self . fail_operation_in_cloud ( & child_id, Some ( operation_id) , & failure_reason)
291
+ . await
292
+ {
293
+ self . fail_operation_in_cloud ( & child_id, Some ( operation_id) , & err. to_string ( ) )
309
294
. await ?;
310
295
}
311
296
}
312
- } else {
313
- error ! ( "Unexpected: Download completed for unknown operation: {operation_id}" ) ;
297
+ Err ( err) => {
298
+ let firmware_url = smartrest_request. url ;
299
+ let failure_reason = format ! ( "Download from {firmware_url} failed with {err}" ) ;
300
+ self . fail_operation_in_cloud ( & child_id, Some ( operation_id) , & failure_reason)
301
+ . await ?;
302
+ }
314
303
}
315
304
Ok ( ( ) )
316
305
}
@@ -743,7 +732,7 @@ pub struct FirmwareManagerMessageBox {
743
732
mqtt_publisher : DynSender < MqttMessage > ,
744
733
jwt_retriever : JwtRetriever ,
745
734
timer_sender : DynSender < SetTimeout < OperationKey > > ,
746
- download_sender : DynSender < IdDownloadRequest > ,
735
+ download_sender : ClientMessageBox < IdDownloadRequest , IdDownloadResult > ,
747
736
}
748
737
749
738
impl FirmwareManagerMessageBox {
@@ -752,7 +741,7 @@ impl FirmwareManagerMessageBox {
752
741
mqtt_publisher : DynSender < MqttMessage > ,
753
742
jwt_retriever : JwtRetriever ,
754
743
timer_sender : DynSender < SetTimeout < OperationKey > > ,
755
- download_sender : DynSender < IdDownloadRequest > ,
744
+ download_sender : ClientMessageBox < IdDownloadRequest , IdDownloadResult > ,
756
745
) -> Self {
757
746
Self {
758
747
input_receiver,
0 commit comments