@@ -25,6 +25,30 @@ impl HostApiHandler {
2525 }
2626}
2727
28+ async fn dispatch_and_wait < T , E > (
29+ dispatcher : & mpsc:: Sender < Command > ,
30+ command_constructor : impl FnOnce ( oneshot:: Sender < Result < T , E > > ) -> Command ,
31+ ) -> Result < Response < T > , Status >
32+ where
33+ E : Into < Status > ,
34+ {
35+ let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
36+ let cmd = command_constructor ( resp_tx) ;
37+
38+ dispatcher
39+ . send ( cmd)
40+ . await
41+ . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
42+
43+ match resp_rx. await {
44+ Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
45+ Ok ( Err ( e) ) => Err ( e. into ( ) ) ,
46+ Err ( _) => Err ( Status :: internal (
47+ "Dispatcher task dropped response channel." ,
48+ ) ) ,
49+ }
50+ }
51+
2852#[ tonic:: async_trait]
2953impl HostService for HostApiHandler {
3054 type StreamKernelLogsStream =
@@ -36,146 +60,64 @@ impl HostService for HostApiHandler {
3660 _request : Request < HostnameRequest > ,
3761 ) -> Result < Response < HostnameResponse > , Status > {
3862 info ! ( "HostApi: Received Hostname request." ) ;
39- let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
40- let cmd = Command :: GetHostname ( resp_tx) ;
41- self . dispatcher_tx
42- . send ( cmd)
43- . await
44- . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
45-
46- match resp_rx. await {
47- Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
48- Ok ( Err ( status) ) => Err ( status) ,
49- Err ( _) => Err ( Status :: internal (
50- "Dispatcher task dropped response channel." ,
51- ) ) ,
52- }
63+ dispatch_and_wait ( & self . dispatcher_tx , Command :: GetHostname ) . await
5364 }
5465
5566 async fn get_memory (
5667 & self ,
5768 _request : Request < MemoryRequest > ,
5869 ) -> Result < Response < MemoryResponse > , Status > {
5970 info ! ( "HostApi: Received GetMemory request." ) ;
60- let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
61- let cmd = Command :: GetMemory ( resp_tx) ;
62- self . dispatcher_tx
63- . send ( cmd)
64- . await
65- . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
66-
67- match resp_rx. await {
68- Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
69- Ok ( Err ( status) ) => Err ( status) ,
70- Err ( _) => Err ( Status :: internal (
71- "Dispatcher task dropped response channel." ,
72- ) ) ,
73- }
71+ dispatch_and_wait ( & self . dispatcher_tx , Command :: GetMemory ) . await
7472 }
7573
7674 async fn get_cpu_info (
7775 & self ,
7876 _request : Request < GetCpuInfoRequest > ,
7977 ) -> Result < Response < GetCpuInfoResponse > , Status > {
8078 info ! ( "HostApi: Received GetCPUInfo request." ) ;
81- let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
82- let cmd = Command :: GetCPUInfo ( resp_tx) ;
83- self . dispatcher_tx
84- . send ( cmd)
85- . await
86- . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
87-
88- match resp_rx. await {
89- Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
90- Ok ( Err ( status) ) => Err ( status) ,
91- Err ( _) => Err ( Status :: internal (
92- "Dispatcher task dropped response channel." ,
93- ) ) ,
94- }
79+ dispatch_and_wait ( & self . dispatcher_tx , Command :: GetCPUInfo ) . await
9580 }
9681
9782 async fn get_network_info (
9883 & self ,
9984 _request : Request < GetNetworkInfoRequest > ,
10085 ) -> Result < Response < GetNetworkInfoResponse > , Status > {
10186 info ! ( "HostApi: Received GetNetworkInfo request." ) ;
102- let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
103- let cmd = Command :: GetNetworkInfo ( resp_tx) ;
104- self . dispatcher_tx
105- . send ( cmd)
106- . await
107- . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
108-
109- match resp_rx. await {
110- Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
111- Ok ( Err ( status) ) => Err ( status) ,
112- Err ( _) => Err ( Status :: internal (
113- "Dispatcher task dropped response channel." ,
114- ) ) ,
115- }
87+ dispatch_and_wait ( & self . dispatcher_tx , Command :: GetNetworkInfo ) . await
11688 }
11789
11890 async fn shutdown (
11991 & self ,
12092 request : Request < ShutdownRequest > ,
12193 ) -> Result < Response < ShutdownResponse > , Status > {
12294 info ! ( "HostApi: Received Shutdown request." ) ;
123- let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
124- let cmd = Command :: Shutdown ( request. into_inner ( ) , resp_tx) ;
125- self . dispatcher_tx
126- . send ( cmd)
127- . await
128- . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
129-
130- match resp_rx. await {
131- Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
132- Ok ( Err ( status) ) => Err ( status) ,
133- Err ( _) => Err ( Status :: internal (
134- "Dispatcher task dropped response channel." ,
135- ) ) ,
136- }
95+ dispatch_and_wait ( & self . dispatcher_tx , |resp_tx| {
96+ Command :: Shutdown ( request. into_inner ( ) , resp_tx)
97+ } )
98+ . await
13799 }
138100
139101 async fn reboot (
140102 & self ,
141103 request : Request < RebootRequest > ,
142104 ) -> Result < Response < RebootResponse > , Status > {
143105 info ! ( "HostApi: Received Reboot request." ) ;
144- let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
145- let cmd = Command :: Reboot ( request. into_inner ( ) , resp_tx) ;
146- self . dispatcher_tx
147- . send ( cmd)
148- . await
149- . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
150-
151- match resp_rx. await {
152- Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
153- Ok ( Err ( status) ) => Err ( status) ,
154- Err ( _) => Err ( Status :: internal (
155- "Dispatcher task dropped response channel." ,
156- ) ) ,
157- }
106+ dispatch_and_wait ( & self . dispatcher_tx , |resp_tx| {
107+ Command :: Reboot ( request. into_inner ( ) , resp_tx)
108+ } )
109+ . await
158110 }
159111
160112 async fn upgrade_feos_binary (
161113 & self ,
162114 request : Request < UpgradeFeosBinaryRequest > ,
163115 ) -> Result < Response < UpgradeFeosBinaryResponse > , Status > {
164116 info ! ( "HostApi: Received UpgradeFeosBinary request." ) ;
165- let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
166- let cmd = Command :: UpgradeFeosBinary ( request. into_inner ( ) , resp_tx) ;
167- self . dispatcher_tx
168- . send ( cmd)
169- . await
170- . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
171-
172- match resp_rx. await {
173- Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
174- Ok ( Err ( status) ) => Err ( status) ,
175- Err ( _) => Err ( Status :: internal (
176- "Dispatcher task dropped response channel." ,
177- ) ) ,
178- }
117+ dispatch_and_wait ( & self . dispatcher_tx , |resp_tx| {
118+ Command :: UpgradeFeosBinary ( request. into_inner ( ) , resp_tx)
119+ } )
120+ . await
179121 }
180122
181123 async fn stream_kernel_logs (
@@ -213,19 +155,6 @@ impl HostService for HostApiHandler {
213155 _request : Request < GetVersionInfoRequest > ,
214156 ) -> Result < Response < GetVersionInfoResponse > , Status > {
215157 info ! ( "HostApi: Received GetVersionInfo request." ) ;
216- let ( resp_tx, resp_rx) = oneshot:: channel ( ) ;
217- let cmd = Command :: GetVersionInfo ( resp_tx) ;
218- self . dispatcher_tx
219- . send ( cmd)
220- . await
221- . map_err ( |e| Status :: internal ( format ! ( "Failed to send command to dispatcher: {e}" ) ) ) ?;
222-
223- match resp_rx. await {
224- Ok ( Ok ( result) ) => Ok ( Response :: new ( result) ) ,
225- Ok ( Err ( status) ) => Err ( status) ,
226- Err ( _) => Err ( Status :: internal (
227- "Dispatcher task dropped response channel." ,
228- ) ) ,
229- }
158+ dispatch_and_wait ( & self . dispatcher_tx , Command :: GetVersionInfo ) . await
230159 }
231160}
0 commit comments