forked from Blightmud/Blightmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.rs
232 lines (207 loc) · 7.5 KB
/
session.rs
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
use libtelnet_rs::{compatibility::CompatibilityTable, telnet::op_option as opt, Parser};
use std::{
net::TcpStream,
sync::{
atomic::{AtomicBool, AtomicU16, Ordering},
mpsc::Sender,
Arc, Mutex,
},
};
use crate::{io::Logger, lua::LuaScript, net::OutputBuffer, timer::TimerEvent, Event};
use log::debug;
#[derive(Default)]
pub struct CommunicationOptions {
pub mccp2: bool,
pub debug_gmcp: bool,
}
#[derive(Clone)]
pub struct Session {
pub connection_id: u16,
pub host: Arc<Mutex<String>>,
pub port: Arc<AtomicU16>,
pub connected: Arc<AtomicBool>,
pub gmcp: Arc<AtomicBool>,
pub stream: Arc<Mutex<Option<TcpStream>>>,
pub main_writer: Sender<Event>,
pub timer_writer: Sender<TimerEvent>,
pub terminate: Arc<AtomicBool>,
pub telnet_parser: Arc<Mutex<Parser>>,
pub output_buffer: Arc<Mutex<OutputBuffer>>,
pub prompt_input: Arc<Mutex<String>>,
pub lua_script: Arc<Mutex<LuaScript>>,
pub comops: Arc<Mutex<CommunicationOptions>>,
pub logger: Arc<Mutex<Logger>>,
}
impl Session {
pub fn connect(&mut self, host: &str, port: u16) -> bool {
self.connection_id += 1;
if let Ok(mut m_host) = self.host.lock() {
*m_host = host.to_string();
}
self.port.store(port, Ordering::Relaxed);
debug!("Connecting to {}:{}", host, port);
if let Ok(stream) = TcpStream::connect(format!("{}:{}", host, port)) {
self.stream.lock().unwrap().replace(stream);
self.connected.store(true, Ordering::Relaxed);
self.main_writer
.send(Event::StartLogging(host.to_string(), false))
.unwrap();
self.main_writer.send(Event::Connected).unwrap();
}
self.connected.load(Ordering::Relaxed)
}
pub fn disconnect(&mut self) {
if self.connected.load(Ordering::Relaxed) {
let host = self.host.lock().unwrap();
let port = self.port.load(Ordering::Relaxed);
debug!("Disconnecting from {}:{}", host, port);
if let Ok(mut stream) = self.stream.lock() {
stream
.as_mut()
.unwrap()
.shutdown(std::net::Shutdown::Both)
.ok();
*stream = None;
self.connected.store(false, Ordering::Relaxed);
}
if let Ok(mut output_buffer) = self.output_buffer.lock() {
output_buffer.clear()
}
self.comops = Arc::new(Mutex::new(CommunicationOptions::default()));
self.telnet_parser = Arc::new(Mutex::new(Parser::with_support_and_capacity(
4096,
build_compatibility_table(),
)));
self.stop_logging();
debug!("Disconnected from {}:{}", host, port);
}
}
pub fn start_logging(&self, host: &str) {
if let Ok(mut logger) = self.logger.lock() {
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 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)?;
Ok(())
}
}
#[derive(Clone)]
pub struct SessionBuilder {
main_writer: Option<Sender<Event>>,
timer_writer: Option<Sender<TimerEvent>>,
screen_dimensions: Option<(u16, u16)>,
}
impl SessionBuilder {
pub fn new() -> Self {
Self {
main_writer: None,
timer_writer: None,
screen_dimensions: None,
}
}
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 build(self) -> Session {
let main_writer = self.main_writer.unwrap();
let timer_writer = self.timer_writer.unwrap();
let dimensions = self.screen_dimensions.unwrap();
Session {
connection_id: 0,
host: Arc::new(Mutex::new(String::new())),
port: Arc::new(AtomicU16::new(0)),
connected: Arc::new(AtomicBool::new(false)),
gmcp: Arc::new(AtomicBool::new(false)),
stream: Arc::new(Mutex::new(None)),
main_writer: main_writer.clone(),
timer_writer,
terminate: Arc::new(AtomicBool::new(false)),
telnet_parser: Arc::new(Mutex::new(Parser::with_support_and_capacity(
4096,
build_compatibility_table(),
))),
output_buffer: Arc::new(Mutex::new(OutputBuffer::new())),
prompt_input: Arc::new(Mutex::new(String::new())),
lua_script: Arc::new(Mutex::new(LuaScript::new(main_writer, dimensions))),
comops: Arc::new(Mutex::new(CommunicationOptions::default())),
logger: Arc::new(Mutex::new(Logger::default())),
}
}
}
fn build_compatibility_table() -> CompatibilityTable {
let mut telnet_compat = CompatibilityTable::default();
telnet_compat.support(opt::MCCP2);
telnet_compat.support(opt::GMCP);
telnet_compat.support(opt::EOR);
telnet_compat.support(opt::ECHO);
telnet_compat
}
#[cfg(test)]
mod session_test {
use super::{Session, SessionBuilder};
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();
(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 (session, reader, _timer_reader) = build_session();
assert!(!session.logger.lock().unwrap().is_logging());
session.start_logging("mysteryhost");
assert!(session.logger.lock().unwrap().is_logging());
session.stop_logging();
assert_eq!(
reader.recv(),
Ok(Event::Info("Logging stopped".to_string()))
);
assert!(!session.logger.lock().unwrap().is_logging());
}
#[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));
}
}