Implement endpoints for notify

This commit is contained in:
David Majdandžić
2023-03-25 14:12:25 +01:00
parent b3d6faa1c9
commit c505c4b44e

134
main.js
View File

@@ -521,48 +521,48 @@ 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.busy) {
// this.logger.log1(`Cannot send many message, not bound to ${this.url} or busy`); this.logger.log1(`Cannot send many message, no clients connected to ${this.port} or busy`);
// reject(`Cannot send many message, not bound to ${this.url} or busy`); reject(`Cannot send many message, no clients connected to ${this.port} or busy`);
// return; return;
// } }
// this.busy = true; this.busy = true;
// this.timer = new NanoTimer(); this.timer = new NanoTimer();
// let counter = 0; let counter = 0;
// let previousUpdateCounter = 0; let previousUpdateCounter = 0;
//
// this.updateTimer = new NanoTimer();
// this.updateTimer.setInterval(() => {
// if (previousUpdateCounter !== counter) {
// this.eventEmitter.emit(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, counter);
// previousUpdateCounter = counter;
// }
// }, '', `${MESSAGE_SEND_UPDATE_DELAY / 1000} s`);
//
// this.timer.setInterval(() => {
// if (count > 0 && counter >= count) {
// this.cancelNotifyInterval();
// } else {
// this.notify(source, destination, message)
// .catch(e => this.logger.log1(`Error sending message: ${e}`));
// counter++;
// }
// }, '', `${interval} s`);
// resolve();
// });
// }
// cancelNotifyInterval() { this.updateTimer = new NanoTimer();
// if (!!this.timer) { this.updateTimer.setInterval(() => {
// this.timer.clearInterval(); if (previousUpdateCounter !== counter) {
// this.updateTimer.clearInterval(); this.eventEmitter.emit(CenterSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, counter);
// this.timer = null; previousUpdateCounter = counter;
// this.updateTimer = null; }
// } }, '', `${MESSAGE_SEND_UPDATE_DELAY / 1000} s`);
// this.busy = false;
// } this.timer.setInterval(() => {
if (count > 0 && counter >= count) {
this.cancelNotifyInterval();
} else {
this.notify(source, destination, message)
.catch(e => this.logger.log1(`Error sending message: ${e}`));
counter++;
}
}, '', `${interval} s`);
resolve();
});
}
cancelNotifyInterval() {
if (!!this.timer) {
this.timer.clearInterval();
this.updateTimer.clearInterval();
this.timer = null;
this.updateTimer = null;
}
this.busy = false;
}
close() { close() {
this.disconnectingPromise.promise = new Promise((resolve, reject) => { this.disconnectingPromise.promise = new Promise((resolve, reject) => {
@@ -865,15 +865,61 @@ class HTTPServer {
} }
notify(req, res) { notify(req, res) {
return undefined; let server = centerSessionManager.getSession(req.params.id);
let source = req.body.source;
let destination = req.body.destination;
let message = req.body.message;
this.logger.log1(`Sending notify message from ${source} to ${destination} with message ${message} on session with ID ${req.params.id}`)
if (server) {
server.notify(source, destination, message)
.then(pdu => res.send(JSON.stringify(pdu)))
.catch(err => res.status(400).send(JSON.stringify(err)));
} else {
this.logger.log1(`No session found with ID ${req.params.id}`);
res.status(404).send();
}
} }
notifyMany(req, res) { notifyMany(req, res) {
return undefined; let server = centerSessionManager.getSession(req.params.id);
let source = req.body.source;
let destination = req.body.destination;
let message = req.body.message;
let interval = req.body.interval / 1000;
let count = req.body.count;
if (!!req.body.perSecond) {
interval = 1 / req.body.perSecond;
}
let perSecond = 1 / interval;
this.logger.log1(
`Sending ${count} notify messages from ${source} to ${destination} with message ${message} on session with ID ${req.params.id} at a rate of ${perSecond} per second.`);
if (session) {
session.notifyOnInterval(source, destination, message, interval, count)
.then(pdu => res.send(JSON.stringify(pdu)))
.catch(err => res.status(400).send(JSON.stringify(err)));
} else {
this.logger.log1(`No session found with ID ${req.params.id}`);
res.status(404).send();
}
} }
cancelNotifyMany(req, res) { cancelNotifyMany(req, res) {
return undefined; let server = centerSessionManager.getSession(req.params.id);
if (!server.busy) {
res.status(400).send({
err: true,
msg: `Session with ID ${req.params.id} is not sending messages`
});
return;
}
this.logger.log1(`Cancelling send timer for server with ID ${req.params.id}`);
if (server) {
server.cancelSendInterval();
res.send();
} else {
this.logger.log1(`No session found with ID ${req.params.id}`);
res.status(404).send();
}
} }
disconnectCenterSession(req, res) { disconnectCenterSession(req, res) {