|
@@ -6,6 +6,9 @@ import { v4 as uuidv4 } from 'uuid'
|
|
import { AdapterSet, TransportEvent, Event } from "../interface/connector.interface";
|
|
import { AdapterSet, TransportEvent, Event } from "../interface/connector.interface";
|
|
import { MessageTransmissionRequestResponse } from "./msg.transmission.request-response";
|
|
import { MessageTransmissionRequestResponse } from "./msg.transmission.request-response";
|
|
import { filter, Observable, Observer, Subject } from "rxjs";
|
|
import { filter, Observable, Observer, Subject } from "rxjs";
|
|
|
|
+import { error } from "console";
|
|
|
|
+import { TransmitterConnectionAdapter } from "../connector/connector.transmitter";
|
|
|
|
+import { ReceiverConnectionAdapter } from "../connector/connector.receiver";
|
|
|
|
|
|
export class MessageTransmissionManager implements MessageTransmissionManagerInterface {
|
|
export class MessageTransmissionManager implements MessageTransmissionManagerInterface {
|
|
private browserEnv!: boolean
|
|
private browserEnv!: boolean
|
|
@@ -36,54 +39,57 @@ export class MessageTransmissionManager implements MessageTransmissionManagerInt
|
|
filter(event => event.event == targetEvent)
|
|
filter(event => event.event == targetEvent)
|
|
).subscribe(event => {
|
|
).subscribe(event => {
|
|
// get all adapters for all the connection
|
|
// get all adapters for all the connection
|
|
- observer.next(this.instantiateComponents((event.data as EventMessage).clientId))
|
|
|
|
|
|
+ this.instantiateComponents((event.data as EventMessage).clientId).then((messageTransmission: MessageTransmission) => {
|
|
|
|
+ observer.next(messageTransmission)
|
|
|
|
+ }).catch(error => {
|
|
|
|
+ console.error(error)
|
|
|
|
+ })
|
|
})
|
|
})
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
- private instantiateComponents(clientId: string): MessageTransmission {
|
|
|
|
- console.log(`Instantiating new transmission set for another ${this.browserEnv ? 'Server' : 'Client'}`)
|
|
|
|
- let adapterSet: AdapterSet[] = []
|
|
|
|
- if (this.connectionManager.getTransportArray().length > 0) {
|
|
|
|
- this.connectionManager.getTransportArray().forEach(transport => {
|
|
|
|
- adapterSet.push(this.connectionManager.getAdapter(clientId, transport))
|
|
|
|
- })
|
|
|
|
- } else {
|
|
|
|
- throw new Error(`Transmission Manager: No transport is Instantiated`)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 1 set only
|
|
|
|
- let transmitter: MessageTransmissionTransmitter = this.getTransmitter(clientId, adapterSet, this.event.asObservable())
|
|
|
|
- let receiver: MessageTransmissionReceiver = this.getReceiver(clientId, adapterSet, this.event.asObservable())
|
|
|
|
- let requestResponse: MessageTransmissionRequestResponse = this.getRequestResponse(transmitter, receiver, this.event.asObservable())
|
|
|
|
- let transmission: MessageTransmission = {
|
|
|
|
- id: clientId,
|
|
|
|
- transmitter: transmitter,
|
|
|
|
- receiver: receiver,
|
|
|
|
- requestResponse: requestResponse,
|
|
|
|
- event: this.event.asObservable()
|
|
|
|
- }
|
|
|
|
- this.transmission.push(transmission)
|
|
|
|
- return transmission
|
|
|
|
|
|
+ private async instantiateComponents(clientId: string): Promise<MessageTransmission> {
|
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
|
+ console.log(`Instantiating new transmission set for another ${this.browserEnv ? 'Server' : 'Client'}`)
|
|
|
|
+ if (this.connectionManager.getTransportArray().length > 0) {
|
|
|
|
+ await this.connectionManager.getAdapter(clientId).then((adapterSet: AdapterSet) => {
|
|
|
|
+ // 1 set only
|
|
|
|
+ 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: MessageTransmission = {
|
|
|
|
+ id: clientId,
|
|
|
|
+ transmitter: transmitter,
|
|
|
|
+ receiver: receiver,
|
|
|
|
+ requestResponse: requestResponse,
|
|
|
|
+ event: this.event.asObservable()
|
|
|
|
+ }
|
|
|
|
+ this.transmission.push(transmission)
|
|
|
|
+ resolve(transmission)
|
|
|
|
+ }).catch((error) => console.error(error))
|
|
|
|
+ } else {
|
|
|
|
+ reject(`Transmission Manager: No transport is Instantiated`)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- private getTransmitter(transmissionId: string, adapterSets: AdapterSet[], event: Observable<TransportEvent>): MessageTransmissionTransmitter {
|
|
|
|
|
|
+ private getTransmitter(transmissionId: string, adapter: TransmitterConnectionAdapter, event: Observable<TransportEvent>): MessageTransmissionTransmitter {
|
|
let transmitterProfile: TransmitterProfile = {
|
|
let transmitterProfile: TransmitterProfile = {
|
|
id: transmissionId,
|
|
id: transmissionId,
|
|
name: '', // for now make it empty. We will use the assigned uuid here
|
|
name: '', // for now make it empty. We will use the assigned uuid here
|
|
dateCreated: new Date()
|
|
dateCreated: new Date()
|
|
}
|
|
}
|
|
- return new MessageTransmissionTransmitter(transmitterProfile, adapterSets, event)
|
|
|
|
|
|
+ return new MessageTransmissionTransmitter(transmitterProfile, adapter, event)
|
|
}
|
|
}
|
|
|
|
|
|
- private getReceiver(transmissionId: string, adapterSets: AdapterSet[], event: Observable<TransportEvent>): MessageTransmissionReceiver {
|
|
|
|
|
|
+ private getReceiver(transmissionId: string, adapter: ReceiverConnectionAdapter, event: Observable<TransportEvent>): MessageTransmissionReceiver {
|
|
let receiverProfile: ReceiverProfile = {
|
|
let receiverProfile: ReceiverProfile = {
|
|
id: transmissionId,
|
|
id: transmissionId,
|
|
name: '', // for now make it empty. We will use the assigned uuid here
|
|
name: '', // for now make it empty. We will use the assigned uuid here
|
|
dateCreated: new Date()
|
|
dateCreated: new Date()
|
|
}
|
|
}
|
|
- return new MessageTransmissionReceiver(receiverProfile, adapterSets, event)
|
|
|
|
|
|
+ return new MessageTransmissionReceiver(receiverProfile, adapter, event)
|
|
}
|
|
}
|
|
|
|
|
|
private getRequestResponse(transmitterInstance: MessageTransmissionTransmitter, receiverInstance: MessageTransmissionReceiver, event: Observable<TransportEvent>): MessageTransmissionRequestResponse {
|
|
private getRequestResponse(transmitterInstance: MessageTransmissionTransmitter, receiverInstance: MessageTransmissionReceiver, event: Observable<TransportEvent>): MessageTransmissionRequestResponse {
|
|
@@ -108,8 +114,11 @@ export class MessageTransmissionManager implements MessageTransmissionManagerInt
|
|
private reconnectionHandler(clientId: string): void {
|
|
private reconnectionHandler(clientId: string): void {
|
|
let transmissionObj: MessageTransmission | undefined = Array.from(this.transmission).find(obj => obj.id === clientId)
|
|
let transmissionObj: MessageTransmission | undefined = Array.from(this.transmission).find(obj => obj.id === clientId)
|
|
if (!transmissionObj) {
|
|
if (!transmissionObj) {
|
|
- let transmission: MessageTransmission = this.instantiateComponents(clientId)
|
|
|
|
- this.transmission.push(transmission)
|
|
|
|
|
|
+ this.instantiateComponents(clientId).then((messageTransmission: MessageTransmission) => {
|
|
|
|
+ this.transmission.push(messageTransmission)
|
|
|
|
+ }).catch((error) => {
|
|
|
|
+ console.error(error)
|
|
|
|
+ })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|