|
|
@ -6,19 +6,9 @@ extern crate failure; |
|
|
|
extern crate smart_default; |
|
|
|
extern crate smart_default; |
|
|
|
#[macro_use] |
|
|
|
#[macro_use] |
|
|
|
extern crate serde; |
|
|
|
extern crate serde; |
|
|
|
#[macro_use] |
|
|
|
|
|
|
|
use elefren::{ |
|
|
|
use websocket::{OwnedMessage}; |
|
|
|
helpers::cli, |
|
|
|
use std::thread; |
|
|
|
prelude::*, |
|
|
|
|
|
|
|
entities::prelude::*, |
|
|
|
|
|
|
|
http_send::HttpSender, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use failure::Fallible; |
|
|
|
|
|
|
|
use native_tls::TlsConnector; |
|
|
|
|
|
|
|
use std::io::{Read, Write}; |
|
|
|
|
|
|
|
use std::net::TcpStream; |
|
|
|
|
|
|
|
use websocket::{OwnedMessage, ClientBuilder}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mod bootstrap; |
|
|
|
mod bootstrap; |
|
|
|
mod store; |
|
|
|
mod store; |
|
|
@ -27,13 +17,18 @@ mod ele; |
|
|
|
use crate::bootstrap::handle_cli_args_and_load_config; |
|
|
|
use crate::bootstrap::handle_cli_args_and_load_config; |
|
|
|
|
|
|
|
|
|
|
|
use crate::store::Store; |
|
|
|
use crate::store::Store; |
|
|
|
|
|
|
|
use crate::ele::{EleSession, EleStreamSocket}; |
|
|
|
|
|
|
|
use std::time::Duration; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(SmartDefault,Serialize,Deserialize,Debug)] |
|
|
|
#[derive(SmartDefault,Serialize,Deserialize,Debug)] |
|
|
|
#[serde(default)] |
|
|
|
#[serde(default)] |
|
|
|
pub struct Config { |
|
|
|
pub struct Config { |
|
|
|
|
|
|
|
/// Logging level to use (can be increased using -v flags)
|
|
|
|
#[default="info"] |
|
|
|
#[default="info"] |
|
|
|
pub logging: String, |
|
|
|
pub logging: String, |
|
|
|
|
|
|
|
/// Instance domain name, e.g. example.com
|
|
|
|
pub instance: String, |
|
|
|
pub instance: String, |
|
|
|
|
|
|
|
/// File to use for session storage
|
|
|
|
#[default="manabu_store.json"] |
|
|
|
#[default="manabu_store.json"] |
|
|
|
pub store: String, |
|
|
|
pub store: String, |
|
|
|
} |
|
|
|
} |
|
|
@ -45,6 +40,7 @@ fn main() { |
|
|
|
|
|
|
|
|
|
|
|
let mut store = Store::from_file(&config.store); |
|
|
|
let mut store = Store::from_file(&config.store); |
|
|
|
store.set_autosave(true); |
|
|
|
store.set_autosave(true); |
|
|
|
|
|
|
|
store.set_pretty_print(true); |
|
|
|
|
|
|
|
|
|
|
|
let registered = ele::register(&mut store, &config); |
|
|
|
let registered = ele::register(&mut store, &config); |
|
|
|
|
|
|
|
|
|
|
@ -52,25 +48,43 @@ fn main() { |
|
|
|
|
|
|
|
|
|
|
|
debug!("Listening to events"); |
|
|
|
debug!("Listening to events"); |
|
|
|
|
|
|
|
|
|
|
|
let mut socket = ele::open_stream_websocket(&session, "user"); |
|
|
|
let m_session = session.clone(); |
|
|
|
|
|
|
|
let handle = thread::spawn(move || handle_stream(m_session)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let _ = handle.join(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn handle_stream(session : EleSession) -> ! { |
|
|
|
|
|
|
|
loop { |
|
|
|
|
|
|
|
debug!("Trying to open websocket"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match ele::open_stream_websocket(&session, "user") { |
|
|
|
|
|
|
|
Ok(socket) => process_incoming_events(socket), |
|
|
|
|
|
|
|
Err(e) => error!("Error opening socket: {}", e) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
debug!("Delay before reconnecting..."); |
|
|
|
|
|
|
|
thread::sleep(Duration::from_secs(10)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for m in socket.incoming_messages() { |
|
|
|
fn process_incoming_events(mut socket : EleStreamSocket) { |
|
|
|
|
|
|
|
'listen: for m in socket.incoming_messages() { |
|
|
|
match m { |
|
|
|
match m { |
|
|
|
Ok(OwnedMessage::Text(text)) => { |
|
|
|
Ok(OwnedMessage::Text(text)) => { |
|
|
|
debug!("Got msg: {}", text); |
|
|
|
debug!("Got msg: {}", text); |
|
|
|
}, |
|
|
|
}, |
|
|
|
Ok(OwnedMessage::Close(text)) => { |
|
|
|
Ok(OwnedMessage::Close(_text)) => { |
|
|
|
debug!("Close"); |
|
|
|
debug!("Close"); |
|
|
|
break; |
|
|
|
break 'listen; |
|
|
|
}, |
|
|
|
}, |
|
|
|
Ok(any) => { |
|
|
|
Ok(any) => { |
|
|
|
debug!("Unhandled msg: {:?}", any); |
|
|
|
debug!("Unhandled msg: {:?}", any); |
|
|
|
} |
|
|
|
} |
|
|
|
Err(e) => { |
|
|
|
Err(e) => { |
|
|
|
error!("{}", e); |
|
|
|
error!("Error reading from socket: {}", e); |
|
|
|
break; |
|
|
|
break 'listen; |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
info!("Exit."); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|