|
@@ -1,6 +1,6 @@
|
|
import * as grpc from '@grpc/grpc-js';
|
|
import * as grpc from '@grpc/grpc-js';
|
|
import { Subject, Subscription } from "rxjs";
|
|
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, ConnectionState } 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'
|
|
import { message_proto } from './protos/server.proto'
|
|
import { message_proto } from './protos/server.proto'
|
|
@@ -11,9 +11,11 @@ export class GrpcServiceMethod {
|
|
private callRequestsFromRemote: ServerWritableStreamImpl<any, ResponseType>[] = []
|
|
private callRequestsFromRemote: ServerWritableStreamImpl<any, ResponseType>[] = []
|
|
|
|
|
|
public async create(request: ConnectionRequest, connectionAttribute: ConnectionAttribute): Promise<any> {
|
|
public async create(request: ConnectionRequest, connectionAttribute: ConnectionAttribute): Promise<any> {
|
|
|
|
+
|
|
// Assuming currently only one client
|
|
// Assuming currently only one client
|
|
this.createGrpcInstance(request.server.serverUrl, { instanceType: 'server' }, connectionAttribute)
|
|
this.createGrpcInstance(request.server.serverUrl, { instanceType: 'server' }, connectionAttribute)
|
|
this.createGrpcInstance(request.client.targetServer, { instanceType: 'client' }, connectionAttribute)
|
|
this.createGrpcInstance(request.client.targetServer, { instanceType: 'client' }, connectionAttribute)
|
|
|
|
+ // connectionAttribute.outGoing.MessageToBePublished?.subscribe(e => console.log((e.message as MessageLog).appData.msgId))
|
|
}
|
|
}
|
|
|
|
|
|
// For testing only
|
|
// For testing only
|
|
@@ -48,12 +50,11 @@ export class GrpcServiceMethod {
|
|
if (connectionAttribute.outGoing.StreamID && connectionAttribute.inComing.StreamID) {
|
|
if (connectionAttribute.outGoing.StreamID && connectionAttribute.inComing.StreamID) {
|
|
connectionAttribute.ConnectionID.local = connectionAttribute.outGoing.StreamID + connectionAttribute.inComing.StreamID
|
|
connectionAttribute.ConnectionID.local = connectionAttribute.outGoing.StreamID + connectionAttribute.inComing.StreamID
|
|
connectionAttribute.ConnectionID.remote = connectionAttribute.inComing.StreamID + connectionAttribute.outGoing.StreamID
|
|
connectionAttribute.ConnectionID.remote = connectionAttribute.inComing.StreamID + connectionAttribute.outGoing.StreamID
|
|
- let report: ReportStatus = {
|
|
|
|
- code: ColorCode.GREEN,
|
|
|
|
- message: `ConnectionID acquired. Informing Restranmission to release Messages...`,
|
|
|
|
- }
|
|
|
|
- connectionAttribute.connectionStatus.next(report)
|
|
|
|
- // console.log(connectionAttribute)
|
|
|
|
|
|
+ // let report: ReportStatus = {
|
|
|
|
+ // code: ColorCode.GREEN,
|
|
|
|
+ // message: `ConnectionID acquired. Informing Restranmission to release Messages...`,
|
|
|
|
+ // }
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -63,7 +64,7 @@ export class GrpcServiceMethod {
|
|
grpcType: GrpcConnectionType,
|
|
grpcType: GrpcConnectionType,
|
|
connectionAttribute: ConnectionAttribute,
|
|
connectionAttribute: ConnectionAttribute,
|
|
) {
|
|
) {
|
|
- let statusControl: Subject<ReportStatus> = connectionAttribute.connectionStatus
|
|
|
|
|
|
+ let statusControl: Subject<ConnectionState> = connectionAttribute.connectionStatus
|
|
let consecutiveResolutions = 0;
|
|
let consecutiveResolutions = 0;
|
|
let lastResolutionTime = Date.now();
|
|
let lastResolutionTime = Date.now();
|
|
let yellowErrorEmission: boolean = false
|
|
let yellowErrorEmission: boolean = false
|
|
@@ -89,10 +90,14 @@ export class GrpcServiceMethod {
|
|
// console.log(`Reconnection Attempt: ${consecutiveResolutions}`)
|
|
// console.log(`Reconnection Attempt: ${consecutiveResolutions}`)
|
|
if (redErrorEmission == false) {
|
|
if (redErrorEmission == false) {
|
|
redErrorEmission = true
|
|
redErrorEmission = true
|
|
- // console.error(`Connection failed ${consecutiveResolutions} times. Stopping connection attempts.`);
|
|
|
|
- let error: ReportStatus = {
|
|
|
|
- code: ColorCode.YELLOW,
|
|
|
|
- message: 'Server is not responding. Proceed to buffer.',
|
|
|
|
|
|
+ console.error(`Connection failed ${consecutiveResolutions} times. Stopping connection attempts.`);
|
|
|
|
+ // let error: ReportStatus = {
|
|
|
|
+ // code: ColorCode.YELLOW,
|
|
|
|
+ // message: 'Server is not responding. Proceed to buffer.',
|
|
|
|
+ // }
|
|
|
|
+ let error: ConnectionState = {
|
|
|
|
+ status: 'BUFFER',
|
|
|
|
+ reason: `Server is not responding...`
|
|
}
|
|
}
|
|
statusControl.next(error)
|
|
statusControl.next(error)
|
|
}
|
|
}
|
|
@@ -102,7 +107,7 @@ export class GrpcServiceMethod {
|
|
consecutiveResolutions = 0;
|
|
consecutiveResolutions = 0;
|
|
console.error('Connection attempt failed:', error);
|
|
console.error('Connection attempt failed:', error);
|
|
}
|
|
}
|
|
- // Check for a pause of more than 3 seconds since the last resolution attempt
|
|
|
|
|
|
+ // Check for a pause of more than 2 seconds since the last resolution attempt
|
|
const currentTime = Date.now();
|
|
const currentTime = Date.now();
|
|
const timeSinceLastResolution = currentTime - lastResolutionTime;
|
|
const timeSinceLastResolution = currentTime - lastResolutionTime;
|
|
if (timeSinceLastResolution > 2000) {
|
|
if (timeSinceLastResolution > 2000) {
|
|
@@ -133,8 +138,11 @@ export class GrpcServiceMethod {
|
|
this.callRequestsFromRemote.push(call)
|
|
this.callRequestsFromRemote.push(call)
|
|
let clientInfo = JSON.parse(call.request.message)
|
|
let clientInfo = JSON.parse(call.request.message)
|
|
this.generateAdditionalAttributes(connectionAttribute, clientInfo)
|
|
this.generateAdditionalAttributes(connectionAttribute, clientInfo)
|
|
|
|
+ // console.log(`Hello 123`)
|
|
|
|
+ // connectionAttribute.outGoing.MessageToBePublished?.subscribe(e => (e.message as MessageLog).appData.msgId)
|
|
|
|
|
|
console.log(`Initializing stream. Opening Channel... Confirmation from ${call.request.id}`)
|
|
console.log(`Initializing stream. Opening Channel... Confirmation from ${call.request.id}`)
|
|
|
|
+
|
|
if (connectionAttribute.outGoing.MessageToBePublished) {
|
|
if (connectionAttribute.outGoing.MessageToBePublished) {
|
|
let subscription: Subscription = connectionAttribute.outGoing.MessageToBePublished.subscribe({
|
|
let subscription: Subscription = connectionAttribute.outGoing.MessageToBePublished.subscribe({
|
|
next: (response: Message) => {
|
|
next: (response: Message) => {
|
|
@@ -159,6 +167,12 @@ export class GrpcServiceMethod {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ console.log(connectionAttribute)
|
|
|
|
+ let report: ConnectionState = {
|
|
|
|
+ status: 'DIRECT_PUBLISH'
|
|
|
|
+ }
|
|
|
|
+ connectionAttribute.connectionStatus.next(report)
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
Check: (_, callback) => {
|
|
Check: (_, callback) => {
|
|
@@ -204,9 +218,14 @@ 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: ReportStatus = {
|
|
|
|
- code: ColorCode.YELLOW,
|
|
|
|
- message: `Server doesn't seem to be alive. Error returned.`,
|
|
|
|
|
|
+ // let report: ReportStatus = {
|
|
|
|
+ // code: ColorCode.YELLOW,
|
|
|
|
+ // message: `Server doesn't seem to be alive. Error returned.`,
|
|
|
|
+ // payload: this.messageToBeSendOver ?? `There's no message at the moment...`
|
|
|
|
+ // }
|
|
|
|
+ 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...`
|
|
payload: this.messageToBeSendOver ?? `There's no message at the moment...`
|
|
}
|
|
}
|
|
connectionAttribute.connectionStatus.next(report)
|
|
connectionAttribute.connectionStatus.next(report)
|