Code polish
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import EventEmitter from "events";
|
||||
import * as repl from "repl";
|
||||
import {Job} from "../Job/Job";
|
||||
import {JobEvents} from "../Job/JobEvents";
|
||||
import Logger from "../Logger";
|
||||
|
@@ -1,6 +1,4 @@
|
||||
import EventEmitter from "events";
|
||||
import fs from "fs";
|
||||
import {Job} from "../Job/Job";
|
||||
import Logger from "../Logger";
|
||||
import {SessionManager} from "../SessionManager";
|
||||
import {SmppSession} from "../SmppSession";
|
||||
@@ -9,7 +7,6 @@ import {Center} from "./Center";
|
||||
const CENTER_SESSIONS_FILE: string = process.env.CENTER_SESSIONS_FILE || "center_sessions.json";
|
||||
|
||||
export class CenterSessionManager extends SessionManager {
|
||||
comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Center).getPort() === arg;
|
||||
StorageFile: string = CENTER_SESSIONS_FILE
|
||||
ManagedSessionClass: any = Center;
|
||||
sessionId: number = 0;
|
||||
@@ -23,6 +20,8 @@ export class CenterSessionManager extends SessionManager {
|
||||
// super.eventEmitter.on(super.SESSION_ADDED_EVENT, (session: SmppSession) => this.eventEmitter.emit(this.SESSION_ADDED_EVENT, session));
|
||||
}
|
||||
|
||||
comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Center).getPort() === arg;
|
||||
|
||||
createSession(port: number, username: string, password: string): Promise<SmppSession> {
|
||||
return new Promise<SmppSession>((resolve, reject) => {
|
||||
this.logger.log1(`Creating session with port ${port}`);
|
||||
|
@@ -9,6 +9,7 @@ export class DebugProcessor implements CenterPDUProcessor {
|
||||
constructor() {
|
||||
this.logger = new Logger('DebugProcessor');
|
||||
}
|
||||
|
||||
processPdu(session: any, pdu: any): Promise<any> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
let promises = [];
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import EventEmitter from "events";
|
||||
import {Center} from "../Center/Center";
|
||||
import Logger from "../Logger";
|
||||
import {SessionManager} from "../SessionManager";
|
||||
import {SmppSession} from "../SmppSession";
|
||||
@@ -8,7 +7,6 @@ import {Client} from "./Client";
|
||||
const CLIENT_SESSIONS_FILE: string = process.env.CLIENT_SESSIONS_FILE || "client_sessions.json";
|
||||
|
||||
export default class ClientSessionManager extends SessionManager {
|
||||
comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Client).getUrl() === arg;
|
||||
StorageFile: string = CLIENT_SESSIONS_FILE;
|
||||
ManagedSessionClass: any = Client;
|
||||
sessionId: number = 0;
|
||||
@@ -23,26 +21,5 @@ export default class ClientSessionManager extends SessionManager {
|
||||
// super.eventEmitter.on(super.SESSION_ADDED_EVENT, (session: SmppSession) => this.eventEmitter.emit(this.SESSION_ADDED_EVENT, session));
|
||||
}
|
||||
|
||||
// setup(): void {
|
||||
// try {
|
||||
// this.logger.log1(`Loading clients from ${CLIENT_SESSIONS_FILE}`)
|
||||
// let sessions: Buffer = fs.readFileSync(CLIENT_SESSIONS_FILE);
|
||||
// let loadedSessions: any[] = JSON.parse(String(sessions));
|
||||
// this.logger.log1(`Loaded ${loadedSessions.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}`);
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// cleanup(): void {
|
||||
// this.logger.log1(`Saving clients to ${CLIENT_SESSIONS_FILE}...`);
|
||||
// fs.writeFileSync(CLIENT_SESSIONS_FILE, JSON.stringify(this.serialize(), null, 4));
|
||||
// }
|
||||
comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Client).getUrl() === arg;
|
||||
}
|
@@ -9,6 +9,12 @@ const LOG_LEVEL: number = process.env.LOG_LEVEL || 6;
|
||||
const LOG_FILE: string = process.env.LOG_FILE || "";
|
||||
|
||||
export default class Logger {
|
||||
log1 = this.log.bind(this, 1);
|
||||
log2 = this.log.bind(this, 2);
|
||||
log3 = this.log.bind(this, 3);
|
||||
log4 = this.log.bind(this, 4);
|
||||
log5 = this.log.bind(this, 5);
|
||||
log6 = this.log.bind(this, 6);
|
||||
private clazz: string;
|
||||
private readonly logLevel: number;
|
||||
private readonly logFile: string;
|
||||
@@ -62,11 +68,4 @@ export default class Logger {
|
||||
this.logFileWriteStream.write(out + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
log1 = this.log.bind(this, 1);
|
||||
log2 = this.log.bind(this, 2);
|
||||
log3 = this.log.bind(this, 3);
|
||||
log4 = this.log.bind(this, 4);
|
||||
log5 = this.log.bind(this, 5);
|
||||
log6 = this.log.bind(this, 6);
|
||||
}
|
@@ -1,11 +1,12 @@
|
||||
import EventEmitter from "events";
|
||||
import fs from "fs";
|
||||
import {Client} from "./Client/Client";
|
||||
import {Job} from "./Job/Job";
|
||||
import Logger from "./Logger";
|
||||
import {SmppSession} from "./SmppSession";
|
||||
|
||||
export abstract class SessionManager {
|
||||
// I could've done this by passing these abstract properties to the constructor, but I wanted to have the possibility
|
||||
// of implementing additional methods
|
||||
abstract sessions: SmppSession[];
|
||||
abstract sessionId: number;
|
||||
abstract comparatorFn: (arg: any, session: SmppSession) => boolean;
|
||||
@@ -63,8 +64,6 @@ export abstract class SessionManager {
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Maybe find a way to include write and read to file here too (instead of child classes)
|
||||
|
||||
createSession(arg: any, username: string, password: string): Promise<SmppSession> {
|
||||
return new Promise<SmppSession>((resolve, reject) => {
|
||||
this.logger.log1(`Creating session of type ${this.ManagedSessionClass.name} with arg ${arg}`);
|
||||
@@ -113,17 +112,17 @@ export abstract class SessionManager {
|
||||
this.logger.log1(`Saving centers to ${this.StorageFile}...`);
|
||||
fs.writeFileSync(this.StorageFile, JSON.stringify(this.serialize(), null, 4));
|
||||
}
|
||||
|
||||
getExisting(arg: any): Promise<SmppSession> {
|
||||
return new Promise<SmppSession>((resolve, reject) => {
|
||||
this.logger.log1(`Looking for session with url ${arg}...`);
|
||||
// let session: SmppSession | undefined = this.sessions.find((s: Client) => s.getUrl() === arg);
|
||||
this.logger.log1(`Looking for session with arg ${arg}...`);
|
||||
let session: SmppSession | undefined = this.sessions.find(this.comparatorFn.bind(this, arg));
|
||||
if (session) {
|
||||
this.logger.log1(`Found session with url ${arg}`);
|
||||
this.logger.log1(`Found session with arg ${arg}`);
|
||||
resolve(session);
|
||||
} else {
|
||||
this.logger.log1(`Session with url ${arg} not found`);
|
||||
reject(`Session with url ${arg} not found`);
|
||||
this.logger.log1(`Session with arg ${arg} not found`);
|
||||
reject(`Session with arg ${arg} not found`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -26,7 +26,7 @@ const MESSAGE_SEND_UPDATE_DELAY: number = Number(process.env.MESSAGE_SEND_UPDATE
|
||||
let logger = new Logger("main");
|
||||
|
||||
let clientManager: ClientSessionManager = new ClientSessionManager();
|
||||
// clientManager.setup();
|
||||
clientManager.setup();
|
||||
|
||||
// let wss: WSServer = new WSServer([clientManager]);
|
||||
|
||||
|
Reference in New Issue
Block a user