Browse Source

multiple tranmission fix

Enzo 9 tháng trước cách đây
mục cha
commit
996dcace50

+ 10 - 19
services/buffer.service.ts

@@ -1,20 +1,10 @@
 // bufferService.ts
-import {
-  BehaviorSubject,
-  Observable,
-  Subject,
-  from,
-  map,
-  switchMap,
-} from "rxjs";
-import mongoose, { Connection, Model, Document } from "mongoose";
-import {
-  ConnectionState,
-  Message,
-  MessageLog,
-} from "../interfaces/general.interface";
+import { BehaviorSubject, Observable, Subject, from } from "rxjs";
+import mongoose, { Connection, Model } from "mongoose";
+import { ConnectionState, Message, MessageLog, } from "../interfaces/general.interface";
 
 export class BufferService {
+  private bufferIdentifier!: string
   private messageStream: Subject<Message>;
   private connectionState: BehaviorSubject<ConnectionState>;
   private messageBuffer: Message[] = [];
@@ -26,6 +16,7 @@ export class BufferService {
     connectionStateSubject: BehaviorSubject<ConnectionState>,
     dbName: string
   ) {
+    this.bufferIdentifier = dbName
     this.messageStream = messageFromApp;
     this.connectionState = connectionStateSubject;
     this.setupSubscriptions(); // Note: The handle buffer will push the data in local array before pushing to mongo via initial check up model
@@ -95,7 +86,7 @@ export class BufferService {
   }
 
   private handleConnectionStateChanges(state: ConnectionState): void {
-    console.log(this.connectionState.getValue().status);
+    console.log(`${this.bufferIdentifier}: ${this.connectionState.getValue().status}`);
     if (state.status === "BUFFER") {
       if (state.payload && typeof state.payload !== "string") {
         this.bufferMessage(state.payload); // Buffer the last message immediately
@@ -113,7 +104,7 @@ export class BufferService {
         // const newMessage = new this.messageModel(message);
         await this.messageModel.create(message);
         this.messageModel.countDocuments({}).then((count) => {
-          console.log(`Message${(message.message as MessageLog).appData.msgId} saved to MongoDB buffer. There is ${count} messages in datatbase at the moment.`);
+          console.log(`Message${(message.message as MessageLog).appData.msgId} saved to MongoDB buffer. There is ${count} messages in datatbase under ${this.bufferIdentifier} at the moment.`);
         });
       } catch (error) {
         console.error("Error saving message to MongoDB:", error);
@@ -121,7 +112,7 @@ export class BufferService {
       }
     } else {
       this.messageBuffer.push(message); // Fallback to local buffer if model is not defined
-      console.log(`pushing ${(message.message as MessageLog).appData.msgId} into local array buffer.... There is now ${this.messageBuffer.length} messages`);
+      console.log(`pushing ${(message.message as MessageLog).appData.msgId} into local array buffer under ${this.bufferIdentifier}.... There is now ${this.messageBuffer.length} messages`);
     }
   }
 
@@ -131,7 +122,7 @@ export class BufferService {
     return new Promise((resolve, reject) => {
       if (this.messageModel) {
         this.messageModel.countDocuments({}).then((count) => {
-          console.log(`There is ${count} messages in database buffer at the moment. Releasing them....`);
+          console.log(`There is ${count} messages in database under ${this.bufferIdentifier} at the moment. Releasing them....`);
         });
         const stream = this.messageModel.find().cursor();
 
@@ -162,7 +153,7 @@ export class BufferService {
       }
       if (!this.messageModel) {
         // If MongoDB model is not defined, use the local buffer
-        console.log(`Releasing buffer Message: currently there is ${this.messageBuffer.length} messages to be released`);
+        console.log(`Releasing buffer Message under ${this.bufferIdentifier}: currently there is ${this.messageBuffer.length} messages to be released`);
         this.messageBuffer.forEach((message) =>
           this.messageStream.next(message)
         );

+ 306 - 0
services/grpc.service.method.bak

@@ -0,0 +1,306 @@
+import * as grpc from '@grpc/grpc-js';
+import { Observable, Subject, Subscription } from "rxjs";
+import { Message, ConnectionAttribute, GrpcConnectionType, ConnectionState, MessageLog, ConnectionStatus, StreamAttribute, State } from "../interfaces/general.interface";
+import { Status } from '@grpc/grpc-js/build/src/constants';
+import { message_proto } from './protos/server.proto'
+import * as _ from 'lodash'
+export class GrpcServiceMethod {
+    private connectionAttributes: ConnectionAttribute[] = []
+    private updateConnectionStatusFlag: boolean = false
+    private server: grpc.Server | any
+    private messageToBeSendOver: Message | any
+    private clientRequest: Subject<ConnectionAttribute> = new Subject()
+    private localServerStatus: Subject<any> = new Subject()
+
+    public async create(connectionAttribute: ConnectionAttribute, connectionAttributes: ConnectionAttribute[]): Promise<any> {
+        this.connectionAttributes = connectionAttributes
+        return new Promise((resolve, reject) => {
+            this.createGrpcInstance({ instanceType: 'server' }, connectionAttribute)
+            this.createGrpcInstance({ instanceType: 'client' }, connectionAttribute)
+            resolve('Just putting it here for now....')
+        })
+    }
+
+    private async createGrpcInstance(grpcType: GrpcConnectionType, connectionAttribute: ConnectionAttribute) {
+        // Reconnection Logic here
+        while (true) {
+            try {
+                let recreatePromise = new Promise((resolve) => {
+                    if (grpcType.instanceType == 'server' && !this.server) {
+                        this.createServerStreamingServer(connectionAttribute).then(() => {
+                            resolve('recreate')
+                        })
+                    }
+                    if (grpcType.instanceType == 'client') {
+                        this.createServerStreamingClient(connectionAttribute).then(() => {
+                            resolve('recreate')
+                        })
+                    }
+                })
+                await recreatePromise
+            } catch (error) {
+                console.error('Connection attempt failed:', error);
+            }
+            await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second before the next attempt
+            // timeout generate message to trigger this reconnection
+        }
+    }
+
+    // Create Server Instance to stream all application Outgoing messages    
+    public async createServerStreamingServer(connectionAttribute: ConnectionAttribute): Promise<any> { // '0.0.0.0:3001'
+        return new Promise((resolve, reject) => {
+            try {
+                let statusChain: State = 1
+                if (statusChain == 1) {
+                    // Bind and start the server
+                    this.server = new grpc.Server()
+                    this.server.bindAsync(connectionAttribute.outGoing.serverUrl, grpc.ServerCredentials.createInsecure(), () => {
+                        console.log(`gRPC server is running on ${connectionAttribute.outGoing.serverUrl}`);
+                        this.server.start();
+                    });
+                    this.localServerStatus.next({
+                        connectionStatus: 'ON',
+                        connectionID: connectionAttribute.ConnectionID.local,
+                        message: `${connectionAttribute.outGoing.serverUrl} started.`
+                    })
+                }
+
+                if (statusChain == 1 && !this.server) {
+                    this.server.addService(message_proto.Message.service, {
+                        HandleMessage: (call) => {
+                            /// add a checking for standard message request
+                            let clientInfo: ConnectionAttribute = JSON.parse(call.request.message)
+                            if (this.isConnectionAttribute(clientInfo)) {
+                                // this.clientRequest.next(clientInfo)
+                                console.log(`Initializing stream. Opening Channel... Confirmation from ${call.request.id}`)
+                                let result: ConnectionAttribute | undefined = this.connectionAttributes.find(connectionAttribute => connectionAttribute.ConnectionID.local = clientInfo.ConnectionID.remote)
+                                if (!result) {
+                                    console.log(`No connectionID match. This should be new connection...`)
+                                    let subscription: Subscription = connectionAttribute.outGoing.MessageToBePublished!.subscribe({
+                                        next: (response: Message) => {
+                                            console.log(`Sending from GRPC server: ${(response.message as MessageLog).appData.msgId} `)
+                                            let message = {
+                                                id: response.id,
+                                                message: JSON.stringify(response.message)
+                                            }
+                                            call.write(message)
+                                        },
+                                        error: err => {
+                                            console.error(err)
+                                            subscription.unsubscribe()
+                                            resolve('')
+                                        },
+                                        complete: () => {
+                                            console.log(`Stream response completed for ${call.request.id}`)
+                                            subscription.unsubscribe()
+                                            resolve('')
+                                        }
+                                    })
+                                    let report: ConnectionState = {
+                                        status: 'DIRECT_PUBLISH'
+                                    }
+                                    connectionAttribute.connectionStatus!.next(report)
+                                }
+                                if (result && result.outGoing.connectionState == `OFF`) {
+                                    // reassign previously matched buffer
+                                    console.log(`Connection info found in array matched. Assigning buffer....`)
+                                    let subscription: Subscription = result.outGoing.MessageToBePublished!.subscribe({
+                                        next: (response: Message) => {
+                                            console.log(`Sending from GRPC server: ${(response.message as MessageLog).appData.msgId} `)
+                                            let message = {
+                                                id: response.id,
+                                                message: JSON.stringify(response.message)
+                                            }
+                                            call.write(message)
+                                        },
+                                        error: err => {
+                                            console.error(err)
+                                            subscription.unsubscribe()
+                                            resolve('')
+                                        },
+                                        complete: () => {
+                                            console.log(`Stream response completed for ${call.request.id}`)
+                                            subscription.unsubscribe()
+                                            resolve('')
+                                        }
+                                    })
+                                    let report: ConnectionState = {
+                                        status: 'DIRECT_PUBLISH'
+                                    }
+                                    result.connectionStatus!.next(report)
+                                }
+
+                            } else {
+                                console.error(`INVALID REQUEST! Client request does not fulfill criteria`)
+                            }
+                        },
+                        Check: (_, callback) => {
+                            // for now it is just sending the status message over to tell the client it is alive
+                            // For simplicity, always return "SERVING" as status
+                            callback(null, { status: 'SERVING' });
+                        },
+                    });
+                }
+            }
+            catch (error) {
+                resolve(error)
+            }
+        })
+    }
+
+    // Send a request over to the other server to open a channel for this server to emit/stream messages over
+    public async createServerStreamingClient(connectionAttribute: ConnectionAttribute): Promise<string> {
+        return new Promise(async (resolve, reject) => {
+            const client = new message_proto.Message(connectionAttribute.inComing.serverUrl, grpc.credentials.createInsecure());
+            let localInfo: ConnectionAttribute = { // need to make a new copy where it doesn't reference the subjects, otherwise circular ref error
+                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) })
+
+            console.log(`Sending request to ${connectionAttribute.inComing.serverUrl} to open response channel...`)
+
+            call.on('status', (status: Status) => {
+                if (status == grpc.status.OK) { // only returns a status when there's error. Otherwise it just waits
+                    console.log(`Message trasmission operation is successful`)
+                    // RPC completed successfully
+                } if (status == grpc.status.UNAVAILABLE) {
+                    let report: ConnectionState = {
+                        status: 'BUFFER',
+                        reason: `Server doesn't seem to be alive. Error returned.`,
+                        payload: this.messageToBeSendOver ?? `There's no message at the moment...`
+                    }
+                    connectionAttribute.connectionStatus!.next(report)
+                    let clientStatusUpdateInfo: any = {
+                        connectionStatus: 'OFF',
+                        connectionID: connectionAttribute.ConnectionID.remote,
+                        message: `${connectionAttribute.outGoing.serverUrl} started.`
+                    }
+                    this.clientRequest.next(clientStatusUpdateInfo)
+                    resolve('No connection established. Server is not responding..')
+                }
+            });
+
+            call.on('data', (data: any) => {
+                let response: Message = {
+                    id: data.id,
+                    message: JSON.parse(data.message)
+                }
+                if (connectionAttribute.inComing.MessageToBeReceived) {
+                    connectionAttribute.inComing.MessageToBeReceived.next(response)
+                }
+            });
+
+            call.on('error', (err) => {
+                console.error(err)
+                resolve('')
+            });
+        })
+    }
+
+
+    // THis is no longer necesarry after the introduction of connection Attribute. But it is still useful for checking for the other side's health
+    public async checkConnectionHealth(client: any, statusControl: Subject<ConnectionState>, alreadyHealthCheck: boolean): Promise<boolean> {
+        return new Promise((resolve, reject) => {
+            client.Check({}, (error, response) => {
+                if (response) {
+                    console.log(`GRPC Health check status: ${response.status} Server Connected`);
+                    // Intepret the response status and implement code logic or handler
+                    resolve(response.status)
+                } else {
+                    if (alreadyHealthCheck == false) console.error(`Health check failed: ${error}`);
+                    reject(false)
+                }
+            })
+        })
+    }
+
+    // TO check or validate if the client request meets the criteria
+    private isConnectionAttribute(obj: any): obj is ConnectionAttribute {
+        const isMatch = (
+            typeof obj.ConnectionID === 'object' && // Further checks can be added based on the structure of ConnectionID
+            isStreamAttribute(obj.outGoing) &&
+            isStreamAttribute(obj.inComing) &&
+            (obj.connectionStatus === null || obj.connectionStatus instanceof Subject)
+        );
+
+        if (isMatch) {
+            console.log('gRPC client call matches ConnectionAttribute type');
+        } else {
+            console.log('gRPC client call does not match ConnectionAttribute type');
+        }
+
+        return isMatch;
+        function isStreamAttribute(obj: any): obj is StreamAttribute {
+            return (
+                (typeof obj.StreamID === 'string' || obj.StreamID === undefined) &&
+                (typeof obj.PublisherID === 'string' || obj.PublisherID === undefined) &&
+                (typeof obj.SubscriberID === 'string' || obj.SubscriberID === undefined) &&
+                // Check other properties like PublisherInstance and SubscriberInstance based on their expected types
+                (typeof obj.serverUrl === 'string' || obj.serverUrl === undefined) &&
+                // Check connectionState based on its type, assuming it's an enum or similar
+                (obj.MessageToBePublished === null || obj.MessageToBePublished instanceof Observable) &&
+                (obj.MessageToBeReceived === null || obj.MessageToBeReceived instanceof Subject)
+            );
+        }
+    }
+
+    // update the referenced array 
+    // public updateConnectionStatus(connectionAttributeArray: ConnectionAttribute[]) {
+    //     if (this.updateConnectionStatusFlag === false) {
+    //         this.updateConnectionStatusFlag = true
+    //         this.localServerStatus.subscribe({
+    //             next: (notification: any) => {
+    //                 if (notification.connectionStatus === `ON`) {
+    //                     let connectionAttribute = connectionAttributeArray.find(connection => connection.ConnectionID.local == notification.connectionID)
+    //                     if (connectionAttribute) {
+    //                         connectionAttribute.outGoing.connectionState = 'ON'
+    //                         console.log(`Local Connection ${notification.connectionID} updated. {${connectionAttribute.outGoing.connectionState}}`)
+    //                     } else {
+    //                         console.log(`Local Connection ${notification.connectionID} attribute is not found.`)
+    //                     }
+    //                 }
+    //             },
+    //             error: err => console.error(err),
+    //             complete: () => { }
+    //         })
+    //         this.clientRequest.subscribe({
+    //             next: (clientConnectionAttribute: ConnectionAttribute) => {
+    //                 console.log(`Received a request from ${clientConnectionAttribute.outGoing.serverUrl}`)
+    //                 let connectionAttribute = connectionAttributeArray.find(connection => connection.ConnectionID.remote == clientConnectionAttribute.ConnectionID.local)
+    //                 if (connectionAttribute) {
+    //                     connectionAttribute.inComing.connectionState = 'ON'
+    //                     console.log(`Client Connection ${clientConnectionAttribute.ConnectionID.local} updated. {${connectionAttribute.inComing.connectionState}}`)
+    //                 } else {
+    //                     console.log(`Client Connection Attribute ${clientConnectionAttribute.inComing.PublisherID} is not found`)
+    //                 }
+    //             },
+    //             error: err => console.error(err),
+    //             complete: () => { }
+    //         })
+    //     } else {
+    //         console.log(`Update Connection Status already called.`)
+    //     }
+    // }
+}
+// https://github.com/grpc/proposal/blob/master/L5-node-client-interceptors.md
+
+
+
+

+ 61 - 127
services/grpc.service.method.ts

@@ -1,29 +1,35 @@
 import * as grpc from '@grpc/grpc-js';
 import { Observable, Subject, Subscription } from "rxjs";
-import { Message, ConnectionAttribute, GrpcConnectionType, ConnectionState, MessageLog, ConnectionStatus, StreamAttribute } from "../interfaces/general.interface";
+import { Message, ConnectionAttribute, GrpcConnectionType, ConnectionState, StreamAttribute, MessageLog } from "../interfaces/general.interface";
 import { Status } from '@grpc/grpc-js/build/src/constants';
 import { message_proto } from './protos/server.proto'
 import * as _ from 'lodash'
 export class GrpcServiceMethod {
+    private connectionAttribute: ConnectionAttribute | undefined
+    private connectionAttributes: ConnectionAttribute[] = []
     private server: grpc.Server | any
     private messageToBeSendOver: Message | any
     private clientRequest: Subject<ConnectionAttribute> = new Subject()
-    private localServerStatus: Subject<any> = new Subject()
 
-    public async create(connectionAttribute: ConnectionAttribute): Promise<any> {
+
+    // public interface for service client to establish connection. They will give server and client information as a pair
+    public async create(connectionAttribute: ConnectionAttribute, connectionAttributes: ConnectionAttribute[]): Promise<any> {
+        this.connectionAttribute = connectionAttribute
+        this.connectionAttributes = connectionAttributes
         return new Promise((resolve, reject) => {
             this.createGrpcInstance({ instanceType: 'server' }, connectionAttribute)
             this.createGrpcInstance({ instanceType: 'client' }, connectionAttribute)
             resolve('Just putting it here for now....')
         })
     }
+
     private async createGrpcInstance(grpcType: GrpcConnectionType, connectionAttribute: ConnectionAttribute) {
-        // Reconnection Logic here
+        // Reconnection Logic 
         while (true) {
             try {
                 let recreatePromise = new Promise((resolve) => {
-                    if (grpcType.instanceType == 'server') {
-                        this.createServerStreamingServer(connectionAttribute).then(() => {
+                    if (grpcType.instanceType == 'server' && !this.server) {
+                        this.createServerStreamingServer().then(() => {
                             resolve('recreate')
                         })
                     }
@@ -42,80 +48,59 @@ export class GrpcServiceMethod {
         }
     }
 
-    // Create Server Instance to stream all application Outgoing messages
-    public async createServerStreamingServer(connectionAttribute: ConnectionAttribute): Promise<any> { // '0.0.0.0:3001'
+    // Create Server Instance to stream all application Outgoing messages    
+    public async createServerStreamingServer(): Promise<any> { // '0.0.0.0:3001'
         return new Promise((resolve, reject) => {
-            try {
-                if (!this.server) {
-                    this.server = new grpc.Server()
-                    this.localServerStatus.next({
-                        connectionStatus: 'ON',
-                        connectionID: connectionAttribute.ConnectionID.local,
-                        message: `${connectionAttribute.outGoing.serverUrl} started.`
-                    })
-                } else {
-                    console.log(`Grpc server alrady started.`)
-                    this.localServerStatus.next({
-                        connectionStatus: 'ON',
-                        connectionID: connectionAttribute.ConnectionID.local,
-                        message: `${connectionAttribute.outGoing.serverUrl} already started.`
-                    })
-                }
-
-
-                this.server.addService(message_proto.Message.service, {
-                    HandleMessage: (call) => {
-                        /// add a checking for standard message request
-                        let clientInfo: ConnectionAttribute = JSON.parse(call.request.message)
-                        if (this.isConnectionAttribute(clientInfo)) {
-                            this.clientRequest.next(clientInfo)
-                            console.log(`Initializing stream. Opening Channel... Confirmation from ${call.request.id}`)
-
-                            if (connectionAttribute.outGoing.MessageToBePublished) {
-                                let subscription: Subscription = connectionAttribute.outGoing.MessageToBePublished.subscribe({
-                                    next: (response: Message) => {
-                                        console.log(`Sending from GRPC server: ${(response.message as MessageLog).appData.msgId} `)
-                                        let message = {
-                                            id: response.id,
-                                            message: JSON.stringify(response.message)
-                                        }
-                                        call.write(message)
-                                    },
-                                    error: err => {
-                                        console.error(err)
-                                        subscription.unsubscribe()
-                                        resolve('')
-                                    },
-                                    complete: () => {
-                                        console.log(`Stream response completed for ${call.request.id}`)
-                                        subscription.unsubscribe()
-                                        resolve('')
+            this.server = new grpc.Server()
+            this.server.addService(message_proto.Message.service, {
+                HandleMessage: (call) => {
+                    let clientRequest: ConnectionAttribute = JSON.parse(call.request.message)
+                    // client Request validation
+                    if (this.isConnectionAttribute(clientRequest)) {
+                        // Check if this connection exists
+                        let result: ConnectionAttribute | undefined = this.connectionAttributes.find((connectionAttribute: ConnectionAttribute) => connectionAttribute.ConnectionID.local === clientRequest.ConnectionID.remote)
+                        if (result) {
+                            // if exist, reassign back the buffer
+                            let subscription: Subscription = result.outGoing.MessageToBePublished!.subscribe({
+                                next: (outGoingMessage: Message) => {
+                                    let message = {
+                                        id: outGoingMessage.id,
+                                        message: JSON.stringify(outGoingMessage.message)
                                     }
-                                })
-                                let report: ConnectionState = {
-                                    status: 'DIRECT_PUBLISH'
+                                    console.log(`Sending ${(outGoingMessage.message as MessageLog).appData.msgId} to ${clientRequest.outGoing.PublisherID}`)
+                                    call.write(message)
+                                },
+                                error: err => {
+                                    console.error(err)
+                                    subscription.unsubscribe()
+                                    resolve(``)
+                                },
+                                complete: () => {
+                                    subscription.unsubscribe()
+                                    resolve(``)
                                 }
-                                connectionAttribute.connectionStatus!.next(report)
+                            })
+                            let report: ConnectionState = {
+                                status: `DIRECT_PUBLISH`
                             }
-                        } else {
-                            console.error(`INVALID REQUEST! Client request does not fulfill criteria`)
+                            result.connectionStatus!.next(report)
                         }
-                    },
-                    Check: (_, callback) => {
-                        // for now it is just sending the status message over to tell the client it is alive
-                        // For simplicity, always return "SERVING" as status
-                        callback(null, { status: 'SERVING' });
-                    },
-                });
-                // Bind and start the server
-                this.server.bindAsync(connectionAttribute.outGoing.serverUrl, grpc.ServerCredentials.createInsecure(), () => {
-                    console.log(`gRPC server is running on ${connectionAttribute.outGoing.serverUrl}`);
-                    this.server.start();
-                });
-            }
-            catch (error) {
-                resolve(error)
-            }
+                        if (!result) {
+                            console.log(`No matching results.... leaving the logic blank for now...`)
+                            /* Currently haven't thought of a solution for this. Even if i do , the simplest one
+                            woul be to assisgn a new buffer, which means the server client service will
+                            have to instantiate a new one for the incoming new client. Right now, there
+                            is no need since the amount of clients and their ID are predetermined. 
+                            TO be discuseed further. */
+                        }
+                    }
+                }
+            })
+
+            this.server.bindAsync(this.connectionAttribute!.outGoing.serverUrl, grpc.ServerCredentials.createInsecure(), () => {
+                console.log(`gRPC server is running on ${this.connectionAttribute?.outGoing.serverUrl}`)
+                this.server.start()
+            })
         })
     }
 
@@ -185,23 +170,6 @@ export class GrpcServiceMethod {
         })
     }
 
-
-    // THis is no longer necesarry after the introduction of connection Attribute. But it is still useful for checking for the other side's health
-    public async checkConnectionHealth(client: any, statusControl: Subject<ConnectionState>, alreadyHealthCheck: boolean): Promise<boolean> {
-        return new Promise((resolve, reject) => {
-            client.Check({}, (error, response) => {
-                if (response) {
-                    console.log(`GRPC Health check status: ${response.status} Server Connected`);
-                    // Intepret the response status and implement code logic or handler
-                    resolve(response.status)
-                } else {
-                    if (alreadyHealthCheck == false) console.error(`Health check failed: ${error}`);
-                    reject(false)
-                }
-            })
-        })
-    }
-
     // TO check or validate if the client request meets the criteria
     private isConnectionAttribute(obj: any): obj is ConnectionAttribute {
         const isMatch = (
@@ -232,43 +200,9 @@ export class GrpcServiceMethod {
         }
     }
 
-    // update the referenced array 
-    public updateConnectionStatus(connectionAttributeArray: ConnectionAttribute[]) {
-        this.localServerStatus.subscribe({
-            next: (notification: any) => {
-                if (notification.connectionStatus === `ON`) {
-                    let connectionAttribute = connectionAttributeArray.find(connection => connection.ConnectionID.local == notification.connectionID)
-                    if (connectionAttribute) {
-                        connectionAttribute.outGoing.connectionState = 'ON'
-                        console.log(`Connection ${notification.connectionID} updated`)
-                    } else {
-                        console.log(`Connection ${notification.connectionID} attribute is not found.`)
-                    }
-                }
-            },
-            error: err => console.error(err),
-            complete: () => { }
-        })
-        this.clientRequest.subscribe({
-            next: (clientConnectionAttribute: ConnectionAttribute) => {
-                console.log(`Received a request from ${clientConnectionAttribute.outGoing.serverUrl}`)
-                let connectionAttribute = connectionAttributeArray.find(connection => connection.ConnectionID.remote == clientConnectionAttribute.ConnectionID.local)
-                if (connectionAttribute) {
-                    console.log(`Connection ${clientConnectionAttribute.ConnectionID.local} updated`)
-                    connectionAttribute.inComing.connectionState = 'ON'
-                } else {
-                    console.log(`Connection Attribut ${clientConnectionAttribute.inComing.PublisherID} is not found`)
-                }
-            },
-            error: err => console.error(err),
-            complete: () => { }
-        })
-    }
-
+}
 
 
-}
 
 
 
-// https://github.com/grpc/proposal/blob/master/L5-node-client-interceptors.md

+ 2 - 2
services/server-client.service.ts

@@ -113,8 +113,8 @@ export class ServerClientManager {
                 }
                 // For each connection type:
                 if (request.client!.connectionType == 'GRPC') {
-                    this.grpcService.updateConnectionStatus(this.connectionAttributes)
-                    this.grpcService.create(connectionAttribute!).then(() => {
+                    // this.grpcService.updateConnectionStatus(this.connectionAttributes)
+                    this.grpcService.create(connectionAttribute!, this.connectionAttributes).then(() => {
                         // logic here
                     }).catch(() => {
                         errorString = `Something wrong with gRPC methods`

+ 20 - 16
test/grpc1.ts

@@ -164,22 +164,26 @@ let connectionRequest2: ConnectionRequest = {
 }
 
 
-connectionService.generateConnection(connectionRequest1)
-connectionService.generateConnection(connectionRequest2)
-
-// let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(50))
-// generateFakeMessagesToBePublished.subscribe({
-//   next: message => {
-//     let payload: Message = {
-//       id: hostServer,
-//       message: message
-//     }
-//     connectionRequest.server.messageToBePublishedFromApplication.next(payload)
-//     connectionRequest2.server.messageToBePublishedFromApplication.next(payload)
-//   },
-//   error: error => console.error(error),
-//   complete: () => console.log(`Completed`)
-// })
+connectionService.generateConnection(connectionRequest1).then((res) => {
+  // console.log(res)
+})
+connectionService.generateConnection(connectionRequest2).then((res) => {
+  // console.log(res)
+})
+
+let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(50))
+generateFakeMessagesToBePublished.subscribe({
+  next: message => {
+    let payload: Message = {
+      id: hostServer,
+      message: message
+    }
+    connectionRequest1.server!.messageToBePublishedFromApplication.next(payload)
+    connectionRequest2.server!.messageToBePublishedFromApplication.next(payload)
+  },
+  error: error => console.error(error),
+  complete: () => console.log(`Completed`)
+})
 
 // let generateFakeMessagesToBePublished = stream().pipe(take(10))
 // generateFakeMessagesToBePublished.subscribe({

+ 13 - 13
test/grpc2.ts

@@ -50,19 +50,19 @@ connectionService.generateConnection(connectionRequest)
 // })
 
 
-// connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
-//   next: response => {
-//     // if((response.message as MessageLog).appData.msgId == `ebf94479-44fe-470d-827c-9f1389396d6a`){
-//     //   console.log(`Received the 1000th message. Running the test. Initiating server restart....`)
-//     // connectionService.restartServerInDuration(10)
-//     // }
-//     console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
-//     // Message.create(response)
-//     array.push(response)
-//   },
-//   error: error => console.error(error),
-//   complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
-// })
+connectionRequest.client!.messageToBeReceivedFromRemote.subscribe({
+  next: response => {
+    // if((response.message as MessageLog).appData.msgId == `ebf94479-44fe-470d-827c-9f1389396d6a`){
+    //   console.log(`Received the 1000th message. Running the test. Initiating server restart....`)
+    // connectionService.restartServerInDuration(10)
+    // }
+    console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client!.targetServer}`)
+    // Message.create(response)
+    array.push(response)
+  },
+  error: error => console.error(error),
+  complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
+})
 
 
 /* Complex Test: 1 to 1*/

+ 13 - 13
test/grpc3.ts

@@ -50,19 +50,19 @@ connectionService.generateConnection(connectionRequest)
 // })
 
 
-// connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
-//   next: response => {
-//     // if((response.message as MessageLog).appData.msgId == `ebf94479-44fe-470d-827c-9f1389396d6a`){
-//     //   console.log(`Received the 1000th message. Running the test. Initiating server restart....`)
-//     // connectionService.restartServerInDuration(10)
-//     // }
-//     console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
-//     // Message.create(response)
-//     array.push(response)
-//   },
-//   error: error => console.error(error),
-//   complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
-// })
+connectionRequest.client!.messageToBeReceivedFromRemote.subscribe({
+  next: response => {
+    // if((response.message as MessageLog).appData.msgId == `ebf94479-44fe-470d-827c-9f1389396d6a`){
+    //   console.log(`Received the 1000th message. Running the test. Initiating server restart....`)
+    // connectionService.restartServerInDuration(10)
+    // }
+    console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client!.targetServer}`)
+    // Message.create(response)
+    array.push(response)
+  },
+  error: error => console.error(error),
+  complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
+})
 
 
 /* Complex Test: 1 to 1*/