|
@@ -1,68 +1,144 @@
|
|
-import { BehaviorSubject } from 'rxjs';
|
|
|
|
-import { ConnectionAttribute, ConnectionRequest, ConnectionState, OutGoingInfo } from '../interfaces/general.interface';
|
|
|
|
|
|
+import { BehaviorSubject, Subject } from 'rxjs';
|
|
|
|
+import { ClientRequest, ConnectionAttribute, ConnectionRequest, ConnectionState, Message, OutGoingInfo, ServerRequest, State } from '../interfaces/general.interface';
|
|
import { GrpcServiceMethod } from './grpc.service.method';
|
|
import { GrpcServiceMethod } from './grpc.service.method';
|
|
import { BufferService } from './buffer.service';
|
|
import { BufferService } from './buffer.service';
|
|
-import { v4 as uuidv4 } from 'uuid'
|
|
|
|
import * as dotenv from 'dotenv'
|
|
import * as dotenv from 'dotenv'
|
|
|
|
+import * as _ from 'lodash'
|
|
dotenv.config()
|
|
dotenv.config()
|
|
|
|
|
|
export class ServerClientManager {
|
|
export class ServerClientManager {
|
|
-
|
|
|
|
private connectionAttributes: ConnectionAttribute[] = []
|
|
private connectionAttributes: ConnectionAttribute[] = []
|
|
- private request: ConnectionRequest | any
|
|
|
|
- private outGoingInfo: OutGoingInfo
|
|
|
|
private grpcService: GrpcServiceMethod = new GrpcServiceMethod()
|
|
private grpcService: GrpcServiceMethod = new GrpcServiceMethod()
|
|
|
|
+ private defaultServerAttribute: ServerRequest = {
|
|
|
|
+ name: 'Default - Server',
|
|
|
|
+ serverUrl: "localhost:3000",
|
|
|
|
+ connectionType: 'GRPC',
|
|
|
|
+ messageToBePublishedFromApplication: new Subject<Message>()
|
|
|
|
+ }
|
|
|
|
+ private defaultClientAttribute: ClientRequest = {
|
|
|
|
+ name: 'Default - Client',
|
|
|
|
+ targetServer: "localhost:3001",
|
|
|
|
+ connectionType: 'GRPC',
|
|
|
|
+ messageToBeReceivedFromRemote: new Subject<Message>()
|
|
|
|
+ }
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
- this.outGoingInfo = {
|
|
|
|
- StreamID: uuidv4(),
|
|
|
|
- PublisherID: uuidv4(),
|
|
|
|
- SubscriberID: uuidv4()
|
|
|
|
- }
|
|
|
|
|
|
+ // logic here
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public generateConnection(request: ConnectionRequest) {
|
|
|
|
- this.request = request
|
|
|
|
- let database: string
|
|
|
|
- if (request.database) {
|
|
|
|
- database = request.database
|
|
|
|
- } else {
|
|
|
|
- database = request.server.name + request.client.name
|
|
|
|
- }
|
|
|
|
- /* Inject retransmission here */
|
|
|
|
- let initialReport: ConnectionState = { status: 'BUFFER' }
|
|
|
|
- let reportSubject: BehaviorSubject<ConnectionState> = new BehaviorSubject(initialReport)
|
|
|
|
- let retransmission: BufferService = new BufferService(request.server.messageToBePublishedFromApplication, reportSubject, database)
|
|
|
|
-
|
|
|
|
- let connectionAttribute: ConnectionAttribute = {
|
|
|
|
- ConnectionID: {
|
|
|
|
- local: '',
|
|
|
|
- remote: ''
|
|
|
|
- },
|
|
|
|
- outGoing: {
|
|
|
|
- MessageToBePublished: retransmission.getMessages(),
|
|
|
|
- MessageToBeReceived: null
|
|
|
|
- },
|
|
|
|
- inComing: {
|
|
|
|
- MessageToBePublished: null,
|
|
|
|
- MessageToBeReceived: request.client.messageToBeReceivedFromRemote
|
|
|
|
- },
|
|
|
|
- connectionStatus: reportSubject
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // connectionAttribute.outGoing.MessageToBePublished?.subscribe(e => console.log((e.message as MessageLog).appData.msgId))
|
|
|
|
- // This is default connection`
|
|
|
|
- if (!request.server.connectionType) {
|
|
|
|
- request.server.connectionType = 'GRPC'
|
|
|
|
- }
|
|
|
|
- // For each connection type:
|
|
|
|
- if (request.server.connectionType == 'GRPC') {
|
|
|
|
- this.grpcService.create(request, connectionAttribute, this.outGoingInfo)
|
|
|
|
- this.connectionAttributes.push(connectionAttribute)
|
|
|
|
- console.log(`There is now ${this.connectionAttributes.length} connection Attributes`)
|
|
|
|
- }
|
|
|
|
|
|
+ public async generateConnection(request: ConnectionRequest): Promise<any> {
|
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
|
+ let initialReport: ConnectionState
|
|
|
|
+ let reportSubject: BehaviorSubject<ConnectionState>
|
|
|
|
+ let retransmission: BufferService
|
|
|
|
+ // let originalRequest = JSON.parse(JSON.stringify(request))
|
|
|
|
+ let originalRequest = _.cloneDeep(request)
|
|
|
|
+ let database: string
|
|
|
|
+ let response: any = { message: `Fail to complete connection generation` }
|
|
|
|
+ let statusChain: State = 0
|
|
|
|
+ let connectionAttribute: ConnectionAttribute
|
|
|
|
+
|
|
|
|
+ if (statusChain == 0) {
|
|
|
|
+ if (!request.server) {
|
|
|
|
+ request.server = this.defaultServerAttribute
|
|
|
|
+ }
|
|
|
|
+ if (!request.client) {
|
|
|
|
+ request.client = this.defaultClientAttribute
|
|
|
|
+ }
|
|
|
|
+ if (request.database) {
|
|
|
|
+ database = request.database
|
|
|
|
+ } else {
|
|
|
|
+ database = request.server.name + request.client.name
|
|
|
|
+ }
|
|
|
|
+ /* Inject retransmission here */
|
|
|
|
+ initialReport = { status: 'BUFFER' }
|
|
|
|
+ reportSubject = new BehaviorSubject(initialReport)
|
|
|
|
+ retransmission = new BufferService(request.server.messageToBePublishedFromApplication, reportSubject, database)
|
|
|
|
+
|
|
|
|
+ statusChain = 1
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (statusChain == 1) {
|
|
|
|
+ // Connection Type checking
|
|
|
|
+ if (request.server!.connectionType != request.client!.connectionType) {
|
|
|
|
+ console.log(`Connection Type DOES NOT MATCH!`)
|
|
|
|
+ statusChain = 0
|
|
|
|
+ } else {
|
|
|
|
+ statusChain = 1
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (statusChain == 1) {
|
|
|
|
+ connectionAttribute = {
|
|
|
|
+ ConnectionID: {
|
|
|
|
+ local: request.server!.name + request.client!.name,
|
|
|
|
+ remote: request.client!.name + request.server!.name
|
|
|
|
+ },
|
|
|
|
+ outGoing: {
|
|
|
|
+ StreamID: request.server!.name,
|
|
|
|
+ PublisherID: request.server!.name,
|
|
|
|
+ SubscriberID: request.server!.name,
|
|
|
|
+ MessageToBePublished: retransmission!.getMessages(),
|
|
|
|
+ MessageToBeReceived: null
|
|
|
|
+ },
|
|
|
|
+ inComing: {
|
|
|
|
+ StreamID: request.client!.name,
|
|
|
|
+ PublisherID: request.client!.name,
|
|
|
|
+ SubscriberID: request.client!.name,
|
|
|
|
+ MessageToBePublished: null,
|
|
|
|
+ MessageToBeReceived: request.client!.messageToBeReceivedFromRemote
|
|
|
|
+ },
|
|
|
|
+ connectionStatus: reportSubject!
|
|
|
|
+ }
|
|
|
|
+ statusChain = 1
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (statusChain == 1) {
|
|
|
|
+ await this.checkConnectionAttribute(connectionAttribute!).then((res) => {
|
|
|
|
+ if (res == true) {
|
|
|
|
+ console.log(`Connection<${connectionAttribute.ConnectionID.local}> already exists `)
|
|
|
|
+ }
|
|
|
|
+ if (res == false) {
|
|
|
|
+ this.connectionAttributes.push(connectionAttribute)
|
|
|
|
+ console.log(`Connection ${connectionAttribute.ConnectionID.local} registered...`)
|
|
|
|
+ response = {
|
|
|
|
+ message: "Channel Response",
|
|
|
|
+ requestedTo: originalRequest,
|
|
|
|
+ data: connectionAttribute
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ console.log(`There is now ${this.connectionAttributes.length} connection Attributes`)
|
|
|
|
+ })
|
|
|
|
+ statusChain = 1
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (statusChain == 1) {
|
|
|
|
+ // This is default connection`
|
|
|
|
+ if (!request.client!.connectionType) {
|
|
|
|
+ request.client!.connectionType = 'GRPC'
|
|
|
|
+ }
|
|
|
|
+ // For each connection type:
|
|
|
|
+ if (request.client!.connectionType == 'GRPC') {
|
|
|
|
+ // this.grpcService.create(request, connectionAttribute, this.outGoingInfo)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resolve(response);
|
|
|
|
+
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private async checkConnectionAttribute(connectionAttribute: ConnectionAttribute): Promise<boolean> {
|
|
|
|
+ return new Promise((resolve) => {
|
|
|
|
+ let result: boolean = this.connectionAttributes.some(connection =>
|
|
|
|
+ connection.ConnectionID.local === connectionAttribute.ConnectionID.local
|
|
|
|
+ );
|
|
|
|
+ console.log(`Checking ${connectionAttribute.ConnectionID.local} and returns ${result}`);
|
|
|
|
+ resolve(result);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|