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
+20 -2
View File
@@ -14,6 +14,8 @@ export class BackupManager {
private userConfig: JsonManager;
private readonly clientPort: number;
private isBusy: boolean = false;
private intervalId: NodeJS.Timeout | null = null;
private stopRequested: boolean = false;
constructor(userConfigPath: string, applicationInfoPath: string, memoryManagerPath: string, clientPort: number) {
this.applicationInfo = new JsonManager(applicationInfoPath);
@@ -23,8 +25,8 @@ export class BackupManager {
}
async start(): Promise<void> {
setInterval(async () => {
if (!this.isBusy) {
this.intervalId = setInterval(async () => {
if (!this.isBusy || !this.stopRequested) {
this.isBusy = true;
this.log('Start successfully. Backup files to users.');
await this.initialize();
@@ -160,6 +162,22 @@ export class BackupManager {
});
}
async stop(): Promise<void> {
this.stopRequested = true; // Signal that stop is requested
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
// Wait for any ongoing process to complete if busy
while (this.isBusy) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
console.log("[BackupManager] Stopped successfully.");
}
// Unified logging function
private log(message: string, level: 'log' | 'error' | 'warn' = 'log'): void {
const prefix = '[BackupManager]';
+22 -2
View File
@@ -14,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);
@@ -33,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) {
@@ -61,10 +65,15 @@ export class BackupRetrievalWorker {
this.log(`Backup retrieved successfully from ${ip}`);
}
process.send?.({ type: 'log', 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');
process.send?.({ type: 'log', 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;
}
if (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 () => {
+20 -2
View File
@@ -14,6 +14,8 @@ export class DepartmentSharer {
private readonly clientPort: number;
private isBusy: boolean = false;
private tcpCommunicator: TcpCommunicator | null = null;
private stopRequested: boolean = false;
private intervalId: NodeJS.Timeout | null = null;
constructor(
userConfigPath: string,
@@ -30,8 +32,8 @@ export class DepartmentSharer {
// Start sharing files with the department every minute
async start(): Promise<void> {
setInterval(async () => {
if (!this.isBusy) {
this.intervalId = setInterval(async () => {
if (!this.isBusy || !this.stopRequested) {
this.isBusy = true;
this.log('Start successfully. Sharing files with the department.');
await this.shareFilesWithDepartment();
@@ -172,6 +174,22 @@ export class DepartmentSharer {
}
}
async stop(): Promise<void> {
this.stopRequested = true; // Signal that stop is requested
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
// 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 () => {
+20 -2
View File
@@ -17,6 +17,8 @@ export class FileSharer {
private readonly clientPort: number;
private isBusy: boolean;
private tcpCommunicator: TcpCommunicator | null = null;
private stopRequested: boolean = false;
private intervalId: NodeJS.Timeout | null = null;
constructor(queueFilePath: string, clientPort: number) {
this.queueManager = new QueueManager<FileItemTask>(queueFilePath, compareFnFileItemTask);
@@ -26,8 +28,8 @@ export class FileSharer {
// Start processing the file queue
async start(): Promise<void> {
setInterval(async () => {
if (!this.isBusy) { // Check if the queue is already being processed
this.intervalId = setInterval(async () => {
if (!this.isBusy || !this.stopRequested) { // Check if the queue is already being processed
this.isBusy = true; // Set busy flag to true before starting
this.log("Start successfully. Processing the queue.");
await this.processQueue();
@@ -104,6 +106,22 @@ export class FileSharer {
return true;
}
async stop(): Promise<void> {
this.stopRequested = true; // Signal that stop is requested
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
// 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 () => {
+2 -2
View File
@@ -54,7 +54,7 @@ export class NetworkScanner {
try {
this.log('UC Check running...', 'log', 'startUCCheck');
const udpClient = new UdpClient(this.udpPort);
const aliveClients = await udpClient.getAliveClients();
const aliveClients = await udpClient.getTargetClients(operationCodes.ARE_YOU_UC);
const storedIp = await this.applicationInfo.readValue('serverIp');
const foundClient = aliveClients.length > 0;
@@ -95,7 +95,7 @@ export class NetworkScanner {
this.log('IP Lookup running...', 'log', 'startUserIPLookup');
const serverIp = await this.applicationInfo.readValue('serverIp');
const udpClient = new UdpClient(this.udpPort);
const activeIPs = await udpClient.getAliveClients();
const activeIPs = await udpClient.getTargetClients(operationCodes.ARE_YOU_HUMAN);
const filteredIPs = activeIPs.filter(ip => ip !== serverIp);
// Save the filtered IPs to 'users_ip'
+11 -2
View File
@@ -11,6 +11,7 @@ export class UsersInfoFetcher {
private readonly clientPort: number;
private memoryId: string;
private readonly activeUsersKey: string;
private intervalId: NodeJS.Timeout | null = null;
constructor(applicationInfoPath: string, memoryManagerPath: string, clientPort: number) {
this.applicationInfo = new JsonManager(applicationInfoPath);
@@ -23,13 +24,13 @@ export class UsersInfoFetcher {
// Method to start checking user info periodically (every minute)
async start(): Promise<void> {
setInterval(async () => {
this.intervalId = setInterval(async () => {
await this.initialize(); // Re-run every minute
if (global.gc) {
global.gc();
}
}, 5000); // 1 minute interval
}, 5000); // 5-second interval for testing
}
// Initialize and fetch user IPs and process users info
@@ -101,6 +102,14 @@ export class UsersInfoFetcher {
await this.memoryManager.updateMetaInformation(this.memoryId, userInfo); // Update active users in memory
}
stop(): void {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
console.log("[UsersInfoFetcher] Stopped successfully.");
}
}
// Unified logging function
private log(message: string, level: 'log' | 'error' = 'log'): void {
const prefix = '[UsersInfoFetcher]';
+14 -6
View File
@@ -5,7 +5,8 @@ import { WindowManager } from "./window_manager";
export class WorkerManager {
private readonly pathToWorkerDir: string;
private windowManager: WindowManager;
private workers: ChildProcess[]; // Array to store running child processes
private workers: ChildProcess[];
private cleanupInProgress: boolean = false;
constructor(pathToWorkerDir: string, windowManager: WindowManager) {
this.pathToWorkerDir = pathToWorkerDir;
@@ -87,17 +88,24 @@ export class WorkerManager {
reject(err);
});
worker.on('exit', (code) => {
console.log(`${scriptName} exited with code ${code}`);
worker.on('exit', (code, signal) => {
this.removeWorker(worker);
if (code === 0) resolve();
else reject(new Error(`${scriptName} exited with code ${code}`));
if (code === 0) {
console.log(`${scriptName} exited successfully`);
resolve();
} else if (signal) {
console.log(`${scriptName} was killed with signal: ${signal}`);
} else {
console.error(`${scriptName} exited with code: ${code}`);;
}
});
});
}
// Close all running workers
closeAllWorkers(): void {
if (this.cleanupInProgress) return; // Prevent duplicate cleanup
this.cleanupInProgress = true;
console.log('Terminating all running workers...');
this.workers.forEach(worker => worker.kill());
this.workers = [];