Skip to content

Commit

Permalink
chore(rust): adds missing docs and minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
conectado committed Feb 12, 2022
1 parent e681418 commit b36a3ec
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 79 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use lazy_static::lazy_static;
use ockam::compat::collections::BTreeMap;
use ockam::compat::sync::Mutex;
use ockam::{Context, Result, Route};
use ockam_transport_smoltcp::{InterfaceConfiguration, SmolTcpTransport, TunTapDevice, TCP, ThreadLocal};
use ockam_transport_smoltcp::{InterfaceConfiguration, SmolTcpTransport, TunTapDevice, TCP, ThreadLocalPortProvider, StdClock};
use smoltcp::iface::Routes;
use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address};

Expand Down Expand Up @@ -43,7 +43,7 @@ async fn main(mut ctx: Context) -> Result<()> {
configuration.set_routes(routes);

// Initialize the TCP stack by opening a connection to a the remote
let tcp = SmolTcpTransport::<ThreadLocal>::create(&ctx, configuration, smoltcp::time::Instant::from_millis(0))
let tcp = SmolTcpTransport::<ThreadLocalPortProvider>::create(&ctx, configuration, Some(StdClock))
.await?;

tcp.connect(&peer_addr).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::str::FromStr;
use lazy_static::lazy_static;
use ockam::compat::sync::Mutex;
use ockam::{Context, Result, Routed, Worker};
use ockam_transport_smoltcp::{InterfaceConfiguration, SmolTcpTransport, TunTapDevice, ThreadLocal};
use ockam_transport_smoltcp::{InterfaceConfiguration, SmolTcpTransport, TunTapDevice, ThreadLocalPortProvider, StdClock};
use smoltcp::iface::Routes;
use smoltcp::wire::{IpAddress, IpCidr};

Expand Down Expand Up @@ -57,7 +57,7 @@ async fn main(ctx: Context) -> Result<()> {
[IpCidr::new(IpAddress::from_str(bind_ip_addr).unwrap(), 24)],
&*DEVICE,
);
let tcp = SmolTcpTransport::<ThreadLocal>::create(&ctx, configuration, smoltcp::time::Instant::from_millis(0))
let tcp = SmolTcpTransport::<ThreadLocalPortProvider>::create(&ctx, configuration, Some(StdClock))
.await?;

tcp.listen(bind_port).await?;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ockam_core = { path = "../ockam_core", version = "0.44.1-dev", default_features
ockam_node = { path = "../ockam_node", version = "0.43.1-dev", default_features = false }
tracing = { version = "0.1", default-features = false }
ockam_transport_core = { path = "../ockam_transport_core", version = "0.18.1-dev", default_features = false }
futures = "0.3"
futures = { version = "0.3", default-features = false }
lazy_static = { version = "1.4", default-features = false }
managed = { version = "0.8", default-features = false }
atomic-pool = { version = "0.2", default-features = false }
Expand Down
8 changes: 3 additions & 5 deletions implementations/rust/ockam/ockam_transport_smoltcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ mod transport;
pub use crate::net::TunTapDevice;

#[cfg(feature = "std")]
pub use port_provider::ThreadLocal;
pub use port_provider::ThreadLocalPortProvider;

pub use net::{Clock, Instant, InterfaceConfiguration};
pub use net::{Clock, Device, Instant, InterfaceConfiguration, StackFacade, StdClock};
pub use ockam_transport_core::TCP;
pub use port_provider::PortProvider;
pub use transport::*;

/// TCP address type constant
pub const TCP: u8 = 1;

pub(crate) const CLUSTER_NAME: &str = "_internals.transport.smoltcp";
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ pub use tuntap::TunTapDevice;

// This Device is very similar to Smoltcp's device however it makes the need
// to handle `Waker` explicit
/// Similar to [smoltcp::phy::Device] but allows passing a `Waker` explicitly.
pub trait Device {
/// Returns whether the device is prepared to start sending packages.
fn is_transmit_ready(&mut self) -> bool;
/// Transmits a package, if the buffer is full the `waker` should be signaled once the device is available to send again.
fn transmit(&mut self, pkt: PacketBuf, waker: &Option<Waker>);
/// Recieves a package, if no package is available yet the `waker` should be signaled once the package is ready.
fn receive(&mut self, waker: &Option<Waker>) -> Option<PacketBuf>;
/// Returns the device's [DeviceCapabilities]
fn capabilities(&mut self) -> DeviceCapabilities;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copied from: https://github.com/embassy-rs/embassy/blob/master/examples/std/src/tuntap.rs
// (Almost verbatim save from `TunTap::transmit` which implements a queue when `WouldBlock` would happen)
#![cfg(feature = "std")]

use async_io::Async;
Expand Down Expand Up @@ -127,6 +129,7 @@ impl io::Write for TunTap {
}
}

/// Similar to smoltcp's `TunTapInterface` but implements [super::Device] instead of [smoltcp::phy::Device], allowing it to work with an `async` runtime.
pub struct TunTapDevice {
device: Async<TunTap>,
send_queue: VecDeque<PacketBuf>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ mod tcp;
mod timer;

pub use device::Device;
pub use stack::InterfaceConfiguration;
pub(crate) use stack::*;
pub use stack::{InterfaceConfiguration, StackFacade};
pub(crate) use tcp::*;
pub use timer::{Clock, Instant};

// Tap devices only make sense in std
#[cfg(feature = "std")]
pub use device::TunTapDevice;

#[cfg(feature = "std")]
pub use timer::StdClock;
Loading

0 comments on commit b36a3ec

Please sign in to comment.