Projekt

Obecné

Profil

Stáhnout (3.66 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
    /*
38

    
39
    It is possible to use previous info fetching method
40

    
41
    /// <summary>
42
    /// Instance of an info fetcher.
43
    /// </summary>
44
    private static readonly IInfoFetcher InfoFetcher = new(
45
        Config.FetchInfoMaxAttempts,
46
        Config.FetchInfoAttemptPeriod,
47
        Config.T32InfoLocation,
48
        Config.T32RemExecutable,
49
        Config.T32RemArguments,
50
        Config.T32RemSuccessExitCode,
51
        Config.T32RemWaitTimeoutMs
52
    );
53
    */
54
    /// <summary>
55
    /// Instance of an info fetcher
56
    /// </summary>
57
    private static readonly IInfoFetcher InfoFetcher = new T32ApiFetcher(
58
        Config.FetchInfoMaxAttempts,
59
        Config.FetchInfoAttemptPeriod,
60
        Config.T32InfoLocation,
61
        Config.T32ApiAddress,
62
        Config.T32ApiPort,
63
        Config.T32ApiPacketLen,
64
        Config.T32ApiCommands
65
    );
66

    
67
    /// <summary>
68
    /// The main entry pint of the application.
69
    /// </summary>
70
    /// <returns>1 if something goes wrong, 0 otherwise.</returns>
71
    public static int Main() {
72
        // Make sure that there is only one running instance of this application.
73
        if (GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1) {
74
            DefaultLogger.Error("Another instance of the application is already running");
75
            return 1;
76
        }
77
        
78
        // Create an instance of an API client with all
79
        // the appropriate parameters defined in the config file.
80
        DefaultApiClient = new ApiClient(
81
            Config.ApiBaseAddress,
82
            Config.ApiPort, 
83
            Config.ApiUsbEndPoint, 
84
            Config.RetryPeriod, Config.MaxEntries,
85
            Config.MaxRetries, 
86
            new PersistentQueue(Config.CacheFileName)
87
        );
88
        
89
        // Create a new process detector.
90
        IProcessDetection processProcessDetection = new ProcessDetection(
91
            Config.T32ProcessName,
92
            Config.DetectionPeriod,
93
            InfoFetcher,
94
            DefaultApiClient,
95
            new ProcessUtils()
96
        );
97
        
98
        // Create and start a new thread that periodically
99
        // resends filed payloads to the server.
100
        var apiClientThread = new Thread(DefaultApiClient.Run) {
101
            IsBackground = true
102
        };
103
        apiClientThread.Start();
104

    
105
        // Create and start a new thread that periodically checks
106
        // if the desired process is currently running or not.
107
        var processThread = new Thread(processProcessDetection.RunPeriodicDetection) {
108
            IsBackground = true
109
        };
110
        processThread.Start();
111

    
112
        // The main thread does "nothing"
113
        while (true) {
114
            Thread.Sleep(MainLoopDelayMs);
115
        }
116

    
117
        // The execution of the program should never reach this point.
118
        return 0;
119
    }
120
}
(2-2/3)