GithubHelp home page GithubHelp logo

skyformat99 / nanomsg.rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thehydroimpulse/nanomsg.rs

0.0 2.0 0.0 2.15 MB

Nanomsg library for Rust

License: Other

Makefile 0.30% Shell 0.34% Rust 99.37%

nanomsg.rs's Introduction

Nanomsg

Cargo 0.6.2 MIT License Build Status Build status

Documentation

Nanomsg is a modern messaging library that is the successor to ZeroMQ, written in C by Martin Sustrik and colleagues. The nanomsg library is licensed under MIT/X11 license. "nanomsg" is a trademark of 250bpm s.r.o.

Requirements

  • Nanomsg 1.0.0

Installing nanomsg:

make deps

Installation

[dependencies]
nanomsg = "0.6.2"

Simply import the crate to use it:

extern crate nanomsg;

Creating a Socket

The basis of Nanomsg is a Socket. Each socket can be of a certain type. The type of a socket defines it's behaviour and limitations (such as only being able to send and not receive).

use nanomsg::{Socket, Protocol, Error};

/// Creating a new `Pull` socket type. Pull sockets can only receive messages
/// from a `Push` socket type.
fn create_socket() -> Result<(), Error> {
    let mut socket = try!(Socket::new(Protocol::Pull));
    Ok(())
}

Now, each socket that is created can be bound to multiple endpoints. Each binding can return an error, so we'll take advantage of the try! macro.

use nanomsg::{Socket, Protocol, Error};

/// Creating a new `Pull` socket type. Pull sockets can only receive messages
/// from a `Push` socket type.
fn create_socket() -> Result<(), Error> {
    let mut socket = try!(Socket::new(Protocol::Pull));
    
    // Create a new endpoint bound to the following protocol string. This returns
    // a new `Endpoint` that lives at-most the lifetime of the original socket.
    let mut endpoint = try!(socket.bind("ipc:///tmp/pipeline.ipc"));

    Ok(())
}

The socket is ready to be used now!

Because this is a Pull socket, we'll implement reading any messages we receive.

// ... After the endpoint we created, we'll start reading some data.
let mut msg = String::new();
loop {
    try!(socket.read_to_string(&mut msg));
    println!("We got a message: {}", &*msg);
    msg.clear();
}
// ...

That's awesome! But... we have no packets being sent to the socket, so we'll read nothing. To fix this, let's implement the accompanying pair Push socket.

use nanomsg::{Socket, Protocol, Error};

fn pusher() -> Result<(), Error> {
    let mut socket = try!(Socket::new(Protocol::Push));
    let mut endpoint = try!(socket.connect("ipc:///tmp/pipeline.ipc"));

    socket.write(b"message in a bottle");

    endpoint.shutdown();
    Ok(())
}

Contributors

(In arbitrary order):

License

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

nanomsg.rs's People

Contributors

alexandermorozov avatar bfops avatar blabaere avatar buster avatar danburkert avatar dcbishop avatar dkhenry avatar drawlerr avatar ggist avatar glycerine avatar jan-schreib avatar musitdev avatar mystal avatar polyfractal avatar rschifflin avatar ryman avatar thehydroimpulse avatar vks avatar wdv4758h avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.