mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +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
|
// insert default values as required
|
||||||
let name = name.unwrap_or(DEFAULT_DEVICE_NAME);
|
let name = name.unwrap_or(DEFAULT_DEVICE_NAME);
|
||||||
let host = host.unwrap_or(DEFAULT_HOST_NAME);
|
let host = host.unwrap_or(DEFAULT_HOST_NAME);
|
||||||
match SignalWS::provision(&host) {
|
match SignalWS::new_provision(&host) {
|
||||||
Ok(ws) => {
|
Ok(ws) => {
|
||||||
if let Some(prior_ws) = &mut self.websocket {
|
if let Some(prior_ws) = &mut self.websocket {
|
||||||
prior_ws.close();
|
prior_ws.close();
|
||||||
|
|
|
||||||
|
|
@ -2,60 +2,51 @@ use rustls::{ClientConnection, StreamOwned};
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use tls::Tls;
|
use tls::Tls;
|
||||||
use tungstenite::WebSocket;
|
use tungstenite::{Message, WebSocket};
|
||||||
|
|
||||||
const PROVISIONING_PATH: &str = "/v1/websocket/provisioning/";
|
const PROVISIONING_PATH: &str = "/v1/websocket/provisioning/";
|
||||||
const REGISTRATION_PATH: &str = "/v1/registration";
|
const REGISTRATION_PATH: &str = "/v1/registration";
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub enum SignalWS {
|
pub struct SignalWS {
|
||||||
Message {
|
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
||||||
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
|
||||||
},
|
|
||||||
Provision {
|
|
||||||
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
|
||||||
},
|
|
||||||
Register {
|
|
||||||
ws: WebSocket<StreamOwned<ClientConnection, TcpStream>>,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SignalWS {
|
impl SignalWS {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn message(_host: &str) -> Result<Self, Error> {
|
pub fn new_message(_host: &str) -> Result<Self, Error> {
|
||||||
todo!();
|
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 server = format!("wss://{host}{}", PROVISIONING_PATH);
|
||||||
// let mut conn = client_connection(&server);
|
|
||||||
match SignalWS::connect(server) {
|
match SignalWS::connect(server) {
|
||||||
Ok(ws) => Ok(Self::Provision { ws }),
|
Ok(ws) => Ok(Self { ws }),
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[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 server = format!("wss://{host}{}", REGISTRATION_PATH);
|
||||||
// let mut conn = client_connection(&server);
|
|
||||||
match SignalWS::connect(server) {
|
match SignalWS::connect(server) {
|
||||||
Ok(ws) => Ok(Self::Register { ws }),
|
Ok(ws) => Ok(Self { ws }),
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&mut self) {
|
pub fn close(&mut self) {
|
||||||
match self {
|
self.ws
|
||||||
SignalWS::Message { ws, .. }
|
.close(None)
|
||||||
| SignalWS::Provision { ws, .. }
|
.unwrap_or_else(|e| log::warn!("failed to close websocket: {e}"));
|
||||||
| SignalWS::Register { ws, .. } => {
|
// TODO close properly https://docs.rs/tungstenite/0.20.1/tungstenite/protocol/struct.WebSocket.html
|
||||||
ws.close(None)
|
self.ws
|
||||||
.unwrap_or_else(|e| log::warn!("failed to close websocket: {e}"));
|
.flush()
|
||||||
// TODO close properly https://docs.rs/tungstenite/0.20.1/tungstenite/protocol/struct.WebSocket.html
|
.unwrap_or_else(|e| log::warn!("failed to flush websocket: {e}"));
|
||||||
ws.flush()
|
}
|
||||||
.unwrap_or_else(|e| log::warn!("failed to flush websocket: {e}"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue