Skip to content

Commit

Permalink
correction to comply with the latest version of libtransport
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime2 committed Sep 13, 2019
1 parent 0638937 commit 519249d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libtransport-tcp"
version = "0.0.1"
version = "0.0.2"
authors = ["Samuel Marks <@SamuelMarks>"]
edition = "2018"

Expand Down
28 changes: 25 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<Id, Pe, Data: 'static, E, PL> Transport<Id, Data, E, PL> for TCPtransport<D
where
Data: Serialize + DeserializeOwned + Send + Clone,
Id: PeerId,
Pe: Peer<Id>,
Pe: Peer<Id, E>,
PL: PeerList<Id, E, P = Pe>,
{
/// Create a new TCPtransport struct and configure its values.
Expand Down Expand Up @@ -178,14 +178,36 @@ where
Ok(())
}

/// Send a message to all peers in a PeerList.
/// Send a message to all peers in a PeerList, on base address.
/// Requires a PeerList and data struct.
fn broadcast(&mut self, peers: &mut PL, data: Data) -> Result<()> {
// Iterate over all peers
for p in peers.iter() {
//dbg!(p.get_net_addr());
// Create a TCP stream to the current net address.
let mut stream = TcpStream::connect(p.get_net_addr())?;
let mut stream = TcpStream::connect(p.get_base_addr())?;
// Serialize data to a bytes.
let bytes = serialize(&data)?;
// Write bytes to the stream.
let sent = stream.write(&bytes)?;
// Check if sent data is same as the bytes initially made.
if sent != bytes.len() {
return Err(Error::Incomplete);
}
// Shut down the stream once the message has been sent.
stream.shutdown(std::net::Shutdown::Write)?;
}
Ok(())
}

/// Send a message to all peers in a PeerList, on base address.
/// Requires a PeerList and data struct.
fn broadcast_n(&mut self, peers: &mut PL, n: usize, data: Data) -> Result<()> {
// Iterate over all peers
for p in peers.iter() {
//dbg!(p.get_net_addr());
// Create a TCP stream to the current net address.
let mut stream = TcpStream::connect(p.get_net_addr(n))?;
// Serialize data to a bytes.
let bytes = serialize(&data)?;
// Write bytes to the stream.
Expand Down
2 changes: 1 addition & 1 deletion src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<Id, Pe, Data: 'static, E, PL> TransportReceiver<Id, Data, E, PL> for TCPrec
where
Data: DeserializeOwned + Send + Clone,
Id: PeerId,
Pe: Peer<Id>,
Pe: Peer<Id, E>,
PL: PeerList<Id, E, P = Pe>,
{
/// Create a new TCPtransport struct and configure its values.
Expand Down
25 changes: 23 additions & 2 deletions src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<Id, Pe, Data: 'static, E, PL> TransportSender<Id, Data, E, PL> for TCPsende
where
Data: Serialize + Send + Clone,
Id: PeerId,
Pe: Peer<Id>,
Pe: Peer<Id, E>,
PL: PeerList<Id, E, P = Pe>,
{
fn new() -> Result<Self> {
Expand Down Expand Up @@ -49,7 +49,28 @@ where
for p in peers.iter() {
//dbg!(p.get_net_addr());
// Create a TCP stream to the current net address.
let mut stream = TcpStream::connect(p.get_net_addr())?;
let mut stream = TcpStream::connect(p.get_base_addr())?;
// Serialize data to a bytes.
let bytes = serialize(&data)?;
// Write bytes to the stream.
let sent = stream.write(&bytes)?;
// Check if sent data is same as the bytes initially made.
if sent != bytes.len() {
return Err(Error::Incomplete);
}
// Shut down the stream once the message has been sent.
stream.shutdown(std::net::Shutdown::Write)?;
}
Ok(())
}
/// Send a message to all peers in a PeerList, on base address.
/// Requires a PeerList and data struct.
fn broadcast_n(&mut self, peers: &mut PL, n: usize, data: Data) -> Result<()> {
// Iterate over all peers
for p in peers.iter() {
//dbg!(p.get_net_addr());
// Create a TCP stream to the current net address.
let mut stream = TcpStream::connect(p.get_net_addr(n))?;
// Serialize data to a bytes.
let bytes = serialize(&data)?;
// Write bytes to the stream.
Expand Down

0 comments on commit 519249d

Please sign in to comment.