Code polish

This commit is contained in:
David Majdandžić
2023-03-29 17:39:06 +02:00
parent b8347d6fa6
commit d683ab870b
9 changed files with 28 additions and 54 deletions

View File

@@ -1,5 +1,4 @@
import EventEmitter from "events"; import EventEmitter from "events";
import * as repl from "repl";
import {Job} from "../Job/Job"; import {Job} from "../Job/Job";
import {JobEvents} from "../Job/JobEvents"; import {JobEvents} from "../Job/JobEvents";
import Logger from "../Logger"; import Logger from "../Logger";

View File

@@ -1,6 +1,4 @@
import EventEmitter from "events"; import EventEmitter from "events";
import fs from "fs";
import {Job} from "../Job/Job";
import Logger from "../Logger"; import Logger from "../Logger";
import {SessionManager} from "../SessionManager"; import {SessionManager} from "../SessionManager";
import {SmppSession} from "../SmppSession"; 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"; const CENTER_SESSIONS_FILE: string = process.env.CENTER_SESSIONS_FILE || "center_sessions.json";
export class CenterSessionManager extends SessionManager { 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 StorageFile: string = CENTER_SESSIONS_FILE
ManagedSessionClass: any = Center; ManagedSessionClass: any = Center;
sessionId: number = 0; 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)); // 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> { createSession(port: number, username: string, password: string): Promise<SmppSession> {
return new Promise<SmppSession>((resolve, reject) => { return new Promise<SmppSession>((resolve, reject) => {
this.logger.log1(`Creating session with port ${port}`); this.logger.log1(`Creating session with port ${port}`);

View File

@@ -9,6 +9,7 @@ export class DebugProcessor implements CenterPDUProcessor {
constructor() { constructor() {
this.logger = new Logger('DebugProcessor'); this.logger = new Logger('DebugProcessor');
} }
processPdu(session: any, pdu: any): Promise<any> { processPdu(session: any, pdu: any): Promise<any> {
return new Promise<any>((resolve, reject) => { return new Promise<any>((resolve, reject) => {
let promises = []; let promises = [];

View File

@@ -1,5 +1,4 @@
import EventEmitter from "events"; import EventEmitter from "events";
import {Center} from "../Center/Center";
import Logger from "../Logger"; import Logger from "../Logger";
import {SessionManager} from "../SessionManager"; import {SessionManager} from "../SessionManager";
import {SmppSession} from "../SmppSession"; 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"; const CLIENT_SESSIONS_FILE: string = process.env.CLIENT_SESSIONS_FILE || "client_sessions.json";
export default class ClientSessionManager extends SessionManager { 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; StorageFile: string = CLIENT_SESSIONS_FILE;
ManagedSessionClass: any = Client; ManagedSessionClass: any = Client;
sessionId: number = 0; 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)); // super.eventEmitter.on(super.SESSION_ADDED_EVENT, (session: SmppSession) => this.eventEmitter.emit(this.SESSION_ADDED_EVENT, session));
} }
// setup(): void { comparatorFn: (arg: any, session: SmppSession) => boolean = (arg: any, session: SmppSession) => (session as Client).getUrl() === arg;
// 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));
// }
} }

View File

@@ -9,6 +9,12 @@ const LOG_LEVEL: number = process.env.LOG_LEVEL || 6;
const LOG_FILE: string = process.env.LOG_FILE || ""; const LOG_FILE: string = process.env.LOG_FILE || "";
export default class Logger { 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 clazz: string;
private readonly logLevel: number; private readonly logLevel: number;
private readonly logFile: string; private readonly logFile: string;
@@ -62,11 +68,4 @@ export default class Logger {
this.logFileWriteStream.write(out + "\n"); 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);
} }

View File

@@ -1,11 +1,12 @@
import EventEmitter from "events"; import EventEmitter from "events";
import fs from "fs"; import fs from "fs";
import {Client} from "./Client/Client";
import {Job} from "./Job/Job"; import {Job} from "./Job/Job";
import Logger from "./Logger"; import Logger from "./Logger";
import {SmppSession} from "./SmppSession"; import {SmppSession} from "./SmppSession";
export abstract class SessionManager { 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 sessions: SmppSession[];
abstract sessionId: number; abstract sessionId: number;
abstract comparatorFn: (arg: any, session: SmppSession) => boolean; 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> { createSession(arg: any, username: string, password: string): Promise<SmppSession> {
return new Promise<SmppSession>((resolve, reject) => { return new Promise<SmppSession>((resolve, reject) => {
this.logger.log1(`Creating session of type ${this.ManagedSessionClass.name} with arg ${arg}`); 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}...`); this.logger.log1(`Saving centers to ${this.StorageFile}...`);
fs.writeFileSync(this.StorageFile, JSON.stringify(this.serialize(), null, 4)); fs.writeFileSync(this.StorageFile, JSON.stringify(this.serialize(), null, 4));
} }
getExisting(arg: any): Promise<SmppSession> { getExisting(arg: any): Promise<SmppSession> {
return new Promise<SmppSession>((resolve, reject) => { return new Promise<SmppSession>((resolve, reject) => {
this.logger.log1(`Looking for session with url ${arg}...`); this.logger.log1(`Looking for session with arg ${arg}...`);
// let session: SmppSession | undefined = this.sessions.find((s: Client) => s.getUrl() === arg);
let session: SmppSession | undefined = this.sessions.find(this.comparatorFn.bind(this, arg)); let session: SmppSession | undefined = this.sessions.find(this.comparatorFn.bind(this, arg));
if (session) { if (session) {
this.logger.log1(`Found session with url ${arg}`); this.logger.log1(`Found session with arg ${arg}`);
resolve(session); resolve(session);
} else { } else {
this.logger.log1(`Session with url ${arg} not found`); this.logger.log1(`Session with arg ${arg} not found`);
reject(`Session with url ${arg} not found`); reject(`Session with arg ${arg} not found`);
} }
}); });
} }

View File

@@ -26,7 +26,7 @@ const MESSAGE_SEND_UPDATE_DELAY: number = Number(process.env.MESSAGE_SEND_UPDATE
let logger = new Logger("main"); let logger = new Logger("main");
let clientManager: ClientSessionManager = new ClientSessionManager(); let clientManager: ClientSessionManager = new ClientSessionManager();
// clientManager.setup(); clientManager.setup();
// let wss: WSServer = new WSServer([clientManager]); // let wss: WSServer = new WSServer([clientManager]);