Skip to content

Added devices API #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod sockopt;

use crate::message::msg_ptr;
pub use crate::message::Message;
pub use crate::DeviceType::*;
pub use crate::SocketType::*;

/// `zmq`-specific Result type.
Expand Down Expand Up @@ -95,6 +96,36 @@ impl SocketType {
}
}

/// Device types
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DeviceType {
STREAMER,
FORWARDER,
QUEUE,
}

impl DeviceType {
fn to_raw(self) -> c_int {
let raw = match self {
STREAMER => zmq_sys::ZMQ_STREAMER,
FORWARDER => zmq_sys::ZMQ_FORWARDER,
QUEUE => zmq_sys::ZMQ_QUEUE,
};
raw as c_int
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this return a result rather than a panic?

Also, could you add a newline before this line?


fn from_raw(raw: c_int) -> DeviceType {
match raw as u32 {
zmq_sys::ZMQ_STREAMER => STREAMER,
zmq_sys::ZMQ_FORWARDER => FORWARDER,
zmq_sys::ZMQ_QUEUE => QUEUE,
// return STREAMER instead of panic
_ => STREAMER,
}
}
}

/// Socket Events
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down Expand Up @@ -1128,6 +1159,18 @@ pub fn poll(items: &mut [PollItem], timeout: i64) -> Result<i32> {
Ok(rc as i32)
}

/// Start a 0MQ device in the current thread.
///
/// A device connects a frontend socket with a backend socket, where both the
/// server and client are dynamic
///
/// This function only returns (always with an `Err`) when the sockets' context
/// has been closed.
pub fn device(device_type: DeviceType, frontend: &Socket, backend: &Socket) -> Result<()> {
zmq_try!(unsafe { zmq_sys::zmq_device(device_type.to_raw(), frontend.sock, backend.sock,) });
Ok(())
}

/// Start a 0MQ proxy in the current thread.
///
/// A proxy connects a frontend socket with a backend socket, where the exact
Expand Down
5 changes: 5 additions & 0 deletions zmq-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ pub use crate::ffi::{

ZMQ_POLLITEMS_DFLT,

// device types
ZMQ_STREAMER,
ZMQ_FORWARDER,
ZMQ_QUEUE,

// Undeprecated types.
zmq_msg_t,
zmq_free_fn,
Expand Down