Projekt

Obecné

Profil

Stáhnout (3.66 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 d49f4d79 Pultak
37
    /*
38
39
    It is possible to use previous info fetching method
40
41 31b6e8b7 silhavyj
    /// <summary>
42
    /// Instance of an info fetcher.
43
    /// </summary>
44 d49f4d79 Pultak
    private static readonly IInfoFetcher InfoFetcher = new(
45 6dab0250 silhavyj
        Config.FetchInfoMaxAttempts,
46
        Config.FetchInfoAttemptPeriod,
47
        Config.T32InfoLocation,
48 d49f4d79 Pultak
        Config.T32RemExecutable,
49
        Config.T32RemArguments,
50 4dcc6c07 silhavyj
        Config.T32RemSuccessExitCode,
51
        Config.T32RemWaitTimeoutMs
52 f281acac silhavyj
    );
53 d49f4d79 Pultak
    */
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 31b6e8b7 silhavyj
    /// <summary>
68
    /// The main entry pint of the application.
69
    /// </summary>
70
    /// <returns>1 if something goes wrong, 0 otherwise.</returns>
71 30c849e4 silhavyj
    public static int Main() {
72 31b6e8b7 silhavyj
        // Make sure that there is only one running instance of this application.
73 6dab0250 silhavyj
        if (GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1) {
74 30c849e4 silhavyj
            DefaultLogger.Error("Another instance of the application is already running");
75
            return 1;
76
        }
77
        
78 31b6e8b7 silhavyj
        // Create an instance of an API client with all
79
        // the appropriate parameters defined in the config file.
80 30c849e4 silhavyj
        DefaultApiClient = new ApiClient(
81
            Config.ApiBaseAddress,
82
            Config.ApiPort, 
83
            Config.ApiUsbEndPoint, 
84
            Config.RetryPeriod, Config.MaxEntries,
85
            Config.MaxRetries, 
86 fa084fc4 Pultak
            new PersistentQueue(Config.CacheFileName)
87 30c849e4 silhavyj
        );
88
        
89 31b6e8b7 silhavyj
        // Create a new process detector.
90 afafbc22 Pultak
        IProcessDetection processProcessDetection = new ProcessDetection(
91 f281acac silhavyj
            Config.T32ProcessName,
92
            Config.DetectionPeriod,
93
            InfoFetcher,
94 afafbc22 Pultak
            DefaultApiClient,
95
            new ProcessUtils()
96 f281acac silhavyj
        );
97
        
98 31b6e8b7 silhavyj
        // Create and start a new thread that periodically
99
        // resends filed payloads to the server.
100 aaad9ad4 Pultak
        var apiClientThread = new Thread(DefaultApiClient.Run) {
101
            IsBackground = true
102
        };
103
        apiClientThread.Start();
104
105 31b6e8b7 silhavyj
        // Create and start a new thread that periodically checks
106
        // if the desired process is currently running or not.
107 f281acac silhavyj
        var processThread = new Thread(processProcessDetection.RunPeriodicDetection) {
108
            IsBackground = true
109
        };
110
        processThread.Start();
111 aaad9ad4 Pultak
112 31b6e8b7 silhavyj
        // The main thread does "nothing"
113 f281acac silhavyj
        while (true) {
114 6dab0250 silhavyj
            Thread.Sleep(MainLoopDelayMs);
115 f5a08dd5 Pultak
        }
116 31b6e8b7 silhavyj
117
        // The execution of the program should never reach this point.
118
        return 0;
119 aaad9ad4 Pultak
    }
120 c318a92c Pultak
}