| | 1 | | ///<summary> |
| | 2 | | ///.NET 8.0 version of console application for NTP Service |
| | 3 | | using System; |
| | 4 | | using NtpServiceLibrary; |
| | 5 | | using NtpServiceConsole; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Entry point for the NtpServiceConsole application. |
| | 9 | | /// This console application retrieves the current time from an NTP server and displays it. |
| | 10 | | /// It attempts to read configuration from the Windows Registry, falling back to default settings if necessary. |
| | 11 | | /// Works as a test client for the NTP service library. |
| | 12 | | /// </summary> |
| | 13 | | class Program |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Main method for the console application. |
| | 17 | | /// Initializes logging, loads settings, retrieves NTP time, and displays results. |
| | 18 | | /// Handles and reports errors gracefully. |
| | 19 | | /// </summary> |
| | 20 | | static void Main() |
| 0 | 21 | | { |
| | 22 | | try |
| 0 | 23 | | { |
| 0 | 24 | | var serviceName = "NtpService"; |
| 0 | 25 | | var logger = new ConsoleLogger(serviceName); |
| 0 | 26 | | Settings settings = new Settings(); |
| | 27 | | try |
| 0 | 28 | | { |
| 0 | 29 | | var settingsProvider = new RegistrySettingsProvider(serviceName, logger); |
| 0 | 30 | | settings = settingsProvider.Read(); |
| 0 | 31 | | } |
| 0 | 32 | | catch (NullReferenceException) |
| 0 | 33 | | { |
| 0 | 34 | | Console.WriteLine("WARNING: Registry settings for {0} does not exist, falling back to default ones:\n{1} |
| 0 | 35 | | } |
| 0 | 36 | | catch (Exception ex) |
| 0 | 37 | | { |
| 0 | 38 | | Console.WriteLine("WARNING: Unknown exception occured while attempting to read {0} settings, falling bac |
| 0 | 39 | | "Error details:\n{2}", serviceName, settings, ex); |
| 0 | 40 | | } |
| 0 | 41 | | if (settings != null) |
| 0 | 42 | | { |
| 0 | 43 | | var currentTime = NtpTime.RetrieveNTPTime((string)settings.NTPServer, (int)settings.NTPPort); |
| 0 | 44 | | Console.WriteLine("Current date and time from NTP server: " + currentTime.ToLocalTime()); |
| 0 | 45 | | } |
| | 46 | | else |
| 0 | 47 | | { |
| 0 | 48 | | Console.WriteLine("ERROR: Cannot get neither default nor registry settings for {0}", serviceName); |
| 0 | 49 | | } |
| 0 | 50 | | TimeSpan timeSpan = new TimeSpan(6, 0, 0); |
| 0 | 51 | | Console.WriteLine("{0:%h} hours {0:%m} minutes {0:%m} seconds", timeSpan); |
| | 52 | |
|
| 0 | 53 | | } |
| 0 | 54 | | catch (Exception ex) |
| 0 | 55 | | { |
| 0 | 56 | | Console.WriteLine("An error occurred: " + ex.Message); |
| 0 | 57 | | } |
| 0 | 58 | | } |
| | 59 | | } |