Do more refactoring
This commit is contained in:
@@ -9,6 +9,7 @@ import {Center} from "./Center";
|
||||
const CENTER_SESSIONS_FILE: string = process.env.CENTER_SESSIONS_FILE || "center_sessions.json";
|
||||
|
||||
export class CenterSessionManager extends SessionManager {
|
||||
ManagedSessionClass: any = Center;
|
||||
sessionId: number = 0;
|
||||
sessions: Center[] = [];
|
||||
identifier: string = "center";
|
||||
@@ -46,13 +47,13 @@ export class CenterSessionManager extends SessionManager {
|
||||
createSession(port: number, username: string, password: string): Promise<SmppSession> {
|
||||
return new Promise<SmppSession>((resolve, reject) => {
|
||||
this.logger.log1(`Creating session with port ${port}`);
|
||||
this.getSessionByPort(port).then(s => {
|
||||
this.getExisting(port).then(s => {
|
||||
resolve(s);
|
||||
}, err => {
|
||||
});
|
||||
this.verifyPort(port, reject);
|
||||
this.verifyUsername(username, reject);
|
||||
this.verifyPassword(password, reject);
|
||||
// this.verifyUsername(username, reject);
|
||||
// this.verifyPassword(password, reject);
|
||||
|
||||
let client = new Center(this.sessionId++, port, username, password);
|
||||
this.addSession(client).then(() => {
|
||||
@@ -61,16 +62,16 @@ export class CenterSessionManager extends SessionManager {
|
||||
});
|
||||
}
|
||||
|
||||
getSessionByPort(port: number): Promise<SmppSession> {
|
||||
getExisting(arg: any): Promise<SmppSession> {
|
||||
return new Promise<SmppSession>((resolve, reject) => {
|
||||
this.logger.log1(`Looking for session with port ${port}...`);
|
||||
let session: SmppSession | undefined = this.sessions.find((s: Center) => s.getPort() === port);
|
||||
this.logger.log1(`Looking for session with port ${arg}...`);
|
||||
let session: SmppSession | undefined = this.sessions.find((s: Center) => s.getPort() === arg);
|
||||
if (session) {
|
||||
this.logger.log1(`Found session with port ${port}`);
|
||||
this.logger.log1(`Found session with port ${arg}`);
|
||||
resolve(session);
|
||||
} else {
|
||||
this.logger.log1(`Session with port ${port} not found`);
|
||||
reject(`Session with port ${port} not found`);
|
||||
this.logger.log1(`Session with port ${arg} not found`);
|
||||
reject(`Session with port ${arg} not found`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -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,6 +7,7 @@ import {Client} from "./Client";
|
||||
const CLIENT_SESSIONS_FILE: string = process.env.CLIENT_SESSIONS_FILE || "client_sessions.json";
|
||||
|
||||
export default class ClientSessionManager extends SessionManager {
|
||||
ManagedSessionClass: any = Client;
|
||||
sessionId: number = 0;
|
||||
sessions: Client[] = [];
|
||||
// Identifier is used in websockets to identify the type of session this manager manages
|
||||
@@ -21,66 +20,49 @@ export default class ClientSessionManager extends SessionManager {
|
||||
// super.eventEmitter.on(super.SESSION_ADDED_EVENT, (session: SmppSession) => this.eventEmitter.emit(this.SESSION_ADDED_EVENT, session));
|
||||
}
|
||||
|
||||
createSession(url: string, username: string, password: string): Promise<SmppSession> {
|
||||
// TODO: Move this to superclass too
|
||||
getExisting(arg: any): Promise<SmppSession> {
|
||||
return new Promise<SmppSession>((resolve, reject) => {
|
||||
this.logger.log1(`Creating session with url ${url}`);
|
||||
this.getSessionByUrl(url).then(s => {
|
||||
resolve(s);
|
||||
}, err => {
|
||||
});
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getSessionByUrl(url: string): Promise<SmppSession> {
|
||||
return new Promise<SmppSession>((resolve, reject) => {
|
||||
this.logger.log1(`Looking for session with url ${url}...`);
|
||||
let session: SmppSession | undefined = this.sessions.find((s: Client) => s.getUrl() === url);
|
||||
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 ${url}`);
|
||||
this.logger.log1(`Found session with url ${arg}`);
|
||||
resolve(session);
|
||||
} else {
|
||||
this.logger.log1(`Session with url ${url} not found`);
|
||||
reject(`Session with url ${url} not found`);
|
||||
this.logger.log1(`Session with url ${arg} not found`);
|
||||
reject(`Session with url ${arg} not found`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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));
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
setup(): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
// 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));
|
||||
// }
|
||||
}
|
@@ -5,7 +5,9 @@ import {SmppSession} from "./SmppSession";
|
||||
export abstract class SessionManager {
|
||||
abstract sessions: SmppSession[];
|
||||
abstract sessionId: number;
|
||||
abstract identifier: string;
|
||||
readonly abstract identifier: string;
|
||||
readonly abstract ManagedSessionClass: any;
|
||||
|
||||
readonly SESSION_ADDED_EVENT: string = "SESSION ADDED";
|
||||
readonly logger: Logger = new Logger("SessionManager");
|
||||
readonly eventEmitter: EventEmitter = new EventEmitter();
|
||||
@@ -58,17 +60,27 @@ export abstract class SessionManager {
|
||||
|
||||
// TODO: Maybe find a way to include write and read to file here too (instead of child classes)
|
||||
|
||||
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);
|
||||
}
|
||||
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}`);
|
||||
this.getExisting(arg).then((s: SmppSession) => {
|
||||
resolve(s);
|
||||
}, err => {
|
||||
});
|
||||
this.verifyField(arg, reject);
|
||||
this.verifyField(username, reject);
|
||||
this.verifyField(password, reject);
|
||||
|
||||
let session = new this.ManagedSessionClass(this.sessionId++, arg, username, password);
|
||||
this.addSession(session).then(() => {
|
||||
resolve(session);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
verifyPassword(password: string, reject: (reason?: any) => void) {
|
||||
if (!password) {
|
||||
let error = `Request to make a new client failed because of missing password.`;
|
||||
verifyField(field: string, reject: (reason?: any) => void) {
|
||||
if (!field) {
|
||||
let error = `Request to make a new client failed because of missing ${field}.`;
|
||||
this.logger.log1(error);
|
||||
reject(error);
|
||||
}
|
||||
@@ -77,4 +89,6 @@ export abstract class SessionManager {
|
||||
abstract cleanup(): void;
|
||||
|
||||
abstract setup(): void;
|
||||
|
||||
abstract getExisting(arg: any): Promise<SmppSession>;
|
||||
}
|
@@ -26,13 +26,13 @@ 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]);
|
||||
|
||||
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;
|
||||
let client: Client = await clientManager.createSession("smpp://localhost:7000", "test", "test") as Client;
|
||||
// let client: Client = await clientManager.getSession(0) as Client;
|
||||
|
||||
// // client.sendMultipleDefault();
|
||||
//
|
||||
|
Reference in New Issue
Block a user