|
|
|
@ -8,6 +8,14 @@ use prost::Message;
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
|
|
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
use std::io::BufReader;
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
|
|
|
|
|
use std::net::TcpStream;
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
// MQTT Topics:
|
|
|
|
|
// - division/{division_id}
|
|
|
|
|
// - division/{division_id}/ranking
|
|
|
|
@ -156,55 +164,97 @@ struct Match {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct TMClient {
|
|
|
|
|
stream: Arc<Mutex<openssl::ssl::SslStream<TcpStream>>>,
|
|
|
|
|
notices: mpsc::Sender<tm::Notice>,
|
|
|
|
|
requests: mpsc::Receiver<tm::Notice>,
|
|
|
|
|
responses: mpsc::Sender<tm::Notice>,
|
|
|
|
|
responses: mpsc::Sender<BackendMessage>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct TMConnection {
|
|
|
|
|
stream: Arc<Mutex<openssl::ssl::SslStream<TcpStream>>>,
|
|
|
|
|
notices: mpsc::Receiver<tm::Notice>,
|
|
|
|
|
requests: mpsc::Sender<tm::Notice>,
|
|
|
|
|
responses: mpsc::Receiver<tm::Notice>,
|
|
|
|
|
responses: mpsc::Receiver<BackendMessage>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct BackendMessage {
|
|
|
|
|
status: u8,
|
|
|
|
|
request_id: u32,
|
|
|
|
|
data: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BackendMessage {
|
|
|
|
|
fn from_bytes(bytes: Vec<u8>) -> Option<BackendMessage> {
|
|
|
|
|
if bytes.len() < 5 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_bytes(self: BackendMessage) -> Vec<u8> {
|
|
|
|
|
return Vec::new();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TMClient {
|
|
|
|
|
fn new() -> (TMClient, TMConnection) {
|
|
|
|
|
let (notice_tx, notice_rx) = mpsc::channel();
|
|
|
|
|
let (request_tx, request_rx) = mpsc::channel();
|
|
|
|
|
let (response_tx, response_rx) = mpsc::channel();
|
|
|
|
|
(TMClient{
|
|
|
|
|
|
|
|
|
|
let mut builder = openssl::ssl::SslConnector::builder(openssl::ssl::SslMethod::tls()).unwrap();
|
|
|
|
|
builder.set_ca_file("tm.crt").unwrap();
|
|
|
|
|
builder.set_verify(openssl::ssl::SslVerifyMode::PEER);
|
|
|
|
|
|
|
|
|
|
let connector = builder.build();
|
|
|
|
|
|
|
|
|
|
let stream = TcpStream::connect("127.0.0.1:5000").unwrap();
|
|
|
|
|
|
|
|
|
|
let mut stream_config = connector.configure().unwrap();
|
|
|
|
|
stream_config.set_verify_hostname(false);
|
|
|
|
|
stream_config.set_certificate_chain_file("tm.crt").unwrap();
|
|
|
|
|
stream_config.set_private_key_file("tm.crt", openssl::ssl::SslFiletype::PEM).unwrap();
|
|
|
|
|
stream_config.set_use_server_name_indication(false);
|
|
|
|
|
|
|
|
|
|
let stream = stream_config.connect("127.0.0.1", stream).unwrap();
|
|
|
|
|
let stream = Arc::new(Mutex::new(stream));
|
|
|
|
|
|
|
|
|
|
return (TMClient{
|
|
|
|
|
stream: stream.clone(),
|
|
|
|
|
notices: notice_tx,
|
|
|
|
|
requests: request_rx,
|
|
|
|
|
responses: response_tx
|
|
|
|
|
},
|
|
|
|
|
TMConnection{
|
|
|
|
|
stream: stream,
|
|
|
|
|
notices: notice_rx,
|
|
|
|
|
requests: request_tx,
|
|
|
|
|
responses: response_rx
|
|
|
|
|
},)
|
|
|
|
|
},);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process(self: &TMClient) {
|
|
|
|
|
// Check if there's any incoming requests to process(aka send)
|
|
|
|
|
match self.requests.try_recv() {
|
|
|
|
|
Ok(request) => println!("Got incoming request"),
|
|
|
|
|
Err(error) => {},
|
|
|
|
|
let mut incoming = [0; 2048];
|
|
|
|
|
let mut stream = self.stream.lock().unwrap();
|
|
|
|
|
match stream.read(&mut incoming) {
|
|
|
|
|
Ok(read) => {
|
|
|
|
|
println!("Data({}): {}", read, String::from_utf8_lossy(&incoming));
|
|
|
|
|
// If the data is a notice, parse it and send it to the notices mpsc
|
|
|
|
|
// If the data is a response, parse it and send it to the responses mpsc
|
|
|
|
|
},
|
|
|
|
|
Err(error) => println!("Error: {}", error),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do per-call processing(currently just pretending to receive an empty notice)
|
|
|
|
|
match self.notices.send(tm::Notice::default()) {
|
|
|
|
|
Ok(_) => println!("Received notice"),
|
|
|
|
|
Err(error) => println!("Recv error {}", error),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: make send functionality
|
|
|
|
|
fn send(request: BackendMessage) -> BackendMessage {
|
|
|
|
|
request
|
|
|
|
|
impl TMConnection {
|
|
|
|
|
fn send(self: &mut TMConnection, request: BackendMessage) -> Result<BackendMessage, std::io::Error> {
|
|
|
|
|
let mut stream = self.stream.lock().unwrap();
|
|
|
|
|
match stream.write(&request.as_bytes()) {
|
|
|
|
|
Ok(_) =>
|
|
|
|
|
match self.responses.recv() {
|
|
|
|
|
Ok(response) => Ok(response),
|
|
|
|
|
Err(_) => Err(std::io::Error::new(std::io::ErrorKind::ConnectionAborted, "channel closed")),
|
|
|
|
|
},
|
|
|
|
|
Err(error) => Err(error),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -307,6 +357,8 @@ fn on_match_list_update(notice: tm::Notice, event: Event) -> (Vec<MQTTMessage>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
|
|
let mut event = Event{
|
|
|
|
|
name: String::from(""),
|
|
|
|
|
divisions: Vec::new(),
|
|
|
|
|