Implement session deleting and generally clean up

This commit is contained in:
David Majdandžić
2023-03-24 17:17:49 +01:00
parent cd9faee388
commit 1ce8390189

79
main.js
View File

@@ -112,6 +112,7 @@ class SessionStatus {
} }
class Session { class Session {
// TODO: Create hash based on url, username and password
auto_enquire_link_period = 500; auto_enquire_link_period = 500;
eventEmitter = new EventEmitter(); eventEmitter = new EventEmitter();
@@ -243,18 +244,21 @@ class Session {
} }
send(source, destination, message) { send(source, destination, message) {
if (this.status !== SessionStatus.BOUND) { return new Promise((resolve, reject) => {
this.logger.log1(`Cannot send message, not bound to ${this.url}`); if (this.status !== SessionStatus.BOUND) {
return; this.logger.log1(`Cannot send message, not bound to ${this.url}`);
} reject(`Cannot send message, not bound to ${this.url}`);
this.logger.log1(`Sending message from ${source} to ${destination} with message ${message}`); return;
this.session.submit_sm({ }
source_addr: source, this.logger.log1(`Sending message from ${source} to ${destination} with message ${message}`);
destination_addr: destination, this.session.submit_sm({
short_message: message source_addr: source,
}, pdu => { destination_addr: destination,
short_message: message
}, pdu => {
resolve(pdu);
});
}); });
this.session.close();
} }
close() { close() {
@@ -287,7 +291,8 @@ class Session {
} }
class SessionManager { class SessionManager {
// TODO: Also enable session deletion (not just disconnection) // TODO: Somehow write the sessions to a file on disk, so that they can be restored on server restart
// And on startup read the file and restore the sessions
sessionIdCounter = 0; sessionIdCounter = 0;
logger = new Logger("SessionManager"); logger = new Logger("SessionManager");
@@ -307,6 +312,15 @@ class SessionManager {
this.sessions.push(session); this.sessions.push(session);
} }
deleteSession(session) {
this.logger.log1(`Deleting session with ID ${session.id}`);
if (session.status === SessionStatus.BOUND || session.status === SessionStatus.CONNECTED) {
session.close();
}
delete this.sessions[this.sessions.indexOf(session)];
this.sessions = this.sessions.filter(Boolean);
}
getSession(id) { getSession(id) {
return this.sessions.find((session) => { return this.sessions.find((session) => {
return session.id == id; return session.id == id;
@@ -333,6 +347,7 @@ class HTTPServer {
app.post('/api/sessions/:id/bind', this.bind.bind(this)); app.post('/api/sessions/:id/bind', this.bind.bind(this));
app.post('/api/sessions/:id/connect', this.connect.bind(this)); app.post('/api/sessions/:id/connect', this.connect.bind(this));
app.delete('/api/sessions/:id/connect', this.disconnect.bind(this)); app.delete('/api/sessions/:id/connect', this.disconnect.bind(this));
app.delete('/api/sessions/:id', this.deleteSession.bind(this));
this.server = app.listen(SERVER_PORT, function() { this.server = app.listen(SERVER_PORT, function() {
this.logger.log1(`HTTPServer listening at http://localhost:${SERVER_PORT}`) this.logger.log1(`HTTPServer listening at http://localhost:${SERVER_PORT}`)
@@ -345,6 +360,7 @@ class HTTPServer {
} }
createSession(req, res) { createSession(req, res) {
// TODO: Check for existing session
this.logger.log1("Creating session"); this.logger.log1("Creating session");
let session = sessionManager.createSession(req.body.url, req.body.username, req.body.password); let session = sessionManager.createSession(req.body.url, req.body.username, req.body.password);
res.send(JSON.stringify(session.serialize())); res.send(JSON.stringify(session.serialize()));
@@ -364,11 +380,15 @@ class HTTPServer {
send(req, res) { send(req, res) {
let session = sessionManager.getSession(req.params.id); let session = sessionManager.getSession(req.params.id);
let source = req.body.source;
let destination = req.body.destination;
let message = req.body.message;
this.logger.log1( this.logger.log1(
`Sending message from ${req.body.source} to ${req.body.destination} with message ${req.body.message} on session with ID ${req.params.id}`) `Sending message from ${source} to ${destination} with message ${message} on session with ID ${req.params.id}`)
if (session) { if (session) {
session.send(req.body.source, req.body.destination, req.body.message); session.send(source, destination, message)
res.send(JSON.stringify(session.serialize())); .then(pdu => res.send(JSON.stringify(pdu)))
.catch(err => res.status(400).send(JSON.stringify(err)));
} else { } else {
this.logger.log1(`No session found with ID ${req.params.id}`); this.logger.log1(`No session found with ID ${req.params.id}`);
res.status(404).send(); res.status(404).send();
@@ -380,7 +400,8 @@ class HTTPServer {
// Maybe make this async? // Maybe make this async?
let session = sessionManager.getSession(req.params.id); let session = sessionManager.getSession(req.params.id);
if (session) { if (session) {
session.bind().then(() => res.send(JSON.stringify(session.serialize()))) session.bind()
.then(() => res.send(JSON.stringify(session.serialize())))
.catch(err => res.status(400).send(JSON.stringify(err))); .catch(err => res.status(400).send(JSON.stringify(err)));
} else { } else {
this.logger.log1(`No session found with ID ${req.params.id}`); this.logger.log1(`No session found with ID ${req.params.id}`);
@@ -392,7 +413,8 @@ class HTTPServer {
this.logger.log1(`Connecting session with ID ${req.params.id}`) this.logger.log1(`Connecting session with ID ${req.params.id}`)
let session = sessionManager.getSession(req.params.id); let session = sessionManager.getSession(req.params.id);
if (session) { if (session) {
session.connect().then(() => res.send(JSON.stringify(session.serialize()))) session.connect()
.then(() => res.send(JSON.stringify(session.serialize())))
.catch(err => res.status(400).send(JSON.stringify(err))); .catch(err => res.status(400).send(JSON.stringify(err)));
} else { } else {
this.logger.log1(`No session found with ID ${req.params.id}`); this.logger.log1(`No session found with ID ${req.params.id}`);
@@ -404,13 +426,26 @@ class HTTPServer {
this.logger.log1(`Disconnecting session with ID ${req.params.id}`) this.logger.log1(`Disconnecting session with ID ${req.params.id}`)
let session = sessionManager.getSession(req.params.id); let session = sessionManager.getSession(req.params.id);
if (session) { if (session) {
session.close().then(() => res.send(JSON.stringify(session.serialize()))) session.close()
.then(() => res.send(JSON.stringify(session.serialize())))
.catch(err => res.status(400).send(JSON.stringify(err))); .catch(err => res.status(400).send(JSON.stringify(err)));
} else { } else {
this.logger.log1(`No session found with ID ${req.params.id}`); this.logger.log1(`No session found with ID ${req.params.id}`);
res.status(404).send(); res.status(404).send();
} }
} }
deleteSession(req, res) {
this.logger.log1(`Deleting session with ID ${req.params.id}`);
let session = sessionManager.getSession(req.params.id);
if (session) {
sessionManager.deleteSession(session);
res.send();
} else {
this.logger.log1(`No session found with ID ${req.params.id}`);
res.status(404).send();
}
}
} }
class WSServer { class WSServer {
@@ -484,14 +519,8 @@ class WSServer {
} }
} }
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
let sessionManager = new SessionManager(); let sessionManager = new SessionManager();
let session = sessionManager.createSession('localhost:7001', 'test', 'test'); let session = sessionManager.createSession('localhost:7001', 'test', 'test');
session.connect(); session.connect().then(() => session.bind());
new WSServer(); new WSServer();
new HTTPServer(); new HTTPServer();