119 lines
3.0 KiB
C#
119 lines
3.0 KiB
C#
using System.Collections.Concurrent;
|
|
using Models.Handler;
|
|
using Models.Model.Backend;
|
|
|
|
namespace Backend.Handler;
|
|
|
|
public class ThreadHandler
|
|
{
|
|
private readonly DbHandler _dbHandler;
|
|
private readonly Communication _communication;
|
|
private readonly IpScanner _ipScanner;
|
|
private readonly ContentFilter _contentFilter;
|
|
|
|
private bool _communicationStopped;
|
|
private bool _ipScannerStopped;
|
|
private bool _contentFilterStopped;
|
|
|
|
public ThreadHandler(string path)
|
|
{
|
|
ConcurrentQueue<QueueItem> contentQueue = new();
|
|
ConcurrentQueue<Discarded> discardedQueue = new();
|
|
|
|
_dbHandler = new(contentQueue, discardedQueue, path);
|
|
_ipScanner = new(contentQueue, _dbHandler, discardedQueue);
|
|
_contentFilter = new(contentQueue, _dbHandler, path);
|
|
_communication = new(_dbHandler, this, _ipScanner, _contentFilter, path);
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
Thread scanner = new(StartScanner);
|
|
Thread indexer = new(StartContentFilter);
|
|
Thread database = new(StartDbHandler);
|
|
Thread discarded = new(StartDiscardedDbHandler);
|
|
Thread communication = new(StartCommunicationHandler);
|
|
|
|
scanner.Start();
|
|
indexer.Start();
|
|
database.Start();
|
|
discarded.Start();
|
|
communication.Start();
|
|
|
|
scanner.Join();
|
|
indexer.Join();
|
|
database.Join();
|
|
discarded.Join();
|
|
communication.Join();
|
|
}
|
|
|
|
private void StartScanner()
|
|
{
|
|
Thread.Sleep(5000); // Let the database handler instantiate and warm up first.
|
|
|
|
WaitHandle[] wait = _ipScanner.Start(64);
|
|
|
|
WaitHandle.WaitAll(wait);
|
|
|
|
Console.WriteLine("Scanner finished");
|
|
|
|
_ipScannerStopped = true;
|
|
}
|
|
|
|
private void StartContentFilter()
|
|
{
|
|
WaitHandle[] wait = _contentFilter.Start();
|
|
|
|
WaitHandle.WaitAll(wait);
|
|
|
|
Console.WriteLine("Content filter finished");
|
|
|
|
_contentFilterStopped = true;
|
|
}
|
|
|
|
private void StartDbHandler()
|
|
{
|
|
_dbHandler.StartContent();
|
|
}
|
|
|
|
private void StartDiscardedDbHandler()
|
|
{
|
|
WaitHandle[] wait = _dbHandler.Start(2);
|
|
|
|
WaitHandle.WaitAll(wait);
|
|
|
|
Console.WriteLine("Discarded DbHandler finished");
|
|
}
|
|
|
|
private void StartCommunicationHandler()
|
|
{
|
|
WaitHandle[] wait = _communication.Start();
|
|
|
|
WaitHandle.WaitAll(wait);
|
|
|
|
Console.WriteLine("Communicator finished");
|
|
|
|
_communicationStopped = true;
|
|
|
|
Stop();
|
|
}
|
|
|
|
private void Stop()
|
|
{
|
|
_ipScanner.Stop();
|
|
_contentFilter.Stop();
|
|
|
|
bool stopping = true;
|
|
|
|
while (stopping)
|
|
{
|
|
if (_communicationStopped && _ipScannerStopped && _contentFilterStopped)
|
|
{
|
|
_dbHandler.Stop();
|
|
stopping = false;
|
|
}
|
|
|
|
Thread.Sleep(3000);
|
|
}
|
|
}
|
|
} |