Begin center implementation
This commit is contained in:
89
src/Center/Center.ts
Normal file
89
src/Center/Center.ts
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import EventEmitter from "events";
|
||||||
|
import {ClientEvents} from "../Client/ClientEvents";
|
||||||
|
import {Job} from "../Job/Job";
|
||||||
|
import {JobEvents} from "../Job/JobEvents";
|
||||||
|
import Logger from "../Logger";
|
||||||
|
import {SmppSession} from "../SmppSession";
|
||||||
|
|
||||||
|
export class Center implements SmppSession {
|
||||||
|
defaultMultipleJob!: Job;
|
||||||
|
defaultSingleJob!: Job;
|
||||||
|
password: string;
|
||||||
|
username: string;
|
||||||
|
port: number;
|
||||||
|
private eventEmitter: EventEmitter = new EventEmitter();
|
||||||
|
private readonly logger: Logger;
|
||||||
|
private readonly _id: number;
|
||||||
|
private session?: any;
|
||||||
|
|
||||||
|
constructor(id: number, port: number, username: string, password: string) {
|
||||||
|
this._id = id;
|
||||||
|
this.port = port;
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
|
||||||
|
this.logger = new Logger(`Center-${id}`);
|
||||||
|
|
||||||
|
this.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
initialize(): void {
|
||||||
|
this.defaultSingleJob = Job.createEmptySingle();
|
||||||
|
this.defaultMultipleJob = Job.createEmptyMultiple();
|
||||||
|
this.defaultSingleJob.on(JobEvents.STATE_CHANGED, () => this.eventEmitter.emit(ClientEvents.STATE_CHANGED, this.serialize()));
|
||||||
|
this.defaultMultipleJob.on(JobEvents.STATE_CHANGED, () => this.eventEmitter.emit(ClientEvents.STATE_CHANGED, this.serialize()));
|
||||||
|
}
|
||||||
|
|
||||||
|
cancelSendInterval(): void {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
close(): Promise<void> {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
getDefaultMultipleJob(): Job {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
getDefaultSingleJob(): Job {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
getId(): number {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMultiple(job: Job): Promise<void> {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMultipleDefault(): Promise<void> {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
sendPdu(pdu: object, force?: boolean): Promise<object> {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
sendSingle(job: Job): Promise<object> {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
sendSingleDefault(): Promise<object> {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
serialize(): object {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
setDefaultMultipleJob(job: Job): void {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
setDefaultSingleJob(job: Job): void {
|
||||||
|
throw new Error("NEBI");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -22,6 +22,8 @@ export class Client implements SmppSession {
|
|||||||
private session?: any;
|
private session?: any;
|
||||||
private connectPromise: PersistentPromise | null = null;
|
private connectPromise: PersistentPromise | null = null;
|
||||||
private bindPromise: PersistentPromise | null = null;
|
private bindPromise: PersistentPromise | null = null;
|
||||||
|
// TODO: Implement close promise
|
||||||
|
// Apparently the sessions are not closed on a dime but instead a .close() call causes eventSessionClose
|
||||||
private sendTimer: any | null = null;
|
private sendTimer: any | null = null;
|
||||||
private counterUpdateTimer: any | null = null;
|
private counterUpdateTimer: any | null = null;
|
||||||
|
|
||||||
@@ -100,37 +102,30 @@ export class Client implements SmppSession {
|
|||||||
this.defaultMultipleJob.on(JobEvents.STATE_CHANGED, () => this.eventEmitter.emit(ClientEvents.STATE_CHANGED, this.serialize()));
|
this.defaultMultipleJob.on(JobEvents.STATE_CHANGED, () => this.eventEmitter.emit(ClientEvents.STATE_CHANGED, this.serialize()));
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(): PersistentPromise {
|
doConnect(): PersistentPromise {
|
||||||
this.connectPromise = new PersistentPromise((resolve, reject) => {
|
this.connectPromise = new PersistentPromise((resolve, reject) => {
|
||||||
if (this._status !== ClientStatus.NOT_CONNECTED) {
|
if (this._status !== ClientStatus.NOT_CONNECTED) {
|
||||||
let errorString = `Client already connected`;
|
let errorString = `Client-${this._id} already connected`;
|
||||||
this.logger.log1(errorString);
|
this.logger.log1(errorString);
|
||||||
reject(errorString);
|
reject(errorString);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.log1(`Client connecting to ${this._url}`);
|
this.logger.log1(`Client-${this._id} connecting to ${this._url}`);
|
||||||
this.setStatus(ClientStatus.CONNECTING);
|
this.setStatus(ClientStatus.CONNECTING);
|
||||||
try {
|
this.connectSession().then(resolve, ((err: any) => {
|
||||||
this.connectSession();
|
this.logger.log1(`Client-${this._id} connection failed: ${err}`);
|
||||||
} catch (e) {
|
|
||||||
let errorString = `Client connection failed to ${this._url}`;
|
|
||||||
this.logger.log1(errorString);
|
|
||||||
|
|
||||||
this.setStatus(ClientStatus.NOT_CONNECTED);
|
this.setStatus(ClientStatus.NOT_CONNECTED);
|
||||||
this.session.close();
|
this.session.close();
|
||||||
|
reject(err);
|
||||||
reject(errorString);
|
}));
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return this.connectPromise;
|
return this.connectPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
bind(): PersistentPromise {
|
doBind(): PersistentPromise {
|
||||||
this.bindPromise = new PersistentPromise((resolve, reject) => {
|
this.bindPromise = new PersistentPromise((resolve, reject) => {
|
||||||
if (!this.fieldsAreOk()) {
|
this.validateFields(reject);
|
||||||
reject();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.session.bind_transceiver({
|
this.session.bind_transceiver({
|
||||||
system_id: this._username, password: this._password,
|
system_id: this._username, password: this._password,
|
||||||
@@ -141,7 +136,7 @@ export class Client implements SmppSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connectAndBind(): Promise<void> {
|
connectAndBind(): Promise<void> {
|
||||||
return this.connect().then(this.bind.bind(this), (error) => {
|
return this.doConnect().then(this.doBind.bind(this), (error) => {
|
||||||
this.logger.log1(`Client-${this._id} connectAndBind failed: ${error}`);
|
this.logger.log1(`Client-${this._id} connectAndBind failed: ${error}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -155,7 +150,9 @@ export class Client implements SmppSession {
|
|||||||
|
|
||||||
close(): Promise<void> {
|
close(): Promise<void> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
this.logger.log1(`Client-${this._id} closing connection`);
|
||||||
this.session.close();
|
this.session.close();
|
||||||
|
this.setStatus(ClientStatus.NOT_CONNECTED);
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -240,18 +237,18 @@ export class Client implements SmppSession {
|
|||||||
return this.sendSingle(this.getDefaultSingleJob());
|
return this.sendSingle(this.getDefaultSingleJob());
|
||||||
}
|
}
|
||||||
|
|
||||||
private connectSession(): void {
|
private connectSession(): Promise<void> {
|
||||||
if (!this.fieldsAreOk()) {
|
return new Promise<void>((resolve, reject) => {
|
||||||
return;
|
this.validateFields(reject);
|
||||||
}
|
this.logger.log1(`Client-${this._id} connecting to ${this._url}`);
|
||||||
this.logger.log1(`Client-${this._id} connecting to ${this._url}`);
|
|
||||||
|
|
||||||
this.session = smpp.connect({
|
this.session = smpp.connect({
|
||||||
url: this._url, auto_enquire_link_period: AUTO_ENQUIRE_LINK_PERIOD,
|
url: this._url, auto_enquire_link_period: AUTO_ENQUIRE_LINK_PERIOD,
|
||||||
}, this.eventSessionConnected.bind(this));
|
}, this.eventSessionConnected.bind(this));
|
||||||
this.session.on('error', this.eventSessionError.bind(this));
|
this.session.on('error', this.eventSessionError.bind(this));
|
||||||
this.session.on('close', this.eventSessionClose.bind(this));
|
this.session.on('close', this.eventSessionClose.bind(this));
|
||||||
this.session.on('pdu', this.eventAnyPdu.bind(this));
|
this.session.on('pdu', this.eventAnyPdu.bind(this));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private eventSessionConnected(): void {
|
private eventSessionConnected(): void {
|
||||||
@@ -304,20 +301,22 @@ export class Client implements SmppSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fieldsAreOk() {
|
private validateFields(reject: (reason?: any) => void) {
|
||||||
if (!this._url) {
|
if (!this._url) {
|
||||||
this.logger.log1(`Client-${this._id} has no url set`);
|
let error = `Client-${this._id} has no url set`;
|
||||||
return false;
|
this.logger.log1(error);
|
||||||
|
reject(error);
|
||||||
}
|
}
|
||||||
if (!this._username) {
|
if (!this._username) {
|
||||||
this.logger.log1(`Client-${this._id} has no username set`);
|
let error = `Client-${this._id} has no username set`;
|
||||||
return false;
|
this.logger.log1(error);
|
||||||
|
reject(error);
|
||||||
}
|
}
|
||||||
if (!this._password) {
|
if (!this._password) {
|
||||||
this.logger.log1(`Client-${this._id} has no password set`);
|
let error = `Client-${this._id} has no password set`;
|
||||||
return false;
|
this.logger.log1(error);
|
||||||
|
reject(error);
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private validateSession(reject: (reason?: any) => void) {
|
private validateSession(reject: (reason?: any) => void) {
|
||||||
|
11
src/main.ts
11
src/main.ts
@@ -43,9 +43,18 @@ async function main() {
|
|||||||
// client.sendMultipleDefault();
|
// client.sendMultipleDefault();
|
||||||
|
|
||||||
// client.on(ClientEvents.ANY_PDU, (pdu: any) => console.log(pdu));
|
// client.on(ClientEvents.ANY_PDU, (pdu: any) => console.log(pdu));
|
||||||
client.on(ClientEvents.STATE_CHANGED, (state: any) => console.log(state.defaultMultipleJob));
|
client.on(ClientEvents.STATUS_CHANGED, (state: any) => console.log(state));
|
||||||
client.setDefaultSingleJob(new Job(pdu1));
|
client.setDefaultSingleJob(new Job(pdu1));
|
||||||
client.setDefaultMultipleJob(new Job(pdu1, 100, 10));
|
client.setDefaultMultipleJob(new Job(pdu1, 100, 10));
|
||||||
|
client.sendSingleDefault();
|
||||||
|
client.close().then(() => {
|
||||||
|
console.log("CLOSED");
|
||||||
|
client.doConnect().then(() => {
|
||||||
|
client.doBind().then(() => {
|
||||||
|
client.sendMultipleDefault();
|
||||||
|
}, reason => console.log(reason));
|
||||||
|
}, err => console.log(err));
|
||||||
|
}, err => console.log(err));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user