Unverified Commit 61656562 authored by Niklas Adolfsson's avatar Niklas Adolfsson Committed by GitHub
Browse files

merge v2 to master (#180)

* update http example

* ungeneric crate

* update dependencies

* [client]: add WebSocket client again.

* [deps]: remove needless dev dependencies

* nits: forgot to commit new files

* [ws client]: `send_text` instead of `send_binary`

This is temporary fix to work with the `server` which assumes that
`WebSocket` resonses are `text`

* chore: add a bunch of more logging

* [ws]: port tests but some are failing.

* chore: fmt

* [server API]: expose `fn local_addr` as public API.

* [tests]: make them less ugly by using `127.0.0.1:0`

* fix: a bunch of compiler warnings.

* [api]: uniform naming, `bind -> new` in transport.

* [websocket server]: reply when deserial fails

When the server receives an request with invalid JSON
`-32700, Parse error shall be returned`

* chore: fmt

* [tests]: fix remaining tests

* [websocket server]: support `binary` and `text`

* [ws server]: fix bug in subscription response.

`.await` was missing in RegisteredSubscription::send() and no responses
were actually sent which this commit fixes.

* [client API]: export `WsSubscription`

* [examples]: use `localhost` instead of `127.0.0.1`

Hostname is required when using `wss` and `127.0.0.1` is not valid hostname.

* [examples]: add subscription example.

* chore: fmt

* [ws server]: fix bug register new subscription.

Fixes newly introduced bug that causes `register_subscription` to have
side-effects even if the subscription fails.

* fmt

* more uniform logs

* [ws server]: simple subscription test.

* [ws server]: subscription tests improved.

* [tests]: extract test helpers to a separate crate (#125)

* [ws server]: don't close connection when `deserialization` fails (#131)

* [ws server]: don't close conn. when `deser` fails

* Update src/ws/transport.rs

* grumbles: prefer matching of if else.

* chore: CI warn `intra_doc_link_resolution_failure` (#139)

Since we have not updated the documentation properly it's annoying that
the entire job fails.

* chore: rustfmt.toml (#138)

* chore: add `rustfmt.toml` for formatting

* style: `cargo fmt --all` with new config

* [server raw params]: fix debug implementation (#137)

* [server]: simply raw params impl

Use debug implementation of `common::Params` instead of doing something
similar that doesn't work properly.

* [raw params]: derive `Debug` impl.

* [ws server]: parse subscription ID for unsubscription instead of hardcoding `JsonValue::Null` (#136)

* [ws server]: fix broken unsubscribe.

Try to parse the subscription ID as the first element of an Array or the `subscription`
field of an Object/Map.

If both of those fails then regard it as a error.

* fmt

* fix grumbles: remove space indentation

* fix(ws server): sub/unsubscribe to same method should generate an error (#140)

* fix(ws server): sub/unsubscribe to same method err

Subscribe and unsubscribe to the same method should generate an error, which this commit fixed.
This bug was introduced by myself in https://github.com/paritytech/jsonrpsee/commit/fc87889de2615dbb3d0cf2d91a306f016d48df2d



* Update src/ws/server.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* chore: add naive benches for request/response (#142)

Co-authored-by: Niklas Adolfsson's avatarNiklas <niklasadolfsson1@gmail.com>

* fix(ws server): remove faulty debug_assert (#145)

The code assumed that `subscription id` is still in `active_subscriptions` when the
connection was dropped.

The list of subscriptions (kept in raw server) are not notified when a client dropped its
subscription/unsubscribed thus it's possible that the actual subscriptions are closed before the
entire client was dropped.

* ci(benches): cargo check on benches. (#146)

* fix(http client): implement `clone` uniform API. (#147)

* chore(deps): update `futures v0.3.7` (#148)

* chore(deps): update remaining crates (#149)

* chore(deps): update `futures v0.3.7`

* chore(deps): bump the rest of deps

* Improve HTTP client background thread (#150)

* refactor: resultify API + some crate reorg (#144)

* [ws client]: resultify API and fix subscribe.

* The commit changes the API to return `Err` when it's possible and to not ignore underlying errors.
* Fix that `fn subscribe` doesn't accept the subscription and unsubscription to be same which causes
errors in the server.

* nits: Err::SubscriptionMetod -> Err::Subscription

* refactor(client): common error type

* refactor(http client): resultify

* refactor(common): rename common -> types..

This commit renames the `common module` to `types` and tries to distinguish the types that is
directly related to the `JSON-RPC v2 specification` from others.

Somethings are a little big sloppy named as naming is hard.

Also, as bonus a removed a bunch of needless stuff in http server related to subscription.

* Update src/ws/tests.rs

* style: cargo fmt

* fix(grumble): matches -> assert(matches)

* fix(grumbles): `jsonrpc_v2` -> `jsonrpc`

* fix(nit): remove unused code.

* fix(benches): make it compile again.

* style: cargo fmt

* fix nits (#151)

* fix(ws client): send binary (1 byte less payload)

* docs(ws server): fix bad comment.

* chore: add `editorconfig` (#152)

* chore: make `debug log` less verbose. (#153)

* chore: make `debug log` less verbose.

The debug logging was just too verbose and this commit simplies it as follows:

```
DEBUG recv: {"jsonrpc":"2.0","method":"<METHOD>","params":<PARAMS>,"id":<ID>}
DEBUG send: {"jsonrpc":"2.0","result":"<RESULT>","id":<ID>}
```

* style: cargo fmt

* fix: missed logs

* [jsonrpc types]: implement Display for Request/Response (#160)

* feat(jsonrpc response/request): impl `Display`

* refactor(logging): use display impl

* use serde_json for verbosity

* [http client]: refactor with "syncronous-like" design (#156)

* experimental

* ci(benches): sync and concurrent roundtrips

Improve benchmarks to take concurrent requests into account.

* ci(benches): sync and concurrent roundtrips

Improve benchmarks to take concurrent requests into account.

* fix(nits)

* feat(http client): limit max request body size

* test(http transport): request limit test

* test(http client): add tests.

* fix typo

* fix(benches): make it compile again.

* fix(ws example): revert unintentional change.

* test(http client): subscription response on call.

* fix(cleanup)

* fix(benches): make it compile again.

* Update src/client/http/transport.rs

* fix(http client): `&str` -> `AsRef<str>`

* docs(client types): better docs for Mismatch type.

* style: `Default::default` -> `HttpConfig::default`

* fix(http client): read body size from header.

Expermential to read number of bytes from `HTTP Content Length` to pre-allocate the number of bytes and bail early
if the length is bigger than the `max_request_body size`

Need to be benched with bigger requests.

* test(raw http): enable tests to works again.

* style: cargo fmt

* benches: address grumbles

* feat(jsonrpc response/request): impl `Display`

* refactor(logging): use display impl

* fix(http client): nits.

* Update benches/benches.rs

* fix bad merge.

* chore(deps): update dependencies. (#164)

* feat(http server): configurable request body limit (#162)

* feat(http server): configurable request body limit

* refactor(crate reorg): to have shared http helpers.

* Merge client and server errors.
* Move `http_server_utils` to `utils/http`
* Minor cleanup

* fix nits

* fix(hyper helper): u64 -> u32

* Update src/utils/http/hyper_helpers.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* Update src/utils/http/hyper_helpers.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* fix: grumbles

* Update src/utils/http/hyper_helpers.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* Update src/http/server.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* ci: remove nightly (#167)

Use stabilized `broken_intra_doc_links` instead of `intra_doc_link_resolution_failure`

* fix(websocket client): drop subscriptions that can't keep up with the internal buffer size (#166)

* fix(ws client): drop subscriptions when full.

This commit changes the behavior in the `WebSocket Client` where each subscription channel is used in a non-blocking
matter until it is determined as full or disconnected. When that occurs the channel is simply dropped and when
the user `poll` the subscription it will return all sent subscriptions before it was and terminate (return None)
once it's polled one last time. Similarly as `Streams` works in Rust.

It also adds configuration for the `WebSocket Client` to configure capacity for the different internal channels to avoid
filling the buffers when it's not expected.

* tests(ws client): simple subscription test.

* fix: nits

* Update src/client/ws/client.rs

* refactor(tests): introduce integration_tests

Make the repo structure more understable w.r.t testing.

* chore(license): add missing license headers

* Update src/client/ws/client.rs

* Update src/client/ws/client.rs

* style: remove unintended spaces.

* tests: add concurrent deadlock test

Ensure that if more than the requested channel buffer capacity is exceeded it should not deadlock.
Such as spawning alot of concurrent requests, notifications or new subscriptions.

* Update src/client/ws/client.rs

* fix: review grumbles

* fix nits: `remove needless closure`

* fix: cargo fmt

* Update src/client/ws/client.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* fix more nits

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* fix(ws client): embed request id in `SubscriptionClosed` (#170)

* fix(ws client): embed request id SubscriptClosed

Fixes https://github.com/paritytech/jsonrpsee/issues/169



* Update src/client/ws/client.rs

* Update src/client/ws/client.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* Update src/client/ws/client.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* chore(deps): bump dependencies (#172)

* [ws client]: add tests (#134)

* [test utils]: add `internal_err` and consts
[errors]: unify client/server errors
[test utils]: fake WebSocket jsonrpc server
[ws client]: export errors
[ws client]: add some basic tests

* fmt

* remove log target

* fix nits

* [ws client]: add subscription test

* revert unintendend changes.

* fmt

* [ws client]: fix panic in tests

* cleanup

* tests(ws client): test for invalid request ID.

* fix nits

* [ws client]: kill raw client (#171)

* getting started

* WIP WIP

* cleanup

* cleanup v2

* cleanup v3

* perf: use BufReader BufWriter

* fix(request manager): resultify insert API

The rationale behind this change is that the `insert_methods` takes ownership of the `send_back_oneshot`
and if the operation fails it should be propagated the frontend.

So returning the `Err(send_back_oneshot)` if it fails makes it possible.

* fix nits

* examples(ws): revert changes

* Update tests/integration_tests.rs

* nits: fix unwraps

* Update src/client/ws/manager.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* Update src/client/ws/transport.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* Update src/client/ws/client.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* fix build

* refactor: simplify `Error::InvalidRequestId`

It was hard to use when the expected id is not known.

* fix(ws client): error handling.

* fix(grumble error type): better error message.

* fix(grumble): docs `JSONRPC WebSocket transport`

* fix(ws manager): fix grumbles.

* Add better documentation
* Rename methods.
* Add `proof` to unreachable!

* fix(ws manager): fix nit in docs.

* fix(grumbles): ws client

* fix more nits

* fix compile warning: export websocket transports.

* Update src/client/ws/manager.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* deps: tokio 1.0 and hyper 0.14 (#176)

* deps: tokio 1.0 and hyper 0.14

* Update Cargo.toml

* refactor: crate re-organization with separate crates (#177)

* [ci]: feature `http` and `ws` removed.

* refactor: re-org crate with smaller crates.

* fmt

* [ci]: remove default features

Currently there are no features in the crates, so that check is not needed.

* [http client]: remove unused dependency tokio

* docs(http client): fix nits tokio 0.2 -> tokio 1.0 (#178)

* docs(http client): tokio 0.2 -> tokio 1.0

* fix: better link

* [ci]: github actions (#179)

* docs(http client): tokio 0.2 -> tokio 1.0

* [ci]: remove travis

* [ci]: add github actions.

* [ci]: fix identation nits

* [ci]: use cache for actions

* [ci]: filter to clippy

* [ci]: remove hacks

* separate action for ci and benchmarks

* [ci]: tweak to run on master branch.

* examples/subscription -> examples/ws_subscription

* force CI

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>
Co-authored-by: default avatarMaciej Hirsz <1096222+maciejhirsz@users.noreply.github.com>
Co-authored-by: default avatarAtkins <atkinschang@gmail.com>
parent 040c4b28
root = true
[*]
indent_style=tab
indent_size=tab
tab_width=4
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=120
insert_final_newline=true
[*.{yml,sh}]
indent_style=space
indent_size=2
tab_width=8
end_of_line=lf
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
name: Benchmarks
on:
schedule:
- cron: "0 0 * * *"
jobs:
bench:
name: Run benchmarks daily
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v2
- name: Install Rust stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Cache Dependencies & Build Outputs
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Store benchmark result
uses: rhysd/github-action-benchmark@v1
with:
tool: 'cargo'
output-file-path: output.txt
fail-on-alert: true
github-token: ${{ secrets.GITHUB_TOKEN }}
comment-on-alert: true
alert-comment-cc-users: '@niklasad1, @maciejhirsz'
auto-push: true
name: Continuous integration
on:
pull_request:
push:
branches:
- master
jobs:
check-style:
name: Check style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: clippy, rustfmt
- name: Cache Dependencies & Build Outputs
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
- name: Cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -A clippy::mutable_key_type
check-code:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Cache Dependencies & Build Outputs
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Cargo check no-default-features
uses: actions-rs/cargo@v1
with:
command: check
args: --benches --tests
- name: Cargo check all-features
uses: actions-rs/cargo@v1
with:
command: check
args: --benches --tests --all-features
tests:
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install Rust stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Cache Dependencies & Build Outputs
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Cargo build
uses: actions-rs/cargo@v1
with:
command: build
args: --workspace
- name: Cargo test
uses: actions-rs/cargo@v1
with:
command: test
language: rust
rust:
- stable
before_script:
- rustup component add rustfmt
- rustup toolchain add nightly
script:
- cargo fmt --all -- --check
- cargo build --verbose --all-features
- cargo test --verbose --all-features
- cargo build --verbose --no-default-features
- cargo build --verbose --features "http"
- cargo build --verbose --features "ws"
- RUSTDOCFLAGS="--deny intra_doc_link_resolution_failure" cargo +nightly doc --verbose --workspace --no-deps --document-private-items
notifications:
email:
on_success: never
on_failure: never
[package]
name = "jsonrpsee"
description = "JSON-RPC crate"
version = "1.0.0"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
license = "MIT"
edition = "2018"
[dependencies]
async-std = "1.6.2"
bs58 = "0.3.0"
fnv = "1.0"
futures = "0.3.4"
futures-timer = "3.0.2"
globset = "0.4"
hashbrown = "0.7.0"
lazy_static = "1.4.0"
log = "0.4"
parking_lot = "0.10"
pin-project = "0.4.22"
jsonrpsee-proc-macros = { path = "proc-macros" }
rand = "0.7"
serde = { version = "1.0.101", default-features = false, features = ["derive"] }
serde_json = "1.0.48"
smallvec = { version = "1.2.0", default-features = false }
thiserror = "1.0.9"
# HTTP-related dependencies
hyper = { version = "0.13.0", features = ["stream"], optional = true }
tokio = { version = "0.2.4", optional = true }
unicase = { version = "2.0", optional = true }
# WS-related dependencies
async-tls = { version = "0.7.0", optional = true }
bytes = { version = "0.5", optional = true }
soketto = { version = "0.4.0", optional = true }
url = { version = "2.1.1", optional = true }
webpki = { version = "0.21", optional = true }
[features]
default = ["http", "ws"]
http = ["hyper", "tokio", "unicase"]
ws = ["async-tls", "bytes", "soketto", "url", "webpki"]
[dev-dependencies]
async-std = "1.6.2"
env_logger = "0.7.1"
rand = "0.7"
serde = { version = "1.0.40", features = ["derive"] }
[workspace]
members = [
"benches",
"http-client",
"http-server",
"test-utils",
"tests",
"types",
"utils",
"ws-client",
"ws-server",
]
[package]
name = "jsonrpsee-benchmarks"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Benchmarks for jsonrpsee"
edition = "2018"
license = "MIT"
[dev-dependencies]
async-std = "1.8"
criterion = "0.3"
futures = "0.3"
jsonrpsee-types = { path = "../types" }
jsonrpsee-http-client = { path = "../http-client" }
jsonrpsee-ws-client = { path = "../ws-client" }
jsonrpsee-ws-server = { path = "../ws-server" }
jsonrpsee-http-server = { path = "../http-server" }
num_cpus = "1"
tokio = { version = "1", features = ["full"] }
[[bench]]
name = "bench"
harness = false
use async_std::task::block_on;
use criterion::*;
use futures::channel::oneshot::{self, Sender};
use jsonrpsee_http_client::{HttpClient, HttpConfig};
use jsonrpsee_http_server::HttpServer;
use jsonrpsee_types::jsonrpc::{JsonValue, Params};
use jsonrpsee_ws_client::{WsClient, WsConfig};
use jsonrpsee_ws_server::WsServer;
use std::net::SocketAddr;
use std::sync::Arc;
criterion_group!(benches, http_requests, websocket_requests);
criterion_main!(benches);
fn concurrent_tasks() -> Vec<usize> {
let cores = num_cpus::get();
vec![cores / 4, cores / 2, cores, cores * 2, cores * 4]
}
async fn http_server(tx: Sender<SocketAddr>) {
let server = HttpServer::new("127.0.0.1:0", HttpConfig { max_request_body_size: u32::MAX }).await.unwrap();
let mut say_hello = server.register_method("say_hello".to_string()).unwrap();
tx.send(*server.local_addr()).unwrap();
loop {
let r = say_hello.next().await;
r.respond(Ok(JsonValue::String("lo".to_owned()))).await.unwrap();
}
}
async fn ws_server(tx: Sender<SocketAddr>) {
let server = WsServer::new("127.0.0.1:0").await.unwrap();
let mut say_hello = server.register_method("say_hello".to_string()).unwrap();
tx.send(*server.local_addr()).unwrap();
loop {
let r = say_hello.next().await;
r.respond(Ok(JsonValue::String("lo".to_owned()))).await.unwrap();
}
}
pub fn http_requests(c: &mut criterion::Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let (tx_addr, rx_addr) = oneshot::channel::<SocketAddr>();
async_std::task::spawn(http_server(tx_addr));
let server_addr = block_on(rx_addr).unwrap();
let client = Arc::new(HttpClient::new(&format!("http://{}", server_addr), HttpConfig::default()).unwrap());
c.bench_function("synchronous http round trip", |b| {
b.iter(|| {
rt.block_on(async {
let _: JsonValue = black_box(client.request("say_hello", Params::None).await.unwrap());
})
})
});
c.bench_function_over_inputs(
"concurrent http round trip",
move |b: &mut Bencher, size: &usize| {
b.iter(|| {
let mut tasks = Vec::new();
for _ in 0..*size {
let client_rc = client.clone();
let task = rt.spawn(async move {
let _: Result<JsonValue, _> = black_box(client_rc.request("say_hello", Params::None)).await;
});
tasks.push(task);
}
for task in tasks {
rt.block_on(task).unwrap();
}
})
},
concurrent_tasks(),
);
}
pub fn websocket_requests(c: &mut criterion::Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let (tx_addr, rx_addr) = oneshot::channel::<SocketAddr>();
async_std::task::spawn(ws_server(tx_addr));
let server_addr = block_on(rx_addr).unwrap();
let client = Arc::new(block_on(WsClient::new(&format!("ws://{}", server_addr), WsConfig::default())).unwrap());
c.bench_function("synchronous WebSocket round trip", |b| {
b.iter(|| {
rt.block_on(async {
let _: JsonValue = black_box(client.request("say_hello", Params::None).await.unwrap());
})
})
});
c.bench_function_over_inputs(
"concurrent WebSocket round trip",
move |b: &mut Bencher, size: &usize| {
b.iter(|| {
let mut tasks = Vec::new();
for _ in 0..*size {
let client_rc = client.clone();
let task = rt.spawn(async move {
let _: Result<JsonValue, _> = black_box(client_rc.request("say_hello", Params::None)).await;
});
tasks.push(task);
}
for task in tasks {
rt.block_on(task).unwrap();
}
})
},
// TODO(niklasad1): This deadlocks when more than 8 tasks are spawned.
concurrent_tasks(),
);
}
......@@ -24,58 +24,39 @@
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
jsonrpsee::rpc_api! {
Health {
/// Test
fn system_name(foo: String, bar: i32) -> String;
use async_std::task;
use futures::channel::oneshot::{self, Sender};
use jsonrpsee::client::{HttpClient, HttpConfig};
use jsonrpsee::http::HttpServer;
use jsonrpsee::types::jsonrpc::{JsonValue, Params};
fn test_notif(foo: String, bar: i32);
const SOCK_ADDR: &str = "127.0.0.1:9933";
const SERVER_URI: &str = "http://localhost:9933";
/// Test2
#[rpc(method = "foo")]
fn system_name2() -> String;
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
System {
fn test_foo() -> String;
}
}
let (server_started_tx, server_started_rx) = oneshot::channel::<()>();
let _server = task::spawn(async move {
run_server(server_started_tx, SOCK_ADDR).await;
});
server_started_rx.await?;
fn main() {
env_logger::try_init().ok();
// Spawning a server in a background task.
async_std::task::spawn(async move {
let listen_addr = "127.0.0.1:8000".parse().unwrap();
let transport_server = jsonrpsee::transport::http::HttpTransportServer::bind(&listen_addr)
.await
.unwrap();
let mut server1 = jsonrpsee::raw::RawServer::new(transport_server);
let client = HttpClient::new(SERVER_URI, HttpConfig::default())?;
let response: Result<JsonValue, _> = client.request("say_hello", Params::None).await;
println!("r: {:?}", response);
while let Ok(request) = Health::next_request(&mut server1).await {
match request {
Health::SystemName { respond, foo, bar } => {
let value = format!("{}, {}", foo, bar);
respond.ok(value);
}
Health::SystemName2 { respond } => {
respond.ok("hello 2");
}
Health::TestNotif { foo, bar } => {
println!("server got notif: {:?} {:?}", foo, bar);
}
}
}
});
Ok(())
}
// Client demo.
let transport_client =
jsonrpsee::transport::http::HttpTransportClient::new("http://127.0.0.1:8000");
let mut client = jsonrpsee::raw::RawClient::new(transport_client);
let v = async_std::task::block_on(async {
Health::test_notif(&mut client, "notif_string", 192)
.await
.unwrap();
Health::system_name(&mut client, "hello", 5).await.unwrap()
});
println!("{:?}", v);
async fn run_server(server_started_tx: Sender<()>, url: &str) {
let server = HttpServer::new(url, HttpConfig::default()).await.unwrap();
let mut say_hello = server.register_method("say_hello".to_string()).unwrap();
server_started_tx.send(()).unwrap();
loop {
let r = say_hello.next().await;
r.respond(Ok(JsonValue::String("lo".to_owned()))).await.unwrap();
}
}
// Copyright 2019 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use async_std::task;
use futures::channel::oneshot::{self, Sender};
use jsonrpsee::client::{WsClient, WsConfig};
use jsonrpsee::types::jsonrpc::{JsonValue, Params};
use jsonrpsee::ws::WsServer;
const SOCK_ADDR: &str = "127.0.0.1:9944";
const SERVER_URI: &str = "ws://localhost:9944";
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let (server_started_tx, server_started_rx) = oneshot::channel::<()>();
let _server = task::spawn(async move {
run_server(server_started_tx, SOCK_ADDR).await;
});
server_started_rx.await?;
let client = WsClient::new(SERVER_URI, WsConfig::default()).await?;
let response: JsonValue = client.request("say_hello", Params::None).await?;
println!("r: {:?}", response);
Ok(())
}
async fn run_server(server_started_tx: Sender<()>, url: &str) {
let server = WsServer::new(url).await.unwrap();
let mut say_hello = server.register_method("say_hello".to_string()).unwrap();
server_started_tx.send(()).unwrap();
loop {
let r = say_hello.next().await;
r.respond(Ok(JsonValue::String("lo".to_owned()))).await.unwrap();
}
}
......@@ -24,49 +24,48 @@
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use jsonrpsee::{client::Subscription, Client};
jsonrpsee::rpc_api! {
System {
/// Get the node's implementation name. Plain old string.
fn system_name() -> String;
use async_std::task;
use futures::channel::oneshot::{self, Sender};
use jsonrpsee::client::{WsClient, WsConfig, WsSubscription};
use jsonrpsee::types::jsonrpc::{JsonValue, Params};
use jsonrpsee::ws::WsServer;
/// Returns the roles the node is running as.
#[rpc(method = "system_nodeRoles")]
fn system_node_roles() -> Vec<String>;
const SOCK_ADDR: &str = "127.0.0.1:9966";
const SERVER_URI: &str = "ws://localhost:9966";
const NUM_SUBSCRIPTION_RESPONSES: usize = 10;
#[rpc(method = "chain_getBlockHash", positional_params)]
fn chain_block_hash(id: Option<String>) -> Option<String>;
}
}
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();