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 connectPromise: 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 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()));
|
||||
}
|
||||
|
||||
connect(): PersistentPromise {
|
||||
doConnect(): PersistentPromise {
|
||||
this.connectPromise = new PersistentPromise((resolve, reject) => {
|
||||
if (this._status !== ClientStatus.NOT_CONNECTED) {
|
||||
let errorString = `Client already connected`;
|
||||
let errorString = `Client-${this._id} already connected`;
|
||||
this.logger.log1(errorString);
|
||||
reject(errorString);
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.log1(`Client connecting to ${this._url}`);
|
||||
this.logger.log1(`Client-${this._id} connecting to ${this._url}`);
|
||||
this.setStatus(ClientStatus.CONNECTING);
|
||||
try {
|
||||
this.connectSession();
|
||||
} catch (e) {
|
||||
let errorString = `Client connection failed to ${this._url}`;
|
||||
this.logger.log1(errorString);
|
||||
|
||||
this.connectSession().then(resolve, ((err: any) => {
|
||||
this.logger.log1(`Client-${this._id} connection failed: ${err}`);
|
||||
this.setStatus(ClientStatus.NOT_CONNECTED);
|
||||
this.session.close();
|
||||
|
||||
reject(errorString);
|
||||
}
|
||||
reject(err);
|
||||
}));
|
||||
});
|
||||
return this.connectPromise;
|
||||
}
|
||||
|
||||
bind(): PersistentPromise {
|
||||
doBind(): PersistentPromise {
|
||||
this.bindPromise = new PersistentPromise((resolve, reject) => {
|
||||
if (!this.fieldsAreOk()) {
|
||||
reject();
|
||||
}
|
||||
this.validateFields(reject);
|
||||
|
||||
this.session.bind_transceiver({
|
||||
system_id: this._username, password: this._password,
|
||||
@@ -141,7 +136,7 @@ export class Client implements SmppSession {
|
||||
}
|
||||
|
||||
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}`);
|
||||
});
|
||||
}
|
||||
@@ -155,7 +150,9 @@ export class Client implements SmppSession {
|
||||
|
||||
close(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.logger.log1(`Client-${this._id} closing connection`);
|
||||
this.session.close();
|
||||
this.setStatus(ClientStatus.NOT_CONNECTED);
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
@@ -240,10 +237,9 @@ export class Client implements SmppSession {
|
||||
return this.sendSingle(this.getDefaultSingleJob());
|
||||
}
|
||||
|
||||
private connectSession(): void {
|
||||
if (!this.fieldsAreOk()) {
|
||||
return;
|
||||
}
|
||||
private connectSession(): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
this.validateFields(reject);
|
||||
this.logger.log1(`Client-${this._id} connecting to ${this._url}`);
|
||||
|
||||
this.session = smpp.connect({
|
||||
@@ -252,6 +248,7 @@ export class Client implements SmppSession {
|
||||
this.session.on('error', this.eventSessionError.bind(this));
|
||||
this.session.on('close', this.eventSessionClose.bind(this));
|
||||
this.session.on('pdu', this.eventAnyPdu.bind(this));
|
||||
});
|
||||
}
|
||||
|
||||
private eventSessionConnected(): void {
|
||||
@@ -304,20 +301,22 @@ export class Client implements SmppSession {
|
||||
}
|
||||
}
|
||||
|
||||
private fieldsAreOk() {
|
||||
private validateFields(reject: (reason?: any) => void) {
|
||||
if (!this._url) {
|
||||
this.logger.log1(`Client-${this._id} has no url set`);
|
||||
return false;
|
||||
let error = `Client-${this._id} has no url set`;
|
||||
this.logger.log1(error);
|
||||
reject(error);
|
||||
}
|
||||
if (!this._username) {
|
||||
this.logger.log1(`Client-${this._id} has no username set`);
|
||||
return false;
|
||||
let error = `Client-${this._id} has no username set`;
|
||||
this.logger.log1(error);
|
||||
reject(error);
|
||||
}
|
||||
if (!this._password) {
|
||||
this.logger.log1(`Client-${this._id} has no password set`);
|
||||
return false;
|
||||
let error = `Client-${this._id} has no password set`;
|
||||
this.logger.log1(error);
|
||||
reject(error);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private validateSession(reject: (reason?: any) => void) {
|
||||
|
11
src/main.ts
11
src/main.ts
@@ -43,9 +43,18 @@ async function main() {
|
||||
// client.sendMultipleDefault();
|
||||
|
||||
// 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.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