Skip to content

Commit 7e09c16

Browse files
authored
chore(vpnx): watch vpn status (#482)
1 parent dedc06f commit 7e09c16

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

nym-vpn-x/src-tauri/src/grpc/client.rs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use time::OffsetDateTime;
1717
use tokio::sync::mpsc;
1818
use tonic::transport::Endpoint as TonicEndpoint;
1919
use tonic::{transport::Channel, Request};
20-
use tracing::{debug, error, info, instrument, warn};
20+
use tracing::{debug, error, info, instrument, trace, warn};
2121
use ts_rs::TS;
2222

2323
use crate::cli::Cli;
@@ -175,9 +175,9 @@ impl GrpcClient {
175175
Ok(())
176176
}
177177

178-
/// Watch VPN status updates
178+
/// Watch VPN state updates
179179
#[instrument(skip_all)]
180-
pub async fn watch_vpn_status(&self, app: &AppHandle) -> Result<()> {
180+
pub async fn watch_vpn_state(&self, app: &AppHandle) -> Result<()> {
181181
let mut vpnd = self.vpnd().await?;
182182

183183
let request = Request::new(Empty {});
@@ -197,25 +197,25 @@ impl GrpcClient {
197197
tx.send(update).await.unwrap();
198198
}
199199
Ok(None) => {
200-
warn!("watch vpn status stream closed by the server");
200+
warn!("watch vpn state stream closed by the server");
201201
return;
202202
}
203203
Err(e) => {
204-
warn!("watch vpn status stream get a grpc error: {}", e);
204+
warn!("watch vpn state stream get a grpc error: {}", e);
205205
}
206206
}
207207
}
208208
});
209209

210-
while let Some(status) = rx.recv().await {
211-
debug!("vpn status update {:?}", status.status());
212-
if let Some(e) = status.error.as_ref() {
210+
while let Some(state) = rx.recv().await {
211+
debug!("vpn state update {:?}", state.status());
212+
if let Some(e) = state.error.as_ref() {
213213
warn!("vpn status error: {}", e.message);
214214
}
215215
vpn_status::update(
216216
app,
217-
ConnectionState::from(status.status()),
218-
status.error.map(BackendError::from),
217+
ConnectionState::from(state.status()),
218+
state.error.map(BackendError::from),
219219
None,
220220
)
221221
.await?;
@@ -224,6 +224,53 @@ impl GrpcClient {
224224
Ok(())
225225
}
226226

227+
/// Watch VPN status updates
228+
#[instrument(skip_all)]
229+
pub async fn watch_vpn_status(&self) -> Result<()> {
230+
let mut vpnd = self.vpnd().await?;
231+
232+
let request = Request::new(Empty {});
233+
let mut stream = vpnd
234+
.listen_to_connection_status(request)
235+
.await
236+
.inspect_err(|e| {
237+
error!("listen_to_connection_status failed: {}", e);
238+
})?
239+
.into_inner();
240+
241+
let (tx, mut rx) = mpsc::channel(32);
242+
tokio::spawn(async move {
243+
loop {
244+
match stream.message().await {
245+
Ok(Some(update)) => {
246+
tx.send(update).await.unwrap();
247+
}
248+
Ok(None) => {
249+
warn!("watch vpn status stream closed by the server");
250+
return;
251+
}
252+
Err(e) => {
253+
warn!("watch vpn status stream get a grpc error: {}", e);
254+
}
255+
}
256+
}
257+
});
258+
259+
while let Some(status) = rx.recv().await {
260+
// TODO handle status updates
261+
debug!(
262+
"vpn status update {:?}, {:?}",
263+
status.kind(),
264+
status.message
265+
);
266+
if !status.details.is_empty() {
267+
trace!("vpn status details: {:?}", status.details);
268+
}
269+
}
270+
271+
Ok(())
272+
}
273+
227274
/// Connect to the VPN
228275
#[instrument(skip_all)]
229276
pub async fn vpn_connect(

nym-vpn-x/src-tauri/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ async fn main() -> Result<()> {
195195
info!("starting vpn status watch");
196196
loop {
197197
if c_grpc.refresh_vpn_status(&handle).await.is_ok() {
198-
c_grpc.watch_vpn_status(&handle).await.ok();
198+
c_grpc.watch_vpn_state(&handle).await.ok();
199+
c_grpc.watch_vpn_status().await.ok();
199200
}
200201
sleep(VPND_RETRY_INTERVAL).await;
201202
debug!("vpn status watch retry");

0 commit comments

Comments
 (0)