Jelajahi Sumber

buffer restructuring

enzo 9 bulan lalu
induk
melakukan
b1a81cec87

+ 9 - 9
interfaces/general.interface.ts

@@ -1,6 +1,6 @@
 /* General interface used for office work/ */
 
-import { Subject } from "rxjs"
+import { Observable, Subject } from "rxjs"
 
 
 export enum ColorCode {
@@ -8,12 +8,12 @@ export enum ColorCode {
     'YELLOW' = 'YELLOW',
     'RED' = 'RED'
 }
-export interface messageTransmissionInterface {
-    id?: string,
-    state: '' | 'attempt to send' | 'failed sent' | 'sent successfully',
-    date?: Date,
-    msg: string
+export interface ConnectionState {
+    status: 'BUFFER' | 'DIRECT_PUBLISH';
+    reason?: string;
+    payload?: any;
 }
+
 export interface MessageLog {
     appLogLocId: string,
     appData: {
@@ -51,7 +51,7 @@ export interface ConnectionAttribute {
     ConnectionID: ConnectionID,
     outGoing: StreamAttribute,
     inComing: StreamAttribute,
-    connectionStatus: Subject<ReportStatus>
+    connectionStatus: Subject<ConnectionState>
 }
 export interface StreamAttribute {
     StreamID?: string,
@@ -59,7 +59,7 @@ export interface StreamAttribute {
     SubscriberID?: string,
     PublisherInstance?: any,
     SubscriberInstance?: any,
-    MessageToBePublished: Subject<Message> | null
+    MessageToBePublished: Observable<Message> | null
     MessageToBeReceived: Subject<Message> | null
 }
 
@@ -73,7 +73,7 @@ export interface ServerRequest {
     name: string,
     serverUrl: string,
     connectionType: 'GRPC' | 'HTTP' | 'Socket',
-    messageToBePublishedfromApplication: Subject<Message>
+    messageToBePublishedFromApplication: Subject<Message>
 }
 export interface ClientRequest {
     name: string,

+ 24 - 4
models/message.schema.ts

@@ -23,9 +23,6 @@ const appData = {
     }
 }
 
-const appDataSchema = new mongoose.Schema(
-    appData
-)
 const messageSchema = new mongoose.Schema({
     appLogLocId: {
         type: String,
@@ -43,4 +40,27 @@ const grpcMessageSchema = new mongoose.Schema({
     message: messageSchema
 })
 
-module.exports = grpcMessageSchema
+module.exports = grpcMessageSchema
+
+// message.interface.ts
+// import { Document, ObjectId } from 'mongoose';
+
+// interface AppData {
+//     msgId: string;
+//     msgLogDateTime: Date;
+//     msgDateTime: Date;
+//     msgTag: string[];
+//     msgPayload: string;
+// }
+
+// interface MessageDocument extends Document {
+//     appLogLocId: string;
+//     appData: AppData;
+// }
+
+// interface GrpcMessageDocument extends Document {
+//     id: string;
+//     message: MessageDocument;
+// }
+
+// export { GrpcMessageDocument, MessageDocument, AppData };

+ 213 - 0
services/buffer.service.ts

@@ -0,0 +1,213 @@
+// 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 { resolve } from 'path';
+
+export class BufferService {
+    private messageFromApplication: Subject<Message>;
+    private messageFromBuffer: Subject<Message>
+    private connectionState: BehaviorSubject<ConnectionState>
+    private currentSource: Subject<Message>
+    private messageBuffer: Message[] = [];
+    private messageModel: Model<Message> | undefined;
+    private readonly dbUrl: string = process.env.MONGO as string;
+
+
+    constructor(
+        messageFromApp: Subject<Message>,
+        connectionStateSubject: BehaviorSubject<ConnectionState>,
+        dbName: string
+    ) {
+        this.messageFromBuffer = new Subject<Message>();
+        this.messageFromApplication = messageFromApp;
+        this.currentSource = this.messageFromBuffer
+        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
+
+        /* Disable for now. Use local array first */
+        // this.initializeDatabaseConnection(dbName).then((connection: mongoose.Connection) => {
+        //     const grpcMessageSchema = require('../models/message.schema');
+        //     this.messageModel = connection.model<Message>('Message', grpcMessageSchema)
+        //     this.transferLocalBufferToMongoDB() // transfer all data from local array into mongodb after the mongo setup is complete
+        // }).catch(error => {
+        //     console.error('Database initialization failed:', error);
+        //     // Implement retry logic or additional error handling here
+        // });
+    }
+
+    public getMessages(): Observable<Message> {
+        return this.currentSource as Observable<Message>
+    }
+
+    private setupSubscriptions(): void {
+        this.messageFromApplication.subscribe({
+            next: (message: Message) => this.handleIncomingMessage(message),
+            error: (err) => console.error('Error in messageToBePublished subject:', err),
+            complete: () => console.log('messageToBePublished subscription completed')
+        });
+
+        this.connectionState.subscribe({
+            next: (state: ConnectionState) => this.handleConnectionStateChanges(state),
+            error: (err) => console.error('Error in connectionState subject:', err),
+            complete: () => console.log('connectionState subscription completed')
+        });
+    }
+
+    private async initializeDatabaseConnection(dbName: string): Promise<Connection> {
+        try {
+            console.log(`${this.dbUrl}${dbName}`)
+            const connection: mongoose.Connection = await mongoose.createConnection(`${this.dbUrl}${dbName}`);
+            console.log(`Connected to ${this.dbUrl}${dbName}`)
+            return connection;
+        } catch (error) {
+            console.error('Error connecting to MongoDB:', error);
+            throw error;
+        }
+    }
+
+    private handleIncomingMessage(message: Message): void {
+        if (this.connectionState.getValue().status === 'BUFFER') {
+            this.bufferMessage(message);
+        }
+        if (this.connectionState.getValue().status === 'DIRECT_PUBLISH') {
+            // console.log(`There is ${this.messageBuffer.length} messages in the buffer`) // somehw this is called repeatedly
+            // do nothing for now
+            // Start releasing buffered messages, but don't wait for it to complete
+            // this.isBufferNotEmpty().then(isNotEmpty => {
+            //     if (isNotEmpty) {
+            //         // this.releaseBufferedMessages(this.messageFromBuffer);
+            //         // Continue to buffer new incoming message during the release process
+            //         this.bufferMessage(message);
+            //     } else {
+            //         // If buffer is empty, switch source and handle the message directly
+            //         this.messageToBePublished.next(message); // Handle the message directly
+            //     }
+            // });
+        }
+    }
+
+    private handleConnectionStateChanges(state: ConnectionState): void {
+        console.log(this.connectionState.getValue().status)
+        if (state.status === 'BUFFER') {
+            this.currentSource = this.messageFromBuffer
+            if (state.payload && typeof state.payload !== 'string') {
+                this.bufferMessage(state.payload); // Buffer the last message immediately
+            }
+        }
+        if (state.status === 'DIRECT_PUBLISH') {
+            if (this.messageBuffer.length > 0) { // temporary only since i am not using mongoDB for buffering atm
+                this.releaseBufferedMessages(this.messageFromBuffer).then(() => {
+                    console.log(`Switching to main publisher from source`)
+                    this.currentSource = this.messageFromApplication
+                }).catch((err) => {
+                    console.error(err)
+                })
+            }
+            if (this.messageBuffer.length < 1) {
+                this.currentSource = this.messageFromApplication
+            }
+            /* This is for mongo  */
+            // this.isBufferNotEmpty().then(isNotEmpty => {
+            //     if (isNotEmpty) {
+            //         this.currentMessageSource = this.messageFromBuffer
+            //     } else {
+            //         this.currentMessageSource = this.messageToBePublished
+            //     }
+            // })
+        }
+    }
+
+    private async isBufferNotEmpty(): Promise<boolean> {
+        if (this.messageModel) {
+            // Check the count in MongoDB
+            const count = await this.messageModel.estimatedDocumentCount().exec();
+            return count > 0;
+        } else {
+            // Check the local buffer
+            return this.messageBuffer.length > 0;
+        }
+    }
+
+    private async bufferMessage(message: Message): Promise<void> {
+        if (this.messageModel) {
+            try {
+                // const newMessage = new this.messageModel(message);
+                await this.messageModel.create(message);
+                console.log(`Message${(message.message as MessageLog).appData.msgId} saved to MongoDB buffer`);
+            } catch (error) {
+                console.error('Error saving message to MongoDB:', error);
+                // Implement retry logic or additional error handling here
+            }
+        } 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`)
+        }
+    }
+
+
+    private releaseBufferedMessages(messageFromBuffer: Subject<Message>): Promise<boolean> {
+        return new Promise((resolve, reject) => {
+            if (this.messageModel) {
+                const stream = this.messageModel.find().cursor();
+
+                stream.on('data', async (message) => {
+                    // Process each message individually
+                    messageFromBuffer.next(message);
+                });
+
+                stream.on('error', (error) => {
+                    console.error('Error streaming messages from MongoDB:', error);
+                    reject(error)
+                });
+
+                stream.on('end', async () => {
+                    // Delete the data once it has been streamed
+                    try {
+                        if (this.messageModel) {
+                            await this.messageModel.deleteMany({});
+                            console.log('Data in Mongo deleted successfully.');
+                        } else {
+                            console.log(`Message Mongoose Model is not intiated properly...`)
+                        }
+                    } catch (err) {
+                        console.error('Error deleting data:', err);
+                    }
+                    resolve(true)
+                });
+            }
+            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`)
+                this.messageBuffer.forEach(message => this.messageFromBuffer.next(message));
+                this.messageBuffer.length = 0 // Clear the local buffer after transferring
+                if (this.messageBuffer.length < 1) {
+                    resolve(true)
+                } else {
+                    reject(`Somehow the array is not emptied. This should not happen`)
+                }
+            }
+        })
+    }
+
+    public getStateObservable(): BehaviorSubject<ConnectionState> {
+        return this.connectionState;
+    }
+
+    private async transferLocalBufferToMongoDB(): Promise<void> {
+        if (this.messageModel) {
+            this.messageBuffer.forEach(async message => {
+                try {
+                    if (this.messageModel) {
+                        await this.messageModel.create(message);
+                    }
+                } catch (error) {
+                    console.error('Error transferring message to MongoDB:', error);
+                }
+            })
+            this.messageBuffer = []; // Clear local buffer after transferring
+        }
+    }
+
+    // Additional methods as required...
+}

+ 16 - 1
services/fis.retransmission.service.ts

@@ -1,6 +1,7 @@
 import mongoose, { Model, Schema } from 'mongoose';
 import { BehaviorSubject, Observable, Subject, Subscription, from } from 'rxjs'
 import { ColorCode, Message, MessageLog, ReportStatus, Status } from '../interfaces/general.interface'
+import { resolve } from 'path';
 require('dotenv').config();
 
 // Implement status chain refactoring
@@ -27,9 +28,9 @@ export class FisRetransmissionService {
             /* Green should release all data from buffer and mongo and also redirect the applicationOutgoingMessage back into the return subject(releaseMessageSubject)
             if there's any. */
             if (report.code == ColorCode.GREEN) {
+                let status: Status = 1
                 // console.log(`Connection status report && ${report.message ?? 'No Message'}`)
                 /* Status Chain begins */
-                let status: Status = 1
                 if (status === 1) {
                     messageStreamToMongo = this.deactivateMongoStreamSubscription(messageStreamToMongo)
                     if (messageStreamToMongo) status = 0
@@ -132,6 +133,20 @@ export class FisRetransmissionService {
         return releaseMessageSubject
     }
 
+    private checkIfMongoBufferHasData(): Promise<boolean> {
+        return new Promise(async (resolve, reject) => {
+            if (this.messageModel) {
+                try {
+                    const count = await this.messageModel.countDocuments();
+                    resolve(count > 0)
+                }
+                catch (error) {
+                    reject(error)
+                }
+            }
+        })
+    }
+
     // IF Buffer exceeds a certain limit, it will trigger RED. Configure in .env file. There's the concern of 2 RED status, one from this and another from other means.
     // Behaviour of this needs to be investigated further
     private checkBufferLimit(message: Subject<Message>, statusReport: Subject<ReportStatus>) {

+ 35 - 16
services/grpc.service.method.ts

@@ -1,6 +1,6 @@
 import * as grpc from '@grpc/grpc-js';
 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 { v4 as uuidv4 } from 'uuid'
 import { message_proto } from './protos/server.proto'
@@ -11,9 +11,11 @@ export class GrpcServiceMethod {
     private callRequestsFromRemote: ServerWritableStreamImpl<any, ResponseType>[] = []
 
     public async create(request: ConnectionRequest, connectionAttribute: ConnectionAttribute): Promise<any> {
+
         // Assuming currently only one client
         this.createGrpcInstance(request.server.serverUrl, { instanceType: 'server' }, 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
@@ -48,12 +50,11 @@ export class GrpcServiceMethod {
         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
-            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,
         connectionAttribute: ConnectionAttribute,
     ) {
-        let statusControl: Subject<ReportStatus> = connectionAttribute.connectionStatus
+        let statusControl: Subject<ConnectionState> = connectionAttribute.connectionStatus
         let consecutiveResolutions = 0;
         let lastResolutionTime = Date.now();
         let yellowErrorEmission: boolean = false
@@ -89,10 +90,14 @@ export class GrpcServiceMethod {
                 // console.log(`Reconnection Attempt: ${consecutiveResolutions}`)
                 if (redErrorEmission == false) {
                     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)
                 }
@@ -102,7 +107,7 @@ export class GrpcServiceMethod {
                 consecutiveResolutions = 0;
                 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 timeSinceLastResolution = currentTime - lastResolutionTime;
             if (timeSinceLastResolution > 2000) {
@@ -133,8 +138,11 @@ export class GrpcServiceMethod {
                         this.callRequestsFromRemote.push(call)
                         let clientInfo = JSON.parse(call.request.message)
                         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}`)
+
                         if (connectionAttribute.outGoing.MessageToBePublished) {
                             let subscription: Subscription = connectionAttribute.outGoing.MessageToBePublished.subscribe({
                                 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) => {
@@ -204,9 +218,14 @@ export class GrpcServiceMethod {
                     console.log(`Message trasmission operation is successful`)
                     // RPC completed successfully
                 } 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...`
                     }
                     connectionAttribute.connectionStatus.next(report)

+ 15 - 7
services/server-client.service.ts

@@ -1,7 +1,10 @@
 import { BehaviorSubject, Subject } from 'rxjs';
-import { ColorCode, Message, ReportStatus, ConnectionAttribute, ConnectionRequest } from '../interfaces/general.interface';
+import { ColorCode, Message, ReportStatus, ConnectionAttribute, ConnectionRequest, ConnectionState, MessageLog } from '../interfaces/general.interface';
 import { GrpcServiceMethod } from './grpc.service.method';
 import { FisRetransmissionService } from './fis.retransmission.service';
+import { BufferService } from './buffer.service';
+import * as dotenv from 'dotenv'
+dotenv.config()
 
 export class ServerClientManager {
 
@@ -11,6 +14,7 @@ export class ServerClientManager {
     constructor(private grpcService: GrpcServiceMethod) {
     }
 
+    // putting it here for later use. to be used for testing
     public restartServerInDuration(time: number) {
         console.log(this.request)
         console.log(this.connectionAttributes)
@@ -31,10 +35,14 @@ export class ServerClientManager {
         } else {
             database = request.server.name + request.client.name
         }
-        let initialReport: ReportStatus = { code: ColorCode.YELLOW, message: 'Initialization of the subject' }
-        let reportSubject: BehaviorSubject<ReportStatus> = new BehaviorSubject(initialReport)
-        let retransmission: FisRetransmissionService = new FisRetransmissionService(database, reportSubject)
-        let messageToBePublished: Subject<Message> = retransmission.handleMessage(request.server.messageToBePublishedfromApplication) ?? request.server.messageToBePublishedfromApplication
+        /* Inject retransmission here */
+        let initialReport: ConnectionState = { status: 'BUFFER' }
+        let reportSubject: BehaviorSubject<ConnectionState> = new BehaviorSubject(initialReport)
+        let retransmission: BufferService = new BufferService(request.server.messageToBePublishedFromApplication, reportSubject, database)
+        // let initialReport: ReportStatus = { code: ColorCode.RED, message: 'Initialization of the subject' }
+        // let reportSubject: BehaviorSubject<ReportStatus> = new BehaviorSubject(initialReport)
+        // let retransmission: FisRetransmissionService = new FisRetransmissionService(database, reportSubject)
+        // let messageToBePublished: Subject<Message> = retransmission.handleMessage(request.server.messageToBePublishedFromApplication) ?? request.server.messageToBePublishedFromApplication
 
         let connectionAttribute: ConnectionAttribute = {
             ConnectionID: {
@@ -42,14 +50,14 @@ export class ServerClientManager {
                 remote: ''
             },
             outGoing: {
-                MessageToBePublished: messageToBePublished,
+                MessageToBePublished: retransmission.getMessages(),
                 MessageToBeReceived: null
             },
             inComing: {
                 MessageToBePublished: null,
                 MessageToBeReceived: request.client.messageToBeReceivedFromRemote
             },
-            connectionStatus: reportSubject // this is not related to report status for the retrasmission service
+            connectionStatus: reportSubject
         }
 
         // This is default connection

+ 35 - 17
test/grpc1.ts

@@ -3,7 +3,11 @@ import { Message, MessageLog, ConnectionRequest } from '../interfaces/general.in
 import { GrpcServiceMethod } from '../services/grpc.service.method';
 import { readFileSync } from 'fs';
 import { ServerClientManager } from '../services/server-client.service';
+import mongoose from 'mongoose';
 
+// Connect to MongoDB
+mongoose.connect('mongodb://localhost:27017/grpc1')
+const Message = mongoose.model('Message', require('../models/message.schema'))
 // Subject for bidirectional communication
 const connectionService: ServerClientManager = new ServerClientManager(new GrpcServiceMethod())
 const messagesJSON: any = readFileSync('payload.json')
@@ -21,7 +25,7 @@ let connectionRequest: ConnectionRequest = {
     name: 'g1',
     serverUrl: hostServer,
     connectionType: 'GRPC',
-    messageToBePublishedfromApplication: new Subject<Message>()
+    messageToBePublishedFromApplication: new Subject<Message>()
   },
   client: {
     name: 'g2',
@@ -34,20 +38,34 @@ let connectionRequest: ConnectionRequest = {
 connectionService.generateConnection(connectionRequest)
 
 // let generateFakeMessagesToBePublished = stream().pipe(take(1000))
-let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(3000))
-generateFakeMessagesToBePublished.subscribe({
+
+// let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(1000))
+// generateFakeMessagesToBePublished.subscribe({
+//   next: message => {
+//     let payload: Message = {
+//       id: hostServer,
+//       message: message
+//     }
+//     connectionRequest.server.messageToBePublishedFromApplication.next(payload)
+//   },
+//   error: error => console.error(error),
+//   complete: () => console.log(`Completed`)
+// })
+
+stream().subscribe({
   next: message => {
-    let payload: Message = {
+    let payload = {
       id: hostServer,
       message: message
     }
-    connectionRequest.server.messageToBePublishedfromApplication.next(payload)
+    connectionRequest.server.messageToBePublishedFromApplication.next(payload)
   }
 })
 
 connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
   next: response => {
     console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
+    // Message.create(response)
     array.push(response)
   },
   error: error => console.error(error),
@@ -324,15 +342,15 @@ function generateFakeStreamResponse(request: any): Subject<any> {
 }
 
 /* Checking the values by the end of the test */
-setTimeout(() => {
-  console.log(`All received data: ${array.length}`)
-}, 5000)
-setTimeout(() => {
-  console.log(`All received data: ${array.length}`)
-}, 10000)
-setTimeout(() => {
-  console.log(`All received data: ${array.length}`)
-}, 15000)
-setTimeout(() => {
-  console.log(`All received data: ${array.length}`)
-}, 20000)
+// setTimeout(() => {
+//   console.log(`All received data: ${array.length}`)
+// }, 5000)
+// setTimeout(() => {
+//   console.log(`All received data: ${array.length}`)
+// }, 10000)
+// setTimeout(() => {
+//   console.log(`All received data: ${array.length}`)
+// }, 15000)
+// setTimeout(() => {
+//   console.log(`All received data: ${array.length}`)
+// }, 20000)

+ 28 - 24
test/grpc2.ts

@@ -3,7 +3,10 @@ import { Message, MessageLog, ConnectionRequest } from '../interfaces/general.in
 import { GrpcServiceMethod } from '../services/grpc.service.method';
 import { readFileSync } from 'fs';
 import { ServerClientManager } from '../services/server-client.service';
+import mongoose from 'mongoose';
 
+mongoose.connect('mongodb://localhost:27017/grpc2')
+const Message = mongoose.model('Message', require('../models/message.schema'))
 // Subject for bidirectional communication
 const connectionService: ServerClientManager = new ServerClientManager(new GrpcServiceMethod())
 const messagesJSON: any = readFileSync('payload.json')
@@ -11,8 +14,8 @@ let parsedMessages: any[] = JSON.parse(messagesJSON) // load the fake messages g
 let targetserver: string = 'localhost:3000'
 let targetserver2: string = 'localhost:3002'
 let hostServer: string = 'localhost:3001'
-let array: any[] = [] // Used for testing    
 let intervalToStreamOutGoingMessage: number = 1
+let array: Message[] = []
 
 
 /* Simple Test: 1 to 1 */
@@ -21,7 +24,7 @@ let connectionRequest: ConnectionRequest = {
     name: 'g2',
     serverUrl: hostServer,
     connectionType: 'GRPC',
-    messageToBePublishedfromApplication: new Subject<Message>()
+    messageToBePublishedFromApplication: new Subject<Message>()
   },
   client: {
     name: 'g1',
@@ -35,24 +38,26 @@ connectionService.generateConnection(connectionRequest)
 // 10000th message == 848438e1-da50-4d98-aa12-e44d6d6a1489
 
 // let generateFakeMessagesToBePublished = stream().pipe(take(1000))
-let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(20000))
-generateFakeMessagesToBePublished.subscribe({
-  next: message => {
-    let payload: Message = {
-      id: hostServer,
-      message: message
-    }
-    // connectionRequest.server.messageToBePublishedfromApplication.next(payload)
-  }
-})
+// let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(1000))
+// generateFakeMessagesToBePublished.subscribe({
+//   next: message => {
+//     let payload: Message = {
+//       id: hostServer,
+//       message: message
+//     }
+//     connectionRequest.server.messageToBePublishedfromApplication.next(payload)
+//   }
+// })
+
 
 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)
-    }
+    // 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),
@@ -60,7 +65,6 @@ connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
 })
 
 
-
 /* Complex Test: 1 to 1*/
 // let connectionRequest: ConnectionRequest = {
 //   server: {
@@ -303,7 +307,7 @@ function stream(): Subject<any> {
   const intervalId = setInterval(() => {
     result.next(messages[count]);
     count++;
-    if (count >= 10000) {
+    if (count >= 1000) {
       clearInterval(intervalId);
       result.complete();
     }
@@ -335,10 +339,10 @@ setTimeout(() => {
 setTimeout(() => {
   console.log(`All received data: ${array.length}`)
 }, 10000)
-setTimeout(() => {
-  console.log(`All received data: ${array.length}`)
-}, 15000)
-setTimeout(() => {
-  console.log(`All received data: ${array.length}`)
-}, 20000)
+// setTimeout(() => {
+//   console.log(`All received data: ${array.length}`)
+// }, 15000)
+// setTimeout(() => {
+//   console.log(`All received data: ${array.length}`)
+// }, 20000)
 

+ 2 - 2
test/grpc3.ts

@@ -21,7 +21,7 @@ let connectionRequest: ConnectionRequest = {
     name: 'g2',
     serverUrl: hostServer,
     connectionType: 'GRPC',
-    messageToBePublishedfromApplication: new Subject<Message>()
+    messageToBePublishedFromApplication: new Subject<Message>()
   },
   client: {
     name: 'g1',
@@ -40,7 +40,7 @@ generateFakeMessagesToBePublished.subscribe({
       id: hostServer,
       message: message
     }
-    connectionRequest.server.messageToBePublishedfromApplication.next(payload)
+    connectionRequest.server.messageToBePublishedFromApplication.next(payload)
   }
 })