From 5a5debb2499d8ca13db9b1e98b18016f3dfc0933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Fri, 5 May 2023 10:17:55 +0200 Subject: [PATCH] Refactor processors to reduce duplicate code --- src/PDUProcessor/PduProcessor.ts | 18 +++++- .../Center/BindTranscieverReplyProcessor.ts | 58 +++++++++---------- .../Center/DeliveryReceiptProcessor.ts | 11 ++-- .../Postprocessor/Center/EchoPduProcessor.ts | 6 +- .../Center/EnquireLinkReplyProcessor.ts | 12 ++-- .../Center/SubmitSmReplyProcessor.ts | 18 +++--- .../Client/DeliverSmReplyProcessor.ts | 12 ++-- .../Client/DeliveryReceiptRequestProcessor.ts | 10 ++-- .../Client/DestinationEnumeratorProcessor.ts | 5 +- .../Preprocessor/Client/LongSmsProcessor.ts | 5 +- .../Client/SourceEnumeratorProcessor.ts | 5 +- 11 files changed, 86 insertions(+), 74 deletions(-) diff --git a/src/PDUProcessor/PduProcessor.ts b/src/PDUProcessor/PduProcessor.ts index b79c8ea..a685346 100644 --- a/src/PDUProcessor/PduProcessor.ts +++ b/src/PDUProcessor/PduProcessor.ts @@ -6,12 +6,26 @@ export default abstract class PduProcessor { readonly sessionType: string; readonly name: string = this.constructor.name; readonly logger: Logger = new Logger(`PduProcessor: ${this.name}`); + abstract applicableCommands: string[]; constructor(type: string) { this.sessionType = type; } - abstract processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise; + protected pduDoesApply(pdu: any): boolean { + if (pdu.command) { + return this.applicableCommands.includes(pdu.command); + } + return false; + } + + protected abstract doProcess(session: any, pdu: any, entity?: SmppSession | undefined): any; + + processPdu(session: any, pdu: any, entity?: SmppSession | undefined): any { + if (this.pduDoesApply(pdu)) { + return this.doProcess(session, pdu, entity); + } + } serialize(): object { return { @@ -20,4 +34,4 @@ export default abstract class PduProcessor { type: this.type }; } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Postprocessor/Center/BindTranscieverReplyProcessor.ts b/src/PDUProcessor/Postprocessor/Center/BindTranscieverReplyProcessor.ts index 6a5209b..043b9b8 100644 --- a/src/PDUProcessor/Postprocessor/Center/BindTranscieverReplyProcessor.ts +++ b/src/PDUProcessor/Postprocessor/Center/BindTranscieverReplyProcessor.ts @@ -4,42 +4,42 @@ import Postprocessor from "../Postprocessor"; const smpp = require("smpp"); export default class BindTranscieverReplyProcessor extends Postprocessor { + applicableCommands: string[] = ['bind_transceiver']; + constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: Center | undefined): Promise { + doProcess(session: any, pdu: any, entity?: Center | undefined): Promise { return new Promise((resolve, reject) => { - if (!!pdu.command && pdu.command === 'bind_transceiver') { - if (!entity) { - reject(); - } + if (!entity) { + reject(); + } - this.logger.log1(`Center-${entity?.id} got a bind_transciever with system_id ${pdu.system_id} and password ${pdu.password}`); - session.pause(); - if (pdu.system_id === entity?.username && pdu.password === entity?.password) { - this.logger.log1(`Center-${entity?.id} client connection successful`); - if (pdu.response) { - entity?.doSendPdu(pdu.response(), session); - } - session.resume(); - // @ts-ignore - entity?.pendingSessions = entity?.pendingSessions.filter((s) => s !== session); - entity?.sessions.push(session); - entity?.updateStatus(); - } else { - this.logger.log1(`Center-${entity?.id} client connection failed, invalid credentials (expected: ${entity?.username}, ${entity?.password})`); - if (pdu.response) { - entity?.doSendPdu(pdu.response({ - command_status: smpp.ESME_RBINDFAIL - }), session); - } - // @ts-ignore - entity?.pendingSessions = entity?.pendingSessions.filter((s) => s !== session); - entity?.updateStatus(); - session.close(); + this.logger.log1(`Center-${entity?.id} got a bind_transceiver with system_id ${pdu.system_id} and password ${pdu.password}`); + session.pause(); + if (pdu.system_id === entity?.username && pdu.password === entity?.password) { + this.logger.log1(`Center-${entity?.id} client connection successful`); + if (pdu.response) { + entity?.doSendPdu(pdu.response(), session); } + session.resume(); + // @ts-ignore + entity?.pendingSessions = entity?.pendingSessions.filter((s) => s !== session); + entity?.sessions.push(session); + entity?.updateStatus(); + } else { + this.logger.log1(`Center-${entity?.id} client connection failed, invalid credentials (expected: ${entity?.username}, ${entity?.password})`); + if (pdu.response) { + entity?.doSendPdu(pdu.response({ + command_status: smpp.ESME_RBINDFAIL + }), session); + } + // @ts-ignore + entity?.pendingSessions = entity?.pendingSessions.filter((s) => s !== session); + entity?.updateStatus(); + session.close(); } }); } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Postprocessor/Center/DeliveryReceiptProcessor.ts b/src/PDUProcessor/Postprocessor/Center/DeliveryReceiptProcessor.ts index fd16af8..0ef1794 100644 --- a/src/PDUProcessor/Postprocessor/Center/DeliveryReceiptProcessor.ts +++ b/src/PDUProcessor/Postprocessor/Center/DeliveryReceiptProcessor.ts @@ -5,13 +5,14 @@ import Postprocessor from "../Postprocessor"; const smpp = require("smpp"); export default class DeliveryReceiptProcessor extends Postprocessor { + applicableCommands: string[] = ['submit_sm']; constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { - if (!!pdu.command && pdu.command === "submit_sm" && pdu.registered_delivery) { + if (pdu.registered_delivery) { let drMessage: string = ""; let date: string = new Date().toISOString().replace(/T/, '').replace(/\..+/, '').replace(/-/g, '').replace(/:/g, '').substring(2, 12); @@ -39,8 +40,4 @@ export default class DeliveryReceiptProcessor extends Postprocessor { } }); } - - private padLeft(str: string, pad: string, length: number): string { - return (new Array(length + 1).join(pad) + str).slice(-length); - } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Postprocessor/Center/EchoPduProcessor.ts b/src/PDUProcessor/Postprocessor/Center/EchoPduProcessor.ts index 4dc71ee..2f78cbd 100644 --- a/src/PDUProcessor/Postprocessor/Center/EchoPduProcessor.ts +++ b/src/PDUProcessor/Postprocessor/Center/EchoPduProcessor.ts @@ -4,13 +4,15 @@ import Postprocessor from "../Postprocessor"; const smpp = require("smpp"); export default class EchoPduProcessor extends Postprocessor { + applicableCommands: string[] = ['submit_sm']; constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { - if (!!pdu.command && pdu.command === "submit_sm" && !pdu.short_message.udh) { + // Temporary (?) safeguard against echoing long sms + if (!pdu.short_message.udh) { let echoPdu = new smpp.PDU('deliver_sm', {...pdu}); echoPdu.source_addr = pdu.destination_addr; echoPdu.destination_addr = pdu.source_addr; diff --git a/src/PDUProcessor/Postprocessor/Center/EnquireLinkReplyProcessor.ts b/src/PDUProcessor/Postprocessor/Center/EnquireLinkReplyProcessor.ts index 10bf1b6..7766e98 100644 --- a/src/PDUProcessor/Postprocessor/Center/EnquireLinkReplyProcessor.ts +++ b/src/PDUProcessor/Postprocessor/Center/EnquireLinkReplyProcessor.ts @@ -2,16 +2,16 @@ import SmppSession from "../../../SmppSession"; import Postprocessor from "../Postprocessor"; export default class EnquireLinkReplyProcessor extends Postprocessor { + applicableCommands: string[] = ['enquire_link']; + constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { - if (!!pdu.command && pdu.command === 'enquire_link') { - entity?.doSendPdu(pdu.response(), session); - resolve(pdu); - } + entity?.doSendPdu(pdu.response(), session); + resolve(pdu); }); } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Postprocessor/Center/SubmitSmReplyProcessor.ts b/src/PDUProcessor/Postprocessor/Center/SubmitSmReplyProcessor.ts index d4b1359..dbbd298 100644 --- a/src/PDUProcessor/Postprocessor/Center/SubmitSmReplyProcessor.ts +++ b/src/PDUProcessor/Postprocessor/Center/SubmitSmReplyProcessor.ts @@ -3,22 +3,20 @@ import SmppSession from "../../../SmppSession"; import Postprocessor from "../Postprocessor"; export default class SubmitSmReplyProcessor extends Postprocessor { + applicableCommands: string[] = ['submit_sm']; private messageIdIterator: number = 0; constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { - if (!!pdu.command && pdu.command === 'submit_sm') { - // Add an ID here! - let response = pdu.response(); - response.message_id = this.messageIdIterator++; - MessageIdManager.addMessageId(pdu, response.message_id); - entity?.doSendPdu(response, session); - resolve(pdu); - } + let response = pdu.response(); + response.message_id = this.messageIdIterator++; + MessageIdManager.addMessageId(pdu, response.message_id); + entity?.doSendPdu(response, session); + resolve(pdu); }); } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Postprocessor/Client/DeliverSmReplyProcessor.ts b/src/PDUProcessor/Postprocessor/Client/DeliverSmReplyProcessor.ts index 2e7dbde..29a9682 100644 --- a/src/PDUProcessor/Postprocessor/Client/DeliverSmReplyProcessor.ts +++ b/src/PDUProcessor/Postprocessor/Client/DeliverSmReplyProcessor.ts @@ -2,16 +2,16 @@ import SmppSession from "../../../SmppSession"; import Postprocessor from "../Postprocessor"; export default class DeliverSmReplyProcessor extends Postprocessor { + applicableCommands: string[] = ['deliver_sm']; + constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { - if (!!pdu.command && pdu.command === 'deliver_sm') { - entity?.doSendPdu(pdu.response(), session); - resolve(pdu); - } + entity?.doSendPdu(pdu.response(), session); + resolve(pdu); }); } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Preprocessor/Client/DeliveryReceiptRequestProcessor.ts b/src/PDUProcessor/Preprocessor/Client/DeliveryReceiptRequestProcessor.ts index 77b61b4..0477926 100644 --- a/src/PDUProcessor/Preprocessor/Client/DeliveryReceiptRequestProcessor.ts +++ b/src/PDUProcessor/Preprocessor/Client/DeliveryReceiptRequestProcessor.ts @@ -2,17 +2,15 @@ import SmppSession from "../../../SmppSession"; import Preprocessor from "../Preprocessor"; export default class DeliveryReceiptRequestProcessor extends Preprocessor { - private iterator: number = 0; + applicableCommands: string[] = ['submit_sm']; constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { - if (!!pdu.command && pdu.command === "submit_sm") { - pdu.registered_delivery = 1; - } + pdu.registered_delivery = 1; }); } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Preprocessor/Client/DestinationEnumeratorProcessor.ts b/src/PDUProcessor/Preprocessor/Client/DestinationEnumeratorProcessor.ts index 867c7bb..5de4e2c 100644 --- a/src/PDUProcessor/Preprocessor/Client/DestinationEnumeratorProcessor.ts +++ b/src/PDUProcessor/Preprocessor/Client/DestinationEnumeratorProcessor.ts @@ -2,13 +2,14 @@ import SmppSession from "../../../SmppSession"; import Preprocessor from "../Preprocessor"; export default class DestinationEnumeratorProcessor extends Preprocessor { + applicableCommands: string[] = ['submit_sm', 'deliver_sm']; private iterator: number = 0; constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { if (!!pdu.destination_addr) { pdu.destination_addr = pdu.destination_addr + this.padLeft(String(this.iterator++), '0', 5); @@ -19,4 +20,4 @@ export default class DestinationEnumeratorProcessor extends Preprocessor { private padLeft(str: string, pad: string, length: number): string { return (new Array(length + 1).join(pad) + str).slice(-length); } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Preprocessor/Client/LongSmsProcessor.ts b/src/PDUProcessor/Preprocessor/Client/LongSmsProcessor.ts index 6886f5b..d6e937c 100644 --- a/src/PDUProcessor/Preprocessor/Client/LongSmsProcessor.ts +++ b/src/PDUProcessor/Preprocessor/Client/LongSmsProcessor.ts @@ -5,6 +5,7 @@ import Preprocessor from "../Preprocessor"; const smpp = require('smpp'); export default class LongSmsProcessor extends Preprocessor { + applicableCommands: string[] = ['submit_sm', 'deliver_sm']; static readonly maxMessageSizeBits = 1072; private iterator: number = 0; @@ -47,7 +48,7 @@ export default class LongSmsProcessor extends Preprocessor { return characterSizeBits; } - processPdu(session: any, pdu: PDU, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: PDU, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { if (!!pdu.short_message) { let characterSizeBits: number = LongSmsProcessor.getCharacterSizeForEncoding(pdu); @@ -83,4 +84,4 @@ export default class LongSmsProcessor extends Preprocessor { } }); } -} \ No newline at end of file +} diff --git a/src/PDUProcessor/Preprocessor/Client/SourceEnumeratorProcessor.ts b/src/PDUProcessor/Preprocessor/Client/SourceEnumeratorProcessor.ts index 50d3726..dd9f69a 100644 --- a/src/PDUProcessor/Preprocessor/Client/SourceEnumeratorProcessor.ts +++ b/src/PDUProcessor/Preprocessor/Client/SourceEnumeratorProcessor.ts @@ -2,13 +2,14 @@ import SmppSession from "../../../SmppSession"; import Preprocessor from "../Preprocessor"; export default class SourceEnumeratorProcessor extends Preprocessor { + applicableCommands: string[] = ['submit_sm', 'deliver_sm']; private iterator: number = 0; constructor(type: string) { super(type); } - processPdu(session: any, pdu: any, entity?: SmppSession | undefined): Promise { + protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise { return new Promise((resolve, reject) => { if (!!pdu.source_addr) { pdu.source_addr = pdu.source_addr + this.padLeft(String(this.iterator++), '0', 5); @@ -19,4 +20,4 @@ export default class SourceEnumeratorProcessor extends Preprocessor { private padLeft(str: string, pad: string, length: number): string { return (new Array(length + 1).join(pad) + str).slice(-length); } -} \ No newline at end of file +}