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
use std::fmt::Debug;
use ::error::*;
pub trait ClientConnection: Sized+Debug {
fn send(&mut self, bytes: Vec<u8>) -> ClientResult<Vec<u8>>;
}
#[cfg(test)]
pub mod test {
use std::fmt;
use super::*;
use ::op::Message;
use ::authority::Catalog;
use ::serialize::binary::{BinDecoder, BinEncoder, BinSerializable};
use ::error::*;
pub struct TestClientConnection<'a> {
catalog: &'a Catalog
}
impl<'a> TestClientConnection<'a> {
pub fn new(catalog: &'a Catalog) -> TestClientConnection<'a> {
TestClientConnection { catalog: catalog }
}
}
impl<'a> ClientConnection for TestClientConnection<'a> {
fn send(&mut self, bytes: Vec<u8>) -> ClientResult<Vec<u8>> {
let mut decoder = BinDecoder::new(&bytes);
let message = try!(Message::read(&mut decoder));
let response = self.catalog.handle_request(&message);
let mut buf = Vec::with_capacity(512);
{
let mut encoder = BinEncoder::new(&mut buf);
try!(response.emit(&mut encoder));
}
Ok(buf)
}
}
impl<'a> fmt::Debug for TestClientConnection<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TestClientConnection catalog")
}
}
}