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
212
213
use super::*;
use exar::*;
use std::net::{ToSocketAddrs, TcpListener};
use std::sync::{Arc, Mutex};
use std::thread;
#[derive(Debug)]
pub struct Server {
credentials: Credentials,
db: Arc<Mutex<Database>>,
listener: TcpListener
}
impl Server {
pub fn new(config: ServerConfig, db: Database) -> Result<Server, DatabaseError> {
let db = Arc::new(Mutex::new(db));
match TcpListener::bind(&*config.address()) {
Ok(listener) => Ok(Server {
credentials: Credentials {
username: config.username,
password: config.password
},
db: db,
listener: listener
}),
Err(err) => Err(DatabaseError::from_io_error(err))
}
}
pub fn bind<A: ToSocketAddrs>(address: A, db: Database) -> Result<Server, DatabaseError> {
let db = Arc::new(Mutex::new(db));
match TcpListener::bind(address) {
Ok(listener) => {
Ok(Server {
credentials: Credentials {
username: None,
password: None
},
db: db,
listener: listener
})
},
Err(err) => Err(DatabaseError::from_io_error(err))
}
}
pub fn with_credentials(mut self, username: &str, password: &str) -> Server {
self.credentials.username = Some(username.to_string());
self.credentials.password = Some(password.to_string());
self
}
pub fn listen(&self) {
for stream in self.listener.incoming() {
match stream {
Ok(stream) => {
let db = self.db.clone();
let config = self.credentials.clone();
thread::spawn(|| {
let peer_addr = match stream.peer_addr() {
Ok(addr) => Some(addr),
Err(_) => None
};
match Handler::new(stream, db, config) {
Ok(mut handler) => {
match peer_addr {
Some(addr) => info!("Client connected: {}", addr),
None => info!("Client connected: unknown peer address")
}
handler.run();
match peer_addr {
Some(addr) => info!("Client disconnected: {}", addr),
None => info!("Client disconnected: unknown peer address")
}
},
Err(err) => warn!("Unable to accept client connection: {}", err)
}
});
},
Err(err) => warn!("Client connection failed: {}", err)
}
};
}
}
#[cfg(test)]
mod tests {
use exar::*;
use exar_net::*;
use exar_testkit::*;
use super::super::*;
use std::fs::*;
use std::net::{TcpStream, ToSocketAddrs};
use std::thread;
fn create_client<A: ToSocketAddrs>(addr: A) -> TcpMessageStream<TcpStream> {
let stream = TcpStream::connect(addr).expect("Unable to connect to the TCP stream");
TcpMessageStream::new(stream).expect("Unable to create TCP message stream client")
}
#[test]
fn test_constructor() {
with_addr(&mut |addr| {
let db = Database::new(DatabaseConfig::default());
let mut config = ServerConfig::default();
let addr_string = format!("{}", addr);
let addr_parts: Vec<_> = addr_string.split(":").collect();
config.host = addr_parts[0].parse().expect("Unable to parse host");
config.port = addr_parts[1].parse().expect("Unable to parse port");
assert!(Server::new(config, db).is_ok());
});
}
#[test]
fn test_constructor_failure() {
let db = Database::new(DatabaseConfig::default());
let mut config = ServerConfig::default();
config.port = 1000;
assert!(Server::new(config, db).is_err());
}
#[test]
fn test_bind() {
with_addr(&mut |addr| {
let db = Database::new(DatabaseConfig::default());
assert!(Server::bind(&addr, db).is_ok());
});
}
#[test]
fn test_bind_failure() {
let db = Database::new(DatabaseConfig::default());
assert!(Server::bind("127.0.0.1:1000", db).is_err());
}
#[test]
fn test_connection() {
with_addr(&mut |addr| {
let collection_name = random_collection_name();
let db = Database::new(DatabaseConfig::default());
let server = Server::bind(addr, db).expect("Unable to start the TCP server");
thread::spawn(move || {
server.listen();
});
let mut client = create_client(addr);
assert!(client.send_message(TcpMessage::Connect(collection_name.to_owned(),
None, None)).is_ok());
assert_eq!(client.recv_message(), Ok(TcpMessage::Connected));
assert!(remove_file(format!("{}.log", collection_name)).is_ok());
assert!(remove_file(format!("{}.index.log", collection_name)).is_ok());
});
}
#[test]
fn test_connection_with_credentials() {
with_addr(&mut |addr| {
let collection_name = random_collection_name();
let db = Database::new(DatabaseConfig::default());
let server = Server::bind(addr, db).expect("Unable to start the TCP server");
let server = server.with_credentials("username", "password");
thread::spawn(move || {
server.listen();
});
let mut client = create_client(addr);
assert!(client.send_message(TcpMessage::Connect(collection_name.to_owned(),
None, None)).is_ok());
assert_eq!(client.recv_message(), Ok(TcpMessage::Error(DatabaseError::AuthenticationError)));
assert!(client.send_message(TcpMessage::Connect(collection_name.to_owned(),
Some("username".to_owned()), Some("password".to_owned()))).is_ok());
assert_eq!(client.recv_message(), Ok(TcpMessage::Connected));
assert!(remove_file(format!("{}.log", collection_name)).is_ok());
assert!(remove_file(format!("{}.index.log", collection_name)).is_ok());
});
}
}