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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#[cfg(not(feature = "use-mock-crust"))]
use maidsafe_utilities::thread::RaiiThreadJoiner;
#[cfg(not(feature = "use-mock-crust"))]
use rust_sodium;
#[cfg(feature = "use-mock-crust")]
use std::cell::RefCell;
use std::sync::mpsc::{Receiver, Sender, channel};
use id::FullId;
use action::Action;
use event::Event;
use cache::NullCache;
use core::{Core, Role};
use data::{Data, DataIdentifier};
use error::{InterfaceError, RoutingError};
use authority::Authority;
use messages::{Request, DEFAULT_PRIORITY, CLIENT_GET_PRIORITY};
use types::MessageId;
use xor_name::XorName;
type RoutingResult = Result<(), RoutingError>;
pub struct Client {
interface_result_tx: Sender<Result<(), InterfaceError>>,
interface_result_rx: Receiver<Result<(), InterfaceError>>,
action_sender: ::types::RoutingActionSender,
#[cfg(feature = "use-mock-crust")]
core: RefCell<Core>,
#[cfg(not(feature = "use-mock-crust"))]
_raii_joiner: ::maidsafe_utilities::thread::RaiiThreadJoiner,
}
impl Client {
#[cfg(not(feature = "use-mock-crust"))]
pub fn new(event_sender: Sender<Event>, keys: Option<FullId>) -> Result<Client, RoutingError> {
rust_sodium::init();
let (action_sender, mut core) =
Core::new(event_sender, Role::Client, keys, Box::new(NullCache), false);
let (tx, rx) = channel();
let raii_joiner = RaiiThreadJoiner::new(thread!("Client thread", move || {
core.run();
}));
Ok(Client {
interface_result_tx: tx,
interface_result_rx: rx,
action_sender: action_sender,
_raii_joiner: raii_joiner,
})
}
#[cfg(feature = "use-mock-crust")]
pub fn new(event_sender: Sender<Event>, keys: Option<FullId>) -> Result<Client, RoutingError> {
let (action_sender, core) =
Core::new(event_sender, Role::Client, keys, Box::new(NullCache), false);
let (tx, rx) = channel();
Ok(Client {
interface_result_tx: tx,
interface_result_rx: rx,
action_sender: action_sender,
core: RefCell::new(core),
})
}
#[cfg(feature = "use-mock-crust")]
pub fn poll(&self) -> bool {
self.core.borrow_mut().poll()
}
#[cfg(feature = "use-mock-crust")]
pub fn resend_unacknowledged(&self) -> bool {
self.core.borrow_mut().resend_unacknowledged()
}
#[cfg(feature = "use-mock-crust")]
pub fn has_unacknowledged(&self) -> bool {
self.core.borrow().has_unacknowledged()
}
pub fn send_get_request(&mut self,
dst: Authority,
data_id: DataIdentifier,
message_id: MessageId)
-> Result<(), InterfaceError> {
self.send_action(Request::Get(data_id, message_id), dst, CLIENT_GET_PRIORITY)
}
pub fn send_put_request(&self,
dst: Authority,
data: Data,
message_id: MessageId)
-> Result<(), InterfaceError> {
self.send_action(Request::Put(data, message_id), dst, DEFAULT_PRIORITY)
}
pub fn send_post_request(&self,
dst: Authority,
data: Data,
message_id: MessageId)
-> Result<(), InterfaceError> {
self.send_action(Request::Post(data, message_id), dst, DEFAULT_PRIORITY)
}
pub fn send_delete_request(&self,
dst: Authority,
data: Data,
message_id: MessageId)
-> Result<(), InterfaceError> {
self.send_action(Request::Delete(data, message_id), dst, DEFAULT_PRIORITY)
}
pub fn send_get_account_info_request(&mut self,
dst: Authority,
message_id: MessageId)
-> Result<(), InterfaceError> {
self.send_action(Request::GetAccountInfo(message_id),
dst,
CLIENT_GET_PRIORITY)
}
pub fn name(&self) -> Result<XorName, InterfaceError> {
let (result_tx, result_rx) = channel();
try!(self.action_sender.send(Action::Name { result_tx: result_tx }));
self.receive_action_result(&result_rx)
}
fn send_action(&self,
content: Request,
dst: Authority,
priority: u8)
-> Result<(), InterfaceError> {
let action = Action::ClientSendRequest {
content: content,
dst: dst,
priority: priority,
result_tx: self.interface_result_tx.clone(),
};
try!(self.action_sender.send(action));
try!(self.receive_action_result(&self.interface_result_rx))
}
#[cfg(not(feature = "use-mock-crust"))]
fn receive_action_result<T>(&self, rx: &Receiver<T>) -> Result<T, InterfaceError> {
Ok(try!(rx.recv()))
}
#[cfg(feature = "use-mock-crust")]
fn receive_action_result<T>(&self, rx: &Receiver<T>) -> Result<T, InterfaceError> {
while self.poll() {}
Ok(try!(rx.recv()))
}
}
impl Drop for Client {
fn drop(&mut self) {
if let Err(err) = self.action_sender.send(Action::Terminate) {
error!("Error {:?} sending event to Core", err);
}
}
}