165 lines
4.4 KiB
C#
165 lines
4.4 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 readonly IpFilterHandler _ipFilterHandler;
|
|
|
|
private bool _communicationStopped;
|
|
private bool _ipScannerStopped;
|
|
private bool _contentFilterStopped;
|
|
private bool _ipFilterStopped;
|
|
|
|
public ThreadHandler(string path)
|
|
{
|
|
ConcurrentQueue<Filtered> filteredQueue = new();
|
|
ConcurrentQueue<Discarded> discardedQueue = new();
|
|
ConcurrentQueue<UnfilteredQueueItem> unfilteredQueue = new();
|
|
ConcurrentQueue<ScannerResumeObject> scannerResumeQueue = new();
|
|
ConcurrentQueue<FilterQueueItem> preFilteredQueue = new();
|
|
|
|
_dbHandler = new(filteredQueue, discardedQueue, unfilteredQueue, scannerResumeQueue, path);
|
|
_ipScanner = new(discardedQueue, scannerResumeQueue, _dbHandler, preFilteredQueue);
|
|
_contentFilter = new(filteredQueue, unfilteredQueue, _dbHandler, path);
|
|
_communication = new(_dbHandler, this, _ipScanner, _contentFilter, path);
|
|
_ipFilterHandler = new(discardedQueue, unfilteredQueue, preFilteredQueue);
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
Thread scanner = new(StartScanner);
|
|
Thread ipFilter = new(StartIpFilter);
|
|
Thread indexer = new(StartContentFilter);
|
|
Thread database = new(StartDbHandler);
|
|
Thread discarded = new(StartDiscardedDbHandler);
|
|
Thread filtered = new(StartFilteredDbHandler);
|
|
Thread resume = new(StartResumeDbHandler);
|
|
Thread communication = new(StartCommunicationHandler);
|
|
|
|
scanner.Start();
|
|
ipFilter.Start();
|
|
indexer.Start();
|
|
database.Start();
|
|
discarded.Start();
|
|
filtered.Start();
|
|
resume.Start();
|
|
communication.Start();
|
|
|
|
scanner.Join();
|
|
ipFilter.Join();
|
|
indexer.Join();
|
|
database.Join();
|
|
discarded.Join();
|
|
filtered.Join();
|
|
resume.Join();
|
|
communication.Join();
|
|
}
|
|
|
|
private void StartScanner()
|
|
{
|
|
Thread.Sleep(15000); // Let the database handler instantiate and warm up first.
|
|
|
|
List<WaitHandle[]> wait = _ipScanner.Start(256);
|
|
|
|
for (int i = 0; i < wait.Count; i++)
|
|
{
|
|
WaitHandle.WaitAll(wait[i]);
|
|
}
|
|
|
|
Console.WriteLine("Scanner finished");
|
|
|
|
_ipScannerStopped = true;
|
|
}
|
|
|
|
private void StartContentFilter()
|
|
{
|
|
Thread.Sleep(5000);
|
|
|
|
WaitHandle[] wait = _contentFilter.Start();
|
|
|
|
WaitHandle.WaitAll(wait);
|
|
|
|
Console.WriteLine("Content filter finished");
|
|
|
|
_contentFilterStopped = true;
|
|
}
|
|
|
|
private void StartIpFilter()
|
|
{
|
|
Thread.Sleep(1000);
|
|
|
|
List<WaitHandle[]> wait = _ipFilterHandler.Start(256);
|
|
|
|
for (int i = 0; i < wait.Count; i++)
|
|
{
|
|
WaitHandle.WaitAll(wait[i]);
|
|
}
|
|
|
|
Console.WriteLine("Ip filter finished");
|
|
|
|
_ipFilterStopped = true;
|
|
}
|
|
|
|
private void StartDbHandler()
|
|
{
|
|
_dbHandler.UnfilteredDbHandler();
|
|
}
|
|
|
|
private void StartFilteredDbHandler()
|
|
{
|
|
_dbHandler.FilteredDbHandler();
|
|
}
|
|
|
|
private void StartResumeDbHandler()
|
|
{
|
|
_dbHandler.ResumeDbHandler();
|
|
}
|
|
|
|
private void StartDiscardedDbHandler()
|
|
{
|
|
WaitHandle[] wait = _dbHandler.Start(4);
|
|
|
|
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 && _ipFilterStopped)
|
|
{
|
|
_dbHandler.Stop();
|
|
stopping = false;
|
|
}
|
|
|
|
Thread.Sleep(3000);
|
|
}
|
|
}
|
|
} |