overall v1
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import {QueueManager} from './queue_manager';
|
||||
import {TcpCommunicator} from "./tcp_communicator"; // Updated to use TcpCommunicator
|
||||
import {operationCodes} from '../network/operation_codes';
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import {compareFnFileItemTask, FileItemTask} from "../interfaces/file_item_task";
|
||||
import {ParsedMessage} from "../network/message_handler";
|
||||
import { QueueManager } from './queue_manager';
|
||||
import { TcpCommunicator } from "./tcp_communicator";
|
||||
import { operationCodes } from '../network/operation_codes';
|
||||
import { compareFnFileItemTask, FileItemTask } from "../interfaces/file_item_task";
|
||||
import { ParsedMessage } from "../network/message_handler";
|
||||
|
||||
interface FileSendTask {
|
||||
ip: string;
|
||||
@@ -28,7 +28,9 @@ export class FileSharer {
|
||||
async start(): Promise<void> {
|
||||
setInterval(async () => {
|
||||
if (!this.isBusy) { // Check if the queue is already being processed
|
||||
this.log("Start successfully. Processing the queue.");
|
||||
await this.processQueue(); // Process the queue at regular intervals
|
||||
this.log("Queue processing completed.");
|
||||
}
|
||||
}, 10000); // 10 seconds interval
|
||||
}
|
||||
@@ -36,7 +38,7 @@ export class FileSharer {
|
||||
// Method to process the queue
|
||||
private async processQueue(): Promise<void> {
|
||||
if (this.isBusy) {
|
||||
console.log("Queue is already being processed. Skipping this interval.");
|
||||
this.log("Queue is already being processed. Skipping this interval.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -46,15 +48,15 @@ export class FileSharer {
|
||||
const task = this.queueManager.peek();
|
||||
|
||||
if (task) {
|
||||
console.log(task);
|
||||
this.log(`Processing task for file: ${task.path} to IP: ${task.ip}`);
|
||||
const success = await this.sendFile(task);
|
||||
|
||||
if (!success) {
|
||||
console.error(`Failed to send file: ${task.path} to IP: ${task.ip}. Re-adding to queue.`);
|
||||
this.log(`Failed to send file: ${task.path} to IP: ${task.ip}. Re-adding to queue.`, 'error');
|
||||
this.queueManager.dequeue();
|
||||
this.queueManager.enqueue(task); // Re-add to queue if failed
|
||||
} else {
|
||||
console.log(`File sent successfully: ${task.path} to IP: ${task.ip}.`);
|
||||
this.log(`File sent successfully: ${task.path} to IP: ${task.ip}`);
|
||||
this.queueManager.dequeue();
|
||||
}
|
||||
}
|
||||
@@ -64,11 +66,11 @@ export class FileSharer {
|
||||
|
||||
// Method to send the file to a specific IP using TcpCommunicator
|
||||
private async sendFile(task: FileSendTask): Promise<boolean> {
|
||||
const {ip, path: filePath, userName} = task;
|
||||
const { ip, path: filePath, userName } = task;
|
||||
|
||||
// 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');
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -86,17 +88,17 @@ export class FileSharer {
|
||||
this.tcpCommunicator = new TcpCommunicator(ip, this.clientPort);
|
||||
|
||||
if (!await this.tcpCommunicator.connect()) {
|
||||
console.error(`Failed to connect to IP: ${ip}`);
|
||||
this.log(`Failed to connect to IP: ${ip}`, 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log(`Sending file: ${filePath} to IP: ${ip}`);
|
||||
this.log(`Sending file: ${filePath} to IP: ${ip}`);
|
||||
|
||||
if (!await this.tcpCommunicator.sendMessage(operationCodes.SHARE_FILE, metaInfo, fileContent)) return false;
|
||||
|
||||
const response = await this.waitForResponse();
|
||||
if (!response || response.operationCode !== operationCodes.OK) {
|
||||
console.error(`Failed to send file: ${filePath} to IP: ${ip}`);
|
||||
this.log(`Failed to send file: ${filePath} to IP: ${ip}`, 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -115,4 +117,14 @@ export class FileSharer {
|
||||
}, 100); // Check every 100 milliseconds if the response has arrived
|
||||
});
|
||||
}
|
||||
|
||||
// Unified logging function
|
||||
private log(message: string, level: 'log' | 'error' = 'log'): void {
|
||||
const prefix = '[FileSharer]';
|
||||
if (level === 'error') {
|
||||
console.error(`${prefix} ${message}`);
|
||||
} else {
|
||||
console.log(`${prefix} ${message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user