Let's begin connecting dchat up to JSON-RPC using DarkFi's rpc module.
We'll start by defining a new struct called JsonRpcInterface that
takes two values, an accept Url that will receive JSON-RPC requests,
and a pointer to the p2p network.
{{#include ../../../../../example/dchat/src/rpc.rs:1:17}}
We'll need to implement a trait called RequestHandler for
the JsonRpcInterface. RequestHandler exposes a method called
handle_request() which is a handle for processing incoming
JSON-RPC requests. handle_request() takes a JsonRequest
and returns a JsonResult. These types are defined inside
jsonrpc.rs
This is JsonResult:
{{#include ../../../../../src/rpc/jsonrpc.rs:49:55}}
This is JsonRequest:
{{#include ../../../../../src/rpc/jsonrpc.rs:75:86}}
We'll use handle_request() to run a match statement on
JsonRequest.method.
Running a match on method will allow us to branch out to functions
that respond to methods received over JSON-RPC. We haven't implemented
any methods yet, so for now let's just return a JsonError.
{{#include ../../../../../example/dchat/src/rpc.rs:19:28}}
{{#include ../../../../../example/dchat/src/rpc.rs:31:34}}