Generify everything a little
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
<option name="extraTypeScriptOptions" value="" />
|
||||
<option name="scriptName" value="$PROJECT_DIR$/src/main.ts" />
|
||||
<option name="programParameters" value="" />
|
||||
<option name="tsnodePackage" value="C:\Program Files\nodejs\node_modules\ts-node" />
|
||||
<option name="tsnodePackage" value="~\WebstormProjects\smsgwtester\node_modules\ts-node" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
@@ -1,3 +1,4 @@
|
||||
import {PDU} from "../CommonObjects";
|
||||
import {Job} from "../Job/Job";
|
||||
import Logger from "../Logger";
|
||||
import {DebugPduProcessor} from "../PDUProcessor/DebugPduProcessor";
|
||||
@@ -136,21 +137,25 @@ export class Center extends SmppSession {
|
||||
return session;
|
||||
}
|
||||
|
||||
private eventBindTransceiver(session: any, pdu: any) {
|
||||
private eventBindTransceiver(session: any, pdu: PDU) {
|
||||
this.logger.log1(`Center-${this.getId()} got a bind_transciever with system_id ${pdu.system_id} and password ${pdu.password}`);
|
||||
session.pause();
|
||||
if (pdu.system_id === this.username && pdu.password === this.password) {
|
||||
this.logger.log1(`Center-${this.getId()} client connection successful`);
|
||||
if (pdu.response) {
|
||||
session.send(pdu.response());
|
||||
}
|
||||
session.resume();
|
||||
this.pendingSessions = this.pendingSessions.filter((s) => s !== session);
|
||||
this.sessions.push(session);
|
||||
this.updateStatus();
|
||||
} else {
|
||||
this.logger.log1(`Center-${this.getId()} client connection failed, invalid credentials (expected: ${this.username}, ${this.password})`);
|
||||
if (pdu.response) {
|
||||
session.send(pdu.response({
|
||||
command_status: smpp.ESME_RBINDFAIL
|
||||
}));
|
||||
}
|
||||
this.pendingSessions = this.pendingSessions.filter((s) => s !== session);
|
||||
this.updateStatus();
|
||||
session.close();
|
||||
|
@@ -7,7 +7,7 @@ const CENTER_SESSIONS_FILE: string = process.env.CENTER_SESSIONS_FILE || "center
|
||||
|
||||
export class CenterSessionManager extends SessionManager {
|
||||
StorageFile: string = CENTER_SESSIONS_FILE
|
||||
ManagedSessionClass: any = Center;
|
||||
ManagedSessionClass: typeof Center = Center;
|
||||
sessionId: number = 0;
|
||||
sessions: Center[] = [];
|
||||
identifier: string = "center";
|
||||
|
@@ -1,3 +1,4 @@
|
||||
import {PDU} from "../CommonObjects";
|
||||
import {Job} from "../Job/Job";
|
||||
import Logger from "../Logger";
|
||||
import {PduProcessor} from "../PDUProcessor/PduProcessor";
|
||||
@@ -180,7 +181,7 @@ export class Client extends SmppSession {
|
||||
}
|
||||
}
|
||||
|
||||
private eventSessionError(pdu: any): void {
|
||||
private eventSessionError(pdu: PDU): void {
|
||||
this.logger.log1(`Client-${this.getId()} error on ${this.url}`);
|
||||
this.setStatus(0);
|
||||
this.rejectPromises();
|
||||
@@ -192,7 +193,7 @@ export class Client extends SmppSession {
|
||||
this.rejectPromises();
|
||||
}
|
||||
|
||||
private eventBindReply(pdu: any): void {
|
||||
private eventBindReply(pdu: PDU): void {
|
||||
if (pdu.command_status === 0) {
|
||||
this.logger.log1(`Client-${this.getId()} bound to ${this.url}`);
|
||||
this.setStatus(4);
|
||||
|
@@ -7,7 +7,7 @@ const CLIENT_SESSIONS_FILE: string = process.env.CLIENT_SESSIONS_FILE || "client
|
||||
|
||||
export default class ClientSessionManager extends SessionManager {
|
||||
StorageFile: string = CLIENT_SESSIONS_FILE;
|
||||
ManagedSessionClass: any = Client;
|
||||
ManagedSessionClass: typeof Client = Client;
|
||||
sessionId: number = 0;
|
||||
sessions: Client[] = [];
|
||||
// Identifier is used in websockets to identify the type of session this manager manages
|
||||
|
15
src/CommonObjects.ts
Normal file
15
src/CommonObjects.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export type PDU = {
|
||||
command?: string;
|
||||
command_status?: number;
|
||||
system_id?: string;
|
||||
password?: string;
|
||||
source_addr?: string;
|
||||
destination_addr?: string;
|
||||
short_message?: string;
|
||||
response?: (...args: any[]) => PDU;
|
||||
}
|
||||
export type SerializedJob = {
|
||||
pdu: PDU;
|
||||
perSecond?: number;
|
||||
count?: number;
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
import EventEmitter from "events";
|
||||
import {PDU, SerializedJob} from "../CommonObjects";
|
||||
|
||||
const smpp = require("smpp");
|
||||
|
||||
@@ -6,19 +7,19 @@ export class Job {
|
||||
static readonly STATE_CHANGED: string = "STATE_CHANGED";
|
||||
private eventEmitter: EventEmitter = new EventEmitter();
|
||||
|
||||
constructor(pdu: any, perSecond?: number, count?: number) {
|
||||
constructor(pdu: PDU, perSecond?: number, count?: number) {
|
||||
this._pdu = pdu;
|
||||
this._perSecond = perSecond;
|
||||
this._count = count;
|
||||
}
|
||||
|
||||
private _pdu: any;
|
||||
private _pdu: PDU;
|
||||
|
||||
get pdu(): any {
|
||||
get pdu(): PDU {
|
||||
return this._pdu;
|
||||
}
|
||||
|
||||
set pdu(value: any) {
|
||||
set pdu(value: PDU) {
|
||||
this._pdu = value;
|
||||
this.eventEmitter.emit(Job.STATE_CHANGED, {});
|
||||
}
|
||||
@@ -45,7 +46,7 @@ export class Job {
|
||||
this.eventEmitter.emit(Job.STATE_CHANGED, {});
|
||||
}
|
||||
|
||||
static deserialize(serialized: any): Job {
|
||||
static deserialize(serialized: SerializedJob): Job {
|
||||
if (!serialized.pdu || !serialized.pdu.command) {
|
||||
if (!serialized.perSecond && !serialized.count) {
|
||||
return Job.createEmptySingle();
|
||||
@@ -53,7 +54,7 @@ export class Job {
|
||||
return Job.createEmptyMultiple();
|
||||
}
|
||||
}
|
||||
let pdu: any = 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);
|
||||
}
|
||||
|
||||
@@ -83,7 +84,7 @@ export class Job {
|
||||
}
|
||||
}
|
||||
|
||||
serialize(): object {
|
||||
serialize(): SerializedJob {
|
||||
return {
|
||||
pdu: this.pdu,
|
||||
perSecond: this.perSecond,
|
||||
|
@@ -1,14 +1,17 @@
|
||||
import {Center} from "../Center/Center";
|
||||
import {PDU} from "../CommonObjects";
|
||||
import {PduProcessor} from "./PduProcessor";
|
||||
|
||||
export class DebugPduProcessor extends PduProcessor {
|
||||
servesSessionType: string = Center.name;
|
||||
serverSessionType: string = Center.name;
|
||||
|
||||
processPdu(session: any, pdu: any, ...args: any[]): Promise<any> {
|
||||
processPdu(session: any, pdu: PDU, ...args: any[]): Promise<any> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
if (pdu.response) {
|
||||
session.send(pdu.response(), (replyPdu: any) => {
|
||||
resolve(replyPdu);
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,12 +1,16 @@
|
||||
import {Center} from "../Center/Center";
|
||||
import {PDU} from "../CommonObjects";
|
||||
import {PduProcessor} from "./PduProcessor";
|
||||
|
||||
const smpp = require("smpp");
|
||||
|
||||
export class EchoPduProcessor extends PduProcessor {
|
||||
servesSessionType: string = Center.name;
|
||||
processPdu(session: any, pdu: any, ...args: any[]): Promise<any> {
|
||||
serverSessionType: string = Center.name;
|
||||
|
||||
processPdu(session: any, pdu: PDU, ...args: any[]): Promise<any> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
let promises = [];
|
||||
if (pdu.response) {
|
||||
let replyPromise = session.send(pdu.response());
|
||||
let sendPromise = session.send(new smpp.PDU('deliver_sm', {
|
||||
source_addr: pdu.destination_addr,
|
||||
@@ -15,6 +19,7 @@ export class EchoPduProcessor extends PduProcessor {
|
||||
}));
|
||||
promises.push(replyPromise);
|
||||
promises.push(sendPromise);
|
||||
}
|
||||
Promise.all(promises).then((replyPdus: any) => {
|
||||
resolve(replyPdus);
|
||||
}).catch((error: any) => {
|
||||
|
@@ -1,17 +1,17 @@
|
||||
import {PDU} from "../CommonObjects";
|
||||
import Logger from "../Logger";
|
||||
import {SmppSession} from "../SmppSession";
|
||||
import {DebugPduProcessor} from "./DebugPduProcessor";
|
||||
|
||||
export abstract class PduProcessor {
|
||||
static processors: PduProcessor[] = [];
|
||||
abstract readonly servesSessionType: string;
|
||||
abstract readonly serverSessionType: string;
|
||||
readonly name: string = this.constructor.name;
|
||||
readonly logger: Logger = new Logger(`PduProcessor: ${this.name}`);
|
||||
private static logger: Logger = new Logger("PduProcessor");
|
||||
|
||||
static getProcessor(name: string): PduProcessor {
|
||||
this.logger.log1(`Looking for processor with name ${name}...`);
|
||||
let pduProcessor = this.processors.find((processor: any) => processor.name === name);
|
||||
let pduProcessor = this.processors.find((processor: PduProcessor) => processor.name === name);
|
||||
if (pduProcessor) {
|
||||
this.logger.log1(`Found processor with name ${name}`);
|
||||
return pduProcessor;
|
||||
@@ -35,22 +35,22 @@ export abstract class PduProcessor {
|
||||
|
||||
static areCompatible(session: SmppSession, processor: PduProcessor): boolean {
|
||||
this.logger.log1(`Checking compatibility between session ${session.constructor.name}-${session.getId()} and processor ${processor.name}`);
|
||||
return session.constructor.name === processor.servesSessionType;
|
||||
return session.constructor.name === processor.serverSessionType;
|
||||
}
|
||||
|
||||
static addProcessor(processor: any): void {
|
||||
static addProcessor(processor: new () => PduProcessor): void {
|
||||
PduProcessor.processors.push(new processor());
|
||||
}
|
||||
|
||||
static getProcessorsForType(type: string): PduProcessor[] {
|
||||
return this.processors.filter((processor: any) => processor.servesSessionType === type);
|
||||
return this.processors.filter((processor: PduProcessor) => processor.serverSessionType === type);
|
||||
}
|
||||
|
||||
abstract processPdu(session: any, pdu: any, ...args: any[]): Promise<any>;
|
||||
abstract processPdu(session: any, pdu: PDU, ...args: any[]): Promise<any>;
|
||||
|
||||
serialize(): object {
|
||||
return {
|
||||
servesSessionType: this.servesSessionType,
|
||||
servesSessionType: this.serverSessionType,
|
||||
name: this.name
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user