Projekt

Obecné

Profil

Stáhnout (3.22 KB) Statistiky
| Větev: | Tag: | Revize:
1
using DiskQueue;
2
using LDClient.detection;
3
using LDClient.network;
4
using LDClient.utils;
5
using LDClient.utils.loggers;
6

    
7
using static System.Diagnostics.Process;
8
using static System.Reflection.Assembly;
9

    
10
namespace LDClient;
11

    
12
/// <summary>
13
/// This class represents the main class of the application.
14
/// </summary>
15
internal static class Program {
16

    
17
    /// <summary>
18
    /// Sleep period of the main thread of the application.
19
    /// </summary>
20
    private const int MainLoopDelayMs = 30000; 
21

    
22
    /// <summary>
23
    /// Instance of a config loader.
24
    /// </summary>
25
    public static ConfigLoader Config { get; } = new();
26
    
27
    /// <summary>
28
    /// Default logger used throughout the application.
29
    /// </summary>
30
    public static ALogger DefaultLogger { get; } = ALogger.Current;
31
    
32
    /// <summary>
33
    /// Instance of an API client.
34
    /// </summary>
35
    private static IApiClient? DefaultApiClient { get; set; }
36
    
37
    /// <summary>
38
    /// Instance of an info fetcher.
39
    /// </summary>
40
    private static readonly InfoFetcher InfoFetcher = new(
41
        Config.FetchInfoMaxAttempts,
42
        Config.FetchInfoAttemptPeriod,
43
        Config.T32InfoLocation,
44
        Config.F32RemExecutable,
45
        Config.F32RemArguments,
46
        Config.T32RemSuccessExitCode,
47
        Config.T32RemWaitTimeoutMs
48
    );
49
    
50
    /// <summary>
51
    /// The main entry pint of the application.
52
    /// </summary>
53
    /// <returns>1 if something goes wrong, 0 otherwise.</returns>
54
    public static int Main() {
55
        // Make sure that there is only one running instance of this application.
56
        if (GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1) {
57
            DefaultLogger.Error("Another instance of the application is already running");
58
            return 1;
59
        }
60
        
61
        // Create an instance of an API client with all
62
        // the appropriate parameters defined in the config file.
63
        DefaultApiClient = new ApiClient(
64
            Config.ApiBaseAddress,
65
            Config.ApiPort, 
66
            Config.ApiUsbEndPoint, 
67
            Config.RetryPeriod, Config.MaxEntries,
68
            Config.MaxRetries, 
69
            new PersistentQueue(Config.CacheFileName)
70
        );
71
        
72
        // Create a new process detector.
73
        IProcessDetection processProcessDetection = new ProcessDetection(
74
            Config.T32ProcessName,
75
            Config.DetectionPeriod,
76
            InfoFetcher,
77
            DefaultApiClient,
78
            new ProcessUtils()
79
        );
80
        
81
        // Create and start a new thread that periodically
82
        // resends filed payloads to the server.
83
        var apiClientThread = new Thread(DefaultApiClient.Run) {
84
            IsBackground = true
85
        };
86
        apiClientThread.Start();
87

    
88
        // Create and start a new thread that periodically checks
89
        // if the desired process is currently running or not.
90
        var processThread = new Thread(processProcessDetection.RunPeriodicDetection) {
91
            IsBackground = true
92
        };
93
        processThread.Start();
94

    
95
        // The main thread does "nothing"
96
        while (true) {
97
            Thread.Sleep(MainLoopDelayMs);
98
        }
99

    
100
        // The execution of the program should never reach this point.
101
        return 0;
102
    }
103
}
(2-2/3)