overall v1
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { TcpCommunicator } from './tcp_communicator'; // Using TcpCommunicator instead of TcpClient
|
||||
import { TcpCommunicator } from './tcp_communicator';
|
||||
import { operationCodes } from '../network/operation_codes';
|
||||
import { JsonManager } from './json_manager'; // Manages JSON configurations
|
||||
import { MemoryManager } from './memory_manager'; // Manages in-memory data structures
|
||||
import { JsonManager } from './json_manager';
|
||||
import { MemoryManager } from './memory_manager';
|
||||
import { ParsedMessage } from "../network/message_handler";
|
||||
|
||||
export class DepartmentSharer {
|
||||
private userConfig: JsonManager;
|
||||
private applicationInfo: JsonManager;
|
||||
private memoryManager: MemoryManager; // To read the department files
|
||||
private memoryManager: MemoryManager;
|
||||
private departmentDirectory: string | null;
|
||||
private readonly clientPort: number;
|
||||
private isBusy: boolean = false; // Busy flag to prevent simultaneous sharing
|
||||
private tcpCommunicator: TcpCommunicator | null = null; // For each user connection
|
||||
private isBusy: boolean = false;
|
||||
private tcpCommunicator: TcpCommunicator | null = null;
|
||||
|
||||
constructor(
|
||||
userConfigPath: string,
|
||||
@@ -32,21 +32,20 @@ export class DepartmentSharer {
|
||||
async start(): Promise<void> {
|
||||
setInterval(async () => {
|
||||
if (!this.isBusy) {
|
||||
this.isBusy = true;
|
||||
this.log('Start successfully. Sharing files with the department.');
|
||||
await this.shareFilesWithDepartment();
|
||||
this.isBusy = false;
|
||||
}
|
||||
}, 10000); // 10-second interval for testing
|
||||
}
|
||||
|
||||
// Share files with users in the same department
|
||||
private async shareFilesWithDepartment(): Promise<void> {
|
||||
console.log('\n\nStarting Department Share Process\n\n');
|
||||
this.isBusy = true;
|
||||
|
||||
// Get the current user's department information
|
||||
const userInfo = await this.userConfig.readValue('user_info');
|
||||
if (!userInfo || !userInfo.departmentId || !userInfo.name) {
|
||||
console.error('User information or department ID is missing in the configuration.');
|
||||
this.log('User information or department ID is missing in the configuration.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -56,27 +55,27 @@ export class DepartmentSharer {
|
||||
// Get the list of active users from applicationInfo
|
||||
const activeUsersId = await this.applicationInfo.readValue('active_users_info');
|
||||
if (!activeUsersId) {
|
||||
console.error('No active users found.');
|
||||
this.log('No active users found.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const activeUsers = await this.memoryManager.retrieveMetaInformation(activeUsersId);
|
||||
if (!activeUsers || activeUsers.length === 0) {
|
||||
console.error('No active users found.');
|
||||
this.log('No active users found.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Filter users who belong to the same department
|
||||
const departmentUsers = activeUsers.filter((user: any) => user.user_info.departmentId === departmentId);
|
||||
if (departmentUsers.length === 0) {
|
||||
console.log('No users found in the same department.');
|
||||
this.log('No users found in the same department.', 'log');
|
||||
return;
|
||||
}
|
||||
|
||||
// Get department directory info
|
||||
const departmentData = await this.applicationInfo.readValue('departmentDirectory');
|
||||
if (!departmentData || !departmentData.path || !departmentData.id) {
|
||||
console.error('No department directory found.');
|
||||
this.log('No department directory found.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -85,7 +84,7 @@ export class DepartmentSharer {
|
||||
// Read files from the MemoryManager related to this department
|
||||
const departmentFiles = await this.memoryManager.retrieveMetaInformation(departmentData.id);
|
||||
if (!departmentFiles || !departmentFiles.structure) {
|
||||
console.error('No files found for this department in the memory manager.');
|
||||
this.log('No files found for this department in the memory manager.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -114,7 +113,7 @@ export class DepartmentSharer {
|
||||
const response = await this.waitForResponse();
|
||||
|
||||
if (!response || response.operationCode !== operationCodes.OK){
|
||||
console.error('Failed to clear the department directory.');
|
||||
this.log('Failed to clear the department directory.', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -124,14 +123,14 @@ export class DepartmentSharer {
|
||||
// Send the files to a user in the department
|
||||
private async sendFilesToUser(files: { [key: string]: string }, userName: string): Promise<void> {
|
||||
if(!this.tcpCommunicator) return;
|
||||
const unsentFiles = Object.keys(files); // Keep track of unsent files
|
||||
const unsentFiles = Object.keys(files);
|
||||
|
||||
for (const fileName of unsentFiles) {
|
||||
const filePath = files[fileName];
|
||||
|
||||
// Ensure the file exists before attempting to send
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
this.log(`File not found: ${filePath}`, 'error');
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -144,8 +143,8 @@ export class DepartmentSharer {
|
||||
|
||||
// Prepare the metaInfo (same structure as FileSharer)
|
||||
const metaInfo = {
|
||||
userName, // Sender's username
|
||||
relativeFilePath // Use the relative path to preserve directory structure
|
||||
userName,
|
||||
relativeFilePath
|
||||
};
|
||||
|
||||
// Send the file
|
||||
@@ -153,7 +152,7 @@ export class DepartmentSharer {
|
||||
|
||||
const response = await this.waitForResponse();
|
||||
if (!response || response.operationCode !== operationCodes.OK) {
|
||||
console.error(`Failed to send file: ${fileName}`);
|
||||
this.log(`Failed to send file: ${fileName}`, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -168,9 +167,19 @@ export class DepartmentSharer {
|
||||
if (!this.tcpCommunicator) return null;
|
||||
if (this.tcpCommunicator.hasResponseArrived()) {
|
||||
clearInterval(idResponseCheck);
|
||||
resolve(this.tcpCommunicator.getLastResult()); // Resolve the response or null if not available
|
||||
resolve(this.tcpCommunicator.getLastResult());
|
||||
}
|
||||
}, 100); // Check every 100 milliseconds if the response has arrived
|
||||
}, 100);
|
||||
});
|
||||
}
|
||||
|
||||
// Unified logging function
|
||||
private log(message: string, level: 'log' | 'error' = 'log'): void {
|
||||
const prefix = '[DepartmentSharer]';
|
||||
if (level === 'error') {
|
||||
console.error(`${prefix} ${message}`);
|
||||
} else {
|
||||
console.log(`${prefix} ${message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user