|
@@ -1,46 +1,66 @@
|
|
|
-import { BehaviorSubject, Observable, Subject } from "rxjs";
|
|
|
+import { BehaviorSubject, distinctUntilChanged, filter, map, Observable, Subject } from "rxjs";
|
|
|
import dotenv from 'dotenv';
|
|
|
-import { AdapterInterface, ConnectionState, TransmissionRole, TransportType, TransportServiceInterface, AdapterProfile } from "../interface/interface";
|
|
|
+import { AdapterInterface, ConnectionState, TransmissionRole, TransportType, TransportServiceInterface, AdapterProfile, ClientObject } from "../interface/interface";
|
|
|
+import ConsoleLogger from "../utils/log.utils";
|
|
|
|
|
|
dotenv.config();
|
|
|
/* This transport manager will be instantiating the necessary transport to deal with tranmission and receiving from different receivers
|
|
|
So how?: */
|
|
|
export class AdapterBase implements AdapterInterface {
|
|
|
- protected AdapterProfile!: AdapterProfile
|
|
|
- // protected clientId!: string
|
|
|
- // protected transportService!: TransportServiceInterface
|
|
|
- // protected role!: TransmissionRole;
|
|
|
- // protected connectionState!: Subject<ConnectionState>
|
|
|
- // protected transport: TransportType;
|
|
|
-
|
|
|
- constructor() {
|
|
|
+ protected console!: ConsoleLogger
|
|
|
+ protected adapterProfile!: AdapterProfile
|
|
|
+
|
|
|
+ constructor(clientId: string, adapterType: TransportType, transportService: TransportServiceInterface, role: TransmissionRole) {
|
|
|
//logic here
|
|
|
+ this.setAdapterProfile(clientId, adapterType, transportService, role)
|
|
|
+ this.setupConnectionState(this.adapterProfile.transportService)
|
|
|
}
|
|
|
|
|
|
- setAdapterProfile(clientId: string, adapterType: TransportType, transportService: TransportServiceInterface, role: TransmissionRole): void {
|
|
|
- this.AdapterProfile.clientId = clientId
|
|
|
- this.AdapterProfile.connectionState = new BehaviorSubject<ConnectionState>(`OFFLINE`)
|
|
|
- this.AdapterProfile.role = role
|
|
|
- this.AdapterProfile.transportType = adapterType
|
|
|
- this.AdapterProfile.transportService = transportService
|
|
|
+ protected setAdapterProfile(clientId: string, adapterType: TransportType, transportService: TransportServiceInterface, role: TransmissionRole): void {
|
|
|
+ this.adapterProfile = {} as AdapterProfile
|
|
|
+ this.adapterProfile.clientId = clientId as string
|
|
|
+ this.adapterProfile.connectionState = new BehaviorSubject<ConnectionState>(`OFFLINE`)
|
|
|
+ this.adapterProfile.role = role
|
|
|
+ this.adapterProfile.transportType = adapterType
|
|
|
+ this.adapterProfile.transportService = transportService
|
|
|
}
|
|
|
|
|
|
- getAdapterProfile(type?: `clientId` | `role` | `transportId` | `transportType` | `connectionState`): string | BehaviorSubject<ConnectionState> | AdapterProfile | undefined {
|
|
|
+ getAdapterProfile(type?: `clientId` | `role` | `transportId` | `transportType` | `connectionState`): string | Observable<ConnectionState> | AdapterProfile | undefined {
|
|
|
if (!type) {
|
|
|
- return this.AdapterProfile
|
|
|
+ return this.adapterProfile
|
|
|
} else if (type == `clientId`) {
|
|
|
- return this.AdapterProfile.clientId
|
|
|
+ return this.adapterProfile.clientId
|
|
|
} else if (type == `connectionState`) {
|
|
|
- return this.AdapterProfile.connectionState
|
|
|
+ return this.adapterProfile.connectionState.asObservable()
|
|
|
} else if (type == `role`) {
|
|
|
- return this.AdapterProfile.role
|
|
|
+ return this.adapterProfile.role
|
|
|
} else if (type == `transportType`) {
|
|
|
- return this.AdapterProfile.transportType
|
|
|
+ return this.adapterProfile.transportType
|
|
|
} else if (type == `transportId`) {
|
|
|
- return this.AdapterProfile.transportService.getInfo().transportServiceId
|
|
|
+ return this.adapterProfile.transportService.getInfo().transportServiceId
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ // this is irrelevant at this point in time. Adapter will just listen regardless of whether there's connection or not
|
|
|
+ protected setupConnectionState(transportService: TransportServiceInterface): void {
|
|
|
+ transportService.subscribeForEvent().pipe(
|
|
|
+ filter(event => event.type === `Transport Event`),
|
|
|
+ filter(event => (event.data as ClientObject).clientId === this.adapterProfile.clientId),
|
|
|
+ map(event => {
|
|
|
+ if (event.event == 'Client Disconnected' || event.event == 'Server Disconnected') {
|
|
|
+ return 'OFFLINE'
|
|
|
+ } else {
|
|
|
+ return `ONLINE`
|
|
|
+ }
|
|
|
+ }),
|
|
|
+ distinctUntilChanged()
|
|
|
+ ).subscribe((signal: ConnectionState) => {
|
|
|
+ this.adapterProfile.connectionState.next(signal)
|
|
|
+ if (signal == 'OFFLINE') this.console.error({ message: `${this.adapterProfile.clientId} disconnected` })
|
|
|
+ if (signal == 'ONLINE') this.console.log({ message: `${this.adapterProfile.clientId} connected and ready to go` })
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|