|
@@ -1,62 +1,60 @@
|
|
|
-import { AdapterManager as AdapterManagerInterface, TransportService, TransportEvent, Transport } from "../interface/connector.interface"
|
|
|
-import { TransmitterAdapter } from './adapter.transmitter'
|
|
|
-import { ReceiverAdapter } from './adapter.receiver'
|
|
|
-import { v4 as uuidv4 } from 'uuid'
|
|
|
-import { Observable, Subject } from "rxjs"
|
|
|
+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 { RequestResponseAdapter } from "./adapter.request.response"
|
|
|
-=import { AdapterEvent } from "../interface/interface"
|
|
|
+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 implements AdapterManagerInterface {
|
|
|
+export class AdapterManager extends AdapterManagerBase {
|
|
|
private console: ConsoleLogger = new ConsoleLogger(`AdapterManager`, ['managers'])
|
|
|
- private transportServiceArray: TransportService[] = []
|
|
|
- private transportSet: TransportSet[] = []
|
|
|
- private adapterSet: AdapterSet[] = []
|
|
|
- private event!: Subject<TransportEvent>
|
|
|
|
|
|
- constructor(event: Subject<TransportEvent>, browserEnv?: boolean | undefined) {
|
|
|
- this.event = event
|
|
|
+ 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, event, browserEnv)
|
|
|
+ this.setUpTransportService(set, this.eventObj.transportEvent, browserEnv)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- subscribe(): Observable<AdapterEvent> {
|
|
|
- throw new Error(`Method not implemented`)
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
}
|
|
|
- /* This one change to subscribe since I want it to return an array of adapters instead of just a set of them, becauise there could
|
|
|
- mulitple adapters that the manager will have to work with */
|
|
|
- public getAdapter(clientId: string): AdapterSet | null {
|
|
|
- this.console.log({ message: `Instantiating an adapter set....` })
|
|
|
- let transportType: Transport = process.env.Transport as unknown as Transport // as default for now
|
|
|
- let adapterId: string = clientId
|
|
|
- let transportService: TransportService | undefined = this.transportServiceArray.find(obj => obj.getInfo() == transportType)
|
|
|
- if (transportService) {
|
|
|
- let transmitterAdapter: TransmitterAdapter = new TransmitterAdapter(adapterId, transportType, transportService)
|
|
|
- let receiverAdapter: ReceiverAdapter = new ReceiverAdapter(adapterId, transportType, transportService)
|
|
|
- let adapterSet: AdapterSet = {
|
|
|
- id: adapterId,
|
|
|
- dateCreated: new Date(),
|
|
|
- transmitterAdapter: transmitterAdapter,
|
|
|
- receiverAdapter: receiverAdapter,
|
|
|
- requestResponsAdapter: new RequestResponseAdapter(transmitterAdapter, receiverAdapter)
|
|
|
- }
|
|
|
- this.adapterSet.push(adapterSet)
|
|
|
|
|
|
- this.event.next({
|
|
|
+ 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',
|
|
|
- data: adapterId
|
|
|
- } as TransportEvent)
|
|
|
- return adapterSet
|
|
|
+ type: 'Adapter Event',
|
|
|
+ date: new Date(),
|
|
|
+ adapters: [transmitterAdapter, receiverAdapter]
|
|
|
+ } as AdapterEvent)
|
|
|
} else {
|
|
|
- return null
|
|
|
+ this.console.error({ message: `No ${transport} service is not properly instantiated....` })
|
|
|
+ throw new Error(`No ${transport} service is not properly instantiated....`)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -88,10 +86,10 @@ export class AdapterManager implements AdapterManagerInterface {
|
|
|
}
|
|
|
|
|
|
private instantiateTransportService(transportType: Transport, event: Subject<TransportEvent>): TransportService {
|
|
|
- if (transportType == Transport.Websocket) {
|
|
|
+ if (transportType === 'Websocket') {
|
|
|
return new WebsocketTransportService(event)
|
|
|
}
|
|
|
- else if (transportType == Transport.Http) {
|
|
|
+ else if (transportType === 'Http') {
|
|
|
return new HttpTransportService(event)
|
|
|
} else {
|
|
|
throw new Error(`No Transport Service Instantiated`)
|
|
@@ -110,8 +108,3 @@ export class AdapterManager implements AdapterManagerInterface {
|
|
|
}
|
|
|
|
|
|
|
|
|
-interface TransportSet {
|
|
|
- transport: Transport,
|
|
|
- port: number
|
|
|
-}
|
|
|
-
|