|
@@ -1,56 +1,62 @@
|
|
|
import * as grpc from '@grpc/grpc-js';
|
|
|
import { Subject, Subscription } from "rxjs";
|
|
|
-import { Message, ConnectionAttribute, ConnectionRequest, GrpcConnectionType, ConnectionState, MessageLog, State, OutGoingInfo } from "../interfaces/general.interface";
|
|
|
+import { Message, ConnectionAttribute, GrpcConnectionType, ConnectionState, MessageLog, ConnectionStatus } from "../interfaces/general.interface";
|
|
|
import { Status } from '@grpc/grpc-js/build/src/constants';
|
|
|
import { message_proto } from './protos/server.proto'
|
|
|
-import { ServerWritableStreamImpl } from '@grpc/grpc-js/build/src/server-call';
|
|
|
+import * as _ from 'lodash'
|
|
|
export class GrpcServiceMethod {
|
|
|
-
|
|
|
-
|
|
|
private server: grpc.Server | any
|
|
|
private messageToBeSendOver: Message | any
|
|
|
- private clientInfo: any[] = []
|
|
|
-
|
|
|
+ private clientRequest: Subject<ConnectionAttribute> = new Subject()
|
|
|
+ private localServerStatus: Subject<any> = new Subject()
|
|
|
|
|
|
- public async create(request: ConnectionRequest, connectionAttribute: ConnectionAttribute, outGoingInfo: OutGoingInfo): Promise<any> {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ public getClientRequest() {
|
|
|
+ return this.clientRequest
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public getLocalServerStatus() {
|
|
|
+ return this.localServerStatus
|
|
|
+ }
|
|
|
+
|
|
|
+ public async create(connectionAttribute: ConnectionAttribute): Promise<any> {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ this.createGrpcInstance({ instanceType: 'server' }, connectionAttribute)
|
|
|
+ this.createGrpcInstance({ instanceType: 'client' }, connectionAttribute)
|
|
|
+ resolve('Just putting it here for now....')
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
private async generateAdditionalAttributes(connectionAttribute: ConnectionAttribute, clientInfo?: any, localInfo?: any) {
|
|
|
- if (clientInfo) {
|
|
|
- connectionAttribute.inComing.StreamID = clientInfo.StreamID
|
|
|
- connectionAttribute.inComing.PublisherID = clientInfo.PublisherID
|
|
|
- connectionAttribute.inComing.SubscriberID = clientInfo.SubscriberID
|
|
|
- }
|
|
|
- if (localInfo) {
|
|
|
- connectionAttribute.outGoing.StreamID = localInfo.StreamID
|
|
|
- connectionAttribute.outGoing.PublisherID = localInfo.PublisherID
|
|
|
- connectionAttribute.outGoing.SubscriberID = localInfo.SubscriberID
|
|
|
- }
|
|
|
- if (connectionAttribute.outGoing.StreamID && connectionAttribute.inComing.StreamID) {
|
|
|
- connectionAttribute.ConnectionID.local = connectionAttribute.outGoing.StreamID + connectionAttribute.inComing.StreamID
|
|
|
- connectionAttribute.ConnectionID.remote = connectionAttribute.inComing.StreamID + connectionAttribute.outGoing.StreamID
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- private async createGrpcInstance(
|
|
|
- serverUrl: string,
|
|
|
- grpcType: GrpcConnectionType,
|
|
|
- connectionAttribute: ConnectionAttribute,
|
|
|
- outGoingInfo: OutGoingInfo
|
|
|
- ) {
|
|
|
+ private async createGrpcInstance(grpcType: GrpcConnectionType, connectionAttribute: ConnectionAttribute) {
|
|
|
+
|
|
|
while (true) {
|
|
|
try {
|
|
|
let recreatePromise = new Promise((resolve) => {
|
|
|
if (grpcType.instanceType == 'server') {
|
|
|
- this.createServerStreamingServer(serverUrl, connectionAttribute).then(() => {
|
|
|
+ this.createServerStreamingServer(connectionAttribute).then(() => {
|
|
|
resolve('recreate')
|
|
|
})
|
|
|
}
|
|
|
if (grpcType.instanceType == 'client') {
|
|
|
- this.createServerStreamingClient(serverUrl, connectionAttribute, outGoingInfo).then(() => {
|
|
|
+ this.createServerStreamingClient(connectionAttribute).then(() => {
|
|
|
resolve('recreate')
|
|
|
})
|
|
|
}
|
|
@@ -65,23 +71,24 @@ export class GrpcServiceMethod {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public async createServerStreamingServer(
|
|
|
- serverUrl: string,
|
|
|
- connectionAttribute: ConnectionAttribute
|
|
|
- ): Promise<any> {
|
|
|
+ public async createServerStreamingServer(connectionAttribute: ConnectionAttribute): Promise<any> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
try {
|
|
|
if (!this.server) {
|
|
|
this.server = new grpc.Server()
|
|
|
+ this.localServerStatus.next({
|
|
|
+ connectionStatus: 'ON',
|
|
|
+ serverName: connectionAttribute.outGoing.PublisherID,
|
|
|
+ message: `${connectionAttribute.outGoing.serverUrl} started.`
|
|
|
+ })
|
|
|
} else {
|
|
|
- console.log(`Grpc server alrady started.`)
|
|
|
+ console.log(`Grpc server alrady started.`)
|
|
|
}
|
|
|
|
|
|
this.server.addService(message_proto.Message.service, {
|
|
|
HandleMessage: (call) => {
|
|
|
- let clientInfo: OutGoingInfo = JSON.parse(call.request.message)
|
|
|
-
|
|
|
- this.generateAdditionalAttributes(connectionAttribute, clientInfo)
|
|
|
+ let clientInfo: ConnectionAttribute = JSON.parse(call.request.message)
|
|
|
+ this.clientRequest.next(clientInfo)
|
|
|
|
|
|
console.log(`Initializing stream. Opening Channel... Confirmation from ${call.request.id}`)
|
|
|
|
|
@@ -106,11 +113,10 @@ export class GrpcServiceMethod {
|
|
|
resolve('')
|
|
|
}
|
|
|
})
|
|
|
- console.log(connectionAttribute)
|
|
|
let report: ConnectionState = {
|
|
|
status: 'DIRECT_PUBLISH'
|
|
|
}
|
|
|
- connectionAttribute.connectionStatus.next(report)
|
|
|
+ connectionAttribute.connectionStatus!.next(report)
|
|
|
}
|
|
|
},
|
|
|
Check: (_, callback) => {
|
|
@@ -120,8 +126,8 @@ export class GrpcServiceMethod {
|
|
|
},
|
|
|
});
|
|
|
|
|
|
- this.server.bindAsync(serverUrl, grpc.ServerCredentials.createInsecure(), () => {
|
|
|
- console.log(`gRPC server is running on ${serverUrl}`);
|
|
|
+ this.server.bindAsync(connectionAttribute.outGoing.serverUrl, grpc.ServerCredentials.createInsecure(), () => {
|
|
|
+ console.log(`gRPC server is running on ${connectionAttribute.outGoing.serverUrl}`);
|
|
|
this.server.start();
|
|
|
});
|
|
|
}
|
|
@@ -132,17 +138,32 @@ export class GrpcServiceMethod {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public async createServerStreamingClient(
|
|
|
- server: string,
|
|
|
- connectionAttribute: ConnectionAttribute,
|
|
|
- outGoingInfo: OutGoingInfo
|
|
|
- ): Promise<string> {
|
|
|
+ public async createServerStreamingClient(connectionAttribute: ConnectionAttribute): Promise<string> {
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
- const client = new message_proto.Message(server, grpc.credentials.createInsecure());
|
|
|
- this.generateAdditionalAttributes(connectionAttribute, {}, outGoingInfo)
|
|
|
+ const client = new message_proto.Message(connectionAttribute.inComing.serverUrl, grpc.credentials.createInsecure());
|
|
|
+ let localInfo: ConnectionAttribute = {
|
|
|
+ ConnectionID: connectionAttribute.ConnectionID,
|
|
|
+ outGoing: {
|
|
|
+ StreamID: connectionAttribute.outGoing.StreamID,
|
|
|
+ PublisherID: connectionAttribute.outGoing.PublisherID,
|
|
|
+ SubscriberID: connectionAttribute.outGoing.SubscriberID,
|
|
|
+ serverUrl: connectionAttribute.outGoing.serverUrl,
|
|
|
+ MessageToBePublished: null,
|
|
|
+ MessageToBeReceived: null
|
|
|
+ },
|
|
|
+ inComing: {
|
|
|
+ StreamID: connectionAttribute.inComing.StreamID,
|
|
|
+ PublisherID: connectionAttribute.inComing.PublisherID,
|
|
|
+ SubscriberID: connectionAttribute.inComing.SubscriberID,
|
|
|
+ serverUrl: connectionAttribute.inComing.serverUrl,
|
|
|
+ MessageToBePublished: null,
|
|
|
+ MessageToBeReceived: null
|
|
|
+ },
|
|
|
+ connectionStatus: null
|
|
|
+ }
|
|
|
+ let call = client.HandleMessage({ id: connectionAttribute.inComing.serverUrl, message: JSON.stringify(localInfo) })
|
|
|
|
|
|
- let call = client.HandleMessage({ id: server, message: JSON.stringify(outGoingInfo) })
|
|
|
- console.log(`Sending request to ${server} to open response channel...`)
|
|
|
+ console.log(`Sending request to ${connectionAttribute.inComing.serverUrl} to open response channel...`)
|
|
|
|
|
|
call.on('status', (status: Status) => {
|
|
|
if (status == grpc.status.OK) {
|
|
@@ -154,7 +175,7 @@ export class GrpcServiceMethod {
|
|
|
reason: `Server doesn't seem to be alive. Error returned.`,
|
|
|
payload: this.messageToBeSendOver ?? `There's no message at the moment...`
|
|
|
}
|
|
|
- connectionAttribute.connectionStatus.next(report)
|
|
|
+ connectionAttribute.connectionStatus!.next(report)
|
|
|
resolve('No connection established. Server is not responding..')
|
|
|
}
|
|
|
});
|