Implement writing to and reading from a sessions file

This commit is contained in:
David Majdandžić
2023-03-24 17:51:34 +01:00
parent 32d5d726af
commit 401497bee0
2 changed files with 23 additions and 7 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.idea .idea
node_modules node_modules
package-lock.json package-lock.json
sessions.json

29
main.js
View File

@@ -9,11 +9,9 @@ const app = express();
const bodyParser = require("body-parser"); const bodyParser = require("body-parser");
const WebSocket = require("ws"); const WebSocket = require("ws");
const crypto = require("crypto");
const SERVER_PORT = process.env.SERVER_PORT || 8190; const SERVER_PORT = process.env.SERVER_PORT || 8190;
const WS_SERVER_PORT = process.env.WS_SERVER_PORT || 8191; const WS_SERVER_PORT = process.env.WS_SERVER_PORT || 8191;
const SESSIONS_FILE = process.env.SESSIONS_FILE || "sessions.json";
[ [
@@ -292,13 +290,16 @@ class Session {
} }
class SessionManager { class SessionManager {
// 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");
constructor() { constructor() {
this.sessions = {}; this.sessions = {};
process.on('exit', this.cleanup.bind(this));
process.on('SIGINT', this.cleanup.bind(this));
process.on('SIGUSR1', this.cleanup.bind(this));
process.on('SIGUSR2', this.cleanup.bind(this));
process.on('uncaughtException', this.cleanup.bind(this));
} }
createSession(url, username, password) { createSession(url, username, password) {
@@ -337,6 +338,21 @@ class SessionManager {
return session.serialize(); return session.serialize();
}); });
} }
cleanup() {
this.logger.log1(`Saving sessions to ${SESSIONS_FILE}...`);
fs.writeFileSync(SESSIONS_FILE, JSON.stringify(this.serialize(), null, 4));
process.exit(0);
}
startup() {
let sessions = fs.readFileSync(SESSIONS_FILE);
sessions = JSON.parse(sessions);
this.logger.log1(`Loaded ${sessions.length} sessions from ${SESSIONS_FILE}...`);
sessions.forEach(session => {
this.createSession(session.url, session.username, session.password);
})
}
} }
class HTTPServer { class HTTPServer {
@@ -541,7 +557,6 @@ class WSServer {
} }
let sessionManager = new SessionManager(); let sessionManager = new SessionManager();
let session = sessionManager.createSession('smpp://localhost:7001', 'test', 'test'); sessionManager.startup();
session.connect().then(() => session.bind());
new WSServer(); new WSServer();
new HTTPServer(); new HTTPServer();