Implement cancel send timer

This commit is contained in:
David Majdandžić
2023-03-24 21:28:38 +01:00
parent b6e50f5e1b
commit 1af1eda9d8

45
main.js
View File

@@ -263,14 +263,12 @@ class Session {
} }
sendOnInterval(source, destination, message, interval, count) { sendOnInterval(source, destination, message, interval, count) {
// TODO: Create method for stopping sending messages
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, not bound to ${this.url} or busy`);
reject(`Cannot send many message, not bound to ${this.url} or busy`); reject(`Cannot send many message, not bound to ${this.url} or busy`);
return; return;
} }
// TODO: Remember to update busy when cancelling the timer
// TODO: Create event for counter update // TODO: Create event for counter update
this.busy = true; this.busy = true;
this.timer = new NanoTimer(); this.timer = new NanoTimer();
@@ -289,6 +287,14 @@ class Session {
}); });
} }
cancelSendInterval() {
if (!!this.timer) {
this.timer.clearInterval()
this.timer = null;
}
this.busy = false;
}
close() { close() {
this.disconnectingPromise.promise = new Promise((resolve, reject) => { this.disconnectingPromise.promise = new Promise((resolve, reject) => {
if (this.status !== SessionStatus.BOUND && this.status !== SessionStatus.CONNECTED) { if (this.status !== SessionStatus.BOUND && this.status !== SessionStatus.CONNECTED) {
@@ -399,6 +405,7 @@ class HTTPServer {
app.get('/api/sessions/:id', this.getById.bind(this)); app.get('/api/sessions/:id', this.getById.bind(this));
app.post('/api/sessions/:id/send', this.send.bind(this)); app.post('/api/sessions/:id/send', this.send.bind(this));
app.post('/api/sessions/:id/sendMany', this.sendMany.bind(this)); app.post('/api/sessions/:id/sendMany', this.sendMany.bind(this));
app.delete('/api/sessions/:id/sendMany', this.cancelSendMany.bind(this));
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));
@@ -409,6 +416,8 @@ class HTTPServer {
}.bind(this)); }.bind(this));
} }
// TODO: These requests deserve error handling
getSessions(req, res) { getSessions(req, res) {
this.logger.log1("Getting sessions"); this.logger.log1("Getting sessions");
res.send(JSON.stringify(sessionManager.serialize())); res.send(JSON.stringify(sessionManager.serialize()));
@@ -449,7 +458,6 @@ class HTTPServer {
} }
sendMany(req, res) { sendMany(req, res) {
// TODO: These requests deserve error handling
let session = sessionManager.getSession(req.params.id); let session = sessionManager.getSession(req.params.id);
let source = req.body.source; let source = req.body.source;
let destination = req.body.destination; let destination = req.body.destination;
@@ -472,6 +480,25 @@ class HTTPServer {
} }
} }
cancelSendMany(req, res) {
let session = sessionManager.getSession(req.params.id);
if (!session.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 session with ID ${req.params.id}`);
if (session) {
session.cancelSendInterval();
res.send();
} else {
this.logger.log1(`No session found with ID ${req.params.id}`);
res.status(404).send();
}
}
bind(req, res) { bind(req, res) {
this.logger.log1(`Binding session with ID ${req.params.id}`) this.logger.log1(`Binding session with ID ${req.params.id}`)
// Maybe make this async? // Maybe make this async?
@@ -479,7 +506,7 @@ class HTTPServer {
if (session) { if (session) {
session.bind() session.bind()
.then(() => res.send(JSON.stringify(session.serialize()))) .then(() => res.send(JSON.stringify(session.serialize())))
.catch(err => res.status(400).send(JSON.stringify(err))); .catch(err => res.status(400).send());
} 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();
@@ -492,7 +519,10 @@ class HTTPServer {
if (session) { if (session) {
session.connect() session.connect()
.then(() => res.send(JSON.stringify(session.serialize()))) .then(() => res.send(JSON.stringify(session.serialize())))
.catch(err => res.status(400).send(JSON.stringify(err))); .catch(err => res.status(400).send({
err: true,
msg: err.message
}));
} 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();
@@ -505,7 +535,10 @@ class HTTPServer {
if (session) { if (session) {
session.close() session.close()
.then(() => res.send(JSON.stringify(session.serialize()))) .then(() => res.send(JSON.stringify(session.serialize())))
.catch(err => res.status(400).send(JSON.stringify(err))); .catch(err => res.status(400).send({
err: true,
msg: err.message
}));
} 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();