Clean up code

This commit is contained in:
David Majdandžić
2023-04-05 17:59:51 +02:00
parent 6dbb108c2b
commit 9181033fda
4 changed files with 45 additions and 34 deletions

View File

@@ -69,6 +69,21 @@ export default class Center extends SmppSession {
super.defaultMultipleJob = job; super.defaultMultipleJob = job;
} }
sendPdu(pdu: PDU, force?: boolean): Promise<object> {
return new Promise((resolve, reject) => {
if (!force) {
this.validateSessions(reject);
}
this.logger.log5(`Center-${this.id} sending PDU: ${JSON.stringify(pdu)}`);
let pduCopy = new smpp.PDU(pdu.command, {...pdu});
let session = this.getNextSession();
this.processors.Preprocessor.forEach((processor: PduProcessor) => processor.processPdu(session, pduCopy));
session.send(pduCopy, (replyPdu: any) => {
resolve(replyPdu);
});
});
}
sendMultiple(job: Job): Promise<void> { sendMultiple(job: Job): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.validateSessions(reject); this.validateSessions(reject);
@@ -102,18 +117,6 @@ export default class Center extends SmppSession {
}); });
} }
sendPdu(pdu: object, force?: boolean): Promise<object> {
return new Promise((resolve, reject) => {
if (!force) {
this.validateSessions(reject);
}
this.logger.log5(`Center-${this.id} sending PDU: ${JSON.stringify(pdu)}`);
this.getNextSession().send(pdu, (replyPdu: any) => {
resolve(replyPdu);
});
});
}
initialize(): void { initialize(): void {
this.server = smpp.createServer({}, this.eventSessionConnected.bind(this)); this.server = smpp.createServer({}, this.eventSessionConnected.bind(this));
this.server.listen(this.port); this.server.listen(this.port);
@@ -140,8 +143,10 @@ export default class Center extends SmppSession {
status: this._status, status: this._status,
defaultSingleJob: this._defaultSingleJob.serialize(), defaultSingleJob: this._defaultSingleJob.serialize(),
defaultMultipleJob: this._defaultMultipleJob.serialize(), defaultMultipleJob: this._defaultMultipleJob.serialize(),
processors: this.pduProcessors.map(p => p.serialize()), preprocessors: this.processors.Preprocessor.map((p: PduProcessor) => p.serialize()),
availableProcessors: ProcessorManager.getProcessorsForType(this.constructor.name) postprocessors: this.processors.Postprocessor.map((p: PduProcessor) => p.serialize()),
availablePreprocessors: ProcessorManager.getPreprocessorsForType(this.constructor.name).map((p: PduProcessor) => p.serialize()),
availablePostprocessors: ProcessorManager.getPostprocessorsForType(this.constructor.name).map((p: PduProcessor) => p.serialize()),
}; };
} }
@@ -160,6 +165,7 @@ export default class Center extends SmppSession {
return session; return session;
} }
// TODO: Move this to smppSession and call postProcessors
private eventBindTransceiver(session: any, pdu: PDU) { private eventBindTransceiver(session: any, pdu: PDU) {
this.logger.log1(`Center-${this.id} got a bind_transciever with system_id ${pdu.system_id} and password ${pdu.password}`); this.logger.log1(`Center-${this.id} got a bind_transciever with system_id ${pdu.system_id} and password ${pdu.password}`);
session.pause(); session.pause();
@@ -218,7 +224,7 @@ export default class Center extends SmppSession {
} }
} }
// No reaason for this to be a promise // TODO: Move this to smppSession and call postProcessors
eventAnyPdu(session: any, pdu: any): Promise<any> { eventAnyPdu(session: any, pdu: any): Promise<any> {
this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu); this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu);
let successful: number = 0; let successful: number = 0;

View File

@@ -112,6 +112,7 @@ export default class Client extends SmppSession {
} }
serialize(): object { serialize(): object {
// TODO: Generify this further by moving it to smpp session and creating a... "postSerialize" that is abstract
return { return {
id: this._id, id: this._id,
url: this.url, url: this.url,
@@ -120,8 +121,10 @@ export default class Client extends SmppSession {
status: this._status, status: this._status,
defaultSingleJob: this._defaultSingleJob.serialize(), defaultSingleJob: this._defaultSingleJob.serialize(),
defaultMultipleJob: this._defaultMultipleJob.serialize(), defaultMultipleJob: this._defaultMultipleJob.serialize(),
processors: this.pduProcessors.map(p => p.serialize()), preprocessors: this.processors.Preprocessor.map((p: PduProcessor) => p.serialize()),
availableProcessors: ProcessorManager.getProcessorsForType(this.constructor.name) postprocessors: this.processors.Postprocessor.map((p: PduProcessor) => p.serialize()),
availablePreprocessors: ProcessorManager.getPreprocessorsForType(this.constructor.name).map((p: PduProcessor) => p.serialize()),
availablePostprocessors: ProcessorManager.getPostprocessorsForType(this.constructor.name).map((p: PduProcessor) => p.serialize()),
}; };
} }
@@ -130,14 +133,15 @@ export default class Client extends SmppSession {
return Promise.resolve(this.session.close()); return Promise.resolve(this.session.close());
} }
sendPdu(pdu: any, force?: boolean): Promise<object> { sendPdu(pdu: PDU, force?: boolean): Promise<object> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!force) { if (!force) {
this.validateSession(reject); this.validateSession(reject);
this.validateBound(reject); this.validateBound(reject);
} }
let pduCopy = new smpp.PDU(pdu.command, {...pdu}) // Is this expensive...?
this.pduProcessors.forEach((processor: PduProcessor) => processor.processPdu(this.session, pduCopy)); let pduCopy = new smpp.PDU(pdu.command, {...pdu});
this.processors.Preprocessor.forEach((processor: PduProcessor) => processor.processPdu(this.session, pduCopy));
this.logger.log5(`Client-${this.id} sending PDU: ${JSON.stringify(pduCopy)}`); this.logger.log5(`Client-${this.id} sending PDU: ${JSON.stringify(pduCopy)}`);
this.session.send(pduCopy, (replyPdu: object) => resolve(replyPdu)); this.session.send(pduCopy, (replyPdu: object) => resolve(replyPdu));
}); });
@@ -171,7 +175,7 @@ export default class Client extends SmppSession {
if (count > 0 && counter >= count) { if (count > 0 && counter >= count) {
this.cancelSendInterval(); this.cancelSendInterval();
} else { } else {
this.sendPdu(job.pdu, true) this.sendPdu(job.pdu, true)
.catch(e => this.logger.log1(`Error sending message: ${e}`)); .catch(e => this.logger.log1(`Error sending message: ${e}`));
counter++; counter++;
} }
@@ -180,6 +184,12 @@ export default class Client extends SmppSession {
}); });
} }
// TODO: Move this to smppSession and call postProcessors
eventAnyPdu(session: any, pdu: any): Promise<any> {
this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu);
return Promise.resolve();
}
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);
@@ -277,9 +287,4 @@ export default class Client extends SmppSession {
reject(errorMessage); reject(errorMessage);
} }
} }
eventAnyPdu(session: any, pdu: any): Promise<any> {
this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu);
return Promise.resolve();
}
} }

View File

@@ -87,4 +87,12 @@ export default class ProcessorManager {
static getProcessorsForType(type: string): PduProcessor[] { static getProcessorsForType(type: string): PduProcessor[] {
return this.processors.filter((processor: PduProcessor) => processor.sessionType === type); return this.processors.filter((processor: PduProcessor) => processor.sessionType === type);
} }
static getPreprocessorsForType(type: string): PduProcessor[] {
return this.preprocessors.filter((processor: PduProcessor) => processor.sessionType === type);
}
static getPostprocessorsForType(type: string): PduProcessor[] {
return this.postprocessors.filter((processor: PduProcessor) => processor.sessionType === type);
}
} }

View File

@@ -177,14 +177,6 @@ export default abstract class SmppSession {
detachPostprocessor(processor: PduProcessor): void { detachPostprocessor(processor: PduProcessor): void {
this.detachProcessor(processor, this.processors.Postprocessor); this.detachProcessor(processor, this.processors.Postprocessor);
} }
serializePduProcessors(): object {
let processors: PduProcessor[] = this.processors.Preprocessor.concat(this.processors.Postprocessor);
return processors.map((processor: PduProcessor) => {
return processor.serialize();
});
}
abstract eventAnyPdu(session: any, pdu: any): Promise<any>; abstract eventAnyPdu(session: any, pdu: any): Promise<any>;
private detachProcessor(processor: PduProcessor, array: PduProcessor[]): void { private detachProcessor(processor: PduProcessor, array: PduProcessor[]): void {