Skip to content

Commit

Permalink
align number of parameter types with libtransport crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime2 committed Sep 16, 2019
1 parent 519249d commit a3f89e6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
29 changes: 23 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ impl<Data> TCPtransportCfg<Data> {

/// Struct which will be implementing the Transport trait.
/// Requires transportconfig to be wrapped in a mutex + arc combination.
pub struct TCPtransport<Data> {
pub struct TCPtransport<Id, Data, E, PL> {
// The configuration struct (defined above)
config: Arc<Mutex<TCPtransportCfg<Data>>>,
quit_tx: Sender<()>,
server_handle: Option<JoinHandle<()>>,
id: PhantomData<Id>,
e: PhantomData<E>,
pl: PhantomData<PL>,
}

/// Checks for quit messages, as well as wake method availabilities. If a wake method becomes available,
Expand Down Expand Up @@ -119,7 +122,7 @@ where
}

/// Specify what occurs when TCPtransport is dropped.
impl<Data> Drop for TCPtransport<Data> {
impl<Id, Data, E, PL> Drop for TCPtransport<Id, Data, E, PL> {
fn drop(&mut self) {
// Send quit message.
self.quit_tx.send(()).unwrap();
Expand All @@ -128,7 +131,7 @@ impl<Data> Drop for TCPtransport<Data> {
}

/// Implementation of the Transport trait.
impl<Id, Pe, Data: 'static, E, PL> Transport<Id, Data, E, PL> for TCPtransport<Data>
impl<Id, Pe, Data: 'static, E, PL> Transport<Id, Data, E, PL> for TCPtransport<Id, Data, E, PL>
where
Data: Serialize + DeserializeOwned + Send + Clone,
Id: PeerId,
Expand Down Expand Up @@ -157,6 +160,9 @@ where
quit_tx: tx,
server_handle: Some(handle),
config: cfg_mutexed,
id: PhantomData,
e: PhantomData,
pl: PhantomData,
})
}

Expand Down Expand Up @@ -223,11 +229,11 @@ where
}
}
/// Allow TCPtransport to be store in Pin (for async usage)
impl<D> Unpin for TCPtransport<D> {}
impl<Id, D, E, PL> Unpin for TCPtransport<Id, D, E, PL> {}

/// Implement stream trait to allow TCPTransport to be used asynchronously. Allows for multiple
/// values to be yielded by a future.
impl<Data> Stream for TCPtransport<Data>
impl<Id, Data, E, PL> Stream for TCPtransport<Id, Data, E, PL>
where
Data: DeserializeOwned,
{
Expand Down Expand Up @@ -307,6 +313,17 @@ mod tests {
String::from("127.0.0.1:9001"),
String::from("127.0.0.1:9002"),
];
lits::common_test::<TCPtransport<lits::Data>>(a);
match lits::common_test::<
TCPtransport<
lits::Id,
lits::Data,
libtransport::errors::Error,
lits::TestPeerList<lits::Id>,
>,
>(a)
{
Err(e) => panic!("{:?}", e),
Ok(_) => {}
};
}
}
20 changes: 14 additions & 6 deletions src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::listener;
use crate::TCPtransportCfg;
use bincode::deserialize;
use buffer::ReadBuffer;
use core::marker::PhantomData;
use futures::stream::Stream;
use futures::task::Context;
use futures::task::Poll;
Expand All @@ -17,15 +18,18 @@ use std::thread;
use std::thread::JoinHandle;

/// Struct which will be implementing the Transport Receiver trait.
pub struct TCPreceiver<Data> {
// The configuration struct (defined above)
pub struct TCPreceiver<Id, Data, E, PL> {
// The configuration struct (defined in lib.rs)
config: Arc<Mutex<TCPtransportCfg<Data>>>,
quit_tx: Sender<()>,
server_handle: Option<JoinHandle<()>>,
id: PhantomData<Id>,
e: PhantomData<E>,
pl: PhantomData<PL>,
}

/// Specify what occurs when TCPreceiver is dropped.
impl<Data> Drop for TCPreceiver<Data> {
impl<Id, Data, E, PL> Drop for TCPreceiver<Id, Data, E, PL> {
fn drop(&mut self) {
// Send quit message.
self.quit_tx.send(()).unwrap();
Expand All @@ -34,7 +38,8 @@ impl<Data> Drop for TCPreceiver<Data> {
}

/// Implementation of the Transport trait.
impl<Id, Pe, Data: 'static, E, PL> TransportReceiver<Id, Data, E, PL> for TCPreceiver<Data>
impl<Id, Pe, Data: 'static, E, PL> TransportReceiver<Id, Data, E, PL>
for TCPreceiver<Id, Data, E, PL>
where
Data: DeserializeOwned + Send + Clone,
Id: PeerId,
Expand Down Expand Up @@ -63,16 +68,19 @@ where
quit_tx: tx,
server_handle: Some(handle),
config: cfg_mutexed,
id: PhantomData,
e: PhantomData,
pl: PhantomData,
})
}
}

/// Allow TCPreceiver to be store in Pin (for async usage)
impl<D> Unpin for TCPreceiver<D> {}
impl<Id, D, E, PL> Unpin for TCPreceiver<Id, D, E, PL> {}

/// Implement stream trait to allow TCPreceiver to be used asynchronously. Allows for multiple
/// values to be yielded by a future.
impl<Data> Stream for TCPreceiver<Data>
impl<Id, Data, E, PL> Stream for TCPreceiver<Id, Data, E, PL>
where
Data: DeserializeOwned,
{
Expand Down
16 changes: 11 additions & 5 deletions src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@ use serde::Serialize;
use std::io::Write;
use std::net::TcpStream;

pub struct TCPsender<Data> {
phantom: PhantomData<Data>,
pub struct TCPsender<Id, Data, E, PL> {
id: PhantomData<Id>,
data: PhantomData<Data>,
e: PhantomData<E>,
pl: PhantomData<PL>,
}

impl<Id, Pe, Data: 'static, E, PL> TransportSender<Id, Data, E, PL> for TCPsender<Data>
impl<Id, Pe, Data: 'static, E, PL> TransportSender<Id, Data, E, PL> for TCPsender<Id, Data, E, PL>
where
Data: Serialize + Send + Clone,
Id: PeerId,
Pe: Peer<Id, E>,
PL: PeerList<Id, E, P = Pe>,
{
fn new() -> Result<Self> {
Ok(TCPsender {
phantom: PhantomData,
Ok(TCPsender::<Id, Data, E, PL> {
id: PhantomData,
data: PhantomData,
e: PhantomData,
pl: PhantomData,
})
}
/// Sends data to a single, specified peer.
Expand Down

0 comments on commit a3f89e6

Please sign in to comment.