-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
137ae4e
commit b51e994
Showing
10 changed files
with
335 additions
and
26 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
//! A Zebra remote procedure call interface | ||
//! A Zebra Remote Procedure Call (RPC) interface | ||
#![doc(html_favicon_url = "https://www.zfnd.org/images/zebra-favicon-128.png")] | ||
#![doc(html_logo_url = "https://www.zfnd.org/images/zebra-icon.png")] | ||
#![doc(html_root_url = "https://doc.zebra.zfnd.org/zebra_rpc")] | ||
|
||
pub mod rpc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//! Zebra supported RPC methods. | ||
use jsonrpc_core; | ||
|
||
use jsonrpc_core::Result; | ||
use jsonrpc_derive::rpc; | ||
|
||
#[rpc] | ||
/// RPC method signatures. | ||
pub trait Rpc { | ||
/// getinfo | ||
#[rpc(name = "getinfo")] | ||
fn getinfo(&self) -> Result<GetInfo>; | ||
} | ||
|
||
/// RPC method implementations. | ||
pub struct RpcImpl; | ||
impl Rpc for RpcImpl { | ||
fn getinfo(&self) -> Result<GetInfo> { | ||
// TODO: dummy output data, fix in the context of #3142 | ||
let info = GetInfo { | ||
build: "Zebra v1.0.0 ...".into(), | ||
subversion: "/Zebra:1.0.0-beta.4/".into(), | ||
}; | ||
|
||
Ok(info) | ||
} | ||
} | ||
|
||
#[derive(serde::Serialize, serde::Deserialize)] | ||
/// Return structure of a `getinfo` RPC method. | ||
pub struct GetInfo { | ||
build: String, | ||
subversion: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//! An RPC endpoint. | ||
use jsonrpc_core; | ||
|
||
use jsonrpc_http_server::ServerBuilder; | ||
|
||
use zebra_rpc::rpc::{Rpc, RpcImpl}; | ||
|
||
pub mod config; | ||
pub use config::Config; | ||
|
||
/// Zebra RPC Server | ||
pub struct RpcServer {} | ||
|
||
impl RpcServer { | ||
/// Start a new RPC server endpoint | ||
pub async fn new(config: Config) -> Self { | ||
if config.listen { | ||
info!("Trying to open RPC endpoint at {}...", config.listen_addr); | ||
|
||
// Create handler compatible with V1 and V2 RPC protocols | ||
let mut io = | ||
jsonrpc_core::IoHandler::with_compatibility(jsonrpc_core::Compatibility::Both); | ||
io.extend_with(RpcImpl.to_delegate()); | ||
|
||
let server = ServerBuilder::new(io) | ||
.threads(1) | ||
.start_http(&config.listen_addr) | ||
.expect("Unable to start RPC server"); | ||
|
||
info!("Opened RPC endpoint at {}", server.address()); | ||
|
||
server.wait(); | ||
} | ||
RpcServer {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//! User-configurable RPC parameters. | ||
use std::net::SocketAddr; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
/// RPC configuration section. | ||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
#[serde(deny_unknown_fields, default)] | ||
pub struct Config { | ||
/// Turn on/off the RPC server | ||
pub listen: bool, | ||
|
||
/// IP address and port for the RPC server | ||
pub listen_addr: SocketAddr, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Self { | ||
listen: false, | ||
listen_addr: "127.0.0.1:8232" | ||
.parse() | ||
.expect("Hardcoded address should be parseable"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters