From 4568bc12f49cdbffcf05c53f708386c9480e796b Mon Sep 17 00:00:00 2001 From: andrei-mihnea-cerbu Date: Wed, 13 Nov 2024 10:45:14 +0200 Subject: [PATCH] network chunk v10 --- UC/package-lock.json | 8 ++++ UC/package.json | 1 + .../socket_communicator_base.ts | 43 ++++++++++--------- .../socket_communicator_base.ts | 43 ++++++++++--------- 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/UC/package-lock.json b/UC/package-lock.json index 0a7a350..5f434d6 100644 --- a/UC/package-lock.json +++ b/UC/package-lock.json @@ -21,6 +21,7 @@ "@types/better-sqlite3": "^7.6.11", "@types/lokijs": "^1.5.14", "@types/node": "^22.6.1", + "@types/ping": "^0.4.4", "@types/sqlite3": "^3.1.11", "@types/uuid": "^10.0.0", "concurrently": "^8.2.2", @@ -179,6 +180,13 @@ "undici-types": "~6.19.2" } }, + "node_modules/@types/ping": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/@types/ping/-/ping-0.4.4.tgz", + "integrity": "sha512-ifvo6w2f5eJYlXm+HiVx67iJe8WZp87sfa683nlqED5Vnt9Z93onkokNoWqOG21EaE8fMxyKPobE+mkPEyxsdw==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/sqlite3": { "version": "3.1.11", "resolved": "https://registry.npmjs.org/@types/sqlite3/-/sqlite3-3.1.11.tgz", diff --git a/UC/package.json b/UC/package.json index 6a45059..1269a2f 100644 --- a/UC/package.json +++ b/UC/package.json @@ -22,6 +22,7 @@ "@types/better-sqlite3": "^7.6.11", "@types/lokijs": "^1.5.14", "@types/node": "^22.6.1", + "@types/ping": "^0.4.4", "@types/sqlite3": "^3.1.11", "@types/uuid": "^10.0.0", "concurrently": "^8.2.2", diff --git a/UC/src/network/socket_communicator/socket_communicator_base.ts b/UC/src/network/socket_communicator/socket_communicator_base.ts index 5176fdd..f23aa76 100644 --- a/UC/src/network/socket_communicator/socket_communicator_base.ts +++ b/UC/src/network/socket_communicator/socket_communicator_base.ts @@ -1,6 +1,6 @@ import { ParsedMessage } from '../message_handler'; import { OperationHandler } from '../operations_base/operation_handler'; -import { exec } from 'child_process'; +import ping from "ping"; export abstract class SocketCommunicatorBase { protected readonly ip: string; @@ -22,35 +22,36 @@ export abstract class SocketCommunicatorBase { } protected async scanNetworkLatency(): Promise { - const targetIp = this.ip; // Use the IP from the superclass - return new Promise((resolve, reject) => { - exec(`ping -c 1 ${targetIp}`, (error, stdout) => { - if (error) { - console.error(`Ping error: ${error}`); - return reject(error); - } + const targetIp = this.ip; // Use the IP from the superclass - const match = stdout.match(/time=([\d.]+) ms/); - if (match && match[1]) { - const latency = parseFloat(match[1]); - console.log(`Network latency to ${targetIp} is approximately ${latency} ms`); - resolve(latency); - } else { - reject(new Error('Unable to determine latency from ping output.')); - } - }); - }); + try { + const response = await ping.promise.probe(targetIp); + + if (!response.alive || response.time === "unknown") { + console.warn(`Ping failed to reach ${targetIp}. Using default network speed.`); + return 200; // Default latency in ms if ping fails + } + + return response.time; // Latency in ms from ping response + } catch (error: any) { + console.error(`Ping error: ${error.message}. Using default network speed.`); + return 200; // Default latency in ms if an error occurs + } } + // Calculate optimal chunk size based on network latency, with fallback if necessary protected async calculateOptimalChunkSize(messageLength: number): Promise { - this.networkSpeed = await this.scanNetworkLatency(); + const latency = await this.scanNetworkLatency(); + this.networkSpeed = latency > 0 ? 1000 / latency : 1; // Speed in bytes/ms based on latency + + // Calculate initial chunk size based on latency (bounded between 512 and 1024 bytes) let chunkSize = Math.min(Math.max(512, Math.floor(5000 / this.networkSpeed)), 1024); - // Ensure chunk size aligns with base64 encoding (multiple of 4) + // Adjust chunk size for base64 alignment (multiple of 4) while (messageLength % chunkSize !== 0 && chunkSize > 0) { chunkSize -= 4; } - return chunkSize || 1024; + return chunkSize || 1024; // Fallback to 1024 if alignment adjustment results in 0 } } diff --git a/User/src/network/socket_communicator/socket_communicator_base.ts b/User/src/network/socket_communicator/socket_communicator_base.ts index 5176fdd..f23aa76 100644 --- a/User/src/network/socket_communicator/socket_communicator_base.ts +++ b/User/src/network/socket_communicator/socket_communicator_base.ts @@ -1,6 +1,6 @@ import { ParsedMessage } from '../message_handler'; import { OperationHandler } from '../operations_base/operation_handler'; -import { exec } from 'child_process'; +import ping from "ping"; export abstract class SocketCommunicatorBase { protected readonly ip: string; @@ -22,35 +22,36 @@ export abstract class SocketCommunicatorBase { } protected async scanNetworkLatency(): Promise { - const targetIp = this.ip; // Use the IP from the superclass - return new Promise((resolve, reject) => { - exec(`ping -c 1 ${targetIp}`, (error, stdout) => { - if (error) { - console.error(`Ping error: ${error}`); - return reject(error); - } + const targetIp = this.ip; // Use the IP from the superclass - const match = stdout.match(/time=([\d.]+) ms/); - if (match && match[1]) { - const latency = parseFloat(match[1]); - console.log(`Network latency to ${targetIp} is approximately ${latency} ms`); - resolve(latency); - } else { - reject(new Error('Unable to determine latency from ping output.')); - } - }); - }); + try { + const response = await ping.promise.probe(targetIp); + + if (!response.alive || response.time === "unknown") { + console.warn(`Ping failed to reach ${targetIp}. Using default network speed.`); + return 200; // Default latency in ms if ping fails + } + + return response.time; // Latency in ms from ping response + } catch (error: any) { + console.error(`Ping error: ${error.message}. Using default network speed.`); + return 200; // Default latency in ms if an error occurs + } } + // Calculate optimal chunk size based on network latency, with fallback if necessary protected async calculateOptimalChunkSize(messageLength: number): Promise { - this.networkSpeed = await this.scanNetworkLatency(); + const latency = await this.scanNetworkLatency(); + this.networkSpeed = latency > 0 ? 1000 / latency : 1; // Speed in bytes/ms based on latency + + // Calculate initial chunk size based on latency (bounded between 512 and 1024 bytes) let chunkSize = Math.min(Math.max(512, Math.floor(5000 / this.networkSpeed)), 1024); - // Ensure chunk size aligns with base64 encoding (multiple of 4) + // Adjust chunk size for base64 alignment (multiple of 4) while (messageLength % chunkSize !== 0 && chunkSize > 0) { chunkSize -= 4; } - return chunkSize || 1024; + return chunkSize || 1024; // Fallback to 1024 if alignment adjustment results in 0 } }