Implement pduprocessors
This commit is contained in:
@@ -5,8 +5,9 @@ import {JobEvents} from "../Job/JobEvents";
|
||||
import Logger from "../Logger";
|
||||
import {SmppSession} from "../SmppSession";
|
||||
import {CenterEvents} from "./CenterEvents";
|
||||
import {CenterPDUProcessor} from "./CenterPDUProcessor";
|
||||
import CenterStatus from "./CenterStatus";
|
||||
import {CenterPDUProcessor} from "./PDUProcessors/CenterPDUProcessor";
|
||||
import {DebugProcessor} from "./PDUProcessors/DebugProcessor";
|
||||
|
||||
const NanoTimer = require('nanotimer');
|
||||
const smpp = require("smpp");
|
||||
@@ -45,8 +46,12 @@ export class Center implements SmppSession {
|
||||
return this._port;
|
||||
}
|
||||
|
||||
// TODO: Implement a few modes and set this to default DEBUG
|
||||
private _processor: CenterPDUProcessor | undefined;
|
||||
// TODO: Implement processor switching
|
||||
private _processor: CenterPDUProcessor = new DebugProcessor();
|
||||
|
||||
get processor(): CenterPDUProcessor {
|
||||
return this._processor;
|
||||
}
|
||||
|
||||
set processor(value: CenterPDUProcessor) {
|
||||
this._processor = value;
|
||||
@@ -116,9 +121,6 @@ export class Center implements SmppSession {
|
||||
this._defaultMultipleJob.on(JobEvents.STATE_CHANGED, () => this.eventEmitter.emit(ClientEvents.STATE_CHANGED, this.serialize()));
|
||||
|
||||
this.server = smpp.createServer({}, this.eventSessionConnected.bind(this));
|
||||
this.server.on('error', this.eventSessionError.bind(this));
|
||||
this.server.on('close', this.eventSessionClose.bind(this));
|
||||
this.server.on('pdu', this.eventAnyPdu.bind(this));
|
||||
this.server.listen(this._port);
|
||||
this.status = CenterStatus.WAITING_CONNECTION;
|
||||
}
|
||||
@@ -300,7 +302,10 @@ export class Center implements SmppSession {
|
||||
}
|
||||
}
|
||||
|
||||
private eventAnyPdu(pdu: any): void {
|
||||
private eventAnyPdu(session: any, pdu: any): void {
|
||||
this.eventEmitter.emit(CenterEvents.ANY_PDU, pdu);
|
||||
this.processor.processPdu(session, pdu).then(() => {
|
||||
}, () => {
|
||||
});
|
||||
}
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
export interface CenterPDUProcessor {
|
||||
|
||||
}
|
3
src/Center/PDUProcessors/CenterPDUProcessor.ts
Normal file
3
src/Center/PDUProcessors/CenterPDUProcessor.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface CenterPDUProcessor {
|
||||
processPdu(session: any, pdu: any): Promise<any>;
|
||||
}
|
16
src/Center/PDUProcessors/DebugProcessor.ts
Normal file
16
src/Center/PDUProcessors/DebugProcessor.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import Logger from "../../Logger";
|
||||
import {CenterPDUProcessor} from "./CenterPDUProcessor";
|
||||
|
||||
export class DebugProcessor implements CenterPDUProcessor {
|
||||
private logger: Logger;
|
||||
|
||||
constructor() {
|
||||
this.logger = new Logger('DebugProcessor');
|
||||
}
|
||||
|
||||
processPdu(session: any, pdu: any): Promise<any> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
session.send(pdu.response()).then((replyPdu: any) => resolve(replyPdu), (error: any) => reject(error));
|
||||
});
|
||||
}
|
||||
}
|
30
src/Center/PDUProcessors/EchoProcessor.ts
Normal file
30
src/Center/PDUProcessors/EchoProcessor.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import Logger from "../../Logger";
|
||||
import {CenterPDUProcessor} from "./CenterPDUProcessor";
|
||||
|
||||
const smpp = require("smpp");
|
||||
|
||||
export class DebugProcessor implements CenterPDUProcessor {
|
||||
private logger: Logger;
|
||||
|
||||
constructor() {
|
||||
this.logger = new Logger('DebugProcessor');
|
||||
}
|
||||
processPdu(session: any, pdu: any): Promise<any> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
let promises = [];
|
||||
let replyPromise = session.send(pdu.response());
|
||||
let sendPromise = session.send(new smpp.PDU('enquire_link', {
|
||||
source_addr: pdu.destination_addr,
|
||||
destination_addr: pdu.source_addr,
|
||||
short_message: pdu.short_message
|
||||
}));
|
||||
promises.push(replyPromise);
|
||||
promises.push(sendPromise);
|
||||
Promise.all(promises).then((replyPdus: any) => {
|
||||
resolve(replyPdus);
|
||||
}).catch((error: any) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user