mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
restructure SignalWS
This commit is contained in:
parent
96a59422b1
commit
a8547ce51c
2 changed files with 20 additions and 29 deletions
|
|
@ -135,7 +135,7 @@ impl Manager {
|
|||
// insert default values as required
|
||||
let name = name.unwrap_or(DEFAULT_DEVICE_NAME);
|
||||
let host = host.unwrap_or(DEFAULT_HOST_NAME);
|
||||
match SignalWS::provision(&host) {
|
||||
match SignalWS::new_provision(&host) {
|
||||
Ok(ws) => {
|
||||
if let Some(prior_ws) = &mut self.websocket {
|
||||
prior_ws.close();
|
||||
|
|
|
|||
|
|
@ -2,60 +2,51 @@ use rustls::{ClientConnection, StreamOwned};
|
|||
use std::io::{Error, ErrorKind};
|
||||
use std::net::TcpStream;
|
||||
use tls::Tls;
|
||||
use tungstenite::WebSocket;
|
||||
use tungstenite::{Message, WebSocket};
|
||||
|
||||
const PROVISIONING_PATH: &str = "/v1/websocket/provisioning/";
|
||||
const REGISTRATION_PATH: &str = "/v1/registration";
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub enum SignalWS {
|
||||
Message {
|
||||
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
||||
},
|
||||
Provision {
|
||||
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
||||
},
|
||||
Register {
|
||||
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
||||
},
|
||||
pub struct SignalWS {
|
||||
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
||||
}
|
||||
|
||||
impl SignalWS {
|
||||
#[allow(dead_code)]
|
||||
pub fn message(_host: &str) -> Result<Self, Error> {
|
||||
pub fn new_message(_host: &str) -> Result<Self, Error> {
|
||||
todo!();
|
||||
}
|
||||
|
||||
pub fn provision(host: &str) -> Result<Self, Error> {
|
||||
pub fn new_provision(host: &str) -> Result<Self, Error> {
|
||||
let server = format!("wss://{host}{}", PROVISIONING_PATH);
|
||||
// let mut conn = client_connection(&server);
|
||||
match SignalWS::connect(server) {
|
||||
Ok(ws) => Ok(Self::Provision { ws }),
|
||||
Ok(ws) => Ok(Self { ws }),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn register(host: &str) -> Result<Self, Error> {
|
||||
pub fn new_register(host: &str) -> Result<Self, Error> {
|
||||
let server = format!("wss://{host}{}", REGISTRATION_PATH);
|
||||
// let mut conn = client_connection(&server);
|
||||
match SignalWS::connect(server) {
|
||||
Ok(ws) => Ok(Self::Register { ws }),
|
||||
Ok(ws) => Ok(Self { ws }),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
match self {
|
||||
SignalWS::Message { ws, .. }
|
||||
| SignalWS::Provision { ws, .. }
|
||||
| SignalWS::Register { ws, .. } => {
|
||||
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}"));
|
||||
}
|
||||
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}"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue