ソースを参照

updated some fixes

Dr-Swopt 3 週間 前
コミット
d9a9f55c50
8 ファイル変更925 行追加38 行削除
  1. 4 3
      .env
  2. 1 0
      .gitignore
  3. 8 1
      doc/explanation.txt
  4. 865 3
      package-lock.json
  5. 5 1
      package.json
  6. 14 16
      src/connector/adapter.manager.ts
  7. 1 1
      src/test/receiver.ts
  8. 27 13
      src/utils/socket.utils.ts

+ 4 - 3
.env

@@ -1,5 +1,6 @@
 ;Transport = "Websocket, Http"
 ;PORT = 3000, 3001
-# Transport = "Websocket"
-Transport = "Http"
-PORT = 3000
+Transport = "Websocket"
+# Transport = "Http"
+PORT = 3000
+FolderPath = "E:/Task/Fis-TransportInterface/clients/"

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 node_modules
 *.js
 *.js.map
+clients

+ 8 - 1
doc/explanation.txt

@@ -70,4 +70,11 @@ probably also use 2 socket, but it just feels wrong.
 DevLog:
 As of 12/12/2024: 
 -Fixed the axios error by cutting out the timeout param, but it also expose other problems, in that the reconnection logic needs to be fixed, because it always
-reconnect under new profile?? have to check again.
+reconnect under new profile?? have to check again.
+
+As of 14/12/2024:
+Discussion points during meeting: Keep track and monitor the performance and it's associated ram and cpu usage. When coding, keep track of the instances instantiated from
+overloading the system. It's not just getting the behavior right, but also making sure unused var and instances are also properly organized and deleted when no longer used to
+prevent memory leak.
+And there's going to be an upcoming meeting to present my findings and progress:
+Target for the day:  Get the HTTP Services ready. at least the channel version, so that I get right on with the testing and proceed with Emulating request Response adapter.

ファイルの差分が大きいため隠しています
+ 865 - 3
package-lock.json


+ 5 - 1
package.json

@@ -10,6 +10,9 @@
     "start": "node dist/index.js",
     "proxy": "node dist/test/proxy.js",
     "transmitter": "node dist/test/transmitter.js",
+    "dev": "node --inspect dist/test/transmitter.js",
+    "pmtransmitter": "pm2 start node dist/test/transmitter.js",
+    "pmreceiver": "pm2 start node dist/test/receiver.js",
     "receiver": "node dist/test/receiver.js"
   },
   "author": "",
@@ -25,10 +28,11 @@
     "chalk": "^4.1.0",
     "dotenv": "^16.4.5",
     "express": "^4.21.1",
+    "pm2": "^5.4.3",
     "rxjs": "^7.8.1",
     "socket.io": "^4.8.0",
     "socket.io-client": "^4.8.0",
     "source-map-support": "^0.5.21",
     "uuid": "^10.0.0"
   }
-}
+}

+ 14 - 16
src/connector/adapter.manager.ts

