Code polish

This commit is contained in:
David Majdandžić
2023-03-31 20:24:59 +02:00
parent fb310fa53a
commit 5b26dd6a61
9 changed files with 26 additions and 20 deletions

View File

@@ -141,10 +141,6 @@ export class Center extends SmppSession {
}; };
} }
getPort(): number {
return this.port;
}
private validateSessions(reject: (reason?: any) => void) { private validateSessions(reject: (reason?: any) => void) {
if (this.sessions.length === 0) { if (this.sessions.length === 0) {
reject(`No clients connected`); reject(`No clients connected`);

View File

@@ -18,5 +18,5 @@ export class CenterSessionManager extends SessionManager {
this.setup(); this.setup();
} }
comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Center).getPort() === arg; comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Center).port === arg;
} }

View File

@@ -175,10 +175,6 @@ export class Client extends SmppSession {
}); });
} }
getUrl(): string {
return this.url;
}
private connectSession(): Promise<void> { private connectSession(): Promise<void> {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
this.validateFields(reject); this.validateFields(reject);
@@ -198,11 +194,13 @@ export class Client extends SmppSession {
this.setStatus(2); this.setStatus(2);
if (this.connectPromise) { if (this.connectPromise) {
this.connectPromise.resolve(); this.connectPromise.resolve();
} else {
this.logger.log1(`Client-${this.id} connected without connect promise`);
} }
} }
private eventSessionError(pdu: PDU): void { private eventSessionError(pdu: any): void {
this.logger.log1(`Client-${this.id} error on ${this.url}`); this.logger.log1(`Client-${this.id} error on ${this.url} - ${pdu.message}`);
this.setStatus(0); this.setStatus(0);
this.rejectPromises(); this.rejectPromises();
} }

View File

@@ -18,5 +18,5 @@ export default class ClientSessionManager extends SessionManager {
this.setup(); this.setup();
} }
comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Client).getUrl() === arg; comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Client).url === arg;
} }

View File

