coap 0.5.1

A CoAP library

Crate coap [] [src]

Implementation of the CoAP Protocol.

This library provides both a client interface (CoAPClient) and a server interface (CoAPServer).

Installation

First add this to your Cargo.toml:

[dependencies]
coap = "0.5"

Then, add this to your crate root:

extern crate coap;

Example

Server:

extern crate coap;
use std::io;
use coap::{CoAPServer, CoAPClient, CoAPRequest, CoAPResponse};
fn request_handler(request: CoAPRequest) -> Option<CoAPResponse> {
    println!("Receive request: {:?}", request);
     
    // Return the auto-generated response
    request.response
}
fn main() {
    let addr = "127.0.0.1:5683";

    let mut server = CoAPServer::new(addr).unwrap();
    server.handle(request_handler).unwrap();

    println!("Server up on {}", addr);
    println!("Press any key to stop...");

    io::stdin().read_line(&mut String::new()).unwrap();

    println!("Server shutdown");
}

Client:

extern crate coap;

use coap::message::response::CoAPResponse;
use coap::CoAPClient;

fn main() {
    let url = "coap://127.0.0.1:5683/Rust";
    println!("Client request: {}", url);

    let response: CoAPResponse = CoAPClient::request(url).unwrap();
    println!("Server reply: {}", String::from_utf8(response.message.payload).unwrap());
}

Reexports

pub use client::CoAPClient;
pub use message::header::MessageType;
pub use message::IsMessage;
pub use message::packet::CoAPOption;
pub use message::request::CoAPRequest;
pub use message::response::CoAPResponse;
pub use server::CoAPServer;

Modules

client
message
server