Refactor processors to reduce duplicate code

This commit is contained in:
2023-05-05 10:17:55 +02:00
parent 7d574e3162
commit 5a5debb249
11 changed files with 86 additions and 74 deletions

View File

@@ -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<any>;
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 {

View File

@@ -4,18 +4,19 @@ 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<any> {
doProcess(session: any, pdu: any, entity?: Center | undefined): Promise<any> {
return new Promise((resolve, reject) => {
if (!!pdu.command && pdu.command === 'bind_transceiver') {
if (!entity) {
reject();
}
this.logger.log1(`Center-${entity?.id} got a bind_transciever with system_id ${pdu.system_id} and password ${pdu.password}`);
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`);
@@ -39,7 +40,6 @@ export default class BindTranscieverReplyProcessor extends Postprocessor {
entity?.updateStatus();
session.close();
}
}
});
}
}

View File

@@ -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<any> {
protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise<any> {
return new Promise<any>((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);
}
}

View File

@@ -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<any> {
protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise<any> {
return new Promise<any>((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;

View File

@@ -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<any> {
protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise<any> {
return new Promise((resolve, reject) => {
if (!!pdu.command && pdu.command === 'enquire_link') {
entity?.doSendPdu(pdu.response(), session);
resolve(pdu);
}
});
}
}

View File

@@ -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<any> {
protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise<any> {
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);
}
});
}
}

View File

@@ -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<any> {
protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise<any> {
return new Promise((resolve, reject) => {
if (!!pdu.command && pdu.command === 'deliver_sm') {
entity?.doSendPdu(pdu.response(), session);
resolve(pdu);
}
});
}
}

View File

@@ -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<any> {
protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise<any> {
return new Promise<any>((resolve, reject) => {
if (!!pdu.command && pdu.command === "submit_sm") {
pdu.registered_delivery = 1;
}
});
}
}

View File

@@ -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<any> {
protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise<any> {
return new Promise<any>((resolve, reject) => {
if (!!pdu.destination_addr) {
pdu.destination_addr = pdu.destination_addr + this.padLeft(String(this.iterator++), '0', 5);

View File

@@ -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<any> {
protected doProcess(session: any, pdu: PDU, entity?: SmppSession | undefined): Promise<any> {
return new Promise<any>((resolve, reject) => {
if (!!pdu.short_message) {
let characterSizeBits: number = LongSmsProcessor.getCharacterSizeForEncoding(pdu);

View File

@@ -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<any> {
protected doProcess(session: any, pdu: any, entity?: SmppSession | undefined): Promise<any> {
return new Promise<any>((resolve, reject) => {
if (!!pdu.source_addr) {
pdu.source_addr = pdu.source_addr + this.padLeft(String(this.iterator++), '0', 5);