@@ -55,7 +55,7 @@ export default class ClientRequestHandler extends RequestHandler {
doConnect(req: any, res: any): void { doConnect(req: any, res: any): void {
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => { this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
this.logger.log1(`Connecting client session with ID ${req.params.id}`) this.logger.log1(`Connecting client session with ID ${req.params.id}`);
let client = session as Client; let client = session as Client;
client.doConnect() client.doConnect()
.then(() => res.send(session.serialize())) .then(() => res.send(session.serialize()))

View File

@@ -43,7 +43,7 @@ export abstract class RequestHandler {
doSendSingleJob(req: any, res: any): void { doSendSingleJob(req: any, res: any): void {
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => { this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
this.logger.log1(`Sending pre-configured message on session with ID ${req.params.id}`) this.logger.log1(`Sending pre-configured message on session with ID ${req.params.id}`);
session.sendSingleDefault() session.sendSingleDefault()
.then(pdu => res.send(pdu), .then(pdu => res.send(pdu),
err => res.status(400).send({ err => res.status(400).send({
@@ -55,7 +55,7 @@ export abstract class RequestHandler {
doSend(req: any, res: any): void { doSend(req: any, res: any): void {
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => { this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
this.logger.log1(`Sending message on session with ID ${req.params.id}`) this.logger.log1(`Sending message on session with ID ${req.params.id}`);
let tempJob: Job = JSON.parse(JSON.stringify(session.defaultSingleJob)); let tempJob: Job = JSON.parse(JSON.stringify(session.defaultSingleJob));
tempJob.pdu.source_addr = req.body.source; tempJob.pdu.source_addr = req.body.source;
tempJob.pdu.destination_addr = req.body.destination; tempJob.pdu.destination_addr = req.body.destination;
@@ -73,14 +73,14 @@ export abstract class RequestHandler {
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => { this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
let job: Job = session.defaultMultipleJob; let job: Job = session.defaultMultipleJob;
job.update(req); job.update(req);
this.logger.log1(`Updating default job on session with ID ${req.params.id}`) this.logger.log1(`Updating default job on session with ID ${req.params.id}`);
res.send(session.serialize()); res.send(session.serialize());
}, this.handleSessionNotFound.bind(this, req, res)); }, this.handleSessionNotFound.bind(this, req, res));
} }
doSendManyJob(req: any, res: any): void { doSendManyJob(req: any, res: any): void {
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => { this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
this.logger.log1(`Sending pre-configured messages on session with ID ${req.params.id}`) this.logger.log1(`Sending pre-configured messages on session with ID ${req.params.id}`);
session.sendMultipleDefault() session.sendMultipleDefault()
.then(() => res.send({}), .then(() => res.send({}),
err => res.status(400).send({ err => res.status(400).send({
@@ -92,7 +92,7 @@ export abstract class RequestHandler {
doSendMany(req: any, res: any) { doSendMany(req: any, res: any) {
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => { this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
this.logger.log1(`Sending message on session with ID ${req.params.id}`) this.logger.log1(`Sending message on session with ID ${req.params.id}`);
let tempJob: Job = JSON.parse(JSON.stringify(session.defaultMultipleJob)); let tempJob: Job = JSON.parse(JSON.stringify(session.defaultMultipleJob));
tempJob.pdu.source_addr = req.body.source; tempJob.pdu.source_addr = req.body.source;
tempJob.pdu.destination_addr = req.body.destination; tempJob.pdu.destination_addr = req.body.destination;
@@ -112,6 +112,7 @@ export abstract class RequestHandler {
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => { this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
this.logger.log1(`Cancelling send timer for session with ID ${req.params.id}`); this.logger.log1(`Cancelling send timer for session with ID ${req.params.id}`);
session.cancelSendInterval(); session.cancelSendInterval();
res.send({});
}, this.handleSessionNotFound.bind(this, req, res)); }, this.handleSessionNotFound.bind(this, req, res));
} }
@@ -127,7 +128,9 @@ export abstract class RequestHandler {
doDelete(req: any, res: any) { doDelete(req: any, res: any) {
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => { this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
this.logger.log1(`Deleting session with ID ${req.params.id}`);
this.sessionManager.removeSession(session); this.sessionManager.removeSession(session);
res.send({});
}, this.handleSessionNotFound.bind(this, req, res)); }, this.handleSessionNotFound.bind(this, req, res));
} }

View File

@@ -5,9 +5,9 @@ export default class PersistentPromise {
constructor(callback: (resolve: (value?: any) => void, reject: (reason?: any) => void) => void) { constructor(callback: (resolve: (value?: any) => void, reject: (reason?: any) => void) => void) {
this.promise = new Promise((resolve, reject) => { this.promise = new Promise((resolve, reject) => {
callback(resolve, reject);
this.promiseResolve = resolve; this.promiseResolve = resolve;
this.promiseReject = reject; this.promiseReject = reject;
callback(resolve, reject);
}); });
} }

View File

@@ -31,6 +31,10 @@ export abstract class SmppSession {
abstract _username: string; abstract _username: string;
get username(): string {
return this._username;
}
set username(username: string) { set username(username: string) {
this._username = username; this._username = username;
this.eventEmitter.emit(this.EVENT.STATE_CHANGED, this.serialize()); this.eventEmitter.emit(this.EVENT.STATE_CHANGED, this.serialize());
@@ -38,6 +42,10 @@ export abstract class SmppSession {
abstract _password: string; abstract _password: string;
get password(): string {
return this._password;
}
set password(password: string) { set password(password: string) {
this._password = password; this._password = password;
this.eventEmitter.emit(this.EVENT.STATE_CHANGED, this.serialize()); this.eventEmitter.emit(this.EVENT.STATE_CHANGED, this.serialize());

View File

@@ -6,7 +6,8 @@
"rootDir": "./src", "rootDir": "./src",
"strict": true, "strict": true,
"moduleResolution": "node", "moduleResolution": "node",
"esModuleInterop": true "esModuleInterop": true,
"noImplicitAny": true,
}, },
"exclude": [ "exclude": [
"./node_modules" "./node_modules"