forked from technosoftware-gmbh/UaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
195 lines (174 loc) · 9.04 KB
/
Program.cs
File metadata and controls
195 lines (174 loc) · 9.04 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#region Copyright (c) 2022-2026 Technosoftware GmbH. All rights reserved
//-----------------------------------------------------------------------------
// Copyright (c) 2022-2026 Technosoftware GmbH. All rights reserved
// Web: https://technosoftware.com
//
// The Software is based on the OPC Foundation MIT License.
// The complete license agreement for that can be found here:
// http://opcfoundation.org/License/MIT/1.00/
//-----------------------------------------------------------------------------
#endregion Copyright (c) 2022-2026 Technosoftware GmbH. All rights reserved
#region Using Directives
using System;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Opc.Ua;
using Technosoftware.ClientGateway;
#endregion Using Directives
namespace Technosoftware.UaClientGateway
{
/// <summary>
/// The program.
/// </summary>
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
/// <param name="args">The arguments.</param>
public static async Task<int> Main(string[] args)
{
Console.WriteLine("OPC UA Client Gateway Server");
#region License validation
//const string licenseData =
// @"";
//bool licensed = Technosoftware.UaServer.LicenseHandler.Validate(licenseData);
//if (!licensed)
//{
// Console.WriteLine("WARNING: No valid license applied.");
//}
string licensedString = $" Licensed Product : {Technosoftware.UaUtilities.Licensing.LicenseHandler.LicensedProduct}";
Console.WriteLine(licensedString);
licensedString = $" Licensed Features : {Technosoftware.UaUtilities.Licensing.LicenseHandler.LicensedFeatures}";
Console.WriteLine(licensedString);
if (Technosoftware.UaUtilities.Licensing.LicenseHandler.IsEvaluation)
{
licensedString = $" Evaluation expires at: {Technosoftware.UaUtilities.Licensing.LicenseHandler.LicenseExpirationDate}";
Console.WriteLine(licensedString);
licensedString = $" Days until Expiration: {Technosoftware.UaUtilities.Licensing.LicenseHandler.LicenseExpirationDays}";
Console.WriteLine(licensedString);
}
licensedString = $" Support Included : {Technosoftware.UaUtilities.Licensing.LicenseHandler.Support}";
Console.WriteLine(licensedString);
if (Technosoftware.UaUtilities.Licensing.LicenseHandler.Support != Technosoftware.UaUtilities.Licensing.SupportType.None)
{
licensedString = $" Support expire at : {Technosoftware.UaUtilities.Licensing.LicenseHandler.SupportExpirationDate}";
Console.WriteLine(licensedString);
licensedString = $" Days until Expiration: {Technosoftware.UaUtilities.Licensing.LicenseHandler.SupportExpirationDays}";
Console.WriteLine(licensedString);
}
if (Technosoftware.UaUtilities.Licensing.LicenseHandler.IsEvaluation)
{
licensedString = $" Evaluation Period : {Technosoftware.UaUtilities.Licensing.LicenseHandler.EvaluationPeriod} minutes.";
Console.WriteLine(licensedString);
}
if (!Technosoftware.UaUtilities.Licensing.LicenseHandler.IsLicensed && !Technosoftware.UaUtilities.Licensing.LicenseHandler.IsEvaluation)
{
Console.WriteLine("ERROR: No valid license applied.");
}
#endregion License validation
// The application name and config file name
const string applicationName = "Technosoftware.UaClientGateway";
const string configSectionName = "Technosoftware.UaClientGateway";
// command line options
bool showHelp = false;
bool autoAccept = false;
bool logConsole = false;
bool appLog = true;
bool fileLog = false;
bool renewCertificate = false;
bool shadowConfig = false;
char[] password = null;
int timeout = -1;
string usage = Utils.IsRunningOnMono()
? $"Usage: mono {applicationName}.exe [OPTIONS]"
: $"Usage: dotnet {applicationName}.dll [OPTIONS]";
var options = new Mono.Options.OptionSet
{
usage,
{ "h|help", "show this message and exit", h => showHelp = h != null },
{ "a|autoaccept", "auto accept certificates (for testing only)", a => autoAccept = a != null },
{ "c|console", "log to console", c => logConsole = c != null },
{ "l|log", "log app output", c => appLog = c != null },
{ "f|file", "log to file", f => fileLog = f != null },
{ "p|password=", "optional password for private key", p => password = p.ToCharArray() },
{ "r|renew", "renew application certificate", r => renewCertificate = r != null },
{ "t|timeout=", "timeout in seconds to exit application", (int t) => timeout = t * 1000 },
{ "s|shadowconfig", "create configuration in pki root", s => shadowConfig = s != null },
};
using var telemetry = new ConsoleTelemetry();
ILogger logger = LoggerUtils.Null.Logger;
try
{
// parse command line and set options
ConsoleUtils.ProcessCommandLine(args, options, ref showHelp, "REFSERVER");
// log console output to logger
if (logConsole && appLog)
{
logger = telemetry.CreateLogger("Main");
}
// create the UA server
var server = new MyUaServer<ComWrapperServer>(telemetry)
{
AutoAccept = autoAccept,
Password = password
};
// load the server configuration, validate certificates
Console.WriteLine($"Loading configuration from {configSectionName}.");
await server.LoadAsync(applicationName, configSectionName).ConfigureAwait(false);
// use the shadow config to map the config to an externally accessible location
if (shadowConfig)
{
Console.WriteLine("Using shadow configuration.");
string shadowPath = Directory
.GetParent(
Path.GetDirectoryName(
Utils.ReplaceSpecialFolderNames(server.Configuration.TraceConfiguration.OutputFilePath)
)
)
.FullName;
string shadowFilePath = Path.Combine(
shadowPath,
Path.GetFileName(server.Configuration.SourceFilePath)
);
if (!File.Exists(shadowFilePath))
{
Console.WriteLine("Create a copy of the config in the shadow location.");
File.Copy(server.Configuration.SourceFilePath, shadowFilePath, true);
}
Console.WriteLine($"Reloading configuration from shadow location {shadowFilePath}.");
await server
.LoadAsync(applicationName, Path.Combine(shadowPath, configSectionName))
.ConfigureAwait(false);
}
// setup the logging
telemetry.ConfigureLogging(server.Configuration, applicationName, logConsole, fileLog, appLog, LogLevel.Information);
// check or renew the certificate
Console.WriteLine("Check the certificate.");
await server.CheckCertificateAsync(renewCertificate).ConfigureAwait(false);
// Create and add the node managers
// server.Create(NodeManagerUtils.NodeManagerFactories);
// start the server
Console.WriteLine("Start the server.");
await server.StartAsync().ConfigureAwait(false);
Console.WriteLine("Server started. Press Ctrl-C to exit...");
// wait for timeout or Ctrl-C
var quitCTS = new CancellationTokenSource();
ManualResetEvent quitEvent = ConsoleUtils.CtrlCHandler(quitCTS);
bool ctrlc = quitEvent.WaitOne(timeout);
// stop server. May have to wait for clients to disconnect.
Console.WriteLine("Server stopped. Waiting for exit...");
await server.StopAsync().ConfigureAwait(false);
return (int)ExitCode.Ok;
}
catch (ErrorExitException eee)
{
Console.WriteLine($"The application exits with error: {eee.Message}");
return (int)eee.ExitCode;
}
}
}
}