-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathclient.rs
129 lines (114 loc) · 3.53 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright 2019 Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use super::{subscription::WsSubscriptionWrapper, HandleMessage};
use crate::{
rpc::{
to_json_req,
ws_client::{MessageContext, RequestHandler, RpcClient, SubscriptionHandler},
Request, Result, Subscribe,
},
RpcParams,
};
use serde::de::DeserializeOwned;
use std::{
fmt::Debug,
sync::mpsc::{channel, Sender as ThreadOut},
thread,
};
use url::Url;
use ws::{connect, Result as WsResult, Sender as WsSender};
#[derive(Debug, Clone)]
pub struct WsRpcClient {
url: Url,
}
impl WsRpcClient {
pub fn new(url: &str) -> Result<Self> {
Ok(Self { url: Url::parse(url)? })
}
pub fn with_default_url() -> Self {
Self::new("ws://127.0.0.1:9944").unwrap()
}
}
impl Request for WsRpcClient {
fn request<R: DeserializeOwned>(&self, method: &str, params: RpcParams) -> Result<R> {
let json_req = to_json_req(method, params)?;
let response = self.direct_rpc_request(json_req, RequestHandler::default())??;
let deserialized_value: R = serde_json::from_str(&response)?;
Ok(deserialized_value)
}
}
impl Subscribe for WsRpcClient {
type Subscription<Notification> = WsSubscriptionWrapper<Notification> where Notification: DeserializeOwned;
fn subscribe<Notification: DeserializeOwned>(
&self,
sub: &str,
params: RpcParams,
_unsub: &str,
) -> Result<Self::Subscription<Notification>> {
let json_req = to_json_req(sub, params)?;
let (result_in, receiver) = channel();
let sender =
self.start_rpc_client_thread(json_req, result_in, SubscriptionHandler::default())?;
let subscription = WsSubscriptionWrapper::new(sender, receiver);
Ok(subscription)
}
}
impl WsRpcClient {
fn start_rpc_client_thread<MessageHandler>(
&self,
jsonreq: String,
result_in: ThreadOut<MessageHandler::ThreadMessage>,
message_handler: MessageHandler,
) -> Result<WsSender>
where
MessageHandler: HandleMessage + Clone + Send + 'static,
MessageHandler::ThreadMessage: Send + Sync + Debug,
MessageHandler::Context: From<MessageContext<MessageHandler::ThreadMessage>>,
{
let mut socket = ws::Builder::new().build(move |out| RpcClient {
out,
request: jsonreq.clone(),
result: result_in.clone(),
message_handler: message_handler.clone(),
})?;
socket.connect(self.url.clone())?;
let handle = socket.broadcaster();
let _client =
thread::Builder::new()
.name("client".to_owned())
.spawn(move || -> WsResult<()> {
socket.run()?;
Ok(())
})?;
Ok(handle)
}
fn direct_rpc_request<MessageHandler>(
&self,
jsonreq: String,
message_handler: MessageHandler,
) -> Result<MessageHandler::ThreadMessage>
where
MessageHandler: HandleMessage + Clone + Send + 'static,
MessageHandler::ThreadMessage: Send + Sync + Debug,
MessageHandler::Context: From<MessageContext<MessageHandler::ThreadMessage>>,
{
let (result_in, result_out) = channel();
connect(self.url.as_str(), |out| RpcClient {
out,
request: jsonreq.clone(),
result: result_in.clone(),
message_handler: message_handler.clone(),
})?;
Ok(result_out.recv()?)
}
}