-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlib.rs
More file actions
305 lines (266 loc) · 11.6 KB
/
lib.rs
File metadata and controls
305 lines (266 loc) · 11.6 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use sustenet_shared as shared;
use std::net::{ IpAddr, Ipv4Addr };
use std::str::FromStr;
use std::sync::{ Arc, LazyLock };
use tokio::io::{ AsyncReadExt, AsyncWriteExt, BufReader };
use tokio::net::TcpStream;
use tokio::sync::mpsc::Sender;
use tokio::sync::{ RwLock, mpsc };
use sustenet_shared::ClientPlugin;
use shared::logging::{ LogType, Logger };
use shared::packets::cluster::ToClient;
use shared::packets::master::ToUnknown;
use shared::utils::constants::{ DEFAULT_IP, MASTER_PORT };
use shared::{ lread_string, lselect };
lazy_static::lazy_static! {
pub static ref CLUSTER_SERVERS: Arc<RwLock<Vec<ClusterInfo>>> = Arc::new(
RwLock::new(Vec::new())
);
pub static ref CONNECTION: Arc<RwLock<Option<Connection>>> = Arc::new(
RwLock::new(
Some(Connection {
ip: get_ip(DEFAULT_IP),
port: MASTER_PORT,
connection_type: ConnectionType::MasterServer,
})
)
);
}
pub static LOGGER: LazyLock<Logger> = LazyLock::new(|| Logger::new(LogType::Cluster));
#[derive(Debug, Clone)]
pub struct ClusterInfo {
pub name: String,
pub ip: String,
pub port: u16,
pub max_connections: u32,
}
#[derive(Clone, Copy)]
pub struct Connection {
pub ip: IpAddr,
pub port: u16,
pub connection_type: ConnectionType,
}
impl From<ClusterInfo> for Connection {
fn from(info: ClusterInfo) -> Self {
Connection {
ip: IpAddr::from_str(info.ip.as_str()).expect("Failed to parse the IP."),
port: info.port,
connection_type: ConnectionType::ClusterServer,
}
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum ConnectionType {
MasterServer,
ClusterServer,
None,
}
impl std::fmt::Display for ConnectionType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ConnectionType::MasterServer => write!(f, "Master Server"),
ConnectionType::ClusterServer => write!(f, "Cluster Server"),
ConnectionType::None => write!(f, "Unknown"),
}
}
}
pub fn get_ip(ip: &str) -> IpAddr {
IpAddr::from_str(ip).unwrap_or(
IpAddr::from_str(DEFAULT_IP).unwrap_or(IpAddr::V4(Ipv4Addr::LOCALHOST))
)
}
pub async fn cleanup() {}
pub async fn start<P>(plugin: P) where P: ClientPlugin + Send + Sync + 'static {
// Get the connection LOGGER.information.
let connection = *CONNECTION.read().await;
if connection.is_none() {
return;
}
let connection = connection.unwrap();
let ip = connection.ip;
let port = connection.port;
let connection_type = connection.connection_type;
{
*CONNECTION.write().await = None;
}
let (tx, mut rx) = mpsc::channel::<Box<[u8]>>(10);
plugin.set_sender(tx.clone());
let handler = tokio::spawn(async move {
LOGGER.warning(format!("Connecting to the {connection_type}...").as_str());
let mut stream = TcpStream::connect(format!("{}:{}", ip, port)).await.expect(
format!("Failed to connect to the {connection_type} at {ip}:{port}.").as_str()
);
LOGGER.success(format!("Connected to the {connection_type} at {ip}:{port}.").as_str());
let (reader, mut writer) = stream.split();
let mut reader = BufReader::new(reader);
lselect! {
command = reader.read_u8() => {
if command.is_err() {
continue;
}
LOGGER.info(format!("Received data: {:?}", command).as_str());
match connection_type {
ConnectionType::MasterServer => match command.unwrap() {
x if x == ToUnknown::SendClusters as u8 => {
let amount = match reader.read_u8().await {
Ok(amount) => amount,
Err(_) => {
LOGGER.error("Failed to read the amount of clusters.");
continue;
}
};
let mut cluster_servers_tmp = Vec::new();
for _ in 0..amount {
let name = lread_string!(reader, |msg| LOGGER.error(msg), "cluster name");
let ip = lread_string!(reader, |msg| LOGGER.error(msg), "cluster IP");
let port = match reader.read_u16().await {
Ok(port) => port,
Err(_) => {
LOGGER.error("Failed to read the cluster port.");
continue;
}
};
let max_connections = match reader.read_u32().await {
Ok(max_connections) => max_connections,
Err(_) => {
LOGGER.error("Failed to read the cluster max connections.");
continue;
}
};
cluster_servers_tmp.push(ClusterInfo {
name,
ip,
port,
max_connections,
});
}
{
{
let mut cluster_servers = CLUSTER_SERVERS.write().await;
*cluster_servers = cluster_servers_tmp;
LOGGER.success(format!("Received {amount} Cluster servers from the {connection_type}.").as_str());
println!("{:?}", *cluster_servers);
}
}
},
cmd => plugin.receive_master(tx.clone(), cmd, &mut reader).await,
}
ConnectionType::ClusterServer => match command.unwrap() {
x if x == ToClient::SendClusters as u8 => {
let amount = match reader.read_u8().await {
Ok(amount) => amount,
Err(_) => {
LOGGER.error("Failed to read the amount of clusters.");
continue;
}
};
let mut cluster_servers_tmp = Vec::new();
for _ in 0..amount {
let name = lread_string!(reader, |msg| LOGGER.error(msg), "cluster name");
let ip = lread_string!(reader, |msg| LOGGER.error(msg), "cluster IP");
let port = match reader.read_u16().await {
Ok(port) => port,
Err(_) => {
LOGGER.error("Failed to read the cluster port.");
continue;
}
};
let max_connections = match reader.read_u32().await {
Ok(max_connections) => max_connections,
Err(_) => {
LOGGER.error("Failed to read the cluster max connections.");
continue;
}
};
cluster_servers_tmp.push(ClusterInfo {
name,
ip,
port,
max_connections,
});
}
{
{
let mut cluster_servers = CLUSTER_SERVERS.write().await;
*cluster_servers = cluster_servers_tmp;
LOGGER.success(format!("Received {amount} Cluster servers from the {connection_type}.").as_str());
println!("{:?}", *cluster_servers);
}
}
},
x if x == ToClient::DisconnectCluster as u8 => todo!(),
x if x == ToClient::LeaveCluster as u8 => todo!(),
x if x == ToClient::VersionOfKey as u8 => todo!(),
x if x == ToClient::SendPubKey as u8 => todo!(),
x if x == ToClient::Authenticate as u8 => todo!(),
x if x == ToClient::Move as u8 => todo!(),
cmd => plugin.receive_cluster(tx.clone(), cmd, &mut reader).await,
}
_ => (),
}
}
result = rx.recv() => {
if let Some(data) = result {
if data.is_empty() {
writer.shutdown().await.expect("Failed to shutdown the writer.");
LOGGER.info("Closing connection...");
break;
}
writer.write_all(&data).await.expect("Failed to write to the Server.");
writer.flush().await.expect("Failed to flush the writer.");
LOGGER.info(format!("Sent {data:?} as data to the {connection_type}.").as_str());
} else {
writer.shutdown().await.expect("Failed to shutdown the writer.");
LOGGER.info("Shutting down connection...");
break;
}
}
}
});
let _ = handler.await;
}
pub async fn send_data(tx: &Sender<Box<[u8]>>, data: Box<[u8]>) {
tx.send(data).await.expect("Failed to send data to the Server.");
}
pub async fn join_cluster(tx: &Sender<Box<[u8]>>, id: usize) {
if id < (0 as usize) {
LOGGER.error("Failed to join a cluster. The cluster ID is invalid (less than 0).");
return;
}
let cluster_servers = CLUSTER_SERVERS.read().await;
if cluster_servers.is_empty() {
LOGGER.error("Failed to join a cluster. No cluster servers are available.");
return;
}
if id >= cluster_servers.len() {
LOGGER.error(
"Failed to join a cluster. The cluster ID is invalid (greater than the amount of clusters)."
);
return;
}
let cluster = (
match cluster_servers.get(id) {
Some(cluster) => cluster,
None => {
LOGGER.error("Failed to join a cluster. The cluster ID is invalid.");
return;
}
}
).clone();
LOGGER.success(format!("Client is joining cluster {}", cluster.name).as_str());
let connection = match std::panic::catch_unwind(|| Connection::from(cluster)) {
Ok(connection) => connection,
Err(_) => {
LOGGER.error("Failed to create a connection with the Cluster Server.");
return;
}
};
{
// Overwrite the current connection with the cluster connection.
*CONNECTION.write().await = Some(connection);
stop(tx).await;
}
}
async fn stop(tx: &Sender<Box<[u8]>>) {
tx.send(Box::new([])).await.expect("Failed to shutdown.");
}