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