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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::net::ToSocketAddrs;
use std::sync::Future;
use core::{Command, Response};
use result::KineticResult;
use channel::Result;
use authentication::Credentials::Pin;
use commands::pin::PinCommand;
use std::sync::mpsc::{Receiver};
use std::marker::PhantomData;
static DEFAULT_MAX_PENDING: usize = 10;
pub struct Client<Ch: ::channel::KineticChannel<T>,T> {
channel: Ch,
cluster_version: i64,
default_credentials: ::authentication::Credentials,
async_return_type: PhantomData<T>,
}
impl Client<::channel::AsyncChannel, Receiver<Result>> {
#[inline]
pub fn new<A: ToSocketAddrs>(addr: A) -> KineticResult<Client<::channel::AsyncChannel, Receiver<Result>>> {
let c = try!(::channel::AsyncChannel::new(addr, DEFAULT_MAX_PENDING));
Ok( Client { channel: c,
cluster_version: 0,
default_credentials: ::std::default::Default::default(),
async_return_type: PhantomData})
}
}
impl<Ch: ::channel::KineticChannel<T>, T> Client<Ch,T> {
#[inline]
pub fn new_with_channel<A: ToSocketAddrs>(channel: Ch) -> KineticResult<Client<Ch, T>> {
Ok( Client { channel: channel,
cluster_version: 0,
default_credentials: ::std::default::Default::default(),
async_return_type: PhantomData})
}
#[inline]
pub fn set_cluster_version(&mut self, value: i64) {
self.cluster_version = value;
}
#[inline]
pub fn get_config<'r>(&'r self) -> &'r ::proto::command::log::Configuration {
self.channel.get_configuration()
}
#[inline]
pub fn get_limits<'r>(&'r self) -> &'r ::proto::command::log::Limits {
self.channel.get_limits()
}
#[inline]
fn send_raw<R : Response, C: Command<R>> (&self, auth: ::authentication::Credentials, cmd: C) -> T {
let (mut cmd, value) = cmd.build_proto();
{
let mut h = cmd.mut_header();
h.set_clusterVersion(self.cluster_version);
}
self.channel.send((auth, cmd, value))
}
#[inline]
fn receive_raw<R : Response> (token: T) -> KineticResult<R> {
let (msg, cmd, value) = Ch::receive(token);
let r:KineticResult<R> = Response::from_proto(msg, cmd, value);
r
}
#[inline]
pub fn send<C: Command<R>, R : Response> (&self, cmd: C) -> KineticResult<R> {
let token = self.send_raw(self.default_credentials.clone(), cmd);
Self::receive_raw(token)
}
#[inline]
pub fn send_with_pin<C: PinCommand<R>, R : Response> (&self, cmd: C, pin: ::std::vec::Vec<u8>) -> KineticResult<R> {
let auth = Pin { pin: pin };
let token = self.send_raw(auth, cmd);
Self::receive_raw(token)
}
}
pub type AsyncClient = Client<::channel::AsyncChannel, Receiver<Result>>;
impl Client<::channel::AsyncChannel, Receiver<Result>> {
#[inline]
pub fn new_with_credentials<A: ToSocketAddrs>(addr: A, credentials: ::authentication::Credentials)
-> KineticResult<Client<::channel::AsyncChannel, Receiver<Result>>> {
let c = try!(::channel::AsyncChannel::new(addr, DEFAULT_MAX_PENDING));
Ok( Client { channel: c,
cluster_version: 0,
default_credentials: credentials,
async_return_type: PhantomData })
}
#[inline]
pub fn send_future<C: Command<R>, R: Response + 'static> (&self, cmd: C) -> Future<KineticResult<R>> {
let rx = self.send_raw(self.default_credentials.clone(), cmd);
Future::spawn(move|| { Self::receive_raw(rx) })
}
}