mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
wrap SignalWS in Arc<Mutex<ws>>
This commit is contained in:
parent
05c991e3a2
commit
535ee5de11
1 changed files with 35 additions and 27 deletions
|
|
@ -1,6 +1,7 @@
|
|||
use rustls::{ClientConnection, StreamOwned};
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::net::TcpStream;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tls::Tls;
|
||||
use tungstenite::{Message, WebSocket};
|
||||
|
||||
|
|
@ -9,7 +10,7 @@ const REGISTRATION_PATH: &str = "/v1/registration";
|
|||
|
||||
#[allow(dead_code)]
|
||||
pub struct SignalWS {
|
||||
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
||||
ws: Arc<Mutex<WebSocket<StreamOwned<ClientConnection, TcpStream>>>>,
|
||||
}
|
||||
|
||||
impl SignalWS {
|
||||
|
|
@ -18,41 +19,50 @@ impl SignalWS {
|
|||
todo!();
|
||||
}
|
||||
|
||||
pub fn new_provision(host: &str) -> Result<Self, Error> {
|
||||
let server = format!("wss://{host}{}", PROVISIONING_PATH);
|
||||
fn new(server: &str) -> Result<Self, Error> {
|
||||
match SignalWS::connect(server) {
|
||||
Ok(ws) => Ok(Self { ws }),
|
||||
Ok(ws) => Ok(Self {
|
||||
ws: Arc::new(Mutex::new(ws)),
|
||||
}),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_provision(host: &str) -> Result<Self, Error> {
|
||||
Ok(Self::new(&format!("wss://{host}{}", PROVISIONING_PATH))?)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn new_register(host: &str) -> Result<Self, Error> {
|
||||
let server = format!("wss://{host}{}", REGISTRATION_PATH);
|
||||
match SignalWS::connect(server) {
|
||||
Ok(ws) => Ok(Self { ws }),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
Ok(Self::new(&format!("wss://{host}{}", REGISTRATION_PATH))?)
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
self.ws
|
||||
.close(None)
|
||||
.unwrap_or_else(|e| log::warn!("failed to close websocket: {e}"));
|
||||
// TODO close properly https://docs.rs/tungstenite/0.20.1/tungstenite/protocol/struct.WebSocket.html
|
||||
self.ws
|
||||
.flush()
|
||||
.unwrap_or_else(|e| log::warn!("failed to flush websocket: {e}"));
|
||||
if let Ok(mut ws) = self.ws.lock() {
|
||||
ws.close(None)
|
||||
.unwrap_or_else(|e| log::warn!("failed to close websocket: {e}"));
|
||||
// TODO close properly https://docs.rs/tungstenite/0.20.1/tungstenite/protocol/struct.WebSocket.html
|
||||
ws.flush()
|
||||
.unwrap_or_else(|e| log::warn!("failed to flush websocket: {e}"));
|
||||
};
|
||||
}
|
||||
|
||||
pub fn read(&mut self) -> Result<Message, Error> {
|
||||
match self.ws.read() {
|
||||
Ok(msg) => Ok(msg),
|
||||
Err(e) => {
|
||||
log::warn!("{e}");
|
||||
Err(Error::new(ErrorKind::Other, "error on reading ws"))
|
||||
let msg = if let Ok(mut ws) =self. ws.lock() {
|
||||
match ws.read() {
|
||||
Ok(msg) => Ok(msg),
|
||||
Err(e) => {
|
||||
log::warn!("{e}");
|
||||
Err(Error::new(ErrorKind::Other, "error on reading ws"))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Err(Error::new(
|
||||
ErrorKind::Other,
|
||||
"failed to get lock on websocket",
|
||||
))
|
||||
};
|
||||
msg
|
||||
}
|
||||
|
||||
/// Make a websocket connection to host server
|
||||
|
|
@ -62,11 +72,9 @@ impl SignalWS {
|
|||
///
|
||||
/// # Returns
|
||||
///
|
||||
fn connect(
|
||||
server: String,
|
||||
) -> Result<WebSocket<StreamOwned<ClientConnection, TcpStream>>, Error> {
|
||||
fn connect(server: &str) -> Result<WebSocket<StreamOwned<ClientConnection, TcpStream>>, Error> {
|
||||
log::info!("connecting websocket");
|
||||
match url::Url::parse(&server) {
|
||||
match url::Url::parse(server) {
|
||||
Ok(url) => {
|
||||
log::info!("attempting websocket connection to {}", url.as_str());
|
||||
let host = url.host_str().expect("failed to extract host from url");
|
||||
|
|
@ -107,4 +115,4 @@ impl SignalWS {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue