Implement new "Busy" state for client and center to signify many message send

This commit is contained in:
David Majdandžić
2023-03-27 17:24:32 +02:00
parent 2e09392627
commit eb9c8468e2
2 changed files with 13 additions and 18 deletions

29
main.js
View File

@@ -16,13 +16,8 @@ const CLIENT_SESSIONS_FILE = process.env.CLIENT_SESSIONS_FILE || "client_session
const CENTER_SESSIONS_FILE = process.env.CENTER_SESSIONS_FILE || "center_sessions.json"; const CENTER_SESSIONS_FILE = process.env.CENTER_SESSIONS_FILE || "center_sessions.json";
const MESSAGE_SEND_UPDATE_DELAY = process.env.MESSAGE_SEND_UPDATE_DELAY || 500; const MESSAGE_SEND_UPDATE_DELAY = process.env.MESSAGE_SEND_UPDATE_DELAY || 500;
// Check if the same happens for the inverse
// TODO: Add support for encodings // TODO: Add support for encodings
// TODO: Currently there is no feedback about the success of the multi send operation save for the counter
// Make a simple event that fires once multi send is complete
// TODO: Implement some sort of metrics on frontend by counting the pdus // TODO: Implement some sort of metrics on frontend by counting the pdus
// TODO: Currently clients don't realize they've been disconnected by time out
// TODO: Currently the center does not realize a session dropped before sending ccredentials
[ [
'debug', 'debug',
@@ -113,6 +108,7 @@ let logger = new Logger("main");
class ClientSessionStatus { class ClientSessionStatus {
static CONNECTING = "CONNECTING"; static CONNECTING = "CONNECTING";
static CONNECTED = "CONNECTED"; static CONNECTED = "CONNECTED";
static BUSY = "BUSY";
static BINDING = "BINDING"; static BINDING = "BINDING";
static BOUND = "BOUND"; static BOUND = "BOUND";
static NOT_CONNECTED = "NOT CONNECTED"; static NOT_CONNECTED = "NOT CONNECTED";
@@ -122,7 +118,6 @@ class ClientSession {
// TODO: Enable requesting DRs // TODO: Enable requesting DRs
auto_enquire_link_period = 500; auto_enquire_link_period = 500;
eventEmitter = new EventEmitter(); eventEmitter = new EventEmitter();
busy = false;
configuredMessageJob = { configuredMessageJob = {
source: "", source: "",
destination: "", destination: "",
@@ -339,12 +334,12 @@ class ClientSession {
sendOnInterval(source, destination, message, interval, count) { sendOnInterval(source, destination, message, interval, count) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.canSend() || this.busy) { if (!this.canSend()) {
this.logger.log1(`Client cannot send many message, not bound to ${this.url} or busy`); this.logger.log1(`Client cannot send many message, not bound to ${this.url} or busy`);
reject(`Client cannot send many message, not bound to ${this.url} or busy`); reject(`Client cannot send many message, not bound to ${this.url} or busy`);
return; return;
} }
this.busy = true; this.setStatus(ClientSessionStatus.BUSY);
let counter = 0; let counter = 0;
let previousUpdateCounter = 0; let previousUpdateCounter = 0;
@@ -392,7 +387,7 @@ class ClientSession {
this.timer = null; this.timer = null;
this.updateTimer = null; this.updateTimer = null;
} }
this.busy = false; this.setStatus(ClientSessionStatus.BOUND);
} }
close() { close() {
@@ -499,8 +494,9 @@ class ClientSessionManager {
class CenterSessionStatus { class CenterSessionStatus {
static CONNECTED = "CONNECTED"; static CONNECTED = "CONNECTED";
static WAITING_CONNECTION = "WAITING_CONNECTION"; static WAITING_CONNECTION = "WAITING CONNECTION";
static CONNECTION_PENDING = "CONNECTION_PENDING"; static CONNECTION_PENDING = "CONNECTION PENDING";
static BUSY = "BUSY";
} }
class CenterMode { class CenterMode {
@@ -512,7 +508,6 @@ class CenterMode {
class CenterSession { class CenterSession {
// TODO: If the port is in use this throws an exception, catch it and log it // TODO: If the port is in use this throws an exception, catch it and log it
eventEmitter = new EventEmitter(); eventEmitter = new EventEmitter();
busy = false;
sessions = []; sessions = [];
nextSession = 0; nextSession = 0;
mode = CenterMode.DEBUG; mode = CenterMode.DEBUG;
@@ -731,12 +726,12 @@ class CenterSession {
notifyOnInterval(source, destination, message, interval, count) { notifyOnInterval(source, destination, message, interval, count) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.canSend() || this.busy) { if (!this.canSend()) {
this.logger.log1(`Center cannot send many message, no sessions active to ${this.port} or busy`); this.logger.log1(`Center cannot send many message, no sessions active to ${this.port} or busy`);
reject(`Center cannot send many message, no sessions active to ${this.port} or busy`); reject(`Center cannot send many message, no sessions active to ${this.port} or busy`);
return; return;
} }
this.busy = true; this.setStatus(CenterSessionStatus.BUSY);
this.timer = new NanoTimer(); this.timer = new NanoTimer();
let counter = 0; let counter = 0;
let previousUpdateCounter = 0; let previousUpdateCounter = 0;
@@ -769,7 +764,7 @@ class CenterSession {
this.timer = null; this.timer = null;
this.updateTimer = null; this.updateTimer = null;
} }
this.busy = false; this.setStatus(CenterSessionStatus.CONNECTED);
} }
getNextSession() { getNextSession() {
@@ -1135,7 +1130,7 @@ class HTTPServer {
cancelSendMany(req, res) { cancelSendMany(req, res) {
let session = clientSessionManager.getSession(req.params.id); let session = clientSessionManager.getSession(req.params.id);
if (!session.busy) { if (session.status !== ClientSessionStatus.BUSY) {
res.status(400).send({ res.status(400).send({
err: true, err: true,
msg: `Session with ID ${req.params.id} is not sending messages` msg: `Session with ID ${req.params.id} is not sending messages`
@@ -1403,7 +1398,7 @@ class HTTPServer {
cancelNotifyMany(req, res) { cancelNotifyMany(req, res) {
let server = centerSessionManager.getSession(req.params.id); let server = centerSessionManager.getSession(req.params.id);
if (!server.busy) { if (server.status !== ClientSessionStatus.BUSY) {
res.status(400).send({ res.status(400).send({
err: true, err: true,
msg: `Session with ID ${req.params.id} is not sending messages` msg: `Session with ID ${req.params.id} is not sending messages`

View File

@@ -34,7 +34,7 @@ let centerMetrics = new Metrics();
const ws = new WebSocket(`ws://localhost:${WS_SERVER_PORT}`); const ws = new WebSocket(`ws://localhost:${WS_SERVER_PORT}`);
ws.on('open', () => { ws.on('open', () => {
console.log('WebSocket connection established'); console.log('WebSocket connection established');
ws.send("client:1"); ws.send("client:0");
}); });
ws.on('message', (data) => { ws.on('message', (data) => {
data = JSON.parse(data); data = JSON.parse(data);