wip messages handling
This commit is contained in:
@@ -1,8 +1,22 @@
|
||||
mod cryptographic_signature;
|
||||
mod data;
|
||||
mod message_handling;
|
||||
mod messages_channels;
|
||||
mod messages_structure;
|
||||
mod registration;
|
||||
|
||||
use crate::{
|
||||
cryptographic_signature::CryptographicSignature,
|
||||
message_handling::EventType,
|
||||
messages_channels::{MultipleSenders, start_receving_thread},
|
||||
registration::{register_ip_addresses, register_with_the_server},
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{
|
||||
net::{SocketAddr, UdpSocket},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
/// Messages sent to the Network thread by the GUI.
|
||||
pub enum NetworkCommand {
|
||||
ConnectToServer(String), // ServerIP
|
||||
@@ -29,6 +43,8 @@ pub enum NetworkEvent {
|
||||
// ...
|
||||
}
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub use crate::data::*;
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
use sha2::{Digest, Sha256};
|
||||
@@ -84,6 +100,46 @@ pub fn start_p2p_executor(
|
||||
|
||||
// Actual server connection
|
||||
|
||||
let messages_list = HashMap::<i32, EventType>::new();
|
||||
|
||||
let username = String::from("Gamixtreize");
|
||||
|
||||
let crypto_pair = CryptographicSignature::new(username);
|
||||
|
||||
let socket = UdpSocket::bind("0.0.0.0:0").expect("bind failed");
|
||||
|
||||
let shared_socket = Arc::new(socket);
|
||||
let shared_cryptopair = Arc::new(crypto_pair);
|
||||
let shared_messageslist = Arc::new(Mutex::new(messages_list));
|
||||
|
||||
let senders = MultipleSenders::new(1, &shared_socket);
|
||||
|
||||
let shared_senders = Arc::new(senders);
|
||||
if let Err(e) = register_with_the_server(&shared_cryptopair, &ip).await {
|
||||
eprintln!("request failed: {}", e);
|
||||
}
|
||||
|
||||
match SocketAddr::from_str(&ip) {
|
||||
Ok(sockaddr) => {
|
||||
start_receving_thread(
|
||||
&shared_socket,
|
||||
&shared_messageslist,
|
||||
&shared_cryptopair,
|
||||
sockaddr,
|
||||
&shared_senders,
|
||||
);
|
||||
register_ip_addresses(
|
||||
&shared_cryptopair,
|
||||
ip,
|
||||
&shared_senders,
|
||||
&shared_messageslist,
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("failed to parse socket address: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_millis(5000)).await;
|
||||
|
||||
let res = event_tx.send(NetworkEvent::Connected());
|
||||
|
||||
Reference in New Issue
Block a user