network chunk v20

This commit is contained in:
andrei-mihnea-cerbu
2024-11-13 17:23:21 +02:00
parent f95b67f93d
commit 9baec7a7cb
41 changed files with 714 additions and 392 deletions
+24 -4
View File
@@ -1,7 +1,6 @@
import { JsonManager } from './json_manager';
import { TcpCommunicator } from "./tcp_communicator";
import { operationCodes } from '../network/operation_codes';
import { parentPort } from 'worker_threads';
import path from 'path';
import fs from 'fs';
import crypto from 'crypto';
@@ -15,6 +14,8 @@ export class BackupRetrievalWorker {
private encryptionKey: Buffer | null = null;
private iv: Buffer | null = null;
private tcpCommunicator: TcpCommunicator | null = null;
private stopRequested: boolean = false;
private isBusy: boolean = false;
constructor(userConfigPath: string, applicationInfoPath: string, clientPort: number, destinationPath: string) {
this.userConfig = new JsonManager(userConfigPath);
@@ -34,6 +35,8 @@ export class BackupRetrievalWorker {
}
async start(): Promise<void> {
if(!this.stopRequested) return;
this.isBusy = true;
try {
const userInfo = await this.userConfig.readValue('user_info');
if (!userInfo || !userInfo.name) {
@@ -62,13 +65,19 @@ export class BackupRetrievalWorker {
this.log(`Backup retrieved successfully from ${ip}`);
}
parentPort?.postMessage({ success: true, message: 'Backup retrieval completed successfully.' });
process.send?.({ type: 'showAlert', message: 'Backup retrieval completed successfully.' });
process.send?.({ type: 'changeContent', page: 'main_menu' });
} catch (error: any) {
this.log(`Error in BackupRetrievalWorker: ${error.message}`, 'error');
parentPort?.postMessage({ success: false, message: `A problem occurred: ${error.message}` });
process.send?.({ type: 'showAlert', message: `A problem occurred: ${error.message}` });
process.send?.({ type: 'changeContent', page: 'main_menu' });
}
finally {
this.isBusy = false;
}
finally{
if (global.gc) {
global.gc();
}
}
@@ -172,6 +181,17 @@ export class BackupRetrievalWorker {
}
}
async stop(): Promise<void> {
this.stopRequested = true; // Signal that stop is requested
// Wait for any ongoing process to complete if busy
while (this.isBusy) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
console.log("[BackupManager] Stopped successfully.");
}
private async waitForResponse(): Promise<ParsedMessage | null> {
return new Promise((resolve) => {
const idResponseCheck = setInterval(async () => {