77 lines
3.5 KiB
TypeScript
77 lines
3.5 KiB
TypeScript
import { JsonManager } from './json_manager'; // Assuming you have this class for managing user/memory
|
|
import { WindowManager } from './window_manager'; // For managing app navigation
|
|
import { UdpClient } from '../network/udp/udp_client';
|
|
|
|
export class TaskScheduler {
|
|
private applicationInfo: JsonManager;
|
|
private windowManager: WindowManager;
|
|
private appStarted: boolean = false;
|
|
private intervalIds: NodeJS.Timeout[] = []; // Array to store interval IDs
|
|
|
|
constructor(applicationInfo: JsonManager, windowManager: WindowManager) {
|
|
this.applicationInfo = applicationInfo;
|
|
this.windowManager = windowManager;
|
|
}
|
|
|
|
// Function to schedule the UC check task with dynamic UDP client creation
|
|
public startUCCheck(udpPort: number, okPage: string, errorPage: string, interval: number = 30000) {
|
|
const intervalId = setInterval(async () => {
|
|
try {
|
|
const udpClient = new UdpClient(udpPort); // Create UdpClient with the provided port
|
|
const aliveClients = await udpClient.getAliveClients();
|
|
const storedIp = await this.applicationInfo.readValue('serverIp');
|
|
const foundClient = aliveClients.length > 0;
|
|
|
|
if (foundClient) {
|
|
const ipAddress = aliveClients[0]; // Just using the first alive client
|
|
|
|
if (!storedIp || storedIp !== ipAddress) {
|
|
await this.applicationInfo.writeValue('serverIp', ipAddress);
|
|
if (!this.appStarted) {
|
|
await this.windowManager.changeContent(okPage);
|
|
}
|
|
this.appStarted = true;
|
|
} else if (!this.appStarted) {
|
|
await this.windowManager.changeContent(okPage);
|
|
this.appStarted = true;
|
|
}
|
|
} else {
|
|
await this.windowManager.changeContent(errorPage);
|
|
}
|
|
} catch (err) {
|
|
console.error('Error checking UC:', err);
|
|
await this.windowManager.changeContent(errorPage);
|
|
}
|
|
}, interval);
|
|
|
|
this.intervalIds.push(intervalId);
|
|
}
|
|
|
|
// Function to schedule the IP lookup task, storing the active addresses in memory
|
|
public async startUserIPLookup(udpPort: number, interval: number = 30000) {
|
|
const serverIp = await this.applicationInfo.readValue('serverIp'); // Ensure it's awaited if it's an async function
|
|
const intervalId = setInterval(async () => {
|
|
try {
|
|
const udpClient = new UdpClient(udpPort); // Create a UDP client with the provided port
|
|
const activeIPs = await udpClient.getAliveClients(); // Get the list of active IPs
|
|
|
|
// Filter out the serverIp from the list of active clients
|
|
const filteredIPs = activeIPs.filter(ip => ip !== serverIp);
|
|
|
|
// Save the filtered IPs to 'users_ip'
|
|
await this.applicationInfo.writeValue('users_ip', filteredIPs);
|
|
} catch (err) {
|
|
console.error('Error during user IP lookup:', err);
|
|
}
|
|
}, interval);
|
|
|
|
this.intervalIds.push(intervalId); // Store the interval ID
|
|
}
|
|
|
|
// Function to stop all tasks (UC check, user worker, etc.)
|
|
public stopAllTasks() {
|
|
this.intervalIds.forEach(clearInterval); // Clear all intervals
|
|
this.intervalIds = []; // Reset the array
|
|
}
|
|
}
|