forked from Blightmud/Blightmud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.rs
More file actions
291 lines (253 loc) · 8.48 KB
/
session.rs
File metadata and controls
291 lines (253 loc) · 8.48 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use libtelnet_rs::{compatibility::CompatibilityTable, telnet::op_option as opt, Parser};
use log::debug;
use std::sync::{
atomic::{AtomicBool, Ordering},
mpsc::Sender,
Arc, Mutex,
};
use crate::{
io::{LogWriter, Logger},
lua::LuaScript,
net::MudConnection,
net::BUFFER_SIZE,
net::{OutputBuffer, TelnetMode},
timer::TimerEvent,
tts::TTSController,
Event,
};
#[cfg(test)]
use mockall::automock;
#[derive(Clone)]
pub struct Session {
pub connection: Arc<Mutex<MudConnection>>,
pub gmcp: Arc<AtomicBool>,
pub main_writer: Sender<Event>,
pub timer_writer: Sender<TimerEvent>,
pub telnet_parser: Arc<Mutex<Parser>>,
pub output_buffer: Arc<Mutex<OutputBuffer>>,
pub prompt_input: Arc<Mutex<String>>,
pub save_history: Arc<AtomicBool>,
pub lua_script: Arc<Mutex<LuaScript>>,
pub logger: Arc<Mutex<dyn LogWriter + Send>>,
pub tts_ctrl: Arc<Mutex<TTSController>>,
}
#[cfg_attr(test, automock)]
impl Session {
pub fn connect(&mut self, host: &str, port: u16, tls: bool) -> bool {
let mut connected = false;
if let Ok(mut connection) = self.connection.lock() {
connected = match connection.connect(host, port, tls) {
Ok(_) => true,
Err(err) => {
debug!("Failed to connect: {}", err);
false
}
};
}
if connected {
self.main_writer
.send(Event::StartLogging(host.to_string(), false))
.unwrap();
self.main_writer.send(Event::Connected).unwrap();
}
connected
}
pub fn disconnect(&mut self) {
let mut connection = self.connection.lock().unwrap();
if connection.connected() {
connection.disconnect().ok();
if let Ok(mut output_buffer) = self.output_buffer.lock() {
output_buffer.clear()
}
if let Ok(mut parser) = self.telnet_parser.lock() {
parser.options.reset_states();
};
self.stop_logging();
}
}
pub fn connected(&self) -> bool {
let connection = self.connection.lock().unwrap();
connection.connected()
}
pub fn connection_id(&self) -> u16 {
let connection = self.connection.lock().unwrap();
connection.id
}
pub fn host(&self) -> String {
let connection = self.connection.lock().unwrap();
connection.host.clone()
}
pub fn port(&self) -> u16 {
let connection = self.connection.lock().unwrap();
connection.port
}
pub fn tls(&self) -> bool {
let connection = self.connection.lock().unwrap();
connection.tls
}
pub fn start_logging(&self, host: &str) {
if let Ok(mut logger) = self.logger.lock() {
self.main_writer
.send(Event::Info(format!("Started logging for: {}", host)))
.unwrap();
logger.start_logging(host).ok();
}
}
pub fn stop_logging(&self) {
if let Ok(mut logger) = self.logger.lock() {
self.main_writer
.send(Event::Info("Logging stopped".to_string()))
.unwrap();
logger.stop_logging().ok();
}
}
pub fn save_history(&self) -> bool {
self.save_history.load(Ordering::Relaxed)
}
pub fn set_save_history(&self, toggle: bool) {
self.save_history.store(toggle, Ordering::Relaxed);
}
pub fn send_event(&mut self, event: Event) {
self.main_writer.send(event).unwrap();
}
pub fn close(&mut self) -> Result<(), Box<dyn std::error::Error>> {
self.disconnect();
self.main_writer.send(Event::Quit)?;
self.timer_writer.send(TimerEvent::Quit)?;
self.tts_ctrl.lock().unwrap().shutdown();
Ok(())
}
}
#[derive(Clone)]
pub struct SessionBuilder {
main_writer: Option<Sender<Event>>,
timer_writer: Option<Sender<TimerEvent>>,
screen_dimensions: Option<(u16, u16)>,
tts_enabled: bool,
save_history: bool,
}
impl SessionBuilder {
pub fn new() -> Self {
Self {
main_writer: None,
timer_writer: None,
screen_dimensions: None,
tts_enabled: false,
save_history: false,
}
}
pub fn main_writer(mut self, main_writer: Sender<Event>) -> Self {
self.main_writer = Some(main_writer);
self
}
pub fn timer_writer(mut self, timer_writer: Sender<TimerEvent>) -> Self {
self.timer_writer = Some(timer_writer);
self
}
pub fn screen_dimensions(mut self, dimensions: (u16, u16)) -> Self {
self.screen_dimensions = Some(dimensions);
self
}
pub fn tts_enabled(mut self, enabled: bool) -> Self {
self.tts_enabled = enabled;
self
}
pub fn save_history(mut self, enabled: bool) -> Self {
self.save_history = enabled;
self
}
pub fn build(self) -> Session {
let main_writer = self.main_writer.unwrap();
let timer_writer = self.timer_writer.unwrap();
let dimensions = self.screen_dimensions.unwrap();
let tts_enabled = self.tts_enabled;
let save_history = self.save_history;
Session {
connection: Arc::new(Mutex::new(MudConnection::new())),
gmcp: Arc::new(AtomicBool::new(false)),
main_writer: main_writer.clone(),
timer_writer,
telnet_parser: Arc::new(Mutex::new(Parser::with_support_and_capacity(
BUFFER_SIZE,
build_compatibility_table(),
))),
output_buffer: Arc::new(Mutex::new(OutputBuffer::new(
&TelnetMode::UnterminatedPrompt,
))),
prompt_input: Arc::new(Mutex::new(String::new())),
save_history: Arc::new(AtomicBool::new(save_history)),
lua_script: Arc::new(Mutex::new(LuaScript::new(main_writer, dimensions))),
logger: Arc::new(Mutex::new(Logger::default())),
tts_ctrl: Arc::new(Mutex::new(TTSController::new(tts_enabled))),
}
}
}
fn build_compatibility_table() -> CompatibilityTable {
let mut telnet_compat = CompatibilityTable::default();
telnet_compat.support(opt::MCCP2);
telnet_compat.support(opt::EOR);
telnet_compat.support(opt::ECHO);
telnet_compat
}
#[cfg(test)]
mod session_test {
use mockall::predicate::eq;
use super::*;
use crate::io::MockLogWriter;
use crate::{event::Event, model::Line, timer::TimerEvent};
use std::sync::mpsc::{channel, Receiver, Sender};
fn build_session() -> (Session, Receiver<Event>, Receiver<TimerEvent>) {
let (writer, reader): (Sender<Event>, Receiver<Event>) = channel();
let (timer_writer, timer_reader): (Sender<TimerEvent>, Receiver<TimerEvent>) = channel();
let session = SessionBuilder::new()
.main_writer(writer.clone())
.timer_writer(timer_writer.clone())
.screen_dimensions((80, 80))
.build();
loop {
if reader.try_recv().is_err() {
break;
}
}
(session, reader, timer_reader)
}
#[test]
fn test_session_build() {
let _ = build_session();
}
#[test]
fn test_session_send_event() {
let (mut session, reader, _timer_reader) = build_session();
session.send_event(Event::Output(Line::from("test test")));
assert_eq!(reader.recv(), Ok(Event::Output(Line::from("test test"))));
}
#[test]
fn test_logging() {
let (mut session, reader, _timer_reader) = build_session();
let mut logger = MockLogWriter::new();
logger
.expect_start_logging()
.with(eq("mysteryhost"))
.times(1)
.returning(|_| Ok(()));
logger.expect_stop_logging().times(1).returning(|| Ok(()));
session.logger = Arc::new(Mutex::new(logger));
session.start_logging("mysteryhost");
assert_eq!(
reader.recv(),
Ok(Event::Info("Started logging for: mysteryhost".to_string()))
);
session.stop_logging();
assert_eq!(
reader.recv(),
Ok(Event::Info("Logging stopped".to_string()))
);
}
#[test]
fn test_close() {
let (mut session, reader, timer_reader) = build_session();
session.close().unwrap();
assert_eq!(reader.recv(), Ok(Event::Quit));
assert_eq!(timer_reader.recv(), Ok(TimerEvent::Quit));
}
}