Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Add UI server factory with server class abstraction
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed May 13, 2022
1 parent 6627109 commit fe94fce
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 22 deletions.
8 changes: 5 additions & 3 deletions src/charging-station/Bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
ChargingStationWorkerMessageEvents,
} from '../types/ChargingStationWorker';

import { AbstractUIServer } from './ui-server/AbstractUIServer';
import { ApplicationProtocol } from '../types/UIProtocol';
import Configuration from '../utils/Configuration';
import { StationTemplateUrl } from '../types/ConfigurationData';
import Statistics from '../types/Statistics';
import { Storage } from '../performance/storage/Storage';
import { StorageFactory } from '../performance/storage/StorageFactory';
import UIServerFactory from './ui-server/UIServerFactory';
import { UIServiceUtils } from './ui-server/ui-services/UIServiceUtils';
import UIWebSocketServer from './ui-server/UIWebSocketServer';
import Utils from '../utils/Utils';
import WorkerAbstract from '../worker/WorkerAbstract';
import WorkerFactory from '../worker/WorkerFactory';
Expand All @@ -24,7 +26,7 @@ import { version } from '../../package.json';
export default class Bootstrap {
private static instance: Bootstrap | null = null;
private workerImplementation: WorkerAbstract<ChargingStationWorkerData> | null = null;
private readonly uiServer!: UIWebSocketServer;
private readonly uiServer!: AbstractUIServer;
private readonly storage!: Storage;
private numberOfChargingStations: number;
private readonly version: string = version;
Expand All @@ -40,7 +42,7 @@ export default class Bootstrap {
);
this.initWorkerImplementation();
Configuration.getUIServer().enabled &&
(this.uiServer = new UIWebSocketServer({
(this.uiServer = UIServerFactory.getUIServerImplementation(ApplicationProtocol.WS, {
...Configuration.getUIServer().options,
handleProtocols: UIServiceUtils.handleProtocols,
}));
Expand Down
20 changes: 20 additions & 0 deletions src/charging-station/ui-server/AbstractUIServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import AbstractUIService from './ui-services/AbstractUIService';
import { Server as HttpServer } from 'http';
import { ProtocolVersion } from '../../types/UIProtocol';
import { Server as WSServer } from 'ws';

export abstract class AbstractUIServer {
public readonly chargingStations: Set<string>;

This comment has been minimized.

Copy link
@HazbinFaulted

HazbinFaulted May 13, 2022

Contributor

why is that in abstractUIServer ? shouldn't this class only be concerned with server in general ? (well i would also question it's presence in UIWebSocketServer, as it should also only be concerned with server although more specific)

This comment has been minimized.

Copy link
@jerome-benoit

jerome-benoit via email May 13, 2022

Owner
protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
protected uiServer: WSServer | HttpServer;

public constructor() {
this.chargingStations = new Set<string>();
this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
}

public abstract start(): void;
public abstract stop(): void;
public abstract sendResponse(message: string): void;
public abstract logPrefix(): string;
}
24 changes: 24 additions & 0 deletions src/charging-station/ui-server/UIServerFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AbstractUIServer } from './AbstractUIServer';
import { ApplicationProtocol } from '../../types/UIProtocol';
import Configuration from '../../utils/Configuration';
import { ServerOptions } from '../../types/ConfigurationData';
import UIWebSocketServer from './UIWebSocketServer';

export default class UIServerFactory {
private constructor() {
// This is intentional
}

public static getUIServerImplementation(
applicationProtocol: ApplicationProtocol,
options?: ServerOptions,
callback?: () => void
): AbstractUIServer | null {
switch (applicationProtocol) {
case ApplicationProtocol.WS:
return new UIWebSocketServer(options ?? Configuration.getUIServer().options, callback);
default:
return null;
}
}
}
19 changes: 7 additions & 12 deletions src/charging-station/ui-server/UIWebSocketServer.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
import WebSocket, { OPEN, Server } from 'ws';

import AbstractUIService from './ui-services/AbstractUIService';
import { AbstractUIServer } from './AbstractUIServer';
import Configuration from '../../utils/Configuration';
import { IncomingMessage } from 'http';
import { ServerOptions } from '../../types/ConfigurationData';
import UIServiceFactory from './ui-services/UIServiceFactory';
import Utils from '../../utils/Utils';
import logger from '../../utils/Logger';

export default class UIWebSocketServer extends Server {
public readonly chargingStations: Set<string>;
private readonly uiServices: Map<ProtocolVersion, AbstractUIService>;

export default class UIWebSocketServer extends AbstractUIServer {
public constructor(options?: ServerOptions, callback?: () => void) {
// Create the WebSocket server
super(options ?? Configuration.getUIServer().options, callback);
this.chargingStations = new Set<string>();
this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
super();
this.uiServer = new Server(options ?? Configuration.getUIServer().options, callback);
}

public start(): void {
this.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
this.uiServer.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
const protocolIndex = socket.protocol.indexOf(Protocol.UI);
const version = socket.protocol.substring(
protocolIndex + Protocol.UI.length
Expand All @@ -45,7 +40,7 @@ export default class UIWebSocketServer extends Server {
}

public stop(): void {
this.close();
this.uiServer.close();
}

public sendResponse(message: string): void {
Expand All @@ -57,7 +52,7 @@ export default class UIWebSocketServer extends Server {
}

private broadcastToClients(message: string): void {
for (const client of this.clients) {
for (const client of (this.uiServer as Server).clients) {
if (client?.readyState === OPEN) {
client.send(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import {
ProtocolRequestHandler,
} from '../../../types/UIProtocol';

import { AbstractUIServer } from '../AbstractUIServer';
import BaseError from '../../../exception/BaseError';
import { JsonType } from '../../../types/JsonType';
import { RawData } from 'ws';
import UIWebSocketServer from '../UIWebSocketServer';
import Utils from '../../../utils/Utils';
import logger from '../../../utils/Logger';

export default abstract class AbstractUIService {
protected readonly uiServer: UIWebSocketServer;
protected readonly uiServer: AbstractUIServer;
protected readonly messageHandlers: Map<ProtocolCommand, ProtocolRequestHandler>;

constructor(uiServer: UIWebSocketServer) {
constructor(uiServer: AbstractUIServer) {
this.uiServer = uiServer;
this.messageHandlers = new Map<ProtocolCommand, ProtocolRequestHandler>([
[ProtocolCommand.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)],
Expand Down
4 changes: 2 additions & 2 deletions src/charging-station/ui-server/ui-services/UIService001.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ProtocolCommand, ProtocolRequestHandler } from '../../../types/UIProtocol';

import { AbstractUIServer } from '../AbstractUIServer';
import AbstractUIService from './AbstractUIService';
import { JsonType } from '../../../types/JsonType';
import UIWebSocketServer from '../UIWebSocketServer';

export default class UIService001 extends AbstractUIService {
constructor(uiServer: UIWebSocketServer) {
constructor(uiServer: AbstractUIServer) {
super(uiServer);
this.messageHandlers.set(
ProtocolCommand.START_TRANSACTION,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AbstractUIServer } from '../AbstractUIServer';
import AbstractUIService from './AbstractUIService';
import { ProtocolVersion } from '../../../types/UIProtocol';
import UIService001 from './UIService001';
import UIWebSocketServer from '../UIWebSocketServer';

export default class UIServiceFactory {
private constructor() {
Expand All @@ -10,7 +10,7 @@ export default class UIServiceFactory {

public static getUIServiceImplementation(
version: ProtocolVersion,
uiServer: UIWebSocketServer
uiServer: AbstractUIServer
): AbstractUIService | null {
switch (version) {
case ProtocolVersion['0.0.1']:
Expand Down

0 comments on commit fe94fce

Please sign in to comment.