diff --git a/main.js b/main.js index 711d25f..8fa36a8 100644 --- a/main.js +++ b/main.js @@ -247,6 +247,7 @@ class ClientSession { this.eventEmitter.emit(ClientSession.ANY_PDU_EVENT, payload); } }); + this.session.send(); this.session.on('pdu', this.sessionPdu.bind(this)); this.connectingPromise.resolve(); } diff --git a/src/Client.ts b/src/Client.ts index dc9b4ab..c20ba8c 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -22,7 +22,7 @@ export class Client implements SmppSession { defaultMultipleJob: Job = Job.createEmptyMultiple(); private readonly eventEmitter: EventEmitter; private readonly logger: Logger; - private readonly id: number; + private readonly _id: number; private session?: any; private connectPromise: PersistentPromise | null = null; private bindPromise: PersistentPromise | null = null; @@ -30,7 +30,7 @@ export class Client implements SmppSession { private counterUpdateTimer: any | null = null; constructor(id: number, url: string, username: string, password: string) { - this.id = id; + this._id = id; this._url = url; this._username = username; this._password = password; @@ -56,6 +56,10 @@ export class Client implements SmppSession { private _url: string; + getUrl(): string { + return this._url; + } + set url(value: string) { this._url = value; } @@ -68,6 +72,24 @@ export class Client implements SmppSession { this.eventEmitter.emit(Client.ClientEvents.STATE_CHANGED, this.serialize()); } + setDefaultSingleJob(job: Job): void { + this.defaultSingleJob = job; + this.eventEmitter.emit(Client.ClientEvents.STATE_CHANGED, this.serialize()); + } + + setDefaultMultipleJob(job: Job): void { + this.defaultMultipleJob = job; + this.eventEmitter.emit(Client.ClientEvents.STATE_CHANGED, this.serialize()); + } + + getDefaultSingleJob(): Job { + return this.defaultSingleJob; + } + + getDefaultMultipleJob(): Job { + return this.defaultMultipleJob; + } + setStatus(status: ClientStatus): void { this._status = status; this.eventEmitter.emit("status", status); @@ -119,15 +141,15 @@ export class Client implements SmppSession { connectAndBind(): Promise { return this.connect().then(this.bind.bind(this), (error) => { - this.logger.log1(`Client-${this.id} connectAndBind failed: ${error}`); + this.logger.log1(`Client-${this._id} connectAndBind failed: ${error}`); }); } - serialize(): string { - return JSON.stringify({ - id: this.id, url: this._url, username: this._username, password: this._password, status: this._status, + serialize(): object { + return { + id: this._id, url: this._url, username: this._username, password: this._password, status: this._status, defaultSingleJob: this.defaultSingleJob, defaultMultipleJob: this.defaultMultipleJob, - }); + }; } close(): Promise { @@ -143,7 +165,7 @@ export class Client implements SmppSession { this.validateSession(reject); this.validateBound(reject); } - this.logger.log5(`Client-${this.id} sending PDU: ${JSON.stringify(pdu)}`); + this.logger.log5(`Client-${this._id} sending PDU: ${JSON.stringify(pdu)}`); this.session.send(pdu); resolve(pdu); }); @@ -154,9 +176,9 @@ export class Client implements SmppSession { this.validateSession(reject); this.validateBound(reject); if (!job.count || !job.perSecond) { - reject(`Client-${this.id} sendMultiple failed: invalid job, missing fields`); + reject(`Client-${this._id} sendMultiple failed: invalid job, missing fields`); } - this.logger.log1(`Client-${this.id} sending multiple messages: ${JSON.stringify(job)}`); + this.logger.log1(`Client-${this._id} sending multiple messages: ${JSON.stringify(job)}`); this.setStatus(ClientStatus.BUSY); @@ -205,11 +227,23 @@ export class Client implements SmppSession { this.eventEmitter.on(event, callback); } + getId(): number { + return this._id; + } + + sendMultipleDefault(): Promise { + return this.sendMultiple(this.getDefaultMultipleJob()); + } + + sendSingleDefault(): Promise { + return this.sendSingle(this.getDefaultSingleJob()); + } + private connectSession(): void { if (!this.fieldsAreOk()) { return; } - this.logger.log1(`Client-${this.id} connecting to ${this._url}`); + this.logger.log1(`Client-${this._id} connecting to ${this._url}`); this.session = smpp.connect({ url: this._url, auto_enquire_link_period: AUTO_ENQUIRE_LINK_PERIOD, @@ -220,7 +254,7 @@ export class Client implements SmppSession { } private eventSessionConnected(): void { - this.logger.log1(`Client-${this.id} connected to ${this._url}`); + this.logger.log1(`Client-${this._id} connected to ${this._url}`); this.setStatus(ClientStatus.CONNECTED); if (this.connectPromise) { this.connectPromise.resolve(); @@ -228,31 +262,31 @@ export class Client implements SmppSession { } private eventAnyPdu(pdu: any): void { - this.logger.log6(`Client-${this.id} received PDU: ${JSON.stringify(pdu)}`); + this.logger.log6(`Client-${this._id} received PDU: ${JSON.stringify(pdu)}`); this.eventEmitter.emit(Client.ClientEvents.ANY_PDU, pdu); } private eventSessionError(pdu: any): void { - this.logger.log1(`Client-${this.id} error on ${this._url}`); + this.logger.log1(`Client-${this._id} error on ${this._url}`); this.setStatus(ClientStatus.NOT_CONNECTED); this.rejectPromises(pdu); } private eventSessionClose(): void { - this.logger.log1(`Client-${this.id} closed on ${this._url}`); + this.logger.log1(`Client-${this._id} closed on ${this._url}`); this.setStatus(ClientStatus.NOT_CONNECTED); this.rejectPromises(); } private eventBindReply(pdu: any): void { if (pdu.command_status === 0) { - this.logger.log1(`Client-${this.id} bound to ${this._url}`); + this.logger.log1(`Client-${this._id} bound to ${this._url}`); this.setStatus(ClientStatus.BOUND); if (this.bindPromise) { this.bindPromise.resolve(); } } else { - this.logger.log1(`Client-${this.id} bind failed to ${this.url}`); + this.logger.log1(`Client-${this._id} bind failed to ${this.url}`); this.setStatus(ClientStatus.CONNECTED); if (this.bindPromise) { this.bindPromise.reject(pdu); @@ -271,15 +305,15 @@ export class Client implements SmppSession { private fieldsAreOk() { if (!this._url) { - this.logger.log1(`Client-${this.id} has no url set`); + this.logger.log1(`Client-${this._id} has no url set`); return false; } if (!this._username) { - this.logger.log1(`Client-${this.id} has no username set`); + this.logger.log1(`Client-${this._id} has no username set`); return false; } if (!this._password) { - this.logger.log1(`Client-${this.id} has no password set`); + this.logger.log1(`Client-${this._id} has no password set`); return false; } return true; @@ -287,7 +321,7 @@ export class Client implements SmppSession { private validateSession(reject: (reason?: any) => void) { if (!this.session) { - let errorMessage = `Client-${this.id} session is not defined`; + let errorMessage = `Client-${this._id} session is not defined`; this.logger.log1(errorMessage); reject(errorMessage); } @@ -295,7 +329,7 @@ export class Client implements SmppSession { private validateBound(reject: (reason?: any) => void) { if (this._status !== ClientStatus.BOUND) { - let errorMessage = `Client-${this.id} is not bound`; + let errorMessage = `Client-${this._id} is not bound`; this.logger.log1(errorMessage); reject(errorMessage); } diff --git a/src/ClientSessionManager.ts b/src/ClientSessionManager.ts new file mode 100644 index 0000000..2c1d22b --- /dev/null +++ b/src/ClientSessionManager.ts @@ -0,0 +1,121 @@ +import fs from "fs"; +import {Client} from "./Client"; +import {Job} from "./Job"; +import Logger from "./Logger"; +import SessionManager from "./SessionManager"; +import {SmppSession} from "./SmppSession"; + +const CLIENT_SESSIONS_FILE: string = process.env.CLIENT_SESSIONS_FILE || "client_sessions.json"; + +export default class ClientSessionManager implements SessionManager { + sessionId: number; + sessions: Client[]; + private readonly logger: any; + + constructor() { + this.sessionId = 0; + this.sessions = []; + this.logger = new Logger("ClientSessionManager"); + } + + addSession(session: SmppSession): Promise { + return new Promise((resolve, reject) => { + this.sessions.push(session as Client); + resolve(); + }); + } + + removeSession(session: SmppSession): Promise { + return new Promise((resolve, reject) => { + this.sessions = this.sessions.filter(s => s.getId() !== session.getId()); + resolve(); + }); + } + + // TODO: Make sure no url duplicates exist + createSession(url: string, username: string, password: string): Promise { + return new Promise((resolve, reject) => { + this.verifyUrl(url, reject); + this.verifyUsername(username, reject); + this.verifyPassword(password, reject); + + let client = new Client(this.sessionId++, url, username, password); + this.addSession(client).then(() => { + resolve(client); + }); + }); + } + + getSession(id: number): Promise { + return new Promise((resolve, reject) => { + let session: SmppSession | undefined = this.sessions.find(s => s.getId() === id); + if (session) { + resolve(session); + } else { + reject(`Session with id ${id} not found`); + } + }); + } + + getSessionByUrl(url: string): Promise { + return new Promise((resolve, reject) => { + let session: SmppSession | undefined = this.sessions.find(s => s.getUrl() === url); + if (session) { + resolve(session); + } else { + reject(`Session with url ${url} not found`); + } + }); + } + + serialize(): object { + return this.sessions.map((session: SmppSession) => { + return session.serialize(); + }); + } + + setup(): void { + try { + let sessions: Buffer = fs.readFileSync(CLIENT_SESSIONS_FILE); + let loadedSessions: any[] = JSON.parse(String(sessions)); + this.logger.log1(`Loaded ${sessions.length} clients from ${CLIENT_SESSIONS_FILE}...`); + loadedSessions.forEach(session => { + this.createSession(session.url, session.username, session.password).then((sessionObj: SmppSession) => { + sessionObj.setDefaultSingleJob(Job.deserialize(session.defaultSingleJob)); + sessionObj.setDefaultMultipleJob(Job.deserialize(session.defaultMultipleJob)); + }); + }); + } catch (e) { + this.logger.log1(`Error loading clients from ${CLIENT_SESSIONS_FILE}: ${e}`); + } + } + + cleanup(): void { + this.logger.log1(`Saving clients to ${CLIENT_SESSIONS_FILE}...`); + fs.writeFileSync(CLIENT_SESSIONS_FILE, JSON.stringify(this.serialize(), null, 4)); + } + + private verifyUrl(url: string, reject: (reason?: any) => void) { + if (!url) { + let error = `Request to make a new client failed because of missing url.`; + this.logger.log1(error); + reject(error); + } + } + + private verifyUsername(username: string, reject: (reason?: any) => void) { + if (!username) { + let error = `Request to make a new client failed because of missing username.`; + this.logger.log1(error); + reject(error); + } + } + + private verifyPassword(password: string, reject: (reason?: any) => void) { + if (!password) { + let error = `Request to make a new client failed because of missing password.`; + this.logger.log1(error); + reject(error); + } + } +} \ No newline at end of file diff --git a/src/Job.ts b/src/Job.ts index ba1737b..0d28550 100644 --- a/src/Job.ts +++ b/src/Job.ts @@ -1,5 +1,6 @@ const smpp = require("smpp"); +// TODO: Implement on change event and propagate it to sessions export class Job { pdu: any; perSecond?: number; @@ -15,6 +16,14 @@ export class Job { return JSON.stringify(this); } + static deserialize(serialized: any): Job { + if (!serialized.pdu.command) { + throw new Error("Invalid serialized job"); + } + let pdu: any = new smpp.PDU(serialized.pdu.command, serialized.pdu); + return new Job(pdu, serialized.perSecond, serialized.count); + } + static createEmptySingle(): Job { return new Job({}); } diff --git a/src/SessionManager.ts b/src/SessionManager.ts new file mode 100644 index 0000000..ab64687 --- /dev/null +++ b/src/SessionManager.ts @@ -0,0 +1,20 @@ +import {SmppSession} from "./SmppSession"; + +export default interface SessionManager { + sessions: SmppSession[]; + sessionId: number; + + addSession(session: SmppSession): Promise; + + removeSession(session: SmppSession): Promise; + + createSession(...args: any[]): Promise; + + getSession(id: number): Promise; + + serialize(): object; + + cleanup(): void; + + setup(): void; +} \ No newline at end of file diff --git a/src/SmppSession.ts b/src/SmppSession.ts index b68ba50..f630016 100644 --- a/src/SmppSession.ts +++ b/src/SmppSession.ts @@ -1,14 +1,28 @@ import {Job} from "./Job"; +// TODO: Implement on change event and propagate it to sessions +// Do something like "onJobChange" here... +// Maybe even make it default export interface SmppSession { username: string, password: string, + defaultSingleJob: Job; + defaultMultipleJob: Job; + + getDefaultSingleJob(): Job; + setDefaultSingleJob(job: Job): void; + getDefaultMultipleJob(): Job; + setDefaultMultipleJob(job: Job): void; + + getId(): number; sendPdu(pdu: object, force?: boolean): Promise; sendSingle(job: Job): Promise; + sendSingleDefault(): Promise; sendMultiple(job: Job): Promise; + sendMultipleDefault(): Promise; cancelSendInterval(): void; @@ -16,5 +30,5 @@ export interface SmppSession { initialize(): void; - serialize(): string; + serialize(): object; } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 818969d..d48c846 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,5 @@ import {Client} from "./Client"; +import ClientSessionManager from "./ClientSessionManager"; import {Job} from "./Job"; import Logger from "./Logger"; @@ -17,7 +18,6 @@ const app = express(); const SERVER_PORT: number = Number(process.env.SERVER_PORT) || 8190; const WS_SERVER_PORT: number = Number(process.env.WS_SERVER_PORT) || 8191; -const CLIENT_SESSIONS_FILE: string = process.env.CLIENT_SESSIONS_FILE || "client_sessions.json"; const CENTER_SESSIONS_FILE: string = process.env.CENTER_SESSIONS_FILE || "center_sessions.json"; // TODO: Add support for encodings @@ -25,1636 +25,36 @@ const CENTER_SESSIONS_FILE: string = process.env.CENTER_SESSIONS_FILE || "center let logger = new Logger("main"); -let client: Client = new Client(0, "smpp://localhost:7000", "test", "test"); -client.connectAndBind().then(() => { - console.log("POGGIES"); - client.sendMultiple(new Job(new smpp.PDU('submit_sm', { - source_addr: "1234567890", - destination_addr: "1234567890", - short_message: "Hello World" - }), 100, 100)); +let clientManager: ClientSessionManager = new ClientSessionManager(); +clientManager.setup(); - client.on(Client.ClientEvents.ANY_PDU, (pdu: any) => console.log(pdu)); -}); +async function main() { + // let client: Client = await clientManager.createSession("smpp://localhost:7000", "test", "test") as Client; + let client: Client = await clientManager.getSession(0) as Client; -// class ClientSession { -// // TODO: Enable requesting DRs -// private auto_enquire_link_period: number = 500; -// private eventEmitter: object = new EventEmitter(); -// configuredMessageJob = { -// source: "", -// destination: "", -// message: "", -// }; -// configuredMultiMessageJob = { -// source: "", -// destination: "", -// message: "", -// interval: 1000, -// count: 1, -// }; -// -// connectingPromise = { -// promise: null, -// resolve: null, -// reject: null -// } -// disconnectingPromise = { -// promise: null, -// resolve: null, -// reject: null -// } -// bindingPromise = { -// promise: null, -// resolve: null, -// reject: null -// } -// -// static STATUS_CHANGED_EVENT = "statusChanged"; -// static ANY_PDU_EVENT = "*"; -// static MESSAGE_SEND_COUNTER_UPDATE_EVENT = "messageSendCounterUpdate"; -// -// constructor(id, url, username, password) { -// this.id = id; -// this.logger = new Logger(`ClientSession-${this.id}`); -// this.url = url; -// -// this.username = username; -// this.password = password; -// -// this.logger.log1(`Client created with url ${this.url}, username ${this.username}, password ${this.password} and ID ${this.id}`); -// this.status = ClientSessionStatus.NOT_CONNECTED; -// } -// -// setUsername(username) { -// this.username = username; -// this.refresh(); -// } -// -// setPassword(password) { -// this.password = password; -// this.refresh(); -// } -// -// refresh() { -// this.logger.log1(`Refreshing client with url ${this.url} and id ${this.id}`); -// let status = this.status; -// this.close().catch(err => err); -// if (status === ClientSessionStatus.CONNECTED) { -// this.connect().catch(err => this.logger.log1(err)); -// } -// if (status === ClientSessionStatus.BOUND) { -// this.connect().then(() => { -// this.bind().catch((err => this.logger.log1(err))); -// }).catch((err => this.logger.log1(err))); -// } -// } -// -// setStatus(newStatus) { -// this.status = newStatus; -// this.eventEmitter.emit(ClientSession.STATUS_CHANGED_EVENT, newStatus); -// } -// -// connect() { -// this.connectingPromise.promise = new Promise((resolve, reject) => { -// if (this.status !== ClientSessionStatus.NOT_CONNECTED) { -// this.logger.log1("Client already connected"); -// reject("Client already connected"); -// return; -// } -// this.logger.log1(`Client connecting to ${this.url}`); -// this.setStatus(ClientSessionStatus.CONNECTING); -// try { -// this.session = smpp.connect({ -// url: this.url, -// auto_enquire_link_period: this.auto_enquire_link_period, -// }, this.connected.bind(this)); -// this.session.on('error', this.error.bind(this)); -// this.session.on('close', this.closed.bind(this)); -// } catch (e) { -// this.logger.log1("Client connection failed to " + this.url); -// this.setStatus(ClientSessionStatus.NOT_CONNECTED); -// this.session.close(); -// reject("Client connection failed to " + this.url); -// } -// this.connectingPromise.resolve = resolve; -// this.connectingPromise.reject = reject; -// }); -// return this.connectingPromise.promise; -// } -// -// closed() { -// this.logger.log1(`Client closed connection to ${this.url}`); -// this.setStatus(ClientSessionStatus.NOT_CONNECTED); -// } -// -// error(error) { -// if (error.code === "ETIMEOUT") { -// this.logger.log1("Client connection timed out to " + this.url); -// } else if (error.code === "ECONNREFUSED") { -// this.logger.log1("Client connection refused to " + this.url); -// } else { -// this.logger.log1("Client connection failed to " + this.url); -// } -// this.session.close(); -// this.connectingPromise.reject(error); -// this.setStatus(ClientSessionStatus.NOT_CONNECTED); -// } -// -// connected() { -// this.logger.log1("Client connected to " + this.url); -// this.setStatus(ClientSessionStatus.CONNECTED); -// this.session.on('debug', (type, msg, payload) => { -// if (type.includes('pdu.')) { -// this.eventEmitter.emit(msg, payload); -// this.eventEmitter.emit(ClientSession.ANY_PDU_EVENT, payload); -// } -// }); -// this.session.on('pdu', this.sessionPdu.bind(this)); -// this.connectingPromise.resolve(); -// } -// -// sessionPdu(pdu) { -// if (pdu.command === 'deliver_sm') { -// this.session.send(pdu.response()); -// } -// } -// -// bind() { -// this.bindingPromise.promise = new Promise((resolve, reject) => { -// if (this.status !== ClientSessionStatus.CONNECTED) { -// this.logger.log1(`Cannot bind, client not connected to ${this.url}`); -// reject(`Cannot bind, client not connected to ${this.url}`); -// return; -// } -// -// this.logger.log1("Trying to bind to " + this.url) -// if (this.status !== ClientSessionStatus.CONNECTED) { -// this.logger.log1(`Cannot bind, client not connected to ${this.url}`); -// return; -// } -// if (!!!this.username || !!!this.password) { -// this.logger.log1(`Cannot bind client, username or password not set`); -// return; -// } -// this.setStatus(ClientSessionStatus.BINDING); -// this.logger.log1(`Client binding to ${this.url} with username ${this.username} and password ${this.password}`); -// -// this.session.bind_transceiver({ -// system_id: this.username, -// password: this.password, -// registered_delivery: 1 -// }, this.bindReply.bind(this)); -// this.bindingPromise.resolve = resolve; -// this.bindingPromise.reject = reject; -// }); -// return this.bindingPromise.promise; -// } -// -// bindReply(pdu) { -// if (pdu.command_status === 0) { -// this.logger.log1(`Client bound to ${this.url} with username ${this.username} and password ${this.password}`); -// this.setStatus(ClientSessionStatus.BOUND); -// this.bindingPromise.resolve(); -// } else { -// this.logger.log1(`Client bind failed to ${this.url} with username ${this.username} and password ${this.password}`); -// this.setStatus(ClientSessionStatus.CONNECTED); -// this.bindingPromise.reject(); -// } -// } -// -// send(source, destination, message, force=false) { -// return new Promise((resolve, reject) => { -// if (!force && !this.canSend()) { -// this.logger.log1(`Client cannot send message, not bound to ${this.url} or busy`); -// reject(`Client cannot send message, not bound to ${this.url} or busy`); -// return; -// } -// this.logger.log1(`Client sending message from ${source} to ${destination} with message ${message}`); -// this.session.submit_sm({ -// source_addr: source, -// destination_addr: destination, -// short_message: message, -// registered_delivery: 1, -// message_id: 10 -// }, pdu => { -// resolve(pdu); -// }); -// }); -// } -// -// sendDefault() { -// return this.send(this.configuredMessageJob.source, this.configuredMessageJob.destination, this.configuredMessageJob.message); -// } -// -// configureDefault(source, destination, message) { -// this.configuredMessageJob = { -// source: source, -// destination: destination, -// message: message -// } -// } -// -// sendOnInterval(source, destination, message, interval, count) { -// return new Promise((resolve, reject) => { -// if (!this.canSend()) { -// this.logger.log1(`Client cannot send many message, not bound to ${this.url} or busy`); -// reject(`Client cannot send many message, not bound to ${this.url} or busy`); -// return; -// } -// this.setStatus(ClientSessionStatus.BUSY); -// let counter = 0; -// let previousUpdateCounter = 0; -// -// this.updateTimer = new NanoTimer(); -// this.updateTimer.setInterval(() => { -// if (previousUpdateCounter !== counter) { -// this.eventEmitter.emit(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, counter); -// previousUpdateCounter = counter; -// } -// }, '', `${MESSAGE_SEND_UPDATE_DELAY / 1000} s`); -// -// this.timer = new NanoTimer(); -// this.timer.setInterval(() => { -// if (count > 0 && counter >= count) { -// this.cancelSendInterval(); -// } else { -// this.send(source, destination, message, true) -// .catch(e => this.logger.log1(`Error sending message: ${e}`)); -// counter++; -// } -// }, '', `${interval} s`); -// resolve(); -// }); -// } -// -// sendDefaultInterval() { -// return this.sendOnInterval(this.configuredMultiMessageJob.source, this.configuredMultiMessageJob.destination, this.configuredMultiMessageJob.message, -// this.configuredMultiMessageJob.interval, this.configuredMultiMessageJob.count); -// } -// -// configureDefaultInterval(source, destination, message, interval, count) { -// this.configuredMultiMessageJob = { -// source: source, -// destination: destination, -// message: message, -// interval: interval, -// count: count -// } -// } -// -// cancelSendInterval() { -// if (!!this.timer) { -// this.timer.clearInterval(); -// this.updateTimer.clearInterval(); -// this.timer = null; -// this.updateTimer = null; -// } -// this.setStatus(ClientSessionStatus.BOUND); -// } -// -// close() { -// this.disconnectingPromise.promise = new Promise((resolve, reject) => { -// if (this.status !== ClientSessionStatus.BOUND && this.status !== ClientSessionStatus.CONNECTED) { -// this.logger.log1(`Cannot close client, not bound to ${this.url}`); -// reject(`Cannot close client, not bound to ${this.url}`); -// return; -// } -// this.logger.log1(`Client closing connection to ${this.url}`); -// this.session.close(); -// this.setStatus(ClientSessionStatus.NOT_CONNECTED); -// resolve(); -// }); -// return this.disconnectingPromise.promise; -// } -// -// on(event, callback) { -// this.eventEmitter.on(event, callback); -// } -// -// serialize() { -// return { -// id: this.id, -// url: this.url, -// username: this.username, -// password: this.password, -// status: this.status, -// configuredMessageJob: this.configuredMessageJob, -// configuredMultiMessageJob: this.configuredMultiMessageJob -// } -// } -// -// canSend() { -// return this.status === ClientSessionStatus.BOUND; -// } -// } + client.connectAndBind().then(() => { + console.log("POGGIES"); + let pdu1 = new smpp.PDU('submit_sm', { + source_addr: "1234567890", + destination_addr: "1234567890", + short_message: "Hello World" + }); + client.setDefaultSingleJob(new Job(pdu1)); + client.setDefaultMultipleJob(new Job(pdu1, 100, 10)); + client.sendMultipleDefault(); + + client.on(Client.ClientEvents.ANY_PDU, (pdu: any) => console.log(pdu)); + }); +} + +main(); + +function cleanup(): void { + logger.log1("Cleaning up..."); + clientManager.cleanup(); + process.exit(0); +} -// class ClientSessionManager { -// sessionIdCounter = 0; -// logger = new Logger("ClientSessionManager"); -// -// constructor() { -// this.sessions = {}; -// } -// -// createSession(url, username, password) { -// let urlB64 = btoa(url); -// if (this.sessions[urlB64]) { -// this.logger.log1(`Client to ${url} already exists`); -// return this.sessions[urlB64]; -// } -// this.logger.log1(`Creating client to ${url} with username ${username} and password ${password}`); -// let session = new ClientSession(this.sessionIdCounter++, url, username, password); -// this.addSession(session); -// return session; -// } -// -// addSession(session) { -// this.logger.log1(`Adding client with ID ${session.id}`); -// this.sessions[btoa(session.url)] = session; -// } -// -// deleteSession(session) { -// this.logger.log1(`Deleting client with ID ${session.id}`); -// if (session.status === ClientSessionStatus.BOUND || session.status === ClientSessionStatus.CONNECTED) { -// session.close(); -// } -// delete this.sessions[btoa(session.url)]; -// } -// -// getSession(id) { -// return Object.values(this.sessions).find((session) => { -// return session.id == id; -// }); -// } -// -// serialize() { -// return Object.values(this.sessions).map((session) => { -// return session.serialize(); -// }); -// } -// -// cleanup() { -// this.logger.log1(`Saving clients to ${CLIENT_SESSIONS_FILE}...`); -// fs.writeFileSync(CLIENT_SESSIONS_FILE, JSON.stringify(this.serialize(), null, 4)); -// } -// -// startup() { -// try { -// let sessions = fs.readFileSync(CLIENT_SESSIONS_FILE); -// sessions = JSON.parse(sessions); -// this.logger.log1(`Loaded ${sessions.length} clients from ${CLIENT_SESSIONS_FILE}...`); -// sessions.forEach(session => { -// let sessionObj = this.createSession(session.url, session.username, session.password); -// sessionObj.configuredMessageJob = session.configuredMessageJob; -// sessionObj.configuredMultiMessageJob = session.configuredMultiMessageJob; -// }); -// } catch (e) { -// this.logger.log1(`Error loading clients from ${CLIENT_SESSIONS_FILE}: ${e}`); -// } -// } -// } -// -// class CenterSessionStatus { -// static CONNECTED = "CONNECTED"; -// static WAITING_CONNECTION = "WAITING CONNECTION"; -// static CONNECTION_PENDING = "CONNECTION PENDING"; -// static BUSY = "BUSY"; -// } -// -// class CenterMode { -// static DEBUG = "DEBUG"; -// static ECHO = "ECHO"; -// static DR = "DR"; -// } -// -// class CenterSession { -// // TODO: If the port is in use this throws an exception, catch it and log it -// eventEmitter = new EventEmitter(); -// sessions = []; -// nextSession = 0; -// mode = CenterMode.DEBUG; -// configuredMessageJob = { -// source: "", -// destination: "", -// message: "", -// }; -// configuredMultiMessageJob = { -// source: "", -// destination: "", -// message: "", -// interval: 1000, -// count: 1, -// }; -// -// disconnectingPromise = { -// promise: null, -// resolve: null, -// reject: null -// } -// -// static STATUS_CHANGED_EVENT = "statusChanged"; -// static MODE_CHANGED_EVENT = "modeChanged"; -// static SESSION_CHANGED_EVENT = "sessionChanged"; -// static ANY_PDU_EVENT = "*"; -// static MESSAGE_SEND_COUNTER_UPDATE_EVENT = "messageSendCounterUpdate"; -// -// constructor(id, port, username, password) { -// this.id = id; -// this.logger = new Logger(`CenterSession-${this.id}`); -// this.port = port; -// -// this.username = username; -// this.password = password; -// -// this.server = smpp.createServer({}, this.connected.bind(this)); -// this.server.on('debug', (type, msg, payload) => { -// if (type.includes('pdu.')) { -// this.eventEmitter.emit(msg, payload); -// this.eventEmitter.emit(CenterSession.ANY_PDU_EVENT, payload); -// } -// }); -// this.server.listen(this.port); -// -// this.logger.log1(`Center created with port ${this.port}, username ${this.username}, password ${this.password} and ID ${this.id}`); -// this.status = CenterSessionStatus.WAITING_CONNECTION; -// } -// -// setStatus(newStatus) { -// this.status = newStatus; -// this.eventEmitter.emit(CenterSession.STATUS_CHANGED_EVENT, newStatus); -// } -// -// setUsername(username) { -// this.username = username; -// this.refresh(); -// } -// -// setPassword(password) { -// this.password = password; -// this.refresh(); -// } -// -// setMode(mode) { -// this.mode = Object.values(CenterMode)[mode]; -// this.eventEmitter.emit(CenterSession.MODE_CHANGED_EVENT, mode); -// } -// -// refresh() { -// this.close().catch(err => { -// }); -// } -// -// error(error) { -// if (error.code === "ETIMEOUT") { -// this.logger.log1("Center connection timed out to " + this.port); -// } else if (error.code === "ECONNREFUSED") { -// this.logger.log1("Center connection refused to " + this.port); -// } else { -// this.logger.log1("Center connection failed to " + this.port); -// } -// this.logger.log1(`Session on center croaked. Error: ${error}`); -// this.setStatus(CenterSessionStatus.CONNECTION_PENDING); -// } -// -// connected(session) { -// this.logger.log1("Center got a connection on port " + this.port); -// this.setStatus(CenterSessionStatus.CONNECTION_PENDING); -// -// session.on('error', this.error.bind(this)); -// session.on('close', this.sessionClosed.bind(this, session)); -// -// function bind_transciever(pdu) { -// this.logger.log1(`Center got a bind_transciever on port ${this.port} 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 session connection successful`); -// session.send(pdu.response()); -// session.resume(); -// session.on('pdu', this.sessionPdu.bind(this, session)); -// this.addSession(session); -// if (this.sessions.length > 0) { -// this.setStatus(CenterSessionStatus.CONNECTED); -// } -// session.on('debug', (type, msg, payload) => { -// if (type.includes('pdu.')) { -// this.eventEmitter.emit(msg, payload); -// this.eventEmitter.emit(CenterSession.ANY_PDU_EVENT, payload); -// } -// }); -// } else { -// this.logger.log1(`Center session connection failed, invalid credentials`); -// session.send(pdu.response({ -// command_status: smpp.ESME_RBINDFAIL -// })); -// if (this.sessions.length === 0) { -// this.setStatus(CenterSessionStatus.WAITING_CONNECTION); -// } -// session.close(); -// this.session = null; -// } -// } -// -// session.on('bind_transceiver', bind_transciever.bind(this)); -// } -// -// sessionClosed(session) { -// this.logger.log1(`Center session closed on port ${this.port}`); -// delete this.sessions[this.sessions.indexOf(session)]; -// this.sessions = this.sessions.filter(Boolean); -// if (this.sessions.length === 0) { -// this.setStatus(CenterSessionStatus.WAITING_CONNECTION); -// } -// } -// -// sessionPdu(session, pdu) { -// if (pdu.command === 'submit_sm') { -// session.send(pdu.response()); -// if (this.mode === CenterMode.ECHO) { -// this.notify(pdu.destination_addr, pdu.source_addr, pdu.short_message); -// } -// // TODO: Figure out how DRs work -// if (this.mode === CenterMode.DR && pdu.registered_delivery === 1) { -// let drPdu = new PDU('deliver_sm', { -// receipted_message_id: pdu.message_id, -// esm_class: 4, -// message_state: 2, -// length: 0, -// }); -// this.send(drPdu).catch(err => console.log(err)); -// } -// } -// if (pdu.command === 'enquire_link') { -// session.send(pdu.response()); -// } -// } -// -// configureDefault(source, destination, message) { -// this.configuredMessageJob = { -// source: source, -// destination: destination, -// message: message -// } -// } -// -// notifyDefault() { -// return this.notify(this.configuredMessageJob.source, this.configuredMessageJob.destination, this.configuredMessageJob.message); -// } -// -// notify(source, destination, message, force=false) { -// return new Promise((resolve, reject) => { -// if (!force && !this.canSend()) { -// this.logger.log1(`Center cannot send message, no sessions active on ${this.port} or busy`); -// reject(`Center cannot send message, no sessions active on ${this.port} or busy`); -// return; -// } -// this.logger.log1(`Sending notify message from ${source} to ${destination} with message ${message}`); -// this.getNextSession().deliver_sm({ -// source_addr: source, -// destination_addr: destination, -// short_message: message, -// }, pdu => { -// resolve(pdu); -// }); -// }); -// } -// -// send(pdu) { -// return new Promise((resolve, reject) => { -// if (!this.canSend()) { -// this.logger.log1(`Center cannot send message, no sessions active on ${this.port} or busy`); -// reject(`Center cannot send message, no sessions active on ${this.port} or busy`); -// return; -// } -// this.getNextSession().send(pdu, pdu => { -// resolve(pdu); -// }); -// }); -// } -// -// configureDefaultInterval(source, destination, message, interval, count) { -// this.configuredMultiMessageJob = { -// source: source, -// destination: destination, -// message: message, -// interval: interval, -// count: count -// } -// } -// -// notifyDefaultInterval() { -// return this.notifyOnInterval(this.configuredMultiMessageJob.source, this.configuredMultiMessageJob.destination, this.configuredMultiMessageJob.message, -// this.configuredMultiMessageJob.interval, this.configuredMultiMessageJob.count); -// } -// -// notifyOnInterval(source, destination, message, interval, count) { -// return new Promise((resolve, reject) => { -// if (!this.canSend()) { -// this.logger.log1(`Center cannot send many message, no sessions active to ${this.port} or busy`); -// reject(`Center cannot send many message, no sessions active to ${this.port} or busy`); -// return; -// } -// this.setStatus(CenterSessionStatus.BUSY); -// this.timer = new NanoTimer(); -// let counter = 0; -// let previousUpdateCounter = 0; -// -// this.updateTimer = new NanoTimer(); -// this.updateTimer.setInterval(() => { -// if (previousUpdateCounter !== counter) { -// this.eventEmitter.emit(CenterSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, counter); -// previousUpdateCounter = counter; -// } -// }, '', `${MESSAGE_SEND_UPDATE_DELAY / 1000} s`); -// -// this.timer.setInterval(() => { -// if (count > 0 && counter >= count) { -// this.cancelNotifyInterval(); -// } else { -// this.notify(source, destination, message, true) -// .catch(e => this.logger.log1(`Error sending message: ${e}`)); -// counter++; -// } -// }, '', `${interval} s`); -// resolve(); -// }); -// } -// -// cancelNotifyInterval() { -// if (!!this.timer) { -// this.timer.clearInterval(); -// this.updateTimer.clearInterval(); -// this.timer = null; -// this.updateTimer = null; -// } -// this.setStatus(CenterSessionStatus.CONNECTED); -// } -// -// getNextSession() { -// if (this.sessions.length === 0) { -// return null; -// } -// let session = this.sessions[this.nextSession]; -// this.nextSession = (this.nextSession + 1) % this.sessions.length; -// return session; -// } -// -// getSessions() { -// return this.sessions.map(session => { -// return this.mapSession(session); -// }) -// } -// -// mapSession(session) { -// return { -// closed: session.closed, -// paused: session.paused, -// remoteAddress: session.remoteAddress, -// remotePort: session.remotePort, -// _id: session._id, -// deleted: session.deleted || false -// } -// } -// -// closeSession(sessionId) { -// this.logger.log1(`Closing center session ${sessionId}`); -// let session = this.sessions.find(session => session._id == sessionId); -// if (!!session) { -// session.close(); -// this.eventEmitter.emit(CenterSession.SESSION_CHANGED_EVENT, this.mapSession(session)); -// } -// } -// -// deleteSession(sessionId) { -// this.logger.log1(`Deleting center session ${sessionId}`); -// let session = this.sessions.find(session => session._id == sessionId); -// if (!!session) { -// session.close(); -// session.destroy(); -// session.deleted = true; -// this.eventEmitter.emit(CenterSession.SESSION_CHANGED_EVENT, this.mapSession(session)); -// delete this.sessions[this.sessions.indexOf(session)]; -// this.sessions = this.sessions.filter(Boolean); -// } -// } -// -// addSession(session) { -// this.logger.log1(`Adding center session ${session._id}`); -// let sessionInfo = this.mapSession(session); -// this.eventEmitter.emit(CenterSession.SESSION_CHANGED_EVENT, sessionInfo); -// this.sessions.push(session); -// } -// -// close() { -// this.disconnectingPromise.promise = new Promise((resolve, reject) => { -// if (this.status !== CenterSessionStatus.CONNECTED) { -// this.logger.log1(`Cannot close center, no sessions active ${this.port}`); -// reject(`Cannot close center, no sessions active ${this.port}`); -// return; -// } -// this.sessions.forEach(session => { -// session.close(); -// }); -// this.sessions = []; -// this.setStatus(CenterSessionStatus.WAITING_CONNECTION); -// resolve(); -// }); -// return this.disconnectingPromise.promise; -// } -// -// on(event, callback) { -// this.eventEmitter.on(event, callback); -// } -// -// serialize() { -// return { -// id: this.id, -// port: this.port, -// username: this.username, -// password: this.password, -// status: this.status, -// activeSessions: this.sessions.length, -// mode: this.mode, -// configuredMessageJob: this.configuredMessageJob, -// configuredMultiMessageJob: this.configuredMultiMessageJob, -// } -// } -// -// canSend() { -// return this.status === CenterSessionStatus.CONNECTED; -// } -// } -// -// class CenterSessionManager { -// sessionIdCounter = 0; -// logger = new Logger("CenterSessionManager"); -// -// constructor() { -// this.servers = {}; -// } -// -// createSession(port, username, password) { -// if (this.servers[port]) { -// this.logger.log1(`Center listening on ${port} already exists`); -// return this.servers[port]; -// } -// this.logger.log1(`Creating center listening on ${port} with username ${username} and password ${password}`); -// let session = new CenterSession(this.sessionIdCounter++, port, username, password); -// this.addSession(session); -// return session; -// } -// -// addSession(server) { -// this.logger.log1(`Adding center with ID ${server.id}`); -// this.servers[server.port] = server; -// } -// -// deleteSession(server) { -// this.logger.log1(`Deleting center with ID ${server.id}`); -// if (server.status === CenterSessionStatus.CONNECTED) { -// server.close(); -// } -// delete this.servers[server.port]; -// } -// -// getSession(id) { -// return Object.values(this.servers).find((session) => { -// return session.id == id; -// }); -// } -// -// serialize() { -// return Object.values(this.servers).map((servers) => { -// return servers.serialize(); -// }); -// } -// -// cleanup() { -// this.logger.log1(`Saving centers to ${CENTER_SESSIONS_FILE}...`); -// fs.writeFileSync(CENTER_SESSIONS_FILE, JSON.stringify(this.serialize(), null, 4)); -// } -// -// startup() { -// try { -// let servers = fs.readFileSync(CENTER_SESSIONS_FILE); -// servers = JSON.parse(servers); -// this.logger.log1(`Loaded ${servers.length} centers from ${CENTER_SESSIONS_FILE}...`); -// servers.forEach(server => { -// let createdServer = this.createSession(server.port, server.username, server.password); -// if (!!server.mode) { -// createdServer.mode = server.mode; -// } -// createdServer.configuredMessageJob = server.configuredMessageJob; -// createdServer.configuredMultiMessageJob = server.configuredMultiMessageJob; -// }); -// } catch (e) { -// this.logger.log1(`Error loading centers from ${CLIENT_SESSIONS_FILE}: ${e}`); -// } -// } -// -// getAvailableCenterModes() { -// let modes = Object.values(CenterMode); -// return modes.reduce((acc, curr, idx) => { -// acc[idx] = curr; -// return acc; -// }, {}); -// } -// } -// -// -// class HTTPServer { -// logger = new Logger("HTTPServer"); -// -// constructor() { -// app.use(bodyParser.json()); -// -// app.get('/api/client', this.getClientSessions.bind(this)); -// app.post('/api/client', this.createClientSession.bind(this)); -// app.get('/api/client/:id', this.getClientSessionById.bind(this)); -// app.patch('/api/client/:id', this.patchClientSession.bind(this)); -// app.put('/api/client/:id/send', this.configSend.bind(this)); -// app.post('/api/client/:id/send/default', this.sendConfig.bind(this)); -// app.post('/api/client/:id/send', this.send.bind(this)); -// app.put('/api/client/:id/sendMany', this.configSendMany.bind(this)); -// app.post('/api/client/:id/sendMany/default', this.sendManyConfig.bind(this)); -// app.post('/api/client/:id/sendMany', this.sendMany.bind(this)); -// app.delete('/api/client/:id/sendMany', this.cancelSendMany.bind(this)); -// app.post('/api/client/:id/bind', this.bindClientSession.bind(this)); -// app.post('/api/client/:id/connect', this.connectClientSession.bind(this)); -// app.delete('/api/client/:id/connect', this.disconnectClientSession.bind(this)); -// app.delete('/api/client/:id', this.deleteClientSession.bind(this)); -// -// app.get('/api/center', this.getCenterSessions.bind(this)); -// app.post('/api/center', this.createCenterSession.bind(this)); -// app.get('/api/center/modes', this.getAvailableModes.bind(this)); -// app.get('/api/center/:id', this.getCenterServerById.bind(this)); -// app.get('/api/center/:id/session', this.getCenterServerSessionsById.bind(this)); -// app.delete('/api/center/:id/session/:sessionId', this.closeCenterServerSessionById.bind(this)); -// app.delete('/api/center/:id/session/:sessionId/destroy', this.deleteCenterServerSessionById.bind(this)); -// app.patch('/api/center/:id', this.patchCenterServer.bind(this)); -// app.put('/api/center/:id/send', this.configNotify.bind(this)); -// app.post('/api/center/:id/send/default', this.notifyConfig.bind(this)); -// app.post('/api/center/:id/send', this.notify.bind(this)); -// app.put('/api/center/:id/sendMany', this.configNotifyMany.bind(this)); -// app.post('/api/center/:id/sendMany/default', this.notifyManyConfig.bind(this)); -// app.post('/api/center/:id/sendMany', this.notifyMany.bind(this)); -// app.delete('/api/center/:id/sendMany', this.cancelNotifyMany.bind(this)); -// app.delete('/api/center/:id/connect', this.disconnectCenterSession.bind(this)); -// app.delete('/api/center/:id', this.deleteCenterServer.bind(this)); -// -// this.server = app.listen(SERVER_PORT, function() { -// this.logger.log1(`HTTPServer listening at http://localhost:${SERVER_PORT}`) -// }.bind(this)); -// } -// -// // TODO: These requests deserve error handling -// -// getClientSessions(req, res) { -// this.logger.log1("Getting client sessions"); -// res.send(clientSessionManager.serialize()); -// } -// -// createClientSession(req, res) { -// this.logger.log1("Creating client session"); -// let session = clientSessionManager.createSession(req.body.url, req.body.username, req.body.password); -// res.send(session.serialize()); -// } -// -// getClientSessionById(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// this.logger.log1(`Getting client session by ID ${req.params.id}`); -// if (session) { -// this.logger.log1(`Client session found with ID ${req.params.id}`) -// res.send(session.serialize()); -// } else { -// this.logger.log1(`No client session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// patchClientSession(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// if (session) { -// this.logger.log1(`Client session found with ID ${req.params.id}`) -// if (!!req.body.username && req.body.username !== session.username) { -// session.setUsername(req.body.username); -// } -// if (!!req.body.password && req.body.password !== session.password) { -// session.setPassword(req.body.password); -// } -// res.send(session.serialize()); -// } else { -// this.logger.log1(`No client session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// configSend(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// let source = req.body.source; -// let destination = req.body.destination; -// let message = req.body.message; -// this.logger.log1(`Setting default message from ${source} to ${destination} with message ${message} on session with ID ${req.params.id}`) -// if (session) { -// session.configureDefault(source, destination, message); -// res.send(session.serialize()); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// sendConfig(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// this.logger.log1(`Sending pre-configured message on session with ID ${req.params.id}`) -// if (session) { -// session.sendDefault() -// .then(pdu => res.send(pdu)) -// .catch(err => res.status(400).send(JSON.stringify(err))); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// send(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// let source = req.body.source; -// let destination = req.body.destination; -// let message = req.body.message; -// this.logger.log1(`Sending message from ${source} to ${destination} with message ${message} on session with ID ${req.params.id}`) -// if (session) { -// session.send(source, destination, message) -// .then(pdu => res.send(pdu)) -// .catch(err => res.status(400).send(JSON.stringify(err))); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// configSendMany(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// let source = req.body.source; -// let destination = req.body.destination; -// let message = req.body.message; -// let interval = req.body.interval / 1000; -// let count = req.body.count; -// if (!!req.body.perSecond) { -// interval = 1 / req.body.perSecond; -// } -// let perSecond = 1 / interval; -// this.logger.log1( -// `Setting default ${count} messages from ${source} to ${destination} with message ${message} on session with ID ${req.params.id} at a rate of ${perSecond} per second.`); -// if (session) { -// session.configureDefaultInterval(source, destination, message, interval, count); -// res.send(session.serialize()); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// sendManyConfig(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// this.logger.log1(`Sending pre-configured messages on session with ID ${req.params.id}`) -// if (session) { -// session.sendDefaultInterval() -// .then(() => res.send({})) -// .catch(err => res.status(400).send(JSON.stringify(err))); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// sendMany(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// let source = req.body.source; -// let destination = req.body.destination; -// let message = req.body.message; -// let interval = req.body.interval / 1000; -// let count = req.body.count; -// if (!!req.body.perSecond) { -// interval = 1 / req.body.perSecond; -// } -// let perSecond = 1 / interval; -// this.logger.log1( -// `Sending ${count} messages from ${source} to ${destination} with message ${message} on session with ID ${req.params.id} at a rate of ${perSecond} per second.`); -// if (session) { -// session.sendOnInterval(source, destination, message, interval, count) -// .then(pdu => res.send(pdu)) -// .catch(err => res.status(400).send((err))); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// cancelSendMany(req, res) { -// let session = clientSessionManager.getSession(req.params.id); -// if (session.status !== ClientSessionStatus.BUSY) { -// res.status(400).send({ -// err: true, -// msg: `Session with ID ${req.params.id} is not sending messages` -// }); -// return; -// } -// this.logger.log1(`Cancelling send timer for session with ID ${req.params.id}`); -// if (session) { -// session.cancelSendInterval(); -// res.send({}); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// bindClientSession(req, res) { -// this.logger.log1(`Binding client session with ID ${req.params.id}`) -// // Maybe make this async? -// let session = clientSessionManager.getSession(req.params.id); -// if (session) { -// session.bind() -// .then(() => res.send(session.serialize())) -// .catch(err => res.status(400).send({ -// err: true, -// msg: err -// })); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// connectClientSession(req, res) { -// this.logger.log1(`Connecting client session with ID ${req.params.id}`) -// let session = clientSessionManager.getSession(req.params.id); -// if (session) { -// session.connect() -// .then(() => res.send(session.serialize())) -// .catch(err => res.status(400).send({ -// err: true, -// msg: err -// })); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// disconnectClientSession(req, res) { -// this.logger.log1(`Disconnecting client session with ID ${req.params.id}`) -// let session = clientSessionManager.getSession(req.params.id); -// if (session) { -// session.close() -// .then(() => res.send(session.serialize())) -// .catch(err => res.status(400).send({ -// err: true, -// msg: err -// })); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// deleteClientSession(req, res) { -// this.logger.log1(`Deleting client session with ID ${req.params.id}`); -// let session = clientSessionManager.getSession(req.params.id); -// if (session) { -// clientSessionManager.deleteSession(session); -// res.send({}); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// getCenterSessions(req, res) { -// this.logger.log1("Getting center sessions"); -// res.send(centerSessionManager.serialize()); -// } -// -// createCenterSession(req, res) { -// this.logger.log1("Creating center session"); -// let session = centerSessionManager.createSession(req.body.port, req.body.username, req.body.password); -// res.send(session.serialize()); -// } -// -// getCenterServerById(req, res) { -// let session = centerSessionManager.getSession(req.params.id); -// this.logger.log1(`Getting center session by ID ${req.params.id}`); -// if (session) { -// this.logger.log1(`Center session found with ID ${req.params.id}`) -// res.send(session.serialize()); -// } else { -// this.logger.log1(`No center session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// getCenterServerSessionsById(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// this.logger.log1(`Getting center session by ID ${req.params.id}`); -// if (server) { -// this.logger.log1(`Center session found with ID ${req.params.id}`) -// res.send(server.getSessions()); -// } else { -// this.logger.log1(`No center session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// closeCenterServerSessionById(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// this.logger.log1(`Getting center session by ID ${req.params.id}`); -// if (server) { -// this.logger.log1(`Center session found with ID ${req.params.id}`) -// server.closeSession(req.params.sessionId) -// res.send({}); -// } else { -// this.logger.log1(`No center session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// deleteCenterServerSessionById(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// this.logger.log1(`Getting center session by ID ${req.params.id}`); -// if (server) { -// this.logger.log1(`Center session found with ID ${req.params.id}`) -// server.deleteSession(req.params.sessionId) -// res.send({}); -// } else { -// this.logger.log1(`No center session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// patchCenterServer(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// if (server) { -// this.logger.log1(`Center server found with ID ${req.params.id}`) -// if (!!req.body.username && req.body.username !== server.username) { -// server.setUsername(req.body.username); -// } -// if (!!req.body.password && req.body.password !== server.password) { -// server.setPassword(req.body.password); -// } -// if (!!req.body.mode) { -// server.setMode(req.body.mode); -// } -// res.send(server.serialize()); -// } else { -// this.logger.log1(`No center server found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// getAvailableModes(req, res) { -// this.logger.log1("Getting available modes"); -// res.send(centerSessionManager.getAvailableCenterModes()); -// } -// -// configNotify(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// let source = req.body.source; -// let destination = req.body.destination; -// let message = req.body.message; -// this.logger.log1(`Setting default message from ${source} to ${destination} with message ${message} on server with ID ${req.params.id}`) -// if (server) { -// server.configureDefault(source, destination, message); -// res.send(server.serialize()); -// } else { -// this.logger.log1(`No server found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// notifyConfig(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// this.logger.log1(`Sending pre-configured message on server with ID ${req.params.id}`) -// if (server) { -// server.notifyDefault() -// .then(pdu => res.send(pdu)) -// .catch(err => res.status(400).send(JSON.stringify(err))); -// } else { -// this.logger.log1(`No server found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// notify(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// let source = req.body.source; -// let destination = req.body.destination; -// let message = req.body.message; -// this.logger.log1(`Sending notify message from ${source} to ${destination} with message ${message} on server with ID ${req.params.id}`) -// if (server) { -// server.notify(source, destination, message) -// .then(pdu => res.send(pdu)) -// .catch(err => res.status(400).send(err)); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// configNotifyMany(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// let source = req.body.source; -// let destination = req.body.destination; -// let message = req.body.message; -// let interval = req.body.interval / 1000; -// let count = req.body.count; -// if (!!req.body.perSecond) { -// interval = 1 / req.body.perSecond; -// } -// let perSecond = 1 / interval; -// this.logger.log1( -// `Setting default ${count} messages from ${source} to ${destination} with message ${message} on server with ID ${req.params.id} at a rate of ${perSecond} per second.`); -// if (server) { -// server.configureDefaultInterval(source, destination, message, interval, count); -// res.send(server.serialize()); -// } else { -// this.logger.log1(`No server found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// notifyManyConfig(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// this.logger.log1(`Sending pre-configured messages on server with ID ${req.params.id}`) -// if (server) { -// server.notifyDefaultInterval() -// .then(pdu => res.send(pdu)) -// .catch(err => res.status(400).send(JSON.stringify(err))); -// } else { -// this.logger.log1(`No server found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// notifyMany(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// let source = req.body.source; -// let destination = req.body.destination; -// let message = req.body.message; -// let interval = req.body.interval / 1000; -// let count = req.body.count; -// if (!!req.body.perSecond) { -// interval = 1 / req.body.perSecond; -// } -// let perSecond = 1 / interval; -// this.logger.log1( -// `Sending ${count} notify messages from ${source} to ${destination} with message ${message} on session with ID ${req.params.id} at a rate of ${perSecond} per second.`); -// if (server) { -// server.notifyOnInterval(source, destination, message, interval, count) -// .then(pdu => res.send(pdu)) -// .catch(err => res.status(400).send(err)); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// cancelNotifyMany(req, res) { -// let server = centerSessionManager.getSession(req.params.id); -// if (server.status !== ClientSessionStatus.BUSY) { -// res.status(400).send({ -// err: true, -// msg: `Session with ID ${req.params.id} is not sending messages` -// }); -// return; -// } -// this.logger.log1(`Cancelling send timer for server with ID ${req.params.id}`); -// if (server) { -// server.cancelNotifyInterval(); -// res.send({}); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// disconnectCenterSession(req, res) { -// this.logger.log1(`Disconnecting center session with ID ${req.params.id}`) -// let server = centerSessionManager.getSession(req.params.id); -// if (server) { -// server.close() -// .then(() => res.send(server.serialize())) -// .catch(err => res.status(400).send({ -// err: true, -// msg: err -// })); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// -// deleteCenterServer(req, res) { -// this.logger.log1(`Deleting center session with ID ${req.params.id}`); -// let server = centerSessionManager.getSession(req.params.id); -// if (server) { -// centerSessionManager.deleteSession(server); -// res.send({}); -// } else { -// this.logger.log1(`No session found with ID ${req.params.id}`); -// res.status(404).send(); -// } -// } -// } -// -// class WSServer { -// clients = {}; -// unknownClients = []; -// listenersAlreadySetup = []; -// -// constructor() { -// this.server = new WebSocket.Server({port: WS_SERVER_PORT}); -// this.logger = new Logger("WSServer"); -// this.server.on('connection', this.onConnection.bind(this)); -// this.logger.log1(`WSServer listening at ws://localhost:${WS_SERVER_PORT}`); -// } -// -// onConnection(ws) { -// this.logger.log1("New connection"); -// this.unknownClients.push(ws); -// ws.on('message', this.onMessage.bind(this, ws)); -// ws.on('close', this.onClose.bind(this, ws)); -// } -// -// addClient(ws, type, sessionId) { -// if (!this.clients[type]) { -// this.clients[type] = {}; -// } -// if (!this.clients[type][sessionId]) { -// this.clients[type][sessionId] = []; -// } -// this.logger.log1(`Adding client ${ws.id} to ${type} session ${sessionId}`); -// -// if (type === "client") { -// if (this.listenersAlreadySetup.indexOf(`client-${sessionId}`) === -1) { -// let session = clientSessionManager.getSession(sessionId); -// if (!!session) { -// this.logger.log1(`Setting up listeners for client session ${sessionId}`); -// session.on(ClientSession.STATUS_CHANGED_EVENT, this.onClientSessionStatusChange.bind(this, sessionId)); -// session.on(ClientSession.ANY_PDU_EVENT, this.onClientSessionPdu.bind(this, sessionId)); -// session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onClientMessageCounterUpdate.bind(this, sessionId)); -// } -// this.listenersAlreadySetup.push(`client-${sessionId}`); -// } else { -// this.logger.log1(`Listeners for client session ${sessionId} already set up`); -// } -// } else if (type === "center") { -// if (this.listenersAlreadySetup.indexOf(`center-${sessionId}`) === -1) { -// let session = centerSessionManager.getSession(sessionId); -// if (!!session) { -// this.logger.log1(`Setting up listeners for center session ${sessionId}`); -// session.on(CenterSession.STATUS_CHANGED_EVENT, this.onCenterStatusChange.bind(this, sessionId)); -// session.on(CenterSession.ANY_PDU_EVENT, this.onCenterServerPdu.bind(this, sessionId)); -// session.on(CenterSession.MODE_CHANGED_EVENT, this.onCenterModeChanged.bind(this, sessionId)); -// session.on(CenterSession.SESSION_CHANGED_EVENT, this.onCenterSessionsChanged.bind(this, sessionId)); -// session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onCenterMessageCounterUpdate.bind(this, sessionId)); -// } -// this.listenersAlreadySetup.push(`center-${sessionId}`); -// } else { -// this.logger.log1(`Listeners for center session ${sessionId} already set up`); -// } -// } -// -// this.clients[type][sessionId].push(ws); -// this.logger.log1(`Now active ${this.clients[type][sessionId].length} clients in session ID: ${sessionId} of type ${type}`); -// } -// -// onMessage(ws, message) { -// this.logger.log1("New message"); -// message = String(message); -// let data = message.split(":"); -// let type = data[0]; -// let sessionId = data[1]; -// -// this.logger.log1(`Moving client to session ID: ${sessionId} of type ${type}`); -// delete this.unknownClients[ws]; -// this.unknownClients = this.unknownClients.filter(Boolean); -// -// this.addClient(ws, type, sessionId); -// this.logger.log1(`Now active ${this.clients[type][sessionId].length} clients in session ID: ${sessionId} of type ${type}`); -// } -// -// onClose(ws) { -// this.removeClient(ws); -// // this.logger.log6(this.clients); -// this.logger.log1("Connection closed"); -// } -// -// removeClient(ws) { -// this.clients.client = this.removeFromArray(this.clients.client, ws); -// this.clients.center = this.removeFromArray(this.clients.center, ws); -// } -// -// removeFromArray(array, element) { -// for (let sessionId in array) { -// let index = array[sessionId].indexOf(element); -// if (index > -1) { -// delete array[sessionId][index]; -// } -// array[sessionId] = array[sessionId].filter(Boolean); -// if (array[sessionId].length === 0) { -// delete array[sessionId]; -// } -// } -// return array; -// } -// -// onClientSessionStatusChange(sessionId, newStatus) { -// this.logger.log1(`Session with ID ${sessionId} changed`); -// let payload = { -// objectType: "client", -// type: 'status', -// sessionId: sessionId, -// value: newStatus -// } -// let clients = this.clients["client"][sessionId]; -// if (!!clients) { -// this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); -// clients.forEach(client => { -// client.send(JSON.stringify(payload)); -// }); -// } -// } -// -// onClientSessionPdu(sessionId, pdu) { -// // TODO: Maybe move this to an "ignored" array against who the pdu.command is compared -// if (pdu.command === 'enquire_link_resp' || pdu.command === 'enquire_link') { -// return; -// } -// let clients = this.clients["client"][sessionId]; -// if (!!clients) { -// this.logger.log2(`Session with ID ${sessionId} fired PDU`); -// let payload = { -// objectType: "client", -// type: 'pdu', -// sessionId: sessionId, -// value: pdu -// } -// this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); -// clients.forEach(client => { -// client.send(JSON.stringify(payload)); -// }); -// } -// } -// -// onClientMessageCounterUpdate(sessionId, counter) { -// this.logger.log2(`Session with ID ${sessionId} updating message send counter`); -// let payload = { -// objectType: "client", -// type: 'counterUpdate', -// sessionId: sessionId, -// value: counter -// } -// let clients = this.clients["client"][sessionId]; -// if (!!clients) { -// this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); -// clients.forEach(client => { -// client.send(JSON.stringify(payload)); -// }); -// } -// } -// -// onCenterStatusChange(sessionId, newStatus) { -// this.logger.log1(`Session with ID ${sessionId} changed`); -// let payload = { -// objectType: "center", -// type: 'status', -// sessionId: sessionId, -// value: newStatus -// } -// let clients = this.clients["center"][sessionId]; -// if (!!clients) { -// this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); -// clients.forEach(client => { -// client.send(JSON.stringify(payload)); -// }); -// } -// } -// -// onCenterServerPdu(sessionId, pdu) { -// if (pdu.command === 'enquire_link_resp' || pdu.command === 'enquire_link') { -// return; -// } -// let clients = this.clients["center"][sessionId]; -// if (!!clients) { -// this.logger.log2(`Session with ID ${sessionId} fired PDU`); -// let payload = { -// objectType: "center", -// type: 'pdu', -// sessionId: sessionId, -// value: pdu -// } -// this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); -// clients.forEach(client => { -// client.send(JSON.stringify(payload)); -// }); -// } -// } -// -// onCenterModeChanged(sessionId, newMode) { -// this.logger.log1(`Session with ID ${sessionId} changed`); -// let payload = { -// objectType: "center", -// type: 'mode', -// sessionId: sessionId, -// value: newMode, -// text: CenterMode[newMode] -// } -// let clients = this.clients["center"][sessionId]; -// if (!!clients) { -// this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); -// clients.forEach(client => { -// client.send(JSON.stringify(payload)); -// }); -// } -// } -// -// onCenterSessionsChanged(sessionId, newSession) { -// this.logger.log1(`Session with ID ${sessionId} changed`); -// let payload = { -// objectType: "center", -// type: 'sessions', -// sessionId: sessionId, -// value: newSession -// } -// let clients = this.clients["center"][sessionId]; -// if (!!clients) { -// this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); -// clients.forEach(client => { -// client.send(JSON.stringify(payload)); -// }); -// } -// } -// -// onCenterMessageCounterUpdate(sessionId, counter) { -// this.logger.log2(`Session with ID ${sessionId} updating message send counter`); -// let payload = { -// objectType: "center", -// type: 'counterUpdate', -// sessionId: sessionId, -// value: counter -// } -// let clients = this.clients["center"][sessionId]; -// if (!!clients) { -// this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); -// clients.forEach(client => { -// client.send(JSON.stringify(payload)); -// }); -// } -// } -// } -// -// let clientSessionManager = new ClientSessionManager(); -// let centerSessionManager = new CenterSessionManager(); -// clientSessionManager.startup(); -// centerSessionManager.startup(); -// -// // let session = clientSessionManager.createSession('smpp://localhost:7001', 'test', 'test'); -// // let server = centerSessionManager.createSession(7001, 'test', 'test'); -// -// let session = clientSessionManager.getSession(0); -// let server = centerSessionManager.getSession(1); -// -// session.connect() -// .then(() => { -// session.bind().then(() => { -// // setTimeout(() => session.close(), 1000); -// }).catch(err => console.log(err)); -// }).catch(err => console.log(err)); -// -// // setTimeout(() => session.setUsername("test123"), 2000); -// // setTimeout(() => session.setPassword("test123"), 4000); -// -// // session.on(CenterSession.ANY_PDU_EVENT, (pdu) => { -// // console.log(pdu); -// // }); -// -// // session.on(ClientSession.ANY_PDU_EVENT, (pdu) => { -// // if (pdu.command.includes('enquire')) { -// // return; -// // } -// // console.log(pdu); -// // }); -// -// new WSServer(); -// new HTTPServer(); -// -// function cleanup() { -// clientSessionManager.cleanup(); -// centerSessionManager.cleanup(); -// process.exit(0); -// } -// // process.on('exit', cleanup); // process.on('SIGINT', cleanup); // process.on('SIGUSR1', cleanup);