@@ -59,7 +59,8 @@ export class AdapterManager implements AdapterManagerInterface {
 
     // Server to be set up as well as acquiring client information if needed. Like in the case for grpc and socket. Http not requ`ired.
     private setUpTransportService(transportSet: TransportSet, event: Subject<TransportEvent>, isClient?: boolean): void {
-        this.instantiateTransportService(transportSet.transport, event).then((transportService: TransportService) => {
+        try {
+            let transportService: TransportService = this.instantiateTransportService(transportSet.transport, event)
             this.transportServiceArray.push(transportService)
             if (transportService instanceof WebsocketTransportService) {
                 this.console.log({ message: `Just Double Checking... this is websocket` })
@@ -78,23 +79,20 @@ export class AdapterManager implements AdapterManagerInterface {
                     transportService.startServer(transportSet.port)
                 }
             }
-        }).catch((error) => {
-            this.console.log({ message: `Promise Error`, details: error })
-            throw new Error(error)
-        })
+        } catch (error) {
+            this.console.error({ message: 'Fail to set transport. Error in setting up transport', details: error })
+        }
     }
 
-    private async instantiateTransportService(transportType: Transport, event: Subject<TransportEvent>): Promise<TransportService> {
-        return new Promise((resolve, reject) => {
-            if (transportType == Transport.Websocket) {
-                resolve(new WebsocketTransportService(event))
-            }
-            else if (transportType == Transport.Http) {
-                resolve(new HttpTransportService(event))
-            } else {
-                reject(`No Transport Service is not properly instantiated`)
-            }
-        })
+    private instantiateTransportService(transportType: Transport, event: Subject<TransportEvent>): TransportService {
+        if (transportType == Transport.Websocket) {
+            return new WebsocketTransportService(event)
+        }
+        else if (transportType == Transport.Http) {
+            return new HttpTransportService(event)
+        } else {
+            throw new Error(`No Transport Service Instantiated`)
+        }
     }
 
     private sortTransportFromEnv(transportSet: TransportSet[]): void {

+ 1 - 1
src/test/receiver.ts

@@ -49,7 +49,7 @@ class Supervisor {
         //     complete: () => this.console.log({ message: `Responses Completed for request: ${request.header.messageID}` })
         // })
 
-        this.startGeneratingRequest(1000, this.outgoingPipe)
+        // this.startGeneratingRequest(1000, this.outgoingPipe)
     }
 
     private request(request: FisMessage, messageTransmission: MessageTransmissionSet): Observable<any> {

+ 27 - 13
src/utils/socket.utils.ts

@@ -8,6 +8,7 @@ import { ConnectionState, Transport, TransportEvent, TransportMessage } from '..
 import { ConnectedSocketClient, ConnectedSocketServer } from '../transport/websocket';
 import { EventMessage } from '../interface/transport.interface';
 import ConsoleLogger from './log.utils';
+import path from 'path';
 const console: ConsoleLogger = new ConsoleLogger(`SocketUtils`, ['transport'])
 
 export function startSocketServer(port: number): Observable<SocketForConnectedClient> {
@@ -259,28 +260,39 @@ export function handleNewSocketClient(socket: SocketForConnectedClient, connecte
     })
 }
 
-
-// Specifically to write receiver profile information
-export async function writeFile(data: ConnectedSocketServer, filename: string): Promise<boolean> {
+async function writeFile(data: ConnectedSocketServer, filename: string): Promise<boolean> {
     return new Promise((resolve, reject) => {
+        // Ensure the folder exists
+        const folderPath = process.env.FolderPath as string
+        console.log({ message: folderPath })
+        // const folderPath = path.join(__dirname, folder);
+        if (!fs.existsSync(folderPath)) {
+            fs.mkdirSync(folderPath, { recursive: true }); // Create folder if it doesn't exist
+        } else {
+            console.log({ message: 'Folder already exist' })
+        }
+
+        // Construct the full file path (include the folder)
+        const filePath = path.join(folderPath, `${filename}.json`);
+
         // Write JSON data to a file
-        fs.writeFile(`${filename}.json`, JSON.stringify(data, null, 2), (err) => {
+        fs.writeFile(filePath, JSON.stringify(data, null, 2), (err) => {
             if (err) {
-                console.log({ message: 'Error writing file', details: err })
-                reject(false)
+                console.log({ message: 'Error writing file', details: err });
+                reject(false);
             } else {
-                console.log({ message: 'File has been written', details: err })
-                resolve(true)
+                console.log({ message: 'File has been written', details: filePath });
+                resolve(true);
             }
         });
-    })
+    });
 }
 
 /* For Internal Usage only. Temporary serve as a way for server to keep track of clients. To be replaced in the future with better alternatives. */
-export function addClientToDB(entry: ConnectedSocketClient, filePath: string = 'clients.json'): void {
+export function addClientToDB(entry: ConnectedSocketClient): void {
     try {
         let data: ConnectedSocketClient[] = [];
-
+        let filePath = process.env.FolderPath as string + 'clients.json'
         // Check if the file exists and load existing data
         if (fs.existsSync(filePath)) {
             const fileContent = fs.readFileSync(filePath, 'utf-8');
@@ -303,10 +315,11 @@ export function addClientToDB(entry: ConnectedSocketClient, filePath: string = '
     }
 }
 
-export async function checkIfClientExists(id: string, filePath: string = 'clients.json'): Promise<ConnectedSocketClient> {
+export async function checkIfClientExists(id: string): Promise<ConnectedSocketClient> {
     return new Promise((resolve, reject) => {
         try {
             // Check if the file exists
+            let filePath = process.env.FolderPath as string + 'clients.json'
             if (!fs.existsSync(filePath)) {
                 console.log({ message: "File does not exist." })
                 reject('File does not exist');
@@ -339,7 +352,8 @@ export async function checkOwnClientInfo(filename?: string): Promise<{ id: strin
         if (fs.existsSync(`${filename}.json`)) {
             try {
                 // Read the file contents
-                const fileData = fs.readFileSync(`${filename}.json`, 'utf8');
+                let filePath = process.env.FolderPath as string
+                const fileData = fs.readFileSync(filePath + `${filename}.json`, 'utf8');
 
                 // If the file is empty, return an error
                 if (fileData.trim() === "") {

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません