Dependencies

First, we need to import the necessary dependencies:

failure = "0.1"
JSON-RPC = { git = "https://github.com/apoelstra/rust-JSON-RPC" }
jsonrpc-http-server = { git = "https://github.com/paritytech/JSON-RPC" }
log = "0.4"
env_logger = "0.6"
serde = "1.0"
serde_derive = "1.0"

Most likely, you are familiar with most crates except jsonrpc and json-rpc-server. The first is a JSON-RPC client that's based on the hyper crate. The second also uses the hyper crate and provides server functionality of JSON-RPC.

Let's import the necessary types and talk a little about them:

use failure::Error;
use JSON-RPC::client::Client;
use JSON-RPC::error::Error as ClientError;
use JSON-RPC_http_server::ServerBuilder;
use JSON-RPC_http_server::JSON-RPC_core::{IoHandler, Error as ServerError, Value};
use log::{debug, error, trace};
use serde::Deserialize;
use std::env;
use std::fmt;
use std::net::SocketAddr;
use std::sync::Mutex;
use std::sync::mpsc::{channel, Sender};
use std::thread;

The JSON-RPC crate has the Client type that we will use to call the remote methods of other services. We also imported Error from that crate as ClientError to avoid a name conflict with Error from the failure crate.

For the server side, we will use ServerBuilder from the jsonrpc-http-server crate. Also, we need Error to be renamed to ServerError from that crate. To implement function handlers, we need to import IoHandler, which can be used to attach functions as RPC methods. Also, we need a Value (actually, this type is reimported from the serde_json crate), which is used as a result type for RPC methods.

To avoid mistakes in method names, because we will use them twice for the server implementation and then in a client, we declare names as string constants:

const START_ROLL_CALL: &str = "start_roll_call";
const MARK_ITSELF: &str = "mark_itself";

The first method will start sending messages from one microservice to the next. The second method is used to stop this roll-calling process.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.74.66