Port more code
This commit is contained in:
18
.run/main.ts.run.xml
Normal file
18
.run/main.ts.run.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="main.ts" type="TypeScriptProgramRunner" factoryName="TypeScript">
|
||||||
|
<module name="smsgwtester" />
|
||||||
|
<envs>
|
||||||
|
<env name="LOG_LEVEL" value="5" />
|
||||||
|
</envs>
|
||||||
|
<option name="interpreterRef" value="project" />
|
||||||
|
<option name="enabledTsNodeEsmLoader" value="false" />
|
||||||
|
<option name="interpreterOptions" value="" />
|
||||||
|
<option name="workingDirectory" value="C:\Users\Administrator\WebstormProjects\smsgwtester\src" />
|
||||||
|
<option name="tsconfigFile" value="" />
|
||||||
|
<option name="extraTypeScriptOptions" value="" />
|
||||||
|
<option name="scriptName" value="$PROJECT_DIR$/src/main.ts" />
|
||||||
|
<option name="programParameters" value="" />
|
||||||
|
<option name="tsnodePackage" value="C:\Program Files\nodejs\node_modules\ts-node" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
33
src/PersistentPromise.ts
Normal file
33
src/PersistentPromise.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
export default class PersistentPromise {
|
||||||
|
private readonly promise: Promise<any>;
|
||||||
|
private promiseResolve: ((value?: any) => void) | undefined;
|
||||||
|
private promiseReject: ((reason?: any) => void) | undefined;
|
||||||
|
|
||||||
|
constructor(callback: (resolve: (value?: any) => void, reject: (reason?: any) => void) => void) {
|
||||||
|
this.promise = new Promise((resolve, reject) => {
|
||||||
|
callback(resolve, reject);
|
||||||
|
this.promiseResolve = resolve;
|
||||||
|
this.promiseReject = reject;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getPromise(): Promise<any> {
|
||||||
|
return this.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(value?: any): void {
|
||||||
|
if (this.promiseResolve) {
|
||||||
|
this.promiseResolve(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reject(reason?: any): void {
|
||||||
|
if (this.promiseReject) {
|
||||||
|
this.promiseReject(reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
then(onfulfilled?: ((value: any) => any) | undefined | null, onrejected?: ((reason: any) => any) | undefined | null): Promise<any> {
|
||||||
|
return this.promise.then(onfulfilled, onrejected);
|
||||||
|
}
|
||||||
|
}
|
209
src/client.ts
209
src/client.ts
@@ -1,50 +1,26 @@
|
|||||||
import {SmppSession} from "./smppSession";
|
import EventEmitter from "events";
|
||||||
|
import ClientStatus from "./clientStatus";
|
||||||
import {Job} from "./job";
|
import {Job} from "./job";
|
||||||
import Logger from "./logger";
|
import Logger from "./logger";
|
||||||
import ClientStatus from "./clientStatus";
|
import PersistentPromise from "./PersistentPromise";
|
||||||
import EventEmitter from "events";
|
import {SmppSession} from "./smppSession";
|
||||||
|
|
||||||
|
const smpp = require("smpp");
|
||||||
|
|
||||||
|
const AUTO_ENQUIRE_LINK_PERIOD: number = Number(process.env.AUTO_ENQUIRE_LINK_PERIOD) || 500;
|
||||||
|
|
||||||
export class Client implements SmppSession {
|
export class Client implements SmppSession {
|
||||||
set username(value: string) {
|
|
||||||
this._username = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
set password(value: string) {
|
|
||||||
this._password = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
set url(value: string) {
|
|
||||||
this._url = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
set status(value: ClientStatus) {
|
|
||||||
this._status = value;
|
|
||||||
this.eventEmitter.emit(Client.ClientEvents.STATUS_CHANGED, this._status);
|
|
||||||
this.eventEmitter.emit(Client.ClientEvents.STATE_CHANGED, this.serialize());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ClientEvents = {
|
public static ClientEvents = {
|
||||||
STATUS_CHANGED: "STATUS_CHANGED",
|
STATUS_CHANGED: "STATUS_CHANGED", STATE_CHANGED: "STATE_CHANGED", ANY_PDU: "ANY_PDU",
|
||||||
STATE_CHANGED: "STATE_CHANGED",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly eventEmitter: EventEmitter;
|
|
||||||
private readonly logger: object;
|
|
||||||
private readonly id: number;
|
|
||||||
private _username: string;
|
|
||||||
private _password: string;
|
|
||||||
private _url: string;
|
|
||||||
private session?: object;
|
|
||||||
private _status: ClientStatus = ClientStatus.NOT_CONNECTED;
|
|
||||||
|
|
||||||
defaultSingleJob?: Job;
|
defaultSingleJob?: Job;
|
||||||
defaultMultipleJob?: Job;
|
defaultMultipleJob?: Job;
|
||||||
|
private readonly eventEmitter: EventEmitter;
|
||||||
private promises: any = {
|
private readonly logger: Logger;
|
||||||
connect: null,
|
private readonly id: number;
|
||||||
close: null,
|
private session?: any;
|
||||||
bind: null
|
private connectPromise: PersistentPromise | null = null;
|
||||||
}
|
private bindPromise: PersistentPromise | null = null;
|
||||||
|
|
||||||
constructor(id: number, url: string, username: string, password: string) {
|
constructor(id: number, url: string, username: string, password: string) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
@@ -59,6 +35,32 @@ export class Client implements SmppSession {
|
|||||||
this.initialize();
|
this.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _username: string;
|
||||||
|
|
||||||
|
set username(value: string) {
|
||||||
|
this._username = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _password: string;
|
||||||
|
|
||||||
|
set password(value: string) {
|
||||||
|
this._password = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _url: string;
|
||||||
|
|
||||||
|
set url(value: string) {
|
||||||
|
this._url = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _status: ClientStatus = ClientStatus.NOT_CONNECTED;
|
||||||
|
|
||||||
|
set status(value: ClientStatus) {
|
||||||
|
this._status = value;
|
||||||
|
this.eventEmitter.emit(Client.ClientEvents.STATUS_CHANGED, this._status);
|
||||||
|
this.eventEmitter.emit(Client.ClientEvents.STATE_CHANGED, this.serialize());
|
||||||
|
}
|
||||||
|
|
||||||
setStatus(status: ClientStatus): void {
|
setStatus(status: ClientStatus): void {
|
||||||
this._status = status;
|
this._status = status;
|
||||||
this.eventEmitter.emit("status", status);
|
this.eventEmitter.emit("status", status);
|
||||||
@@ -68,32 +70,47 @@ export class Client implements SmppSession {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(): Promise<void> {
|
connect(): PersistentPromise {
|
||||||
this.promises.connect = new Promise((resolve, reject) => {
|
this.connectPromise = new PersistentPromise((resolve, reject) => {
|
||||||
if (this._status !== ClientStatus.NOT_CONNECTED) {
|
if (this._status !== ClientStatus.NOT_CONNECTED) {
|
||||||
this.logger.log1("Client already connected");
|
let errorString = `Client already connected`;
|
||||||
reject("Client already connected");
|
this.logger.log1(errorString);
|
||||||
|
reject(errorString);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.log1(`Client connecting to ${this._url}`);
|
this.logger.log1(`Client connecting to ${this._url}`);
|
||||||
this.setStatus(ClientStatus.CONNECTING);
|
this.setStatus(ClientStatus.CONNECTING);
|
||||||
try {
|
try {
|
||||||
this.session = smpp.connect({
|
this.connectSession();
|
||||||
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) {
|
} catch (e) {
|
||||||
this.logger.log1("Client connection failed to " + this._url);
|
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("Client connection failed to " + this._url);
|
|
||||||
|
reject(errorString);
|
||||||
}
|
}
|
||||||
this.connectingPromise.resolve = resolve;
|
|
||||||
this.connectingPromise.reject = reject;
|
|
||||||
});
|
});
|
||||||
return this.connectingPromise.promise;
|
return this.connectPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
bind(): PersistentPromise {
|
||||||
|
this.bindPromise = new PersistentPromise((resolve, reject) => {
|
||||||
|
if (!this.fieldsAreOk()) {
|
||||||
|
reject();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.session.bind_transceiver({
|
||||||
|
system_id: this.username, password: this.password,
|
||||||
|
}, this.eventBindReply.bind(this));
|
||||||
|
});
|
||||||
|
return this.bindPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
connectAndBind(): Promise<void> {
|
||||||
|
return this.connect().then(this.bind.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize(): string {
|
serialize(): string {
|
||||||
@@ -104,15 +121,95 @@ export class Client implements SmppSession {
|
|||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMultiple(Job: object): Promise<object> {
|
sendPdu(pdu: object): Promise<object> {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
|
||||||
sendPdu(pdu: object): Promise<object> {
|
sendMultiple(Job: object): Promise<object> {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
|
||||||
sendSingle(Job: object): Promise<object> {
|
sendSingle(Job: object): Promise<object> {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private connectSession(): void {
|
||||||
|
if (!this.fieldsAreOk()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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,
|
||||||
|
}, this.eventSessionConnected.bind(this));
|
||||||
|
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 {
|
||||||
|
this.logger.log1(`Client-${this.id} connected to ${this._url}`);
|
||||||
|
this.setStatus(ClientStatus.CONNECTED);
|
||||||
|
if (this.connectPromise) {
|
||||||
|
this.connectPromise.resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private eventAnyPdu(pdu: any): void {
|
||||||
|
this.logger.log6(`Client-${this.id} received PDU: ${JSON.stringify(pdu)}`);
|
||||||
|
this.eventEmitter.emit(Client.ClientEvents.ANY_PDU, pdu);
|
||||||
|
}
|
||||||
|
|
||||||
|
private eventSessionError(): void {
|
||||||
|
this.logger.log1(`Client-${this.id} error on ${this._url}`);
|
||||||
|
this.setStatus(ClientStatus.NOT_CONNECTED);
|
||||||
|
this.rejectPromises();
|
||||||
|
}
|
||||||
|
|
||||||
|
private eventSessionClose(): void {
|
||||||
|
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.setStatus(ClientStatus.BOUND);
|
||||||
|
if (this.bindPromise) {
|
||||||
|
this.bindPromise.resolve();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.logger.log1(`Client-${this.id} bind failed to ${this.url}`);
|
||||||
|
this.setStatus(ClientStatus.CONNECTED);
|
||||||
|
if (this.bindPromise) {
|
||||||
|
this.bindPromise.reject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private rejectPromises(): void {
|
||||||
|
if (this.connectPromise) {
|
||||||
|
this.connectPromise.reject();
|
||||||
|
}
|
||||||
|
if (this.bindPromise) {
|
||||||
|
this.bindPromise.reject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fieldsAreOk() {
|
||||||
|
if (!this._url) {
|
||||||
|
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`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!this._password) {
|
||||||
|
this.logger.log1(`Client-${this.id} has no password set`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -60,6 +60,9 @@ let logger = new Logger("main");
|
|||||||
import {Client} from "./client";
|
import {Client} from "./client";
|
||||||
|
|
||||||
let client: Client = new Client(0, "smpp://localhost:7001", "test", "test");
|
let client: Client = new Client(0, "smpp://localhost:7001", "test", "test");
|
||||||
|
client.connectAndBind().then(() => {
|
||||||
|
console.log("POGGIES");
|
||||||
|
});
|
||||||
|
|
||||||
// class ClientSession {
|
// class ClientSession {
|
||||||
// // TODO: Enable requesting DRs
|
// // TODO: Enable requesting DRs
|
||||||
|
Reference in New Issue
Block a user