|
@@ -1,125 +1,89 @@
|
|
|
import { MessageTransmissionTransmitter } from "./msg.transmission.transmitter";
|
|
|
import { MessageTransmissionReceiver } from "./msg.transmission.receiver";
|
|
|
import { AdapterManager } from "../connector/adapter.manager";
|
|
|
-import { EventMessage, MessageTransmissionSet, MessageTransmissionManager as MessageTransmissionManagerInterface, ReceiverProfile, TransmitterProfile } from "../interface/transport.interface";
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
-import { TransportEvent, Event } from "../interface/connector.interface";
|
|
|
import { MessageTransmissionRequestResponse } from "./msg.transmission.request-response";
|
|
|
import { filter, Observable, Observer, Subject } from "rxjs";
|
|
|
import ConsoleLogger from "../utils/log.utils";
|
|
|
import { TransmitterAdapter } from "../connector/adapter.transmitter";
|
|
|
import { ReceiverAdapter } from "../connector/adapter.receiver";
|
|
|
-import { AdapterSet } from "../interface/general.interface";
|
|
|
+import { AdapterEvent, AdapterInterface, EventMessage, EventObject, GeneralEvent, Transmission, TransmissionEvent, TransportEvent, TransportService } from '../interface/interface'
|
|
|
+import { MessageTransmissionManagerBase } from "./msg.transmission.manager.base";
|
|
|
|
|
|
-export class MessageTransmissionManager implements MessageTransmissionManagerInterface {
|
|
|
+export class MessageTransmissionManager extends MessageTransmissionManagerBase {
|
|
|
private console: ConsoleLogger = new ConsoleLogger(`MessageTransmissionManager`, ['managers'])
|
|
|
private browserEnv!: boolean
|
|
|
- transmission: MessageTransmissionSet[] = []
|
|
|
- connectionManager!: AdapterManager
|
|
|
- event!: Subject<TransportEvent>
|
|
|
+ private transmissionSet: Transmission[] = []
|
|
|
+ private adapterManager!: AdapterManager
|
|
|
|
|
|
- constructor(browserEnv?: boolean) {
|
|
|
- if (browserEnv) this.browserEnv = browserEnv
|
|
|
- // logic here
|
|
|
+ constructor(transportRef: Observable<TransportService>, browserEnv?: boolean) {
|
|
|
+ super()
|
|
|
this.console.log({ message: `Constructing self...` })
|
|
|
- this.event = new Subject()
|
|
|
- this.connectionManager = new AdapterManager(this.event, browserEnv)
|
|
|
-
|
|
|
- // this.event.subscribe(event => this.console.log({ message: 'event', details: event }))
|
|
|
+ this.eventObj.globalEvent = new Subject()
|
|
|
+ this.eventObj.transportEvent = new Subject()
|
|
|
+ transportRef.subscribe((transport: TransportService) => {
|
|
|
+ this.eventObj.transportEvent.next({
|
|
|
+ id: uuidv4(),
|
|
|
+ type: `Transport Event`,
|
|
|
+ event: `New Transport`,
|
|
|
+ date: new Date(),
|
|
|
+ data: transport
|
|
|
+ } as TransportEvent)
|
|
|
+ })
|
|
|
+ if (browserEnv) this.browserEnv = browserEnv
|
|
|
+ // Subscribe for adapterManager and it's relevent event
|
|
|
+ this.adapterManager = new AdapterManager(this.eventObj, browserEnv)
|
|
|
+ this.adapterManager.subscribe().subscribe(this.eventObj.adapterEvent)
|
|
|
|
|
|
- // note that if this server is down, all these instances of transmission and connector would be lost as well. SO cannot just simply find "instances" and reuse them. Must reinstantiate them again
|
|
|
- this.handleEvent('Client Re-connected' as Event, this.event)
|
|
|
}
|
|
|
|
|
|
- /* so there will be some changes here. will nto be assigning just one, but all of them dynamically to pour into this boy
|
|
|
- Transmitter only have to call this once. */
|
|
|
- subscribe(): Observable<MessageTransmissionSet> {
|
|
|
- return new Observable((observer: Observer<MessageTransmissionSet>) => {
|
|
|
- const targetEvent: Event = this.browserEnv ? 'New Server' : 'New Client';
|
|
|
- this.event.pipe(
|
|
|
+ public subscribe(): Observable<TransmissionEvent> {
|
|
|
+ return new Observable((observer: Observer<TransmissionEvent>) => {
|
|
|
+ const targetEvent: EventMessage = this.browserEnv ? 'New Server' : 'New Client';
|
|
|
+ this.eventObj.transportEvent.pipe(
|
|
|
filter(event => event.event == targetEvent)
|
|
|
).subscribe(event => {
|
|
|
// get all adapters for all the connection
|
|
|
- let messageTransmissionSet: MessageTransmissionSet | undefined = this.instantiateComponents((event.data as EventMessage).clientId)
|
|
|
- if (messageTransmissionSet) {
|
|
|
- observer.next(messageTransmissionSet)
|
|
|
+ let transmission: Transmission | undefined = this.instantiateTransmissionComponents(event?.data?.clientId)
|
|
|
+ if (transmission) {
|
|
|
+ observer.next({
|
|
|
+ id: uuidv4(),
|
|
|
+ type: `Transmission Event`,
|
|
|
+ event: 'New Transmission',
|
|
|
+ date: new Date(),
|
|
|
+ transmission: transmission
|
|
|
+ })
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- getEvent(): Observable<TransportEvent> {
|
|
|
- return this.event.asObservable()
|
|
|
- }
|
|
|
-
|
|
|
- private instantiateComponents(clientId: string): MessageTransmissionSet | undefined {
|
|
|
- this.console.log({ message: `Instantiating new transmission set for ${this.browserEnv ? 'Server' : 'Client'}: ${clientId}` })
|
|
|
- let adapterSet: AdapterSet | null = this.connectionManager.getAdapter(clientId)
|
|
|
- if (adapterSet) {
|
|
|
- let transmitter: MessageTransmissionTransmitter = this.getTransmitter(clientId, adapterSet.transmitterAdapter, this.event.asObservable())
|
|
|
- let receiver: MessageTransmissionReceiver = this.getReceiver(clientId, adapterSet.receiverAdapter, this.event.asObservable())
|
|
|
- let requestResponse: MessageTransmissionRequestResponse = this.getRequestResponse(transmitter, receiver, this.event.asObservable())
|
|
|
- let transmission: MessageTransmissionSet = {
|
|
|
- id: clientId,
|
|
|
- transmitter: transmitter,
|
|
|
- receiver: receiver,
|
|
|
- requestResponse: requestResponse,
|
|
|
- event: this.event.asObservable()
|
|
|
- }
|
|
|
- this.transmission.push(transmission)
|
|
|
- return transmission
|
|
|
- } else {
|
|
|
- this.console.error({ message: 'No Adapter Set' })
|
|
|
- return undefined
|
|
|
+ private instantiateTransmissionComponents(clientId: string): Transmission {
|
|
|
+ let receiverInstance: MessageTransmissionReceiver = this.getReceiver(clientId, this.eventObj)
|
|
|
+ let transmitterInstance: MessageTransmissionTransmitter = this.getTransmitter(clientId, this.eventObj)
|
|
|
+ let requestResponseInstance: MessageTransmissionRequestResponse = this.getRequestResponse(clientId, this.eventObj, transmitterInstance, receiverInstance)
|
|
|
+ let transmissionObj: Transmission = {
|
|
|
+ clientId: clientId,
|
|
|
+ transmitter: transmitterInstance,
|
|
|
+ receiver: receiverInstance,
|
|
|
+ requestResponse: requestResponseInstance,
|
|
|
+ event: this.eventObj.globalEvent
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- private getTransmitter(transmissionId: string, adapter: TransmitterAdapter, event: Observable<TransportEvent>): MessageTransmissionTransmitter {
|
|
|
- let transmitterProfile: TransmitterProfile = {
|
|
|
- id: transmissionId,
|
|
|
- name: `${adapter.getInfo().transportType} Transmitter Adapter`,
|
|
|
- dateCreated: new Date()
|
|
|
- }
|
|
|
- return new MessageTransmissionTransmitter(transmitterProfile, adapter, event)
|
|
|
+ return transmissionObj
|
|
|
}
|
|
|
|
|
|
- private getReceiver(transmissionId: string, adapter: ReceiverAdapter, event: Observable<TransportEvent>): MessageTransmissionReceiver {
|
|
|
- let receiverProfile: ReceiverProfile = {
|
|
|
- id: transmissionId,
|
|
|
- name: `${adapter.getInfo().transportType} Receiver Adapter`,
|
|
|
- dateCreated: new Date()
|
|
|
- }
|
|
|
- return new MessageTransmissionReceiver(receiverProfile, adapter, event)
|
|
|
+ private getReceiver(clientId: string, eventObj: EventObject): MessageTransmissionReceiver {
|
|
|
+ throw new Error(`Method not defined`)
|
|
|
}
|
|
|
-
|
|
|
- private getRequestResponse(transmitterInstance: MessageTransmissionTransmitter, receiverInstance: MessageTransmissionReceiver, event: Observable<TransportEvent>): MessageTransmissionRequestResponse {
|
|
|
- return new MessageTransmissionRequestResponse(transmitterInstance, receiverInstance, event)
|
|
|
+ private getTransmitter(clientId: string, eventObj: EventObject): MessageTransmissionTransmitter {
|
|
|
+ throw new Error(`Method not defined`)
|
|
|
}
|
|
|
-
|
|
|
- private handleEvent(eventName: Event, eventObs: Observable<TransportEvent>): void {
|
|
|
- eventObs.pipe(
|
|
|
- filter((event: TransportEvent) => event.event === eventName)
|
|
|
- ).subscribe(event => {
|
|
|
- // assuming this is reconnection case
|
|
|
- this.reconnectionHandler((event.data as EventMessage).clientId)
|
|
|
- // can include more event handlers here
|
|
|
- })
|
|
|
+ private getRequestResponse(clientId: string, eventObj: EventObject, transmitter: MessageTransmissionTransmitter, receiver: MessageTransmissionReceiver): MessageTransmissionRequestResponse {
|
|
|
+ throw new Error(`Method not defined`)
|
|
|
}
|
|
|
|
|
|
- private reconnectionHandler(clientId: string): void {
|
|
|
- this.console.log({ message: `TransmissionManager: A reconnection occured. Client: ${clientId}` })
|
|
|
- let transmissionObj: MessageTransmissionSet | undefined = Array.from(this.transmission).find(obj => obj.id === clientId)
|
|
|
- if (!transmissionObj) {
|
|
|
- let transmissionSet: MessageTransmissionSet | undefined = this.instantiateComponents(clientId)
|
|
|
- if (transmissionSet) {
|
|
|
- this.transmission.push(transmissionSet)
|
|
|
- } else {
|
|
|
- this.console.error({ message: `Cannot find client transmission obj : ${clientId}` })
|
|
|
- }
|
|
|
- } {
|
|
|
- this.console.log({ message: `Transmission Object for ${clientId} Found` })
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|