Implement source and desination enumerators for client
This commit is contained in:
@@ -35,8 +35,8 @@ export class Center extends SmppSession {
|
|||||||
this._password = password;
|
this._password = password;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
|
||||||
this._defaultSingleJob = Job.createEmptySingle();
|
this._defaultSingleJob = Job.createEmptySingle('deliver_sm');
|
||||||
this._defaultMultipleJob = Job.createEmptyMultiple();
|
this._defaultMultipleJob = Job.createEmptyMultiple('deliver_sm');
|
||||||
|
|
||||||
this.logger = new Logger(`Center-${id}`);
|
this.logger = new Logger(`Center-${id}`);
|
||||||
|
|
||||||
@@ -218,4 +218,21 @@ export class Center extends SmppSession {
|
|||||||
this.setStatus(0);
|
this.setStatus(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No reaason for this to be a promise
|
||||||
|
eventAnyPdu(session: any, pdu: any): Promise<any> {
|
||||||
|
this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu);
|
||||||
|
let successful: number = 0;
|
||||||
|
this.pduProcessors.forEach((pduProcessor: PduProcessor) => {
|
||||||
|
pduProcessor.processPdu(session, pdu).then((result: any) => {
|
||||||
|
successful++;
|
||||||
|
}, (error: any) => {
|
||||||
|
});
|
||||||
|
});
|
||||||
|
if (successful === 0) {
|
||||||
|
return Promise.resolve("No PDU processor was able to process the PDU");
|
||||||
|
} else {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -39,8 +39,8 @@ export class Client extends SmppSession {
|
|||||||
this._password = password;
|
this._password = password;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
|
||||||
this._defaultSingleJob = Job.createEmptySingle();
|
this._defaultSingleJob = Job.createEmptySingle('submit_sm');
|
||||||
this._defaultMultipleJob = Job.createEmptyMultiple();
|
this._defaultMultipleJob = Job.createEmptyMultiple('submit_sm');
|
||||||
|
|
||||||
this.logger = new Logger(`Client-${id}`);
|
this.logger = new Logger(`Client-${id}`);
|
||||||
}
|
}
|
||||||
@@ -130,14 +130,16 @@ export class Client extends SmppSession {
|
|||||||
return Promise.resolve(this.session.close());
|
return Promise.resolve(this.session.close());
|
||||||
}
|
}
|
||||||
|
|
||||||
sendPdu(pdu: object, force?: boolean): Promise<object> {
|
sendPdu(pdu: any, 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);
|
||||||
}
|
}
|
||||||
this.logger.log5(`Client-${this.id} sending PDU: ${JSON.stringify(pdu)}`);
|
let pduCopy = new smpp.PDU(pdu.command, {...pdu})
|
||||||
this.session.send(pdu, (replyPdu: object) => resolve(replyPdu));
|
this.pduProcessors.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));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +171,7 @@ export 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++;
|
||||||
}
|
}
|
||||||
@@ -275,4 +277,9 @@ export 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();
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,6 +1,8 @@
|
|||||||
|
import {Center} from "../Center/Center";
|
||||||
import {Client} from "../Client/Client";
|
import {Client} from "../Client/Client";
|
||||||
import ClientSessionManager from "../Client/ClientSessionManager";
|
import ClientSessionManager from "../Client/ClientSessionManager";
|
||||||
import Logger from "../Logger";
|
import Logger from "../Logger";
|
||||||
|
import {PduProcessor} from "../PDUProcessor/PduProcessor";
|
||||||
import {SessionManager} from "../SessionManager";
|
import {SessionManager} from "../SessionManager";
|
||||||
import {SmppSession} from "../SmppSession";
|
import {SmppSession} from "../SmppSession";
|
||||||
import {RequestHandler} from "./RequestHandler";
|
import {RequestHandler} from "./RequestHandler";
|
||||||
@@ -15,19 +17,32 @@ export default class ClientRequestHandler extends RequestHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
doGetAvailableProcessors(req: any, res: any): void {
|
doGetAvailableProcessors(req: any, res: any): void {
|
||||||
res.send([]);
|
this.logger.log1("Getting available processors");
|
||||||
|
let processors: PduProcessor[] = PduProcessor.getProcessorsForType(Client.name);
|
||||||
|
res.send(processors.map((processor: any) => processor.serialize()));
|
||||||
}
|
}
|
||||||
|
|
||||||
doGetAppliedProcessors(req: any, res: any): void {
|
doGetAppliedProcessors(req: any, res: any): void {
|
||||||
res.send([]);
|
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
|
||||||
|
let processors: PduProcessor[] = session.pduProcessors;
|
||||||
|
res.send(processors.map((processor: any) => processor.serialize()));
|
||||||
|
}, this.handleSessionNotFound.bind(this, req, res));
|
||||||
}
|
}
|
||||||
|
|
||||||
doAddProcessor(req: any, res: any): void {
|
doAddProcessor(req: any, res: any): void {
|
||||||
res.send([]);
|
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
|
||||||
|
let processor: PduProcessor = PduProcessor.getProcessor(req.body.name);
|
||||||
|
PduProcessor.attachProcessor(session, processor);
|
||||||
|
res.send(session.serialize());
|
||||||
|
}, this.handleSessionNotFound.bind(this, req, res));
|
||||||
}
|
}
|
||||||
|
|
||||||
doRemoveProcessor(req: any, res: any): void {
|
doRemoveProcessor(req: any, res: any): void {
|
||||||
res.send([]);
|
this.sessionManager.getSession(req.params.id).then((session: SmppSession) => {
|
||||||
|
let processor: PduProcessor = PduProcessor.getProcessor(req.body.name);
|
||||||
|
PduProcessor.detachProcessor(session, processor);
|
||||||
|
res.send(session.serialize());
|
||||||
|
}, this.handleSessionNotFound.bind(this, req, res));
|
||||||
}
|
}
|
||||||
|
|
||||||
doPost(req: any, res: any): void {
|
doPost(req: any, res: any): void {
|
||||||
|
@@ -9,15 +9,21 @@ export class Job {
|
|||||||
private eventEmitter: EventEmitter = new EventEmitter();
|
private eventEmitter: EventEmitter = new EventEmitter();
|
||||||
|
|
||||||
constructor(pdu: PDU, perSecond?: number, count?: number) {
|
constructor(pdu: PDU, perSecond?: number, count?: number) {
|
||||||
if (pdu.short_message && pdu.short_message.type === "Buffer") {
|
Job.pduParseShortMessage(pdu);
|
||||||
pdu.short_message = Buffer.from(pdu.short_message.data, 'ascii').toString();
|
|
||||||
}
|
|
||||||
pdu.short_message = 'test123';
|
|
||||||
this._pdu = pdu;
|
this._pdu = pdu;
|
||||||
this._perSecond = perSecond;
|
this._perSecond = perSecond;
|
||||||
this._count = count;
|
this._count = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static pduParseShortMessage(pdu: PDU) {
|
||||||
|
if (pdu.short_message && pdu.short_message.type === "Buffer") {
|
||||||
|
pdu.short_message = Buffer.from(pdu.short_message.data, 'ascii').toString();
|
||||||
|
}
|
||||||
|
if (typeof pdu.short_message === "object") {
|
||||||
|
pdu.short_message = pdu.short_message.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private _pdu: PDU;
|
private _pdu: PDU;
|
||||||
|
|
||||||
get pdu(): PDU {
|
get pdu(): PDU {
|
||||||
@@ -51,22 +57,19 @@ export class Job {
|
|||||||
this.eventEmitter.emit(Job.STATE_CHANGED, {});
|
this.eventEmitter.emit(Job.STATE_CHANGED, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
static createEmptySingle(): Job {
|
static createEmptySingle(command: string): Job {
|
||||||
return new Job({});
|
let pdu1 = new smpp.PDU(command, {});
|
||||||
|
Job.pduParseShortMessage(pdu1);
|
||||||
|
return new Job(pdu1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static createEmptyMultiple(): Job {
|
static createEmptyMultiple(command: string): Job {
|
||||||
return new Job({}, 1, 1);
|
let pdu1 = new smpp.PDU(command, {});
|
||||||
|
Job.pduParseShortMessage(pdu1);
|
||||||
|
return new Job(pdu1, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static deserialize(serialized: SerializedJob): Job {
|
static deserialize(serialized: SerializedJob): Job {
|
||||||
if (!serialized.pdu || !serialized.pdu.command) {
|
|
||||||
if (!serialized.perSecond && !serialized.count) {
|
|
||||||
return Job.createEmptySingle();
|
|
||||||
} else {
|
|
||||||
return Job.createEmptyMultiple();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let pdu: PDU = new smpp.PDU(serialized.pdu.command, serialized.pdu);
|
let pdu: PDU = new smpp.PDU(serialized.pdu.command, serialized.pdu);
|
||||||
return new Job(pdu, serialized.perSecond, serialized.count);
|
return new Job(pdu, serialized.perSecond, serialized.count);
|
||||||
}
|
}
|
||||||
|
20
src/PDUProcessor/Client/DestinationEnumeratorProcessor.ts
Normal file
20
src/PDUProcessor/Client/DestinationEnumeratorProcessor.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import {Client} from "../../Client/Client";
|
||||||
|
import {PDU} from "../../CommonObjects";
|
||||||
|
import {PduProcessor} from "../PduProcessor";
|
||||||
|
|
||||||
|
export class DestinationEnumeratorProcessor extends PduProcessor {
|
||||||
|
serverSessionType: string = Client.name;
|
||||||
|
private iterator = 0;
|
||||||
|
|
||||||
|
processPdu(session: any, pdu: PDU, ...args: any[]): 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private padLeft(str: string, pad: string, length: number): string {
|
||||||
|
return (new Array(length + 1).join(pad) + str).slice(-length);
|
||||||
|
}
|
||||||
|
}
|
20
src/PDUProcessor/Client/SourceEnumeratorProcessor.ts
Normal file
20
src/PDUProcessor/Client/SourceEnumeratorProcessor.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import {Client} from "../../Client/Client";
|
||||||
|
import {PDU} from "../../CommonObjects";
|
||||||
|
import {PduProcessor} from "../PduProcessor";
|
||||||
|
|
||||||
|
export class SourceEnumeratorProcessor extends PduProcessor {
|
||||||
|
serverSessionType: string = Client.name;
|
||||||
|
private iterator = 0;
|
||||||
|
|
||||||
|
processPdu(session: any, pdu: PDU, ...args: any[]): 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private padLeft(str: string, pad: string, length: number): string {
|
||||||
|
return (new Array(length + 1).join(pad) + str).slice(-length);
|
||||||
|
}
|
||||||
|
}
|
@@ -12,6 +12,6 @@ export class DebugPduProcessor extends PduProcessor {
|
|||||||
resolve(replyPdu);
|
resolve(replyPdu);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -183,19 +183,5 @@ export abstract class SmppSession {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
eventAnyPdu(session: any, pdu: any): Promise<any> {
|
abstract eventAnyPdu(session: any, pdu: any): Promise<any>;
|
||||||
this.eventEmitter.emit(this.EVENT.ANY_PDU, pdu);
|
|
||||||
let successful: number = 0;
|
|
||||||
this.pduProcessors.forEach((pduProcessor: PduProcessor) => {
|
|
||||||
pduProcessor.processPdu(session, pdu).then((result: any) => {
|
|
||||||
successful++;
|
|
||||||
}, (error: any) => {
|
|
||||||
});
|
|
||||||
});
|
|
||||||
if (successful === 0) {
|
|
||||||
return Promise.resolve("No PDU processor was able to process the PDU");
|
|
||||||
} else {
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
12
src/main.ts
12
src/main.ts
@@ -4,6 +4,8 @@ import {Client} from "./Client/Client";
|
|||||||
import ClientSessionManager from "./Client/ClientSessionManager";
|
import ClientSessionManager from "./Client/ClientSessionManager";
|
||||||
import {HttpServer} from "./HttpServer/HttpServer";
|
import {HttpServer} from "./HttpServer/HttpServer";
|
||||||
import Logger from "./Logger";
|
import Logger from "./Logger";
|
||||||
|
import {DestinationEnumeratorProcessor} from "./PDUProcessor/Client/DestinationEnumeratorProcessor";
|
||||||
|
import {SourceEnumeratorProcessor} from "./PDUProcessor/Client/SourceEnumeratorProcessor";
|
||||||
import {DebugPduProcessor} from "./PDUProcessor/DebugPduProcessor";
|
import {DebugPduProcessor} from "./PDUProcessor/DebugPduProcessor";
|
||||||
import {EchoPduProcessor} from "./PDUProcessor/EchoPduProcessor";
|
import {EchoPduProcessor} from "./PDUProcessor/EchoPduProcessor";
|
||||||
import {PduProcessor} from "./PDUProcessor/PduProcessor";
|
import {PduProcessor} from "./PDUProcessor/PduProcessor";
|
||||||
@@ -15,6 +17,8 @@ let logger = new Logger("main");
|
|||||||
|
|
||||||
PduProcessor.addProcessor(DebugPduProcessor);
|
PduProcessor.addProcessor(DebugPduProcessor);
|
||||||
PduProcessor.addProcessor(EchoPduProcessor);
|
PduProcessor.addProcessor(EchoPduProcessor);
|
||||||
|
PduProcessor.addProcessor(DestinationEnumeratorProcessor);
|
||||||
|
PduProcessor.addProcessor(SourceEnumeratorProcessor);
|
||||||
|
|
||||||
let clientManager: ClientSessionManager = new ClientSessionManager();
|
let clientManager: ClientSessionManager = new ClientSessionManager();
|
||||||
let centerManager: CenterSessionManager = new CenterSessionManager();
|
let centerManager: CenterSessionManager = new CenterSessionManager();
|
||||||
@@ -47,7 +51,7 @@ async function main() {
|
|||||||
|
|
||||||
// main();
|
// main();
|
||||||
|
|
||||||
// process.on('exit', cleanup);
|
process.on('exit', cleanup);
|
||||||
// process.on('SIGINT', cleanup);
|
process.on('SIGINT', cleanup);
|
||||||
// process.on('SIGUSR1', cleanup);
|
process.on('SIGUSR1', cleanup);
|
||||||
// process.on('SIGUSR2', cleanup);
|
process.on('SIGUSR2', cleanup);
|
Reference in New Issue
Block a user