program finalizat

This commit is contained in:
andrei-mihnea-cerbu
2024-10-29 15:07:26 +02:00
parent ab1eaec413
commit 979539d3db
138 changed files with 14602 additions and 5068 deletions
+62 -16
View File
@@ -7,20 +7,20 @@ import {WorkerManager} from "../helpers/worker_manager";
import {DirectoryWatcher} from "../helpers/directory_watcher";
import {QueueManager} from "../helpers/queue_manager";
import {compareFnFileItemTask, FileItemTask} from "../interfaces/file_item_task";
import {TaskScheduler} from "../helpers/task_scheduler";
import {WindowManager} from "../helpers/window_manager";
import {JsonManager} from "../helpers/json_manager";
import {MemoryManager} from "../helpers/memory_manager";
import {TcpCommunicator} from "../helpers/tcp_communicator";
import {operationCodes} from "../network/operation_codes";
import os from "os";
// Load environment variables
dotenv.config({ path: path.join(__dirname, '..', '..', '.env') });
const UDP_PORT = process.env.UDP_PORT ? parseInt(process.env.UDP_PORT) : 41233;
const TCP_PORT = process.env.TCP_PORT ? parseInt(process.env.TCP_PORT) : 41234;
const HOST = process.env.HOST || '0.0.0.0';
const HOST = getLocalIp();
let mainWindow: BrowserWindow | null = null;
let windowManager: WindowManager | null = null;
@@ -29,10 +29,11 @@ let userConfig: JsonManager | null = null;
let applicationInfo: JsonManager | null = null;
let memoryManager: MemoryManager | null = null;
let workerManager: WorkerManager | null = null;
let taskScheduler: TaskScheduler | null = null;
let backupDirectoryManager: DirectoryWatcher | null = null;
let departmentShareManager: DirectoryWatcher | null = null;
let sendFileQueue: QueueManager<FileItemTask> | null = null;
let announcementWatcher: NodeJS.Timeout | null = null;
let resetApplicationWatcher: NodeJS.Timeout | null = null;
const pathToPagesDir = path.join(__dirname, '..', '..', 'render', 'html');
const pathToWorkerDir = path.join(__dirname, '..', 'workers');
@@ -62,11 +63,6 @@ async function cleanupAndExit() {
departmentShareManager.closeWatcher(); // Add this method to DirectoryWatcher to close the watcher
}
if(taskScheduler){
console.log('Stopping all tasks...');
taskScheduler.stopAllTasks()
}
if(workerManager){
console.log('Terminating all workers...');
workerManager.closeAllWorkers()
@@ -86,6 +82,43 @@ async function ensureDirectoryExists(dirPath: string): Promise<void> {
}
}
function getLocalIp() {
const interfaces = os.networkInterfaces();
for (let interfaceName in interfaces) {
const addresses = interfaces[interfaceName];
if(!addresses) continue;
for (let address of addresses) {
// Filter for IPv4 and ignore internal (127.0.0.1) addresses
if (address.family === 'IPv4' && !address.internal) {
return address.address;
}
}
}
return ''; // Fallback if no IP is found
}
function startAnnouncementWatcher() {
const checkInterval = 5000; // Check every 5 seconds
announcementWatcher = setInterval(async () => {
if(!windowManager || !applicationInfo) return;
const announcement = await applicationInfo.readValue('announcement');
if (announcement) await windowManager.displayAnnouncement();
}, checkInterval);
}
function startResetApplicationWatcher() {
const checkInterval = 5000; // Check every 5 seconds
resetApplicationWatcher = setInterval(async () => {
if(!applicationInfo || !windowManager) return;
const resetApplicationPreferences = await applicationInfo.readValue('reset_application_preferences');
if (resetApplicationPreferences) await windowManager.changeContent('reset-database');
}, checkInterval);
}
app.whenReady().then(async () => {
const title = 'Application';
const mainScreen = require('electron').screen.getPrimaryDisplay();
@@ -103,6 +136,8 @@ app.whenReady().then(async () => {
},
});
mainWindow.removeMenu();
await ensureDirectoryExists(pathToJsons);
await ensureDirectoryExists(pathToClientsBackups);
@@ -112,14 +147,20 @@ app.whenReady().then(async () => {
memoryManager = new MemoryManager(path.join(pathToJsons, 'memory.json'));
sendFileQueue = new QueueManager(path.join(pathToJsons, 'sendFileTasks.json'), compareFnFileItemTask);
taskScheduler = new TaskScheduler(applicationInfo, windowManager);
workerManager = new WorkerManager(pathToWorkerDir, windowManager);
await userConfig.writeValue('app_type', 'client');
await applicationInfo.writeValue('users_ip', []);
await applicationInfo.writeValue('serverIp', '');
await applicationInfo.writeValue('announcement', '');
await applicationInfo.writeValue('reset_application_preferences', false);
await memoryManager.resetFile();
workerManager.startWatchersWorker(path.join(pathToJsons, 'memory.json'), path.join(pathToJsons, 'application.json'));
startAnnouncementWatcher();
startResetApplicationWatcher();
workerManager.startNetworkScannerWorker(UDP_PORT, TCP_PORT, 'login', 'uc_not_found', 'reset_database', path.join(pathToJsons, 'userConfig.json'), path.join(pathToJsons, 'application.json'));
workerManager.startDirectoriesWatchersWorker(path.join(pathToJsons, 'memory.json'), path.join(pathToJsons, 'application.json'));
workerManager.startServersWorker(HOST, UDP_PORT, TCP_PORT);
workerManager.startResourceCoordinatorWorker(
path.join(pathToJsons, 'userConfig.json'),
@@ -129,10 +170,6 @@ app.whenReady().then(async () => {
TCP_PORT
);
taskScheduler.startUCCheck(UDP_PORT, 'login', 'uc_not_found');
taskScheduler.startUserIPLookup(UDP_PORT);
registerIPCHandlers();
await windowManager.changeContent('welcome');
@@ -251,7 +288,11 @@ function registerIPCHandlers() {
ipcMain.handle('reset-application-json-files', async () => {
if (!applicationInfo) throw new Error('ApplicationInfo is not initialized.');
return applicationInfo.resetFile();
const serverIp = await applicationInfo.readValue('serverIp');
await applicationInfo.resetFile();
if(serverIp) {
await applicationInfo.writeValue('serverIp', serverIp);
}
});
ipcMain.handle('remove-application-json-files-key', async (_event: IpcMainInvokeEvent, key: string) => {
@@ -280,13 +321,18 @@ function registerIPCHandlers() {
return memoryManager.removeMetaInformation(id);
});
ipcMain.handle('memory-reset', async () => {
if (!memoryManager) throw new Error('MemoryManager is not initialized.');
return memoryManager.resetFile();
});
// Queue IPC Handlers
ipcMain.handle('add-task-to-send-file-queue', async (event: IpcMainInvokeEvent, task: FileItemTask) => {
if (!sendFileQueue) throw new Error('SendFileQueue is not initialized.');
sendFileQueue.enqueue(task);
});
// BackupRetrievalWorker IPC Handler
// Workers IPC Handlers
ipcMain.handle('start-backup-retrieval', async (_event: IpcMainInvokeEvent, destinationPath: string) => {
if (!workerManager) throw new Error('WorkerManager is not initialized.');
return workerManager.startBackupRetrievalWorker(
+3 -2
View File
@@ -27,6 +27,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
readMemoryEntry: (id: string): Promise<any> => ipcRenderer.invoke('memory-read-entry', id),
updateMemoryEntry: (id: string, data: any): Promise<boolean> => ipcRenderer.invoke('memory-update-entry', id, data),
removeMemoryEntry: (id: string): Promise<boolean> => ipcRenderer.invoke('memory-remove-entry', id),
resetMemory: (): Promise<boolean> => ipcRenderer.invoke('memory-reset'),
// UI methods
showAlert: (message: string): Promise<void> => ipcRenderer.invoke('show-alert', message),
@@ -38,6 +39,6 @@ contextBridge.exposeInMainWorld('electronAPI', {
// Queue methods
addTaskToSendFileQueue: (task: FileItemTask): Promise<void> => ipcRenderer.invoke('add-task-to-send-file-queue', task),
// BackupRetrievalWorker
startBackupRetrieval: (destinationPath: string): Promise<void> => ipcRenderer.invoke('start-backup-retrieval', destinationPath)
// Workers
startBackupRetrieval: (destinationPath: string): Promise<void> => ipcRenderer.invoke('start-backup-retrieval', destinationPath),
});