diff --git a/src/Center/Center.ts b/src/Center/Center.ts index e9187b5..b36bca3 100644 --- a/src/Center/Center.ts +++ b/src/Center/Center.ts @@ -69,6 +69,21 @@ export default class Center extends SmppSession { super.defaultMultipleJob = job; } + sendPdu(pdu: PDU, force?: boolean): Promise { + 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 { return new Promise((resolve, reject) => { this.validateSessions(reject); @@ -102,18 +117,6 @@ export default class Center extends SmppSession { }); } - sendPdu(pdu: object, force?: boolean): Promise { - 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 { this.server = smpp.createServer({}, this.eventSessionConnected.bind(this)); this.server.listen(this.port); @@ -140,8 +143,10 @@ export default class Center extends SmppSession { status: this._status, defaultSingleJob: this._defaultSingleJob.serialize(), defaultMultipleJob: this._defaultMultipleJob.serialize(), - processors: this.pduProcessors.map(p => p.serialize()), - availableProcessors: ProcessorManager.getProcessorsForType(this.constructor.name) + preprocessors: this.processors.Preprocessor.map((p: PduProcessor) => p.serialize()), + 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; } + // TODO: Move this to smppSession and call postProcessors 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}`); 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 { this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu); let successful: number = 0; diff --git a/src/Client/Client.ts b/src/Client/Client.ts index 861f7d3..76c2b4b 100644 --- a/src/Client/Client.ts +++ b/src/Client/Client.ts @@ -112,6 +112,7 @@ export default class Client extends SmppSession { } serialize(): object { + // TODO: Generify this further by moving it to smpp session and creating a... "postSerialize" that is abstract return { id: this._id, url: this.url, @@ -120,8 +121,10 @@ export default class Client extends SmppSession { status: this._status, defaultSingleJob: this._defaultSingleJob.serialize(), defaultMultipleJob: this._defaultMultipleJob.serialize(), - processors: this.pduProcessors.map(p => p.serialize()), - availableProcessors: ProcessorManager.getProcessorsForType(this.constructor.name) + preprocessors: this.processors.Preprocessor.map((p: PduProcessor) => p.serialize()), + 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()); } - sendPdu(pdu: any, force?: boolean): Promise { + sendPdu(pdu: PDU, force?: boolean): Promise { return new Promise((resolve, reject) => { if (!force) { this.validateSession(reject); this.validateBound(reject); } - let pduCopy = new smpp.PDU(pdu.command, {...pdu}) - this.pduProcessors.forEach((processor: PduProcessor) => processor.processPdu(this.session, pduCopy)); + // Is this expensive...? + 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.session.send(pduCopy, (replyPdu: object) => resolve(replyPdu)); }); @@ -171,7 +175,7 @@ export default class Client extends SmppSession { if (count > 0 && counter >= count) { this.cancelSendInterval(); } else { - this.sendPdu(job.pdu, true) + this.sendPdu(job.pdu, true) .catch(e => this.logger.log1(`Error sending message: ${e}`)); 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 { + this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu); + return Promise.resolve(); + } + private connectSession(): Promise { return new Promise((resolve, reject) => { this.validateFields(reject); @@ -277,9 +287,4 @@ export default class Client extends SmppSession { reject(errorMessage); } } - - eventAnyPdu(session: any, pdu: any): Promise { - this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu); - return Promise.resolve(); - } } \ No newline at end of file diff --git a/src/PDUProcessor/ProcessorManager.ts b/src/PDUProcessor/ProcessorManager.ts index 34b92d7..a3e8d19 100644 --- a/src/PDUProcessor/ProcessorManager.ts +++ b/src/PDUProcessor/ProcessorManager.ts @@ -87,4 +87,12 @@ export default class ProcessorManager { static getProcessorsForType(type: string): PduProcessor[] { 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); + } } \ No newline at end of file diff --git a/src/SmppSession.ts b/src/SmppSession.ts index d389a88..5a757bb 100644 --- a/src/SmppSession.ts +++ b/src/SmppSession.ts @@ -177,14 +177,6 @@ export default abstract class SmppSession { detachPostprocessor(processor: PduProcessor): void { 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; private detachProcessor(processor: PduProcessor, array: PduProcessor[]): void {