|
@@ -1,110 +0,0 @@
|
|
|
-import { filter, Observable, Observer, Subject } from "rxjs"
|
|
|
-import { WebsocketTransportService } from "../transport/websocket"
|
|
|
-import { HttpTransportService } from "../transport/http"
|
|
|
-import config from '../config/config.json';
|
|
|
-import { v4 as uuidv4 } from 'uuid'
|
|
|
-import ConsoleLogger from "../utils/log.utils"
|
|
|
-import { AdapterEvent, EventObject, ReceiverAdapterInterface, TransmitterAdapterInterface, Transport, TransportEvent, TransportService, TransportSet } from "../interface/interface"
|
|
|
-import { AdapterManagerBase } from "./adapter.manager.base"
|
|
|
-import { TransmitterAdapter } from "./adapter.transmitter";
|
|
|
-import { ReceiverAdapter } from "./adapter.receiver";
|
|
|
-
|
|
|
-/* Note: There will be a need to use the logic in place for socket utility. Especially for client identification
|
|
|
-Will think about that later. Because that mechanism needs to be made universal somehow. If let's say utilizing already
|
|
|
-existing transport, there are no logic in place to exchange information to identify connected clients. */
|
|
|
-
|
|
|
-export class AdapterManager extends AdapterManagerBase {
|
|
|
- private console: ConsoleLogger = new ConsoleLogger(`AdapterManager`, ['managers'])
|
|
|
-
|
|
|
- constructor(eventObj: EventObject, browserEnv?: boolean) {
|
|
|
- super()
|
|
|
- this.eventObj = eventObj
|
|
|
- this.console.log({ message: `Contructing self...` })
|
|
|
-
|
|
|
- this.sortTransportFromEnv(this.transportSet)
|
|
|
- // for now set up these transportEvent
|
|
|
- this.transportSet.forEach(set => {
|
|
|
- this.setUpTransportService(set, this.eventObj.transportEvent, browserEnv)
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- public subscribe(): Observable<AdapterEvent> {
|
|
|
- return new Observable((event: Observer<AdapterEvent>) => {
|
|
|
- this.eventObj.transportEvent.pipe(
|
|
|
- filter(event => event.event === `New Client`)
|
|
|
- ).subscribe({
|
|
|
- next: (event: TransportEvent) => {
|
|
|
- this.handleTransportEvent(event, this.eventObj.adapterEvent, event.transport)
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- private handleTransportEvent(event: TransportEvent, adapterEvent: Subject<AdapterEvent>, transport: Transport): void {
|
|
|
- let transportService: TransportService | undefined = this.transportServiceArray.find(obj => obj.getInfo() === transport)
|
|
|
- if (transportService) {
|
|
|
- let transmitterAdapter: TransmitterAdapterInterface = new TransmitterAdapter(event.data?.clientId, event.transport, transportService)
|
|
|
- let receiverAdapter: ReceiverAdapterInterface = new ReceiverAdapter(event.data?.clientId, event.transport, transportService)
|
|
|
- adapterEvent.next({
|
|
|
- id: uuidv4(),
|
|
|
- event: 'New Adapter',
|
|
|
- type: 'Adapter Event',
|
|
|
- date: new Date(),
|
|
|
- adapters: [transmitterAdapter, receiverAdapter]
|
|
|
- } as AdapterEvent)
|
|
|
- } else {
|
|
|
- this.console.error({ message: `No ${transport} service is not properly instantiated....` })
|
|
|
- throw new Error(`No ${transport} service is not properly instantiated....`)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Server to be set up as well as acquiring client information if needed. Like in the case for grpc and socket. Http not requ`ired.
|
|
|
- private setUpTransportService(transportSet: TransportSet, event: Subject<TransportEvent>, isClient?: boolean): void {
|
|
|
- try {
|
|
|
- let transportService: TransportService = this.instantiateTransportService(transportSet.transport, event)
|
|
|
- this.transportServiceArray.push(transportService)
|
|
|
- if (transportService instanceof WebsocketTransportService) {
|
|
|
- this.console.log({ message: `Just Double Checking... this is websocket` })
|
|
|
- if (isClient) {
|
|
|
- // please note this is subject to change depending on the UI environemnt. Angular has their own built in function to read json file based on Swopt-UI
|
|
|
- transportService.startClient(config.connection.transmitter)
|
|
|
- } else {
|
|
|
- transportService.startServer(transportSet.port);
|
|
|
- }
|
|
|
- } else if (transportService instanceof HttpTransportService) {
|
|
|
- this.console.log({ message: `Just Double Checking... this is http` })
|
|
|
- // Additional Http-specific setup if needed.
|
|
|
- if (isClient) {
|
|
|
- transportService.startClient(config.connection.transmitter)
|
|
|
- } else {
|
|
|
- transportService.startServer(transportSet.port)
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- this.console.error({ message: 'Fail to set transport. Error in setting up transport', details: error })
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private instantiateTransportService(transportType: Transport, event: Subject<TransportEvent>): TransportService {
|
|
|
- if (transportType === 'Websocket') {
|
|
|
- return new WebsocketTransportService(event)
|
|
|
- }
|
|
|
- else if (transportType === 'Http') {
|
|
|
- return new HttpTransportService(event)
|
|
|
- } else {
|
|
|
- throw new Error(`No Transport Service Instantiated`)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private sortTransportFromEnv(transportSet: TransportSet[]): void {
|
|
|
- let transportList: string[] = process.env.Transport?.split(',') || []
|
|
|
- let portList: number[] = (process.env.PORT?.split(',') || []).map(port => Number(port));
|
|
|
- transportList.forEach((transport, index) => {
|
|
|
- transportSet.push({ transport: transport, port: portList[index] } as unknown as TransportSet)
|
|
|
- })
|
|
|
- this.console.log({ message: 'TransportSetList', details: this.transportSet })
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|