Do more refactoring
This commit is contained in:
@@ -9,6 +9,8 @@ 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;
|
||||
sessions: Center[] = [];
|
||||
@@ -21,29 +23,6 @@ export class CenterSessionManager 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 ${CENTER_SESSIONS_FILE}`)
|
||||
let sessions: Buffer = fs.readFileSync(CENTER_SESSIONS_FILE);
|
||||
let loadedSessions: any[] = JSON.parse(String(sessions));
|
||||
this.logger.log1(`Loaded ${loadedSessions.length} clients from ${CENTER_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 ${CENTER_SESSIONS_FILE}: ${e}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
cleanup(): void {
|
||||
this.logger.log1(`Saving clients to ${CENTER_SESSIONS_FILE}...`);
|
||||
fs.writeFileSync(CENTER_SESSIONS_FILE, JSON.stringify(this.serialize(), null, 4));
|
||||
}
|
||||
|
||||
createSession(port: number, username: string, password: string): Promise<SmppSession> {
|
||||
return new Promise<SmppSession>((resolve, reject) => {
|
||||
this.logger.log1(`Creating session with port ${port}`);
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import EventEmitter from "events";
|
||||
import {Center} from "../Center/Center";
|
||||
import Logger from "../Logger";
|
||||
import {SessionManager} from "../SessionManager";
|
||||
import {SmppSession} from "../SmppSession";
|
||||
@@ -7,6 +8,8 @@ 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;
|
||||
sessions: Client[] = [];
|
||||
@@ -20,29 +23,6 @@ export default class ClientSessionManager extends SessionManager {
|
||||
// super.eventEmitter.on(super.SESSION_ADDED_EVENT, (session: SmppSession) => this.eventEmitter.emit(this.SESSION_ADDED_EVENT, session));
|
||||
}
|
||||
|
||||
// TODO: Move this to superclass too
|
||||
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);
|
||||
if (session) {
|
||||
this.logger.log1(`Found session with url ${arg}`);
|
||||
resolve(session);
|
||||
} else {
|
||||
this.logger.log1(`Session with url ${arg} not found`);
|
||||
reject(`Session with url ${arg} not found`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
cleanup(): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
setup(): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
// setup(): void {
|
||||
// try {
|
||||
// this.logger.log1(`Loading clients from ${CLIENT_SESSIONS_FILE}`)
|
||||
|
@@ -1,12 +1,17 @@
|
||||
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 {
|
||||
abstract sessions: SmppSession[];
|
||||
abstract sessionId: number;
|
||||
abstract comparatorFn: (arg: any, session: SmppSession) => boolean;
|
||||
readonly abstract identifier: string;
|
||||
readonly abstract ManagedSessionClass: any;
|
||||
readonly abstract StorageFile: string;
|
||||
|
||||
readonly SESSION_ADDED_EVENT: string = "SESSION ADDED";
|
||||
readonly logger: Logger = new Logger("SessionManager");
|
||||
@@ -86,9 +91,40 @@ export abstract class SessionManager {
|
||||
}
|
||||
}
|
||||
|
||||
abstract cleanup(): void;
|
||||
setup(): void {
|
||||
try {
|
||||
this.logger.log1(`Loading ${this.ManagedSessionClass.name} from ${this.StorageFile}`)
|
||||
let sessions: Buffer = fs.readFileSync(this.StorageFile);
|
||||
let loadedSessions: any[] = JSON.parse(String(sessions));
|
||||
this.logger.log1(`Loaded ${loadedSessions.length} clients from ${this.StorageFile}`);
|
||||
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 centers from ${this.StorageFile}: ${e}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
abstract setup(): void;
|
||||
|
||||
abstract getExisting(arg: any): Promise<SmppSession>;
|
||||
cleanup(): void {
|
||||
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);
|
||||
let session: SmppSession | undefined = this.sessions.find(this.comparatorFn.bind(this, arg));
|
||||
if (session) {
|
||||
this.logger.log1(`Found session with url ${arg}`);
|
||||
resolve(session);
|
||||
} else {
|
||||
this.logger.log1(`Session with url ${arg} not found`);
|
||||
reject(`Session with url ${arg} not found`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user