-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHost.cs
174 lines (138 loc) · 5.87 KB
/
Host.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using CommandLine;
using DIPOL_Remote;
namespace Host
{
internal static class Host
{
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
private sealed class Options
{
private static List<(PropertyInfo Property, object Default)> props =
typeof(Options).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.CanWrite &&
(x.GetCustomAttribute<OptionAttribute>()?.Default != null
|| x.GetCustomAttribute<ValueAttribute>()?.Default != null))
.Select(x => (Property: x,
Default: x.GetCustomAttribute<OptionAttribute>()?.Default ??
x.GetCustomAttribute<ValueAttribute>()?.Default))
.ToList();
[Value(0, HelpText = @"Service connection string", Required = true)]
public string Uri { get; set; }
[Option("console-width", Default = 120, HelpText = @"Width of the console window")]
public int ConsoleWidth { get; set; }
[Option("console-height", Default = 80, HelpText = @"Height of the console window")]
public int ConsoleHeight { get; set; }
[Option('l', "log", Default = false, HelpText = @"Enable logging")]
public bool Log { get; set; }
public static Options MakeDefault()
{
var opt = new Options();
foreach (var (property, @default) in props)
property.SetValue(opt, @default);
return opt;
}
}
private static TextWriter Output { get; } = Console.Out;
private static Options HandleArgs(IEnumerable<string> args)
{
if (args is null)
return Options.MakeDefault();
using (var parser = new Parser(settings =>
{
settings.AutoHelp = true;
settings.AutoVersion = true;
settings.CaseInsensitiveEnumValues = true;
settings.HelpWriter = Output;
settings.IgnoreUnknownArguments = true;
}))
{
var arguments = parser.ParseArguments<Options>(args);
return arguments.MapResult(x => x, y => Options.MakeDefault());
}
}
private static string MessageTemplate => $"[{DateTime.Now:yyyy/MM/dd\t HH:mm:ss.fff}] > ";
private static int Main(string[] args)
{
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo(@"en-US");
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo(@"en-US");
var options = HandleArgs(args);
if (options.Uri is null || !Uri.TryCreate(options.Uri, UriKind.RelativeOrAbsolute, out var uri))
{
Console.ReadKey();
return 13;
}
if (options.ConsoleWidth < Console.LargestWindowWidth)
Console.WindowWidth = options.ConsoleWidth;
if (options.ConsoleHeight < Console.LargestWindowHeight)
Console.WindowHeight = options.ConsoleHeight;
using (var host = new DipolHost(uri))
{
if (options.Log)
{
host.Opening += (sender, e) => OnHostOpenFired("opening");
host.Opened += (sender, e) => OnHostOpenFired("opened");
host.Closing += (sender, e) => OnHostCloseFired("closing");
host.Closed += (sender, e) => OnHostCloseFired("closed");
host.Faulted += (sender, e) => OnHostFaultingFired("faulted");
host.UnknownMessageReceived += (sender, e) => OnHostFaultingFired(e.Message.ToString());
host.EventReceived += OnServiceMessageFired;
}
host.Open();
//#if !DEBUG
// while (Console.ReadLine() != "exit")
// {
// }
//#else
try
{
while (Console.ReadKey().Key is not ConsoleKey.Escape and not ConsoleKey.Q) { }
}
catch (InvalidOperationException)
{
while (Console.ReadLine().ToLowerInvariant() != "exit") { }
}
//#endif
}
return 0;
}
private static async void OnHostOpenFired(string message)
{
if(Output is null)
return;
var str = $"{MessageTemplate} Initialization: {message}";
await Output.WriteLineAsync(str);
await Output.FlushAsync();
}
private static async void OnHostCloseFired(string message)
{
if (Output is null)
return;
var str = $"{MessageTemplate} Finalization: {message}";
await Output.WriteLineAsync(str);
await Output.FlushAsync();
}
private static async void OnHostFaultingFired(string message)
{
if (Output is null)
return;
var str = $"{MessageTemplate} service failing: {message}";
await Output.WriteLineAsync(str);
await Output.FlushAsync();
}
private static async void OnServiceMessageFired(object sender, string message)
{
if (Output is null)
return;
var str = $"{MessageTemplate} [{sender}]: {message}";
await Output.WriteLineAsync(str);
await Output.FlushAsync();
}
}
}