Fix center send
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import EventEmitter from "events";
|
import EventEmitter from "events";
|
||||||
import {ClientEvents} from "../Client/ClientEvents";
|
import {ClientEvents} from "../Client/ClientEvents";
|
||||||
|
import ClientStatus from "../Client/ClientStatus";
|
||||||
import {Job} from "../Job/Job";
|
import {Job} from "../Job/Job";
|
||||||
import {JobEvents} from "../Job/JobEvents";
|
import {JobEvents} from "../Job/JobEvents";
|
||||||
import Logger from "../Logger";
|
import Logger from "../Logger";
|
||||||
@@ -14,7 +15,6 @@ const smpp = require("smpp");
|
|||||||
const MESSAGE_SEND_UPDATE_DELAY: number = Number(process.env.MESSAGE_SEND_UPDATE_DELAY) || 500;
|
const MESSAGE_SEND_UPDATE_DELAY: number = Number(process.env.MESSAGE_SEND_UPDATE_DELAY) || 500;
|
||||||
|
|
||||||
export class Center implements SmppSession {
|
export class Center implements SmppSession {
|
||||||
port: number;
|
|
||||||
private pendingSessions: any[] = [];
|
private pendingSessions: any[] = [];
|
||||||
private sessions: any[] = [];
|
private sessions: any[] = [];
|
||||||
private nextSession: number = 0;
|
private nextSession: number = 0;
|
||||||
@@ -27,7 +27,7 @@ export class Center implements SmppSession {
|
|||||||
|
|
||||||
constructor(id: number, port: number, username: string, password: string) {
|
constructor(id: number, port: number, username: string, password: string) {
|
||||||
this._id = id;
|
this._id = id;
|
||||||
this.port = port;
|
this._port = port;
|
||||||
this._username = username;
|
this._username = username;
|
||||||
this._password = password;
|
this._password = password;
|
||||||
|
|
||||||
@@ -36,6 +36,16 @@ export class Center implements SmppSession {
|
|||||||
this.initialize();
|
this.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get id(): number {
|
||||||
|
return this._id;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _port: number;
|
||||||
|
|
||||||
|
get port(): number {
|
||||||
|
return this._port;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement a few modes and set this to default DEBUG
|
// TODO: Implement a few modes and set this to default DEBUG
|
||||||
private _processor: CenterPDUProcessor | undefined;
|
private _processor: CenterPDUProcessor | undefined;
|
||||||
|
|
||||||
@@ -68,6 +78,10 @@ export class Center implements SmppSession {
|
|||||||
|
|
||||||
private _password: string;
|
private _password: string;
|
||||||
|
|
||||||
|
get password(): string {
|
||||||
|
return this._password;
|
||||||
|
}
|
||||||
|
|
||||||
set password(value: string) {
|
set password(value: string) {
|
||||||
this._password = value;
|
this._password = value;
|
||||||
this.eventEmitter.emit(CenterEvents.STATE_CHANGED, this.serialize());
|
this.eventEmitter.emit(CenterEvents.STATE_CHANGED, this.serialize());
|
||||||
@@ -75,6 +89,10 @@ export class Center implements SmppSession {
|
|||||||
|
|
||||||
private _username: string;
|
private _username: string;
|
||||||
|
|
||||||
|
get username(): string {
|
||||||
|
return this._username;
|
||||||
|
}
|
||||||
|
|
||||||
set username(value: string) {
|
set username(value: string) {
|
||||||
this._username = value;
|
this._username = value;
|
||||||
this.eventEmitter.emit(CenterEvents.STATE_CHANGED, this.serialize());
|
this.eventEmitter.emit(CenterEvents.STATE_CHANGED, this.serialize());
|
||||||
@@ -82,6 +100,10 @@ export class Center implements SmppSession {
|
|||||||
|
|
||||||
private _status: CenterStatus = CenterStatus.WAITING_CONNECTION;
|
private _status: CenterStatus = CenterStatus.WAITING_CONNECTION;
|
||||||
|
|
||||||
|
get status(): CenterStatus {
|
||||||
|
return this._status;
|
||||||
|
}
|
||||||
|
|
||||||
set status(value: CenterStatus) {
|
set status(value: CenterStatus) {
|
||||||
this._status = value;
|
this._status = value;
|
||||||
this.eventEmitter.emit(CenterEvents.STATUS_CHANGED, this._status);
|
this.eventEmitter.emit(CenterEvents.STATUS_CHANGED, this._status);
|
||||||
@@ -98,12 +120,17 @@ export class Center implements SmppSession {
|
|||||||
this.server.on('error', this.eventSessionError.bind(this));
|
this.server.on('error', this.eventSessionError.bind(this));
|
||||||
this.server.on('close', this.eventSessionClose.bind(this));
|
this.server.on('close', this.eventSessionClose.bind(this));
|
||||||
this.server.on('pdu', this.eventAnyPdu.bind(this));
|
this.server.on('pdu', this.eventAnyPdu.bind(this));
|
||||||
this.server.listen(this.port);
|
this.server.listen(this._port);
|
||||||
this.status = CenterStatus.WAITING_CONNECTION;
|
this.status = CenterStatus.WAITING_CONNECTION;
|
||||||
}
|
}
|
||||||
|
|
||||||
cancelSendInterval(): void {
|
cancelSendInterval(): void {
|
||||||
throw new Error("NEBI");
|
if (this.sendTimer) {
|
||||||
|
this.sendTimer.clearInterval();
|
||||||
|
this.counterUpdateTimer.clearInterval();
|
||||||
|
this.sendTimer = null;
|
||||||
|
this.counterUpdateTimer = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close(): Promise<void> {
|
close(): Promise<void> {
|
||||||
@@ -149,7 +176,8 @@ export class Center implements SmppSession {
|
|||||||
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}`));
|
.then(() => {
|
||||||
|
}, err => this.logger.log1(`Error sending message: ${err}`));
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
}, '', `${interval} s`);
|
}, '', `${interval} s`);
|
||||||
@@ -167,7 +195,7 @@ export class Center implements SmppSession {
|
|||||||
this.validateSessions(reject);
|
this.validateSessions(reject);
|
||||||
}
|
}
|
||||||
this.logger.log5(`Center-${this._id} sending PDU: ${JSON.stringify(pdu)}`);
|
this.logger.log5(`Center-${this._id} sending PDU: ${JSON.stringify(pdu)}`);
|
||||||
this.getNextSession().send(pdu, (replyPdu: object) => resolve(replyPdu));
|
this.getNextSession().send(pdu, (replyPdu: object) => resolve(replyPdu), (err: object) => reject(err));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,8 +209,13 @@ export class Center implements SmppSession {
|
|||||||
|
|
||||||
serialize(): object {
|
serialize(): object {
|
||||||
return {
|
return {
|
||||||
id: this._id, port: this.port, username: this._username, password: this._password, status: this._status,
|
id: this._id,
|
||||||
defaultSingleJob: this._defaultSingleJob, defaultMultipleJob: this._defaultMultipleJob,
|
port: this._port,
|
||||||
|
username: this._username,
|
||||||
|
password: this._password,
|
||||||
|
status: this._status,
|
||||||
|
defaultSingleJob: this._defaultSingleJob,
|
||||||
|
defaultMultipleJob: this._defaultMultipleJob,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +242,7 @@ export class Center implements SmppSession {
|
|||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
private eventBindTransciever(session: any, pdu: any) {
|
private eventBindTransceiver(session: any, pdu: any) {
|
||||||
this.logger.log1(`Center-${this._id} got a bind_transciever with system_id ${pdu.system_id} and password ${pdu.password}`);
|
this.logger.log1(`Center-${this._id} 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) {
|
||||||
@@ -220,7 +253,7 @@ export class Center implements SmppSession {
|
|||||||
this.sessions.push(session);
|
this.sessions.push(session);
|
||||||
this.updateStatus();
|
this.updateStatus();
|
||||||
} else {
|
} else {
|
||||||
this.logger.log1(`Center-${this._id} client connection failed, invalid credentials`);
|
this.logger.log1(`Center-${this._id} client connection failed, invalid credentials (expected: ${this.username}, ${this.password})`);
|
||||||
session.send(pdu.response({
|
session.send(pdu.response({
|
||||||
command_status: smpp.ESME_RBINDFAIL
|
command_status: smpp.ESME_RBINDFAIL
|
||||||
}));
|
}));
|
||||||
@@ -235,7 +268,7 @@ export class Center implements SmppSession {
|
|||||||
this.pendingSessions.push(session);
|
this.pendingSessions.push(session);
|
||||||
session.on('close', this.eventSessionClose.bind(this, session));
|
session.on('close', this.eventSessionClose.bind(this, session));
|
||||||
session.on('error', this.eventSessionError.bind(this, session));
|
session.on('error', this.eventSessionError.bind(this, session));
|
||||||
session.on('bind_transciever', this.eventBindTransciever.bind(this, session));
|
session.on('bind_transceiver', this.eventBindTransceiver.bind(this, session));
|
||||||
session.on('pdu', this.eventAnyPdu.bind(this, session));
|
session.on('pdu', this.eventAnyPdu.bind(this, session));
|
||||||
this.updateStatus();
|
this.updateStatus();
|
||||||
this.eventEmitter.emit(CenterEvents.STATE_CHANGED, this.serialize());
|
this.eventEmitter.emit(CenterEvents.STATE_CHANGED, this.serialize());
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
import {Center} from "./Center/Center";
|
import {Center} from "./Center/Center";
|
||||||
import {Client} from "./Client/Client";
|
import {Client} from "./Client/Client";
|
||||||
import {ClientEvents} from "./Client/ClientEvents";
|
|
||||||
import ClientSessionManager from "./Client/ClientSessionManager";
|
import ClientSessionManager from "./Client/ClientSessionManager";
|
||||||
import {Job} from "./Job/Job";
|
import {Job} from "./Job/Job";
|
||||||
import Logger from "./Logger";
|
import Logger from "./Logger";
|
||||||
@@ -60,6 +59,13 @@ async function main() {
|
|||||||
// });
|
// });
|
||||||
|
|
||||||
let center: Center = new Center(0, 7000, "test", "test");
|
let center: Center = new Center(0, 7000, "test", "test");
|
||||||
|
setTimeout(() => {
|
||||||
|
center.sendMultiple(new Job(new PDU("deliver_sm", {
|
||||||
|
source_addr: "1234567890",
|
||||||
|
destination_addr: "1234567890",
|
||||||
|
short_message: "Hello World"
|
||||||
|
}), 100, 100));
|
||||||
|
}, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
Reference in New Issue
Block a user