Refactor processors to reduce duplicate code
This commit is contained in:
@@ -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 {
|
||||
|
@@ -4,41 +4,41 @@ 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();
|
||||
}
|
||||
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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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;
|
||||
|
@@ -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);
|
||||
}
|
||||
entity?.doSendPdu(pdu.response(), session);
|
||||
resolve(pdu);
|
||||
});
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
let response = pdu.response();
|
||||
response.message_id = this.messageIdIterator++;
|
||||
MessageIdManager.addMessageId(pdu, response.message_id);
|
||||
entity?.doSendPdu(response, session);
|
||||
resolve(pdu);
|
||||
});
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
entity?.doSendPdu(pdu.response(), session);
|
||||
resolve(pdu);
|
||||
});
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
pdu.registered_delivery = 1;
|
||||
});
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
@@ -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);
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user