diff --git a/src/main.rs b/src/main.rs index 997b063..4717ed3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -142,12 +142,16 @@ struct Event { } impl Event { - fn from_match_list_response(response: BackendMessage) -> Event { + fn new(name: String) -> Event { Event{ - name: String::from(""), + name, divisions: Vec::new(), } } + + fn parse_match_list(self: &Event, match_list: BackendMessage) { + let divisions: Vec = Vec::new(); + } } struct Division { @@ -190,10 +194,7 @@ impl BackendMessage { request_id: u32::from_le_bytes(bytes[0..4].try_into().unwrap()), data, }), - Err(error) => { - println!("Protobuf parse error: {:?}", error); - return None; - }, + Err(_) => None, } } @@ -442,7 +443,7 @@ impl TMClient { log::debug!("Sent: {:?}", packet); self.last_seq_num += 1; }, - Err(error) => println!("Request send error: {:?}", error), + Err(error) => log::error!("Request send error: {:?}", error), } } } @@ -464,32 +465,27 @@ impl TMClient { let ack = BackendPacket::new(packet.header, packet.timestamp, 5, self.last_seq_num+1, notice.notice_id.to_le_bytes().to_vec()); self.last_seq_num += 1; match self.stream.write(&ack.as_bytes()) { - Ok(_) => println!("Sent ACK for notice {}", notice.notice_id), - Err(error) => println!("ACK error: {:?}", error), + Ok(_) => log::debug!("Sent ACK for notice {}", notice.notice_id), + Err(error) => log::error!("ACK error: {:?}", error), } match self.notices.send(Box::new(notice.notice)) { - Ok(_) => println!("Forwarded notice to callback engine"), - Err(error) => println!("Notice forward error {:?}", error), + Ok(_) => log::debug!("Forwarded notice to callback engine"), + Err(error) => log::error!("Notice forward error {:?}", error), } }, - None => println!("Notice parse error: {:?}", packet), + None => log::error!("Notice parse error: {:?}", packet), } }, // Response message 3 => { match BackendMessage::from_bytes(packet.data.clone()) { Some(message) => { - println!("About to send"); match self.responses.send(Box::new(message)) { - Ok(_) => { - println!("Forwarded response to callback engine"); - } - Err(error) => { - println!("Response forward error {:?}", error) - } + Ok(_) => log::debug!("Forwarded response to callback engine"), + Err(error) => log::error!("Response forward error {:?}", error), } }, - None => println!("BackendMessage parse error: {:?}", packet), + None => log::error!("BackendMessage parse error: {:?}", packet), } }, // Server Message @@ -500,30 +496,30 @@ impl TMClient { let connect_response = ConnectMsg::from_welcome(welcome_msg, &self.password, self.uuid, self.client_name, self.username); let response = BackendPacket::new(packet.header, packet.timestamp, packet.msg_type, self.last_seq_num+1, connect_response.as_bytes()); match self.stream.write(&response.as_bytes()) { - Err(error) => println!("Send error: {:?}", error), + Err(error) => log::error!("Send error: {:?}", error), Ok(_) => self.last_seq_num += 1, } } else if welcome_msg.state_valid == 0 { - println!("pw_valid but not state_valid"); + log::error!("pw_valid but not state_valid"); } else { self.connected = true; - println!("Connected to TM backend!"); + log::info!("Connected to TM backend!"); } }, - None => println!("Failed to parse welcome msg"), + None => log::error!("Failed to parse welcome msg"), } }, - _ => println!("Unhandled message type: {}", packet.msg_type), + _ => log::warn!("Unhandled message type: {}", packet.msg_type), } }, None => { - println!("Failed to parse BackendPacket({}): {}", read, String::from_utf8_lossy(&incoming)); + log::error!("Failed to parse BackendPacket({}): {}", read, String::from_utf8_lossy(&incoming)); // Sleep to prevent busy loop when TM is spamming 0 length packets thread::sleep(Duration::from_millis(100)); } } }, - Err(error) => {}, + Err(_) => {}, } } @@ -674,6 +670,11 @@ fn main() { } ); + let get_event_name_req = BackendMessage::new(101, tm::BackendMessageData::default()); + tm_connection.requests.send(Box::new(get_event_name_req)).unwrap(); + let get_event_name_resp = tm_connection.responses.recv().unwrap(); + + let mut event = Event::new(String::from(get_event_name_resp.data.event_config.unwrap().event_name.unwrap())); let mut get_match_list_tuple = tm::MatchTuple::default(); get_match_list_tuple.division = Some(0); get_match_list_tuple.round = None; @@ -688,9 +689,8 @@ fn main() { tm_connection.requests.send(Box::new(get_match_list_req)).unwrap(); let get_match_list_resp = tm_connection.responses.recv().unwrap(); - println!("Get Match List Response: {:?}", get_match_list_resp); - let mut event = Event::from_match_list_response(*get_match_list_resp); + event.parse_match_list(*get_match_list_resp); while running { thread::sleep(Duration::from_millis(1000)); @@ -700,8 +700,8 @@ fn main() { match callback { None => { match notice.id { - None => println!("Notice without NoticeId received"), - Some(notice_id) => println!("Unhandled NoticeId: {}", notice_id), + None => log::error!("Notice without NoticeId received"), + Some(notice_id) => log::warn!("Unhandled NoticeId: {}", notice_id), } }, Some(callback) => { @@ -711,13 +711,13 @@ fn main() { let result = client.publish(message.topic, QoS::AtMostOnce, true, message.payload); match result { Ok(_) => {}, - Err(error) => println!("Publish error: {}", error), + Err(error) => log::error!("Publish error: {}", error), } } }, } }, - Err(error) => println!("Notice recv error: {}", error), + Err(error) => log::error!("Notice recv error: {}", error), } }