Refactor current structure to represent clientSessions
This commit is contained in:
127
main.js
127
main.js
@@ -101,7 +101,7 @@ class Logger {
|
|||||||
|
|
||||||
let logger = new Logger("main");
|
let logger = new Logger("main");
|
||||||
|
|
||||||
class SessionStatus {
|
class ClientSessionStatus {
|
||||||
static OK = "OK";
|
static OK = "OK";
|
||||||
static CONNECTING = "CONNECTING";
|
static CONNECTING = "CONNECTING";
|
||||||
static CONNECTED = "CONNECTED";
|
static CONNECTED = "CONNECTED";
|
||||||
@@ -113,7 +113,7 @@ class SessionStatus {
|
|||||||
static NOT_CONNECTED = "NOT_CONNECTED";
|
static NOT_CONNECTED = "NOT_CONNECTED";
|
||||||
}
|
}
|
||||||
|
|
||||||
class Session {
|
class ClientSession {
|
||||||
auto_enquire_link_period = 500;
|
auto_enquire_link_period = 500;
|
||||||
eventEmitter = new EventEmitter();
|
eventEmitter = new EventEmitter();
|
||||||
busy = false;
|
busy = false;
|
||||||
@@ -147,23 +147,23 @@ class Session {
|
|||||||
this.password = password;
|
this.password = password;
|
||||||
|
|
||||||
this.logger.log1(`Session created with url ${this.url}, username ${this.username}, password ${this.password} and ID ${this.id}`);
|
this.logger.log1(`Session created with url ${this.url}, username ${this.username}, password ${this.password} and ID ${this.id}`);
|
||||||
this.status = SessionStatus.NOT_CONNECTED;
|
this.status = ClientSessionStatus.NOT_CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
setStatus(newStatus) {
|
setStatus(newStatus) {
|
||||||
this.status = newStatus;
|
this.status = newStatus;
|
||||||
this.eventEmitter.emit(Session.STATUS_CHANGED_EVENT, newStatus);
|
this.eventEmitter.emit(ClientSession.STATUS_CHANGED_EVENT, newStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
this.connectingPromise.promise = new Promise((resolve, reject) => {
|
this.connectingPromise.promise = new Promise((resolve, reject) => {
|
||||||
if (this.status !== SessionStatus.NOT_CONNECTED) {
|
if (this.status !== ClientSessionStatus.NOT_CONNECTED) {
|
||||||
this.logger.log1("Session already connected");
|
this.logger.log1("ClientSession already connected");
|
||||||
reject("Session already connected");
|
reject("ClientSession already connected");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.logger.log1("Connecting to " + this.url);
|
this.logger.log1("Connecting to " + this.url);
|
||||||
this.setStatus(SessionStatus.CONNECTING);
|
this.setStatus(ClientSessionStatus.CONNECTING);
|
||||||
try {
|
try {
|
||||||
this.session = smpp.connect({
|
this.session = smpp.connect({
|
||||||
url: this.url,
|
url: this.url,
|
||||||
@@ -172,7 +172,7 @@ class Session {
|
|||||||
this.session.on('error', this.clientError.bind(this));
|
this.session.on('error', this.clientError.bind(this));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.log1("Connection failed to " + this.url);
|
this.logger.log1("Connection failed to " + this.url);
|
||||||
this.setStatus(SessionStatus.CONNECT_FAILED);
|
this.setStatus(ClientSessionStatus.CONNECT_FAILED);
|
||||||
reject("Connection failed to " + this.url);
|
reject("Connection failed to " + this.url);
|
||||||
}
|
}
|
||||||
this.connectingPromise.resolve = resolve;
|
this.connectingPromise.resolve = resolve;
|
||||||
@@ -193,11 +193,11 @@ class Session {
|
|||||||
|
|
||||||
connected() {
|
connected() {
|
||||||
this.logger.log1("Connected to " + this.url);
|
this.logger.log1("Connected to " + this.url);
|
||||||
this.setStatus(SessionStatus.CONNECTED);
|
this.setStatus(ClientSessionStatus.CONNECTED);
|
||||||
this.session.on('debug', (type, msg, payload) => {
|
this.session.on('debug', (type, msg, payload) => {
|
||||||
if (type.includes('pdu.')) {
|
if (type.includes('pdu.')) {
|
||||||
this.eventEmitter.emit(msg, payload);
|
this.eventEmitter.emit(msg, payload);
|
||||||
this.eventEmitter.emit(Session.ANY_PDU_EVENT, payload);
|
this.eventEmitter.emit(ClientSession.ANY_PDU_EVENT, payload);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.connectingPromise.resolve();
|
this.connectingPromise.resolve();
|
||||||
@@ -205,14 +205,14 @@ class Session {
|
|||||||
|
|
||||||
bind() {
|
bind() {
|
||||||
this.bindingPromise.promise = new Promise((resolve, reject) => {
|
this.bindingPromise.promise = new Promise((resolve, reject) => {
|
||||||
if (this.status !== SessionStatus.CONNECTED) {
|
if (this.status !== ClientSessionStatus.CONNECTED) {
|
||||||
this.logger.log1(`Cannot bind, not connected to ${this.url}`);
|
this.logger.log1(`Cannot bind, not connected to ${this.url}`);
|
||||||
reject(`Cannot bind, not connected to ${this.url}`);
|
reject(`Cannot bind, not connected to ${this.url}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.log1("Trying to bind to " + this.url)
|
this.logger.log1("Trying to bind to " + this.url)
|
||||||
if (this.status !== SessionStatus.CONNECTED) {
|
if (this.status !== ClientSessionStatus.CONNECTED) {
|
||||||
this.logger.log1(`Cannot bind, not connected to ${this.url}`);
|
this.logger.log1(`Cannot bind, not connected to ${this.url}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -220,7 +220,7 @@ class Session {
|
|||||||
this.logger.log1(`Cannot bind, username or password not set`);
|
this.logger.log1(`Cannot bind, username or password not set`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.setStatus(SessionStatus.BINDING);
|
this.setStatus(ClientSessionStatus.BINDING);
|
||||||
this.logger.log1(`Binding to ${this.url} with username ${this.username} and password ${this.password}`);
|
this.logger.log1(`Binding to ${this.url} with username ${this.username} and password ${this.password}`);
|
||||||
|
|
||||||
this.session.bind_transceiver({
|
this.session.bind_transceiver({
|
||||||
@@ -236,11 +236,11 @@ class Session {
|
|||||||
bindReply(pdu) {
|
bindReply(pdu) {
|
||||||
if (pdu.command_status === 0) {
|
if (pdu.command_status === 0) {
|
||||||
this.logger.log1(`Bound to ${this.url} with username ${this.username} and password ${this.password}`);
|
this.logger.log1(`Bound to ${this.url} with username ${this.username} and password ${this.password}`);
|
||||||
this.setStatus(SessionStatus.BOUND);
|
this.setStatus(ClientSessionStatus.BOUND);
|
||||||
this.bindingPromise.resolve();
|
this.bindingPromise.resolve();
|
||||||
} else {
|
} else {
|
||||||
this.logger.log1(`Bind failed to ${this.url} with username ${this.username} and password ${this.password}`);
|
this.logger.log1(`Bind failed to ${this.url} with username ${this.username} and password ${this.password}`);
|
||||||
this.setStatus(SessionStatus.BIND_FAILED);
|
this.setStatus(ClientSessionStatus.BIND_FAILED);
|
||||||
this.bindingPromise.reject();
|
this.bindingPromise.reject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -278,7 +278,7 @@ class Session {
|
|||||||
this.updateTimer = new NanoTimer();
|
this.updateTimer = new NanoTimer();
|
||||||
this.updateTimer.setInterval(() => {
|
this.updateTimer.setInterval(() => {
|
||||||
if (previousUpdateCounter !== counter) {
|
if (previousUpdateCounter !== counter) {
|
||||||
this.eventEmitter.emit(Session.MESSAGE_SEND_COUNTER_UPDATE_EVENT, counter);
|
this.eventEmitter.emit(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, counter);
|
||||||
previousUpdateCounter = counter;
|
previousUpdateCounter = counter;
|
||||||
}
|
}
|
||||||
}, '', `${MESSAGE_SEND_UPDATE_DELAY / 1000} s`);
|
}, '', `${MESSAGE_SEND_UPDATE_DELAY / 1000} s`);
|
||||||
@@ -308,13 +308,13 @@ class Session {
|
|||||||
|
|
||||||
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 !== ClientSessionStatus.BOUND && this.status !== ClientSessionStatus.CONNECTED) {
|
||||||
this.logger.log1(`Cannot close session, not bound to ${this.url}`);
|
this.logger.log1(`Cannot close session, not bound to ${this.url}`);
|
||||||
reject(`Cannot close session, not bound to ${this.url}`);
|
reject(`Cannot close session, not bound to ${this.url}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.session.close();
|
this.session.close();
|
||||||
this.setStatus(SessionStatus.NOT_CONNECTED);
|
this.setStatus(ClientSessionStatus.NOT_CONNECTED);
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
return this.disconnectingPromise.promise;
|
return this.disconnectingPromise.promise;
|
||||||
@@ -335,13 +335,13 @@ class Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
canSend() {
|
canSend() {
|
||||||
return this.status === SessionStatus.BOUND;
|
return this.status === ClientSessionStatus.BOUND;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SessionManager {
|
class ClientSessionManager {
|
||||||
sessionIdCounter = 0;
|
sessionIdCounter = 0;
|
||||||
logger = new Logger("SessionManager");
|
logger = new Logger("ClientSessionManager");
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.sessions = {};
|
this.sessions = {};
|
||||||
@@ -359,7 +359,7 @@ class SessionManager {
|
|||||||
return this.sessions[urlB64];
|
return this.sessions[urlB64];
|
||||||
}
|
}
|
||||||
this.logger.log1(`Creating session to ${url} with username ${username} and password ${password}`);
|
this.logger.log1(`Creating session to ${url} with username ${username} and password ${password}`);
|
||||||
let session = new Session(this.sessionIdCounter++, url, username, password);
|
let session = new ClientSession(this.sessionIdCounter++, url, username, password);
|
||||||
this.addSession(session);
|
this.addSession(session);
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
@@ -371,7 +371,7 @@ class SessionManager {
|
|||||||
|
|
||||||
deleteSession(session) {
|
deleteSession(session) {
|
||||||
this.logger.log1(`Deleting session with ID ${session.id}`);
|
this.logger.log1(`Deleting session with ID ${session.id}`);
|
||||||
if (session.status === SessionStatus.BOUND || session.status === SessionStatus.CONNECTED) {
|
if (session.status === ClientSessionStatus.BOUND || session.status === ClientSessionStatus.CONNECTED) {
|
||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
delete this.sessions[btoa(session.url)];
|
delete this.sessions[btoa(session.url)];
|
||||||
@@ -411,16 +411,16 @@ class HTTPServer {
|
|||||||
constructor() {
|
constructor() {
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
app.get('/api/sessions', this.getSessions.bind(this));
|
app.get('/api/client', this.getClientSessions.bind(this));
|
||||||
app.post('/api/sessions', this.createSession.bind(this));
|
app.post('/api/client', this.createClientSession.bind(this));
|
||||||
app.get('/api/sessions/:id', this.getById.bind(this));
|
app.get('/api/client/:id', this.getClientSessionById.bind(this));
|
||||||
app.post('/api/sessions/:id/send', this.send.bind(this));
|
app.post('/api/client/:id/send', this.send.bind(this));
|
||||||
app.post('/api/sessions/:id/sendMany', this.sendMany.bind(this));
|
app.post('/api/client/:id/sendMany', this.sendMany.bind(this));
|
||||||
app.delete('/api/sessions/:id/sendMany', this.cancelSendMany.bind(this));
|
app.delete('/api/client/:id/sendMany', this.cancelSendMany.bind(this));
|
||||||
app.post('/api/sessions/:id/bind', this.bind.bind(this));
|
app.post('/api/client/:id/bind', this.bindClientSession.bind(this));
|
||||||
app.post('/api/sessions/:id/connect', this.connect.bind(this));
|
app.post('/api/client/:id/connect', this.connectClientSession.bind(this));
|
||||||
app.delete('/api/sessions/:id/connect', this.disconnect.bind(this));
|
app.delete('/api/client/:id/connect', this.disconnectClientSession.bind(this));
|
||||||
app.delete('/api/sessions/:id', this.deleteSession.bind(this));
|
app.delete('/api/client/:id', this.deleteClientSession.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}`)
|
||||||
@@ -429,19 +429,19 @@ class HTTPServer {
|
|||||||
|
|
||||||
// TODO: These requests deserve error handling
|
// TODO: These requests deserve error handling
|
||||||
|
|
||||||
getSessions(req, res) {
|
getClientSessions(req, res) {
|
||||||
this.logger.log1("Getting sessions");
|
this.logger.log1("Getting sessions");
|
||||||
res.send(JSON.stringify(sessionManager.serialize()));
|
res.send(JSON.stringify(clientSessionManager.serialize()));
|
||||||
}
|
}
|
||||||
|
|
||||||
createSession(req, res) {
|
createClientSession(req, res) {
|
||||||
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 = clientSessionManager.createSession(req.body.url, req.body.username, req.body.password);
|
||||||
res.send(JSON.stringify(session.serialize()));
|
res.send(JSON.stringify(session.serialize()));
|
||||||
}
|
}
|
||||||
|
|
||||||
getById(req, res) {
|
getClientSessionById(req, res) {
|
||||||
let session = sessionManager.getSession(req.params.id);
|
let session = clientSessionManager.getSession(req.params.id);
|
||||||
this.logger.log1(`Getting session by ID ${req.params.id}`);
|
this.logger.log1(`Getting session by ID ${req.params.id}`);
|
||||||
if (session) {
|
if (session) {
|
||||||
this.logger.log1(`Session found with ID ${req.params.id}`)
|
this.logger.log1(`Session found with ID ${req.params.id}`)
|
||||||
@@ -453,7 +453,7 @@ class HTTPServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
send(req, res) {
|
send(req, res) {
|
||||||
let session = sessionManager.getSession(req.params.id);
|
let session = clientSessionManager.getSession(req.params.id);
|
||||||
let source = req.body.source;
|
let source = req.body.source;
|
||||||
let destination = req.body.destination;
|
let destination = req.body.destination;
|
||||||
let message = req.body.message;
|
let message = req.body.message;
|
||||||
@@ -469,7 +469,7 @@ class HTTPServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sendMany(req, res) {
|
sendMany(req, res) {
|
||||||
let session = sessionManager.getSession(req.params.id);
|
let session = clientSessionManager.getSession(req.params.id);
|
||||||
let source = req.body.source;
|
let source = req.body.source;
|
||||||
let destination = req.body.destination;
|
let destination = req.body.destination;
|
||||||
let message = req.body.message;
|
let message = req.body.message;
|
||||||
@@ -492,7 +492,7 @@ class HTTPServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cancelSendMany(req, res) {
|
cancelSendMany(req, res) {
|
||||||
let session = sessionManager.getSession(req.params.id);
|
let session = clientSessionManager.getSession(req.params.id);
|
||||||
if (!session.busy) {
|
if (!session.busy) {
|
||||||
res.status(400).send({
|
res.status(400).send({
|
||||||
err: true,
|
err: true,
|
||||||
@@ -510,29 +510,32 @@ class HTTPServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bind(req, res) {
|
bindClientSession(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?
|
||||||
let session = sessionManager.getSession(req.params.id);
|
let session = clientSessionManager.getSession(req.params.id);
|
||||||
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());
|
.catch(err => res.status(400).send({
|
||||||
|
err: true,
|
||||||
|
msg: 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(req, res) {
|
connectClientSession(req, res) {
|
||||||
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 = clientSessionManager.getSession(req.params.id);
|
||||||
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({
|
.catch(err => res.status(400).send({
|
||||||
err: true,
|
err: true,
|
||||||
msg: err.message
|
msg: 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}`);
|
||||||
@@ -540,15 +543,15 @@ class HTTPServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect(req, res) {
|
disconnectClientSession(req, res) {
|
||||||
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 = clientSessionManager.getSession(req.params.id);
|
||||||
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({
|
.catch(err => res.status(400).send({
|
||||||
err: true,
|
err: true,
|
||||||
msg: err.message
|
msg: 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}`);
|
||||||
@@ -556,11 +559,11 @@ class HTTPServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteSession(req, res) {
|
deleteClientSession(req, res) {
|
||||||
this.logger.log1(`Deleting session with ID ${req.params.id}`);
|
this.logger.log1(`Deleting session with ID ${req.params.id}`);
|
||||||
let session = sessionManager.getSession(req.params.id);
|
let session = clientSessionManager.getSession(req.params.id);
|
||||||
if (session) {
|
if (session) {
|
||||||
sessionManager.deleteSession(session);
|
clientSessionManager.deleteSession(session);
|
||||||
res.send();
|
res.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}`);
|
||||||
@@ -589,11 +592,11 @@ class WSServer {
|
|||||||
addClient(ws, sessionId) {
|
addClient(ws, sessionId) {
|
||||||
if (!this.clients[sessionId]) {
|
if (!this.clients[sessionId]) {
|
||||||
this.clients[sessionId] = [];
|
this.clients[sessionId] = [];
|
||||||
let session = sessionManager.getSession(sessionId);
|
let session = clientSessionManager.getSession(sessionId);
|
||||||
if (session) {
|
if (session) {
|
||||||
// session.on(Session.STATUS_CHANGED_EVENT, this.onSessionChange.bind(this, sessionId));
|
// session.on(ClientSession.STATUS_CHANGED_EVENT, this.onSessionChange.bind(this, sessionId));
|
||||||
// session.on(Session.ANY_PDU_EVENT, this.pduEvent.bind(this, sessionId));
|
// session.on(ClientSession.ANY_PDU_EVENT, this.pduEvent.bind(this, sessionId));
|
||||||
session.on(Session.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onMessageSendCounterUpdate.bind(this, sessionId));
|
session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onMessageSendCounterUpdate.bind(this, sessionId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.logger.log1(`Added client to session ID: ${sessionId}`);
|
this.logger.log1(`Added client to session ID: ${sessionId}`);
|
||||||
@@ -675,9 +678,9 @@ class WSServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let sessionManager = new SessionManager();
|
let clientSessionManager = new ClientSessionManager();
|
||||||
sessionManager.startup();
|
clientSessionManager.startup();
|
||||||
let session = sessionManager.getSession(0);
|
let session = clientSessionManager.getSession(0);
|
||||||
session.connect().then(() => session.bind());
|
session.connect().then(() => session.bind());
|
||||||
new WSServer();
|
new WSServer();
|
||||||
new HTTPServer();
|
new HTTPServer();
|
Reference in New Issue
Block a user