network chunk v18

This commit is contained in:
andrei-mihnea-cerbu
2024-11-13 16:02:13 +02:00
parent ca1e77f322
commit 207580018f
11 changed files with 158 additions and 215 deletions
+4 -4
View File
@@ -6,10 +6,10 @@
"scripts": { "scripts": {
"clean": "del-cli dist && del-cli out", "clean": "del-cli dist && del-cli out",
"build-dist": "tsc && copyfiles -u 1 'src/**/*' dist", "build-dist": "tsc && copyfiles -u 1 'src/**/*' dist",
"start-dev": "tsc && copyfiles -u 1 'src/**/*' dist && electron dist/main/main.js", "start-dev": "tsc && copyfiles -u 1 'src/**/*' dist && electron dist/main/main.js -- --expose-gc",
"start": "npm run clean && npm run build-dist && electron-forge start", "start": "npm run clean && npm run build-dist && electron-forge start -- --expose-gc",
"package": "npm run clean && npm run build-dist && electron-forge package", "package": "npm run clean && npm run build-dist && electron-forge package -- --expose-gc",
"make": "npm run clean && npm run build-dist && electron-forge make" "make": "npm run clean && npm run build-dist && electron-forge make -- --expose-gc"
}, },
"main": "dist/main/main.js", "main": "dist/main/main.js",
"author": "Cerbu Andrei - Mihnea", "author": "Cerbu Andrei - Mihnea",
+2 -3
View File
@@ -5,7 +5,6 @@ import { MemoryManager } from './memory_manager';
import { JsonManager } from './json_manager'; import { JsonManager } from './json_manager';
import { TcpCommunicator } from './tcp_communicator'; import { TcpCommunicator } from './tcp_communicator';
import { operationCodes } from '../network/operation_codes'; import { operationCodes } from '../network/operation_codes';
import { parentPort } from 'worker_threads';
import { ParsedMessage } from "../network/message_handler"; import { ParsedMessage } from "../network/message_handler";
export class BackupManager { export class BackupManager {
@@ -143,9 +142,9 @@ export class BackupManager {
} }
if (unsentFiles.length > 0) { if (unsentFiles.length > 0) {
parentPort?.postMessage({ success: false, message: 'Backup could not be completed for all files', unsentFiles }); process.send?.({type: 'log', message: 'Backup could not be completed for all files'});
} else { } else {
parentPort?.postMessage({ success: true, message: 'Backup completed successfully' }); process.send?.({type: 'log', message: 'Backup completed successfully' });
} }
} }
+2 -3
View File
@@ -1,7 +1,6 @@
import { JsonManager } from './json_manager'; import { JsonManager } from './json_manager';
import { TcpCommunicator } from "./tcp_communicator"; import { TcpCommunicator } from "./tcp_communicator";
import { operationCodes } from '../network/operation_codes'; import { operationCodes } from '../network/operation_codes';
import { parentPort } from 'worker_threads';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import crypto from 'crypto'; import crypto from 'crypto';
@@ -62,10 +61,10 @@ export class BackupRetrievalWorker {
this.log(`Backup retrieved successfully from ${ip}`); this.log(`Backup retrieved successfully from ${ip}`);
} }
parentPort?.postMessage({ success: true, message: 'Backup retrieval completed successfully.' }); process.send?.({ type: 'log', message: 'Backup retrieval completed successfully.' });
} catch (error: any) { } catch (error: any) {
this.log(`Error in BackupRetrievalWorker: ${error.message}`, 'error'); this.log(`Error in BackupRetrievalWorker: ${error.message}`, 'error');
parentPort?.postMessage({ success: false, message: `A problem occurred: ${error.message}` }); process.send?.({ type: 'log', message: `A problem occurred: ${error.message}` });
} }
if (global.gc) { if (global.gc) {
+5 -6
View File
@@ -1,6 +1,5 @@
import {JsonManager} from "./json_manager"; import {JsonManager} from "./json_manager";
import {UdpClient} from "../network/udp/udp_client"; import {UdpClient} from "../network/udp/udp_client";
import {parentPort} from "worker_threads";
import {TcpCommunicator} from "./tcp_communicator"; import {TcpCommunicator} from "./tcp_communicator";
import {operationCodes} from "../network/operation_codes"; import {operationCodes} from "../network/operation_codes";
import {ParsedMessage} from "../network/message_handler"; import {ParsedMessage} from "../network/message_handler";
@@ -65,19 +64,19 @@ export class NetworkScanner {
if (!storedIp || storedIp !== ipAddress) { if (!storedIp || storedIp !== ipAddress) {
await this.applicationInfo.writeValue('serverIp', ipAddress); await this.applicationInfo.writeValue('serverIp', ipAddress);
if (!this.appStarted) { if (!this.appStarted) {
parentPort?.postMessage({ type: 'changeContent', page: this.okPage }); process.send?.({ type: 'changeContent', page: this.okPage });
} }
this.appStarted = true; this.appStarted = true;
} else if (!this.appStarted) { } else if (!this.appStarted) {
parentPort?.postMessage({ type: 'changeContent', page: this.okPage }); process.send?.({ type: 'changeContent', page: this.okPage });
this.appStarted = true; this.appStarted = true;
} }
} else { } else {
parentPort?.postMessage({ type: 'changeContent', page: this.errorPage }); process.send?.({ type: 'changeContent', page: this.errorPage });
} }
} catch (err) { } catch (err) {
this.log(`Error checking UC: ${err}`, 'error', 'startUCCheck'); this.log(`Error checking UC: ${err}`, 'error', 'startUCCheck');
parentPort?.postMessage({ type: 'changeContent', page: this.errorPage }); process.send?.({ type: 'changeContent', page: this.errorPage });
} finally { } finally {
this.ucCheckBusy = false; this.ucCheckBusy = false;
} }
@@ -152,7 +151,7 @@ export class NetworkScanner {
if (response?.operationCode !== operationCodes.OK) { if (response?.operationCode !== operationCodes.OK) {
await this.userConfig.resetFile(); await this.userConfig.resetFile();
await this.userConfig.writeValue('app_type', app_type); await this.userConfig.writeValue('app_type', app_type);
parentPort?.postMessage({ type: 'changeContent', page: this.databaseResetPage }); process.send?.({ type: 'changeContent', page: this.databaseResetPage });
} }
} catch (err) { } catch (err) {
this.log(`Error during login request: ${err}`, 'error', 'sendLoginRequest'); this.log(`Error during login request: ${err}`, 'error', 'sendLoginRequest');
+61 -138
View File
@@ -1,174 +1,97 @@
import { Worker } from 'worker_threads'; import { fork, ChildProcess } from 'child_process';
import path from 'path'; import path from 'path';
import {WindowManager} from "./window_manager"; import { WindowManager } from "./window_manager";
export class WorkerManager { export class WorkerManager {
private readonly pathToWorkerDir: string; private readonly pathToWorkerDir: string;
private windowManager: WindowManager private windowManager: WindowManager;
private workers: Worker[]; // Array to store running workers private workers: ChildProcess[]; // Array to store running child processes
constructor(pathToWorkerDir: string, windowManager: WindowManager) { constructor(pathToWorkerDir: string, windowManager: WindowManager) {
this.pathToWorkerDir = pathToWorkerDir; this.pathToWorkerDir = pathToWorkerDir;
this.windowManager = windowManager; this.windowManager = windowManager;
this.workers = []; // Initialize the array to store workers this.workers = []; // Initialize the array to store child processes
} }
async startNetworkScannerWorker(udpPort: number, tcpPort: number, okPage: string, errorPage: string, databaseResetPage: string, userConfigPath: string, applicationInfoPath: string): Promise<void> { async startNetworkScannerWorker(udpPort: number, tcpPort: number, okPage: string, errorPage: string, databaseResetPage: string, userConfigPath: string, applicationInfoPath: string): Promise<void> {
return new Promise((resolve, reject) => { return this.startForkedWorker('network_scanner_worker.js', {
const worker = new Worker(path.join(this.pathToWorkerDir, 'network_scanner_worker.js', ), { UDP_PORT: udpPort.toString(),
execArgv: ['--max-old-space-size=4096'], TCP_PORT: tcpPort.toString(),
workerData: { udpPort, tcpPort, okPage, errorPage, databaseResetPage, userConfigPath, applicationInfoPath }, // Pass necessary data to the worker OK_PAGE: okPage,
}); ERROR_PAGE: errorPage,
DATABASE_RESET_PAGE: databaseResetPage,
this.workers.push(worker); // Store the worker reference USER_CONFIG_PATH: userConfigPath,
APPLICATION_INFO_PATH: applicationInfoPath
worker.on('message', (data) => {
if (data.type === 'changeContent') {
this.windowManager.changeContent(data.page);
}
});
worker.on('error', (err) => {
console.error('Network Scanner Worker error:', err);
worker.terminate();
this.removeWorker(worker);
reject(err); // Reject the promise if there's an error
});
worker.on('exit', (code) => {
console.log(`Network Scanner Worker exited with code ${code}`);
this.removeWorker(worker); // Remove worker reference when it exits
resolve(); // Resolve when the worker exits cleanly
});
}); });
} }
// Start the Directories Watcher Worker
async startDirectoriesWatchersWorker(memoryManagerPath: string, applicationInfoPath: string): Promise<void> { async startDirectoriesWatchersWorker(memoryManagerPath: string, applicationInfoPath: string): Promise<void> {
return new Promise((resolve, reject) => { return this.startForkedWorker('directories_watcher_worker.js', {
const worker = new Worker(path.join(this.pathToWorkerDir, 'directories_watcher_worker.js'), { MEMORY_MANAGER_PATH: memoryManagerPath,
execArgv: ['--max-old-space-size=4096'], APPLICATION_INFO_PATH: applicationInfoPath
workerData: { memoryManagerPath, applicationInfoPath }, // Pass the port to the worker
});
this.workers.push(worker); // Store the worker reference
worker.on('message', (data) => {
console.log('DirectoriesWatcher message:', data);
});
worker.on('error', (err) => {
console.error('DirectoriesWatcher error:', err);
worker.terminate();
this.removeWorker(worker);
reject(err); // Reject the promise if there's an error
});
worker.on('exit', (code) => {
console.log(`DirectoriesWatcher exited with code ${code}`);
this.removeWorker(worker); // Remove worker reference when it exits
resolve(); // Resolve when the worker exits cleanly
});
}); });
} }
// Start the Servers Worker (UDP and TCP servers)
async startServersWorker(host: string, udpPort: number, tcpPort: number): Promise<void> { async startServersWorker(host: string, udpPort: number, tcpPort: number): Promise<void> {
return new Promise((resolve, reject) => { return this.startForkedWorker('servers_worker.js', {
const worker = new Worker(path.join(this.pathToWorkerDir, 'servers_worker.js'), { HOST: host,
execArgv: ['--max-old-space-size=4096'], USER_UDP_PORT: udpPort.toString(),
workerData: { HOST: host, USER_UDP_PORT: udpPort, USER_TCP_PORT: tcpPort }, // Pass host and ports to the worker USER_TCP_PORT: tcpPort.toString()
});
this.workers.push(worker); // Store the worker reference
worker.on('message', (data) => {
console.log('Servers Worker message:', data);
});
worker.on('error', (err) => {
console.error('Servers Worker error:', err);
worker.terminate();
this.removeWorker(worker);
reject(err); // Reject the promise if there's an error
});
worker.on('exit', (code) => {
console.log(`Servers Worker exited with code ${code}`);
this.removeWorker(worker); // Remove worker reference when it exits
resolve(); // Resolve when the worker exits cleanly
});
}); });
} }
// Start the Users Info Worker
async startResourceCoordinatorWorker(usersConfigPath: string, applicationInfoPath: string, memoryManagerPath: string, queueManagerPath: string, tcpPort: number): Promise<void> { async startResourceCoordinatorWorker(usersConfigPath: string, applicationInfoPath: string, memoryManagerPath: string, queueManagerPath: string, tcpPort: number): Promise<void> {
return new Promise((resolve, reject) => { return this.startForkedWorker('resource_coordinator_worker.js', {
const worker = new Worker(path.join(this.pathToWorkerDir, 'resource_coordinator_worker.js'), { USERS_CONFIG_PATH: usersConfigPath,
execArgv: ['--max-old-space-size=4096'], APPLICATION_INFO_PATH: applicationInfoPath,
workerData: { MEMORY_MANAGER_PATH: memoryManagerPath,
usersConfigPath, QUEUE_MANAGER_PATH: queueManagerPath,
applicationInfoPath, TCP_PORT: tcpPort.toString()
memoryManagerPath,
queueManagerPath,
tcpPort
}, // Pass necessary parameters to the worker
});
this.workers.push(worker); // Store the worker reference
worker.on('message', (data) => {
console.log('Users Info Worker message:', data);
});
worker.on('error', (err) => {
console.error('Users Info Worker error:', err);
worker.terminate();
this.removeWorker(worker);
reject(err); // Reject the promise if there's an error
});
worker.on('exit', (code) => {
console.log(`Users Info Worker exited with code ${code}`);
this.removeWorker(worker); // Remove worker reference when it exits
resolve(); // Resolve when the worker exits cleanly
});
}); });
} }
// Start the Backup Retrieval Worker async startBackupRetrievalWorker(userConfigPath: string, applicationInfoPath: string, clientPort: number, destinationPath: string): Promise<void> {
async startBackupRetrievalWorker( return this.startForkedWorker('backup_retrieval_worker.js', {
userConfigPath: string, USER_CONFIG_PATH: userConfigPath,
applicationInfoPath: string, APPLICATION_INFO_PATH: applicationInfoPath,
clientPort: number, CLIENT_PORT: clientPort.toString(),
destinationPath: string DESTINATION_PATH: destinationPath
): Promise<void> { });
return new Promise(async (resolve, reject) => { }
const worker = new Worker(path.join(this.pathToWorkerDir, 'backup_retrieval_worker.js'), {
execArgv: ['--max-old-space-size=4096'], private async startForkedWorker(scriptName: string, envData: { [key: string]: string }): Promise<void> {
workerData: { userConfigPath, applicationInfoPath, clientPort, destinationPath }, // Pass parameters to the worker return new Promise((resolve, reject) => {
const worker = fork(path.join(this.pathToWorkerDir, scriptName), {
execArgv: ['--max-old-space-size=4096'], // Set memory limit for the forked process
env: { ...process.env, ...envData } // Merge environment variables
}); });
this.workers.push(worker); // Store the worker reference this.workers.push(worker); // Store the worker reference
worker.on('message', async (data) => { worker.on('message', (data: unknown) => {
console.log('Backup Retrieval Worker message:', data); const message = data as { type: string, page?: string, message?: string }; // Type casting for message
await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait for 1 second
await this.windowManager.changeContent('main_menu'); if (message.type === 'changeContent' && message.page) {
await this.windowManager.showAlert(data.message); this.windowManager.changeContent(message.page);
} else if (message.type === 'showAlert' && message.message) {
this.windowManager.showAlert(message.message);
} else {
console.log(`${scriptName} message:`, message);
}
}); });
worker.on('error', (err) => { worker.on('error', (err) => {
console.error('Backup Retrieval Worker error:', err); console.error(`${scriptName} error:`, err);
worker.terminate(); worker.kill();
this.removeWorker(worker); this.removeWorker(worker);
reject(err); // Reject the promise if there's an error reject(err);
}); });
worker.on('exit', (code) => { worker.on('exit', (code) => {
console.log(`Backup Retrieval Worker exited with code ${code}`); console.log(`${scriptName} exited with code ${code}`);
this.removeWorker(worker); // Remove worker reference when it exits this.removeWorker(worker);
resolve(); // Resolve when the worker exits cleanly if (code === 0) resolve();
else reject(new Error(`${scriptName} exited with code ${code}`));
}); });
}); });
} }
@@ -176,15 +99,15 @@ export class WorkerManager {
// Close all running workers // Close all running workers
closeAllWorkers(): void { closeAllWorkers(): void {
console.log('Terminating all running workers...'); console.log('Terminating all running workers...');
this.workers.forEach(worker => worker.terminate()); // Terminate each worker this.workers.forEach(worker => worker.kill());
this.workers = []; // Clear the array after terminating all workers this.workers = [];
} }
// Helper method to remove a worker from the workers array when it exits // Helper method to remove a worker from the workers array when it exits
private removeWorker(worker: Worker): void { private removeWorker(worker: ChildProcess): void {
const index = this.workers.indexOf(worker); const index = this.workers.indexOf(worker);
if (index > -1) { if (index > -1) {
this.workers.splice(index, 1); // Remove the worker from the array this.workers.splice(index, 1);
} }
} }
} }
+6
View File
@@ -0,0 +1,6 @@
// Define a type for the message structure
interface WorkerMessage {
type: 'changeContent' | 'showAlert' | 'log';
page?: string;
message?: string;
}
+16 -14
View File
@@ -1,18 +1,20 @@
import { workerData, parentPort } from 'worker_threads'; import { BackupRetrievalWorker } from '../helpers/backup_retrieval';
import { BackupRetrievalWorker} from '../helpers/backup_retrieval'; // Assuming the class is in the same folder import dotenv from 'dotenv';
// Destructure the data passed from the WorkerManager // Load environment variables from .env file if it exists
const { dotenv.config();
userConfigPath,
applicationInfoPath, // Retrieve configuration from environment variables
clientPort, const userConfigPath = process.env.USER_CONFIG_PATH as string;
destinationPath const applicationInfoPath = process.env.APPLICATION_INFO_PATH as string;
}: { const clientPort = Number(process.env.CLIENT_PORT);
userConfigPath: string, const destinationPath = process.env.DESTINATION_PATH as string;
applicationInfoPath: string,
clientPort: number, // Validate that all required environment variables are present
destinationPath: string if (!userConfigPath || !applicationInfoPath || !clientPort || !destinationPath) {
} = workerData; console.error('Error: Missing required environment variables.');
process.exit(1);
}
// Initialize the BackupRetrievalWorker // Initialize the BackupRetrievalWorker
const backupRetrievalWorker = new BackupRetrievalWorker( const backupRetrievalWorker = new BackupRetrievalWorker(
+15 -7
View File
@@ -1,11 +1,20 @@
import {DirectoryWatcher} from "../helpers/directory_watcher"; import { DirectoryWatcher } from "../helpers/directory_watcher";
import {workerData} from "worker_threads"; import dotenv from 'dotenv';
const { // Load environment variables from .env file if it exists
memoryManagerPath, dotenv.config();
applicationInfoPath,
} = workerData;
// Retrieve configuration from environment variables
const memoryManagerPath = process.env.MEMORY_MANAGER_PATH as string;
const applicationInfoPath = process.env.APPLICATION_INFO_PATH as string;
// Validate that all required environment variables are present
if (!memoryManagerPath || !applicationInfoPath) {
console.error('Error: Missing required environment variables.');
process.exit(1);
}
// Initialize and start DirectoryWatcher instances
const backupDirectoryManager = new DirectoryWatcher(memoryManagerPath, applicationInfoPath, 'backupDirectory'); const backupDirectoryManager = new DirectoryWatcher(memoryManagerPath, applicationInfoPath, 'backupDirectory');
backupDirectoryManager.start(); backupDirectoryManager.start();
@@ -14,4 +23,3 @@ departmentShareManager.start();
const shareFileManager = new DirectoryWatcher(memoryManagerPath, applicationInfoPath, 'shareDirectory'); const shareFileManager = new DirectoryWatcher(memoryManagerPath, applicationInfoPath, 'shareDirectory');
shareFileManager.start(); shareFileManager.start();
+18 -15
View File
@@ -1,19 +1,22 @@
import { parentPort, workerData } from 'worker_threads'; import { parentPort } from 'worker_threads';
import { NetworkScanner } from '../helpers/network_scanner'; import { NetworkScanner } from '../helpers/network_scanner';
// Define the structure of workerData // Extract data from environment variables
interface WorkerData { const udpPort = parseInt(process.env.UDP_PORT || '0', 10);
udpPort: number; const tcpPort = parseInt(process.env.TCP_PORT || '0', 10);
tcpPort: number; const okPage = process.env.OK_PAGE || '';
okPage: string; const errorPage = process.env.ERROR_PAGE || '';
errorPage: string; const databaseResetPage = process.env.DATABASE_RESET_PAGE || '';
databaseResetPage: string; const userConfigPath = process.env.USER_CONFIG_PATH || '';
userConfigPath: string; const applicationInfoPath = process.env.APPLICATION_INFO_PATH || '';
applicationInfoPath: string;
}
// Extract the data passed to the worker
const { udpPort, tcpPort, okPage, errorPage, databaseResetPage, userConfigPath, applicationInfoPath }: WorkerData = workerData;
// Start the NetworkScanner instance // Start the NetworkScanner instance
const networkScanner = new NetworkScanner(applicationInfoPath, userConfigPath, udpPort, tcpPort, okPage, errorPage, databaseResetPage); const networkScanner = new NetworkScanner(
applicationInfoPath,
userConfigPath,
udpPort,
tcpPort,
okPage,
errorPage,
databaseResetPage
);
@@ -1,11 +1,14 @@
import { workerData } from 'worker_threads';
import { UsersInfoFetcher } from '../helpers/users_info_fetcher'; import { UsersInfoFetcher } from '../helpers/users_info_fetcher';
import {BackupManager} from "../helpers/backup_manager"; import { BackupManager } from '../helpers/backup_manager';
import {FileSharer} from "../helpers/file_sharer"; import { FileSharer } from '../helpers/file_sharer';
import {DepartmentSharer} from "../helpers/department_sharer"; import { DepartmentSharer } from '../helpers/department_sharer';
// Destructure the required information from workerData // Retrieve data from environment variables
const { usersConfigPath, applicationInfoPath, memoryManagerPath, queueManagerPath, tcpPort } = workerData; const usersConfigPath = process.env.USERS_CONFIG_PATH || '';
const applicationInfoPath = process.env.APPLICATION_INFO_PATH || '';
const memoryManagerPath = process.env.MEMORY_MANAGER_PATH || '';
const queueManagerPath = process.env.QUEUE_MANAGER_PATH || '';
const tcpPort = parseInt(process.env.TCP_PORT || '0', 10);
const usersInfoFetcher = new UsersInfoFetcher(applicationInfoPath, memoryManagerPath, tcpPort); const usersInfoFetcher = new UsersInfoFetcher(applicationInfoPath, memoryManagerPath, tcpPort);
usersInfoFetcher.start(); usersInfoFetcher.start();
@@ -16,7 +19,5 @@ backupManager.start();
const fileSharer = new FileSharer(queueManagerPath, tcpPort); const fileSharer = new FileSharer(queueManagerPath, tcpPort);
fileSharer.start(); fileSharer.start();
const departmentSharer = new DepartmentSharer(usersConfigPath, applicationInfoPath, memoryManagerPath, tcpPort); const departmentSharer = new DepartmentSharer(usersConfigPath, applicationInfoPath, memoryManagerPath, tcpPort);
departmentSharer.start(); departmentSharer.start();
+7 -4
View File
@@ -1,12 +1,15 @@
import {UdpServer} from "../network/udp/udp_server"; import { UdpServer } from "../network/udp/udp_server";
import {TcpServer} from "../network/tcp/tcp_server"; import { TcpServer } from "../network/tcp/tcp_server";
import {workerData} from "worker_threads";
let udpServer: UdpServer | null = null; let udpServer: UdpServer | null = null;
let tcpServer: TcpServer | null = null; let tcpServer: TcpServer | null = null;
const {USER_UDP_PORT, USER_TCP_PORT, HOST} = workerData; // Retrieve data from environment variables
const USER_UDP_PORT = parseInt(process.env.USER_UDP_PORT || '0', 10);
const USER_TCP_PORT = parseInt(process.env.USER_TCP_PORT || '0', 10);
const HOST = process.env.HOST || '';
// Initialize and start the servers
udpServer = new UdpServer(HOST, USER_UDP_PORT); udpServer = new UdpServer(HOST, USER_UDP_PORT);
udpServer.start(); udpServer.start();