Enzo vor 11 Monaten
Ursprung
Commit
e6a47c0461
2 geänderte Dateien mit 45 neuen und 49 gelöschten Zeilen
  1. 44 14
      services/grpc.service.method.ts
  2. 1 35
      services/server-client.service.ts

+ 44 - 14
services/grpc.service.method.ts

@@ -10,16 +10,6 @@ export class GrpcServiceMethod {
     private clientRequest: Subject<ConnectionAttribute> = new Subject()
     private localServerStatus: Subject<any> = new Subject()
 
-    // keep track of all the incoming channel request from the servers
-    public getClientRequest() {
-        return this.clientRequest
-    }
-
-    // keep track of all the incoming channel request from the servers
-    public getLocalServerStatus() {
-        return this.localServerStatus
-    }
-
     public async create(connectionAttribute: ConnectionAttribute): Promise<any> {
         return new Promise((resolve, reject) => {
             this.createGrpcInstance({ instanceType: 'server' }, connectionAttribute)
@@ -60,14 +50,14 @@ export class GrpcServiceMethod {
                     this.server = new grpc.Server()
                     this.localServerStatus.next({
                         connectionStatus: 'ON',
-                        connectionIDlocal: connectionAttribute.ConnectionID.local,
+                        connectionID: connectionAttribute.ConnectionID.local,
                         message: `${connectionAttribute.outGoing.serverUrl} started.`
                     })
                 } else {
                     console.log(`Grpc server alrady started.`)
                     this.localServerStatus.next({
                         connectionStatus: 'ON',
-                        connectionIDlocal: connectionAttribute.ConnectionID.local,
+                        connectionID: connectionAttribute.ConnectionID.local,
                         message: `${connectionAttribute.outGoing.serverUrl} already started.`
                     })
                 }
@@ -108,7 +98,7 @@ export class GrpcServiceMethod {
                                 connectionAttribute.connectionStatus!.next(report)
                             }
                         } else {
-                            console.error(`INVALID REQUEST`)
+                            console.error(`INVALID REQUEST! Client request does not fulfill criteria`)
                         }
                     },
                     Check: (_, callback) => {
@@ -168,6 +158,12 @@ export class GrpcServiceMethod {
                         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..')
                 }
             });
@@ -206,7 +202,7 @@ export class GrpcServiceMethod {
         })
     }
 
-
+    // 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
@@ -236,6 +232,40 @@ 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: () => { }
+        })
+    }
+
+
 
 }
 

+ 1 - 35
services/server-client.service.ts

@@ -113,8 +113,7 @@ export class ServerClientManager {
                 }
                 // For each connection type:
                 if (request.client!.connectionType == 'GRPC') {
-                    this.grpcServerStatusHandler()
-                    this.grpcClientStatusHandler()
+                    this.grpcService.updateConnectionStatus(this.connectionAttributes)
                     this.grpcService.create(connectionAttribute!).then(() => {
                         // logic here
                     }).catch(() => {
@@ -151,40 +150,7 @@ export class ServerClientManager {
         });
     }
 
-    private grpcServerStatusHandler() {
-        this.grpcService.getLocalServerStatus().subscribe({
-            next: (notification: any) => {
-                if (notification.connectionStatus === `ON`) {
-                    let connectionAttribute = this.connectionAttributes.find(connection => connection.ConnectionID.local == notification.connectionIDlocal)
-                    if (connectionAttribute) {
-                        connectionAttribute.outGoing.connectionState = 'ON'
-                        console.log(`Connection ${notification.connectionIDlocal} updated`)
-                    } else {
-                        console.log(`Connection ${notification.connectionIDlocal} attribute is not found.`)
-                    }
-                }
-            },
-            error: err => console.error(err),
-            complete: () => { }
-        })
-    }
 
-    private grpcClientStatusHandler() {
-        this.grpcService.getClientRequest().subscribe({
-            next: (clientConnectionAttribute: ConnectionAttribute) => {
-                console.log(`Received a request from ${clientConnectionAttribute.outGoing.serverUrl}`)
-                let connectionAttribute = this.connectionAttributes.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: () => { }
-        })
-    }
 }