|
@@ -3,7 +3,7 @@ import { Subject, Subscription } from "rxjs";
|
|
import { ReportStatus, ColorCode, Message, MessageLog, ConnectionAttribute, ConnectionRequest, GrpcConnectionType } from "../interfaces/general.interface";
|
|
import { ReportStatus, ColorCode, Message, MessageLog, ConnectionAttribute, ConnectionRequest, GrpcConnectionType } from "../interfaces/general.interface";
|
|
import { Status } from '@grpc/grpc-js/build/src/constants';
|
|
import { Status } from '@grpc/grpc-js/build/src/constants';
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
-const message_proto = require('./protos/server.proto')
|
|
|
|
|
|
+import { message_proto } from './protos/server.proto'
|
|
export class GrpcServiceMethod {
|
|
export class GrpcServiceMethod {
|
|
|
|
|
|
public async create(request: ConnectionRequest, connectionAttribute: ConnectionAttribute): Promise<any> {
|
|
public async create(request: ConnectionRequest, connectionAttribute: ConnectionAttribute): Promise<any> {
|
|
@@ -38,7 +38,7 @@ export class GrpcServiceMethod {
|
|
connectionAttribute.ConnectionID.remote = connectionAttribute.inComing.ChannelID + connectionAttribute.outGoing.ChannelID
|
|
connectionAttribute.ConnectionID.remote = connectionAttribute.inComing.ChannelID + connectionAttribute.outGoing.ChannelID
|
|
let report: ReportStatus = {
|
|
let report: ReportStatus = {
|
|
code: ColorCode.GREEN,
|
|
code: ColorCode.GREEN,
|
|
- message: `ConnectionID: ${connectionAttribute.ConnectionID.local} && ${connectionAttribute.ConnectionID.remote} `,
|
|
|
|
|
|
+ message: `ConnectionID acquired. Informing Restranmission to release Messages...`,
|
|
}
|
|
}
|
|
connectionAttribute.connectionStatus.next(report)
|
|
connectionAttribute.connectionStatus.next(report)
|
|
console.log(connectionAttribute)
|
|
console.log(connectionAttribute)
|
|
@@ -77,25 +77,25 @@ export class GrpcServiceMethod {
|
|
consecutiveResolutions++;
|
|
consecutiveResolutions++;
|
|
// console.log(`Reconnection Attempt: ${consecutiveResolutions}`)
|
|
// console.log(`Reconnection Attempt: ${consecutiveResolutions}`)
|
|
alreadyHealthCheck = true
|
|
alreadyHealthCheck = true
|
|
- // If there are x consecutive resolutions, log an error and break the loop
|
|
|
|
- if (consecutiveResolutions >= parseInt(process.env.ReconnectionAttempt as string) && redErrorEmission == false) {
|
|
|
|
|
|
+ if (redErrorEmission == false) {
|
|
redErrorEmission = true
|
|
redErrorEmission = true
|
|
- console.error(`Connection failed ${consecutiveResolutions} times. Stopping connection attempts.`);
|
|
|
|
|
|
+ // console.error(`Connection failed ${consecutiveResolutions} times. Stopping connection attempts.`);
|
|
let error: ReportStatus = {
|
|
let error: ReportStatus = {
|
|
code: ColorCode.RED,
|
|
code: ColorCode.RED,
|
|
- message: 'Initiate Doomsday protocol....',
|
|
|
|
|
|
+ message: 'Server is not responding. Proceed to buffer.',
|
|
}
|
|
}
|
|
statusControl.next(error)
|
|
statusControl.next(error)
|
|
}
|
|
}
|
|
- if (consecutiveResolutions < parseInt(process.env.ReconnectionAttempt as string) && yellowErrorEmission == false) {
|
|
|
|
- yellowErrorEmission = true
|
|
|
|
- let error: ReportStatus = {
|
|
|
|
- code: ColorCode.YELLOW,
|
|
|
|
- // message: `Reconnection Attempt: ${consecutiveResolutions}. Server has yet to respond`
|
|
|
|
- message: `Attempting reconnection... Server has yet to respond`,
|
|
|
|
- }
|
|
|
|
- statusControl.next(error);
|
|
|
|
- }
|
|
|
|
|
|
+ // Comment it out if Client wishes to use YELLOW for memory buffer instead of persistent storage buffer
|
|
|
|
+ // if (consecutiveResolutions < parseInt(process.env.ReconnectionAttempt as string) && yellowErrorEmission == false) {
|
|
|
|
+ // yellowErrorEmission = true
|
|
|
|
+ // let error: ReportStatus = {
|
|
|
|
+ // code: ColorCode.YELLOW,
|
|
|
|
+ // // message: `Reconnection Attempt: ${consecutiveResolutions}. Server has yet to respond`
|
|
|
|
+ // message: `Attempting reconnection... Server has yet to respond`,
|
|
|
|
+ // }
|
|
|
|
+ // statusControl.next(error);
|
|
|
|
+ // }
|
|
} catch (error) {
|
|
} catch (error) {
|
|
// Connection did not resolve, reset the count
|
|
// Connection did not resolve, reset the count
|
|
consecutiveResolutions = 0;
|
|
consecutiveResolutions = 0;
|
|
@@ -184,9 +184,7 @@ export class GrpcServiceMethod {
|
|
return new Promise(async (resolve, reject) => {
|
|
return new Promise(async (resolve, reject) => {
|
|
const client = new message_proto.Message(server, grpc.credentials.createInsecure());
|
|
const client = new message_proto.Message(server, grpc.credentials.createInsecure());
|
|
// perform check to see if server is alive, if not terminate this grpc instant and create again
|
|
// perform check to see if server is alive, if not terminate this grpc instant and create again
|
|
- this.checkConnectionHealth(client, connectionAttribute.connectionStatus, alreadyHealthCheck).catch(() => {
|
|
|
|
- resolve('')
|
|
|
|
- })
|
|
|
|
|
|
+
|
|
let outGoingInfo: any = {
|
|
let outGoingInfo: any = {
|
|
channelID: uuidv4(),
|
|
channelID: uuidv4(),
|
|
publisherID: uuidv4(),
|
|
publisherID: uuidv4(),
|
|
@@ -203,10 +201,9 @@ export class GrpcServiceMethod {
|
|
console.log(`Message trasmission operation is successful`)
|
|
console.log(`Message trasmission operation is successful`)
|
|
// RPC completed successfully
|
|
// RPC completed successfully
|
|
} if (status == grpc.status.UNAVAILABLE) {
|
|
} if (status == grpc.status.UNAVAILABLE) {
|
|
- let report = {
|
|
|
|
- code: ColorCode.YELLOW,
|
|
|
|
|
|
+ let report: ReportStatus = {
|
|
|
|
+ code: ColorCode.RED,
|
|
message: `Server doesn't seem to be alive. Error returned.`,
|
|
message: `Server doesn't seem to be alive. Error returned.`,
|
|
- from: `Server Streaming Client Instance`
|
|
|
|
}
|
|
}
|
|
connectionAttribute.connectionStatus.next(report)
|
|
connectionAttribute.connectionStatus.next(report)
|
|
resolve('No connection established. Server is not responding..')
|
|
resolve('No connection established. Server is not responding..')
|
|
@@ -231,20 +228,17 @@ export class GrpcServiceMethod {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- // Check connection To be Update. This function is destroying my code flow
|
|
|
|
|
|
+ // 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<ReportStatus>, alreadyHealthCheck: boolean): Promise<boolean> {
|
|
public async checkConnectionHealth(client: any, statusControl: Subject<ReportStatus>, alreadyHealthCheck: boolean): Promise<boolean> {
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
client.Check({}, (error, response) => {
|
|
client.Check({}, (error, response) => {
|
|
if (response) {
|
|
if (response) {
|
|
console.log(`GRPC Health check status: ${response.status} Server Connected`);
|
|
console.log(`GRPC Health check status: ${response.status} Server Connected`);
|
|
- let report: ReportStatus = {
|
|
|
|
- code: ColorCode.GREEN,
|
|
|
|
- message: `Good to go!!!`,
|
|
|
|
- }
|
|
|
|
- statusControl.next(report)
|
|
|
|
|
|
+ // Intepret the response status and implement code logic or handler
|
|
|
|
+ resolve(response.status)
|
|
} else {
|
|
} else {
|
|
if (alreadyHealthCheck == false) console.error(`Health check failed: ${error}`);
|
|
if (alreadyHealthCheck == false) console.error(`Health check failed: ${error}`);
|
|
- resolve(false)
|
|
|
|
|
|
+ reject(false)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})
|
|
})
|