Projekt

Obecné

Profil

Stáhnout (3.22 KB) Statistiky
| Větev: | Tag: | Revize:
1 fa084fc4 Pultak
using DiskQueue;
2 f5a08dd5 Pultak
using LDClient.detection;
3 aaad9ad4 Pultak
using LDClient.network;
4 4b0f0b36 Pultak
using LDClient.utils;
5 74bd1e40 Pultak
using LDClient.utils.loggers;
6 c318a92c Pultak
7 30c849e4 silhavyj
using static System.Diagnostics.Process;
8
using static System.Reflection.Assembly;
9
10 f5a08dd5 Pultak
namespace LDClient;
11 c318a92c Pultak
12 31b6e8b7 silhavyj
/// <summary>
13
/// This class represents the main class of the application.
14
/// </summary>
15 30c849e4 silhavyj
internal static class Program {
16 c318a92c Pultak
17 31b6e8b7 silhavyj
    /// <summary>
18
    /// Sleep period of the main thread of the application.
19
    /// </summary>
20 6dab0250 silhavyj
    private const int MainLoopDelayMs = 30000; 
21
22 31b6e8b7 silhavyj
    /// <summary>
23
    /// Instance of a config loader.
24
    /// </summary>
25 30c849e4 silhavyj
    public static ConfigLoader Config { get; } = new();
26 31b6e8b7 silhavyj
    
27
    /// <summary>
28
    /// Default logger used throughout the application.
29
    /// </summary>
30 4b0f0b36 Pultak
    public static ALogger DefaultLogger { get; } = ALogger.Current;
31 31b6e8b7 silhavyj
    
32
    /// <summary>
33
    /// Instance of an API client.
34
    /// </summary>
35 30c849e4 silhavyj
    private static IApiClient? DefaultApiClient { get; set; }
36
    
37 31b6e8b7 silhavyj
    /// <summary>
38
    /// Instance of an info fetcher.
39
    /// </summary>
40 f281acac silhavyj
    private static readonly InfoFetcher InfoFetcher = new(
41 6dab0250 silhavyj
        Config.FetchInfoMaxAttempts,
42
        Config.FetchInfoAttemptPeriod,
43
        Config.T32InfoLocation,
44
        Config.F32RemExecutable,
45 4dcc6c07 silhavyj
        Config.F32RemArguments,
46
        Config.T32RemSuccessExitCode,
47
        Config.T32RemWaitTimeoutMs
48 f281acac silhavyj
    );
49
    
50 31b6e8b7 silhavyj
    /// <summary>
51
    /// The main entry pint of the application.
52
    /// </summary>
53
    /// <returns>1 if something goes wrong, 0 otherwise.</returns>
54 30c849e4 silhavyj
    public static int Main() {
55 31b6e8b7 silhavyj
        // Make sure that there is only one running instance of this application.
56 6dab0250 silhavyj
        if (GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1) {
57 30c849e4 silhavyj
            DefaultLogger.Error("Another instance of the application is already running");
58
            return 1;
59
        }
60
        
61 31b6e8b7 silhavyj
        // Create an instance of an API client with all
62
        // the appropriate parameters defined in the config file.
63 30c849e4 silhavyj
        DefaultApiClient = new ApiClient(
64
            Config.ApiBaseAddress,
65
            Config.ApiPort, 
66
            Config.ApiUsbEndPoint, 
67
            Config.RetryPeriod, Config.MaxEntries,
68
            Config.MaxRetries, 
69 fa084fc4 Pultak
            new PersistentQueue(Config.CacheFileName)
70 30c849e4 silhavyj
        );
71
        
72 31b6e8b7 silhavyj
        // Create a new process detector.
73 afafbc22 Pultak
        IProcessDetection processProcessDetection = new ProcessDetection(
74 f281acac silhavyj
            Config.T32ProcessName,
75
            Config.DetectionPeriod,
76
            InfoFetcher,
77 afafbc22 Pultak
            DefaultApiClient,
78
            new ProcessUtils()
79 f281acac silhavyj
        );
80
        
81 31b6e8b7 silhavyj
        // Create and start a new thread that periodically
82
        // resends filed payloads to the server.
83 aaad9ad4 Pultak
        var apiClientThread = new Thread(DefaultApiClient.Run) {
84
            IsBackground = true
85
        };
86
        apiClientThread.Start();
87
88 31b6e8b7 silhavyj
        // Create and start a new thread that periodically checks
89
        // if the desired process is currently running or not.
90 f281acac silhavyj
        var processThread = new Thread(processProcessDetection.RunPeriodicDetection) {
91
            IsBackground = true
92
        };
93
        processThread.Start();
94 aaad9ad4 Pultak
95 31b6e8b7 silhavyj
        // The main thread does "nothing"
96 f281acac silhavyj
        while (true) {
97 6dab0250 silhavyj
            Thread.Sleep(MainLoopDelayMs);
98 f5a08dd5 Pultak
        }
99 31b6e8b7 silhavyj
100
        // The execution of the program should never reach this point.
101
        return 0;
102 aaad9ad4 Pultak
    }
103 c318a92c Pultak
}