|
@@ -1,6 +1,6 @@
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import dotenv from 'dotenv';
|
|
import dotenv from 'dotenv';
|
|
-import { TransportEvent, ConnectionManager as ConnectionManagerInterface, AdapterSet, Transport, TransportService } from "../interface/connector.interface"
|
|
|
|
|
|
+import { TransportEvent, ConnectionManager as ConnectionManagerInterface, AdapterSet, Transport, TransportService, ClientObject } from "../interface/connector.interface"
|
|
import { TransmitterConnectionAdapter } from './connector.transmitter'
|
|
import { TransmitterConnectionAdapter } from './connector.transmitter'
|
|
import { ReceiverConnectionAdapter } from './connector.receiver'
|
|
import { ReceiverConnectionAdapter } from './connector.receiver'
|
|
import { EventMessage } from '../interface/transport.interface'
|
|
import { EventMessage } from '../interface/transport.interface'
|
|
@@ -18,13 +18,14 @@ export class ConnectionManager implements ConnectionManagerInterface {
|
|
adapterSet: AdapterSet[] = []
|
|
adapterSet: AdapterSet[] = []
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
|
+ console.log(`Connection Manager: Contructing ConnectionManager....`)
|
|
// logic here
|
|
// logic here
|
|
- this.setUpServer(process.env.PORT as unknown as number, this.getTransportService(process.env.Transport as unknown as Transport))
|
|
|
|
|
|
+ this.getTransportService(process.env.Transport as unknown as Transport)
|
|
|
|
+ this.setUpServer(process.env.PORT as unknown as number)
|
|
|
|
|
|
// Everything that happens will go through here. Can also plug in logging service here at a later time
|
|
// Everything that happens will go through here. Can also plug in logging service here at a later time
|
|
this.transportEvent.subscribe((event: TransportEvent) => {
|
|
this.transportEvent.subscribe((event: TransportEvent) => {
|
|
console.log(`TranpsortEvent: `, event)
|
|
console.log(`TranpsortEvent: `, event)
|
|
-
|
|
|
|
if (event.event == 'New Client') {
|
|
if (event.event == 'New Client') {
|
|
this.handleNewClient((event.data as EventMessage).payload) // payload is connectedclientInstance
|
|
this.handleNewClient((event.data as EventMessage).payload) // payload is connectedclientInstance
|
|
}
|
|
}
|
|
@@ -40,47 +41,41 @@ export class ConnectionManager implements ConnectionManagerInterface {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- getTransportService(transportType: Transport): any {
|
|
|
|
- console.log(`Instanting ${transportType} tranport service to be used.`)
|
|
|
|
- if (transportType == Transport.Websocket) {
|
|
|
|
|
|
+ getTransportService(transportType: Transport): void {
|
|
|
|
+ console.log(`Instantiating ${transportType} tranport service to be used.`)
|
|
|
|
+ if (transportType === Transport.Websocket) {
|
|
this.transportService = new WebsocketTransportService()
|
|
this.transportService = new WebsocketTransportService()
|
|
- this.transportEvent.next({
|
|
|
|
- id: uuidv4(),
|
|
|
|
- event: `New Transport`,
|
|
|
|
- data: {
|
|
|
|
- message: `Setting up Websocket Transport Service`
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
|
|
+ this.triggerTransportEvent(`Setting up Websocket Transport Service`)
|
|
}
|
|
}
|
|
- if (transportType == Transport.Http) {
|
|
|
|
|
|
+ if (transportType === Transport.Http) {
|
|
this.transportService = new HttpTransportService()
|
|
this.transportService = new HttpTransportService()
|
|
- this.transportEvent.next({
|
|
|
|
- id: uuidv4(),
|
|
|
|
- event: `New Transport`,
|
|
|
|
- data: {
|
|
|
|
- message: `Setting up Http Transport Service`
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
|
|
+ this.triggerTransportEvent(`Setting up Http Transport Service`)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // Server to be set up as well as acquiring client information if needed. Like in the case for grpc and socket. Http not required.
|
|
|
|
- private setUpServer(port: number, transportService: TransportService): void {
|
|
|
|
- if ((transportService as TransportService).getInfo().transport == Transport.Websocket) {
|
|
|
|
- (transportService as WebsocketTransportService).startServer(port);
|
|
|
|
- // this doesn't mean all the responses come through here, but rather to record and assign new instances based on the new clinets connected
|
|
|
|
- (transportService as WebsocketTransportService).getTransportEvent().subscribe(this.transportEvent)
|
|
|
|
- }
|
|
|
|
|
|
+ private triggerTransportEvent(message: string): void {
|
|
|
|
+ this.transportEvent.next({
|
|
|
|
+ id: uuidv4(),
|
|
|
|
+ event: `New Transport`,
|
|
|
|
+ data: { message }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
|
|
- if ((transportService as TransportService).getInfo().transport == Transport.Http) {
|
|
|
|
- (transportService as HttpTransportService).startServer(port)
|
|
|
|
- // To be Enhanced at a later time
|
|
|
|
|
|
+ // 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 setUpServer(port: number): void {
|
|
|
|
+ if (this.transportService instanceof WebsocketTransportService) {
|
|
|
|
+ this.transportService.startServer(port);
|
|
|
|
+ this.transportService.getTransportEvent().subscribe(this.transportEvent);
|
|
|
|
+ } else if (this.transportService instanceof HttpTransportService) {
|
|
|
|
+ this.transportService.startServer(port);
|
|
|
|
+ this.transportService.getTransportEvent().subscribe(this.transportEvent);
|
|
|
|
+ // Additional Http-specific setup if needed.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private handleNewClient(clientInstance: any): void {
|
|
|
|
|
|
+ private handleNewClient(clientInstance: ClientObject): void {
|
|
// need to setup the adapters to be called by the transmission manager
|
|
// need to setup the adapters to be called by the transmission manager
|
|
- let transmitter: TransmitterConnectionAdapter = new TransmitterConnectionAdapter(this.transportService, clientInstance.id, this.transportEvent)
|
|
|
|
|
|
+ let transmitter: TransmitterConnectionAdapter = new TransmitterConnectionAdapter(this.transportService, clientInstance.id, this.transportEvent, clientInstance.connectionState.asObservable())
|
|
let receiver: ReceiverConnectionAdapter = new ReceiverConnectionAdapter(this.transportService, clientInstance.id, this.transportEvent)
|
|
let receiver: ReceiverConnectionAdapter = new ReceiverConnectionAdapter(this.transportService, clientInstance.id, this.transportEvent)
|
|
let requestResponse: RequestResponseConnectionAdapter = new RequestResponseConnectionAdapter(transmitter, receiver, clientInstance.id)
|
|
let requestResponse: RequestResponseConnectionAdapter = new RequestResponseConnectionAdapter(transmitter, receiver, clientInstance.id)
|
|
let adapterSet: AdapterSet = {
|
|
let adapterSet: AdapterSet = {
|
|
@@ -98,6 +93,7 @@ export class ConnectionManager implements ConnectionManagerInterface {
|
|
data: {
|
|
data: {
|
|
adapterId: adapterSet.id,
|
|
adapterId: adapterSet.id,
|
|
message: `New Adapter Set${adapterSet.id} has been added`,
|
|
message: `New Adapter Set${adapterSet.id} has been added`,
|
|
|
|
+ payload: adapterSet
|
|
} as EventMessage
|
|
} as EventMessage
|
|
})
|
|
})
|
|
}
|
|
}
|