Module rmp_rpc::client [] [src]

This module provides a MessagePack-RPC asynchronous client.

Examples

extern crate futures;
extern crate rmp_rpc;
extern crate tokio_core;

use std::net::SocketAddr;

use futures::Future;
use rmp_rpc::{Value, Integer};
use rmp_rpc::client::Client;
use tokio_core::reactor::Core;

fn main() {
   // Create the tokio event loop
   let mut core = Core::new().unwrap();
   let handle = core.handle();

   let addr: SocketAddr = "127.0.0.1:54321".parse().unwrap();

   let task =
       // Connect to the server
       Client::connect(&addr, &handle)
       .or_else(|e| {
           println!("Connection to server failed: {}", e);
           Err(())
       })
       .and_then(|client| {
           // Send a msgpack-rpc notification, with method "ping" and no argument
           client.notify("ping", &[]).and_then(|_| {
               // Return the client, so that we can reuse it
               Ok(client)
           })
       })
       .and_then(|client| {
           // Send a msgpack-rpc request, with method "add" and two arguments
           let args = vec![Value::Integer(Integer::from(3)),
                           Value::Integer(Integer::from(4))];
           client.request("add", &args).and_then(|response| {
               // Handle the response [...]
               Ok(())
           })
       });
   core.run(task).unwrap();
}

Structs

Client

A client used to send requests on notifications to a MessagePack-RPC server.

Connection

A future that returns a Client when it completes successfully.

Response

A future response.to a request.