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