use std::error::Error;
use std::net::SocketAddr;
use std::collections::{VecDeque, HashSet, HashMap};
use std::task::{Context, Poll};
use std::future::Future;
use std::task::Waker;
use std::pin::Pin;
use std::time::{Instant, Duration};
use serde::{Serialize, Deserialize};
use bincode;
use rand::Rng;
use tokio::io::ReadBuf;
use tokio::net::UdpSocket;
use tokio::sync::Mutex;
use super::*;
use crate::crypto::history::*;
use crate::peer::*;
const RECEIVE_BUFFER_SIZE: usize = 1220;
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct MeshContacts {
static_nodes: Vec<(Node, u16)>,
dynamic_nodes: Vec<(Node, u16)>
}
impl MeshContacts {
pub(crate) fn serialize(&self) -> Vec<u8> {
bincode::serialize(&self).unwrap()
}
#[track_caller]
pub(crate) fn deserialize(bytes: Vec<u8>) -> MeshContacts {
match bincode::deserialize(&bytes) {
Ok(contacts) => contacts,
Err(_) => panic!("Wrong size of `MeshContacts`"),
}
}
}
struct Scheduler {
echoed: Instant,
rtt: Duration
}
pub struct MbpDriver {
socket: UdpSocket,
static_nodes: Vec<(Node, u16)>,
dynamic_nodes: Vec<(Node, u16)>,
nodes_scheduler: HashMap<Node, Scheduler>
}
impl MbpDriver {
pub fn new(
socket: UdpSocket,
static_nodes: Vec<(Node, u16)>,
dynamic_nodes: Vec<(Node, u16)>
) -> MbpDriver {
MbpDriver {
socket,
static_nodes,
dynamic_nodes,
nodes_scheduler: HashMap::new()
}
}
}
impl Future for MbpDriver {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
let mut buf_array = [0u8; RECEIVE_BUFFER_SIZE];
let mut readbuf = ReadBuf::new(&mut buf_array);
match self.socket.poll_recv_from(cx, &mut readbuf) {
Poll::Ready(Ok(src_socket)) => {
let packet = readbuf.filled().to_vec();
let header = Header::deserialize(packet[0..36].to_vec());
let contacts = MeshContacts::deserialize(packet[36..].to_vec());
if header.protocol_type == ProtocolType::MBP {
match header.packet_type {
PacketType::INIT => {
},
PacketType::ACK_INIT => {
},
PacketType::HI => {
},
PacketType::ACK_HI => {
},
PacketType::ECHO => {
},
_ => {
}
}
} else {
}
if let Some(contact) = self.static_nodes.iter()
.chain(self.dynamic_nodes.iter())
.find(|(node, port)|
node.addrs.lock().unwrap().satisfies(src_socket.ip())
&& port.to_owned() == src_socket.port()
) {
}
},
Poll::Ready(Err(_)) => {
continue;
},
Poll::Pending => {}
}
}
return Poll::Ready(());
}
}