-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUaServer.cs
279 lines (242 loc) · 10.2 KB
/
UaServer.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Configuration;
using Opc.Ua.Server;
namespace vc2opcua
{
public class Server
{
OpcUaServer server;
Task status;
DateTime lastEventTime;
int serverRunTime = Timeout.Infinite;
static bool autoAccept = false;
static ExitCode exitCode;
public Thread ServerThread;
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public Server(bool _autoAccept, int _stopTimeout)
{
autoAccept = _autoAccept;
serverRunTime = _stopTimeout == 0 ? Timeout.Infinite : _stopTimeout * 1000;
}
public void Run()
{
try
{
exitCode = ExitCode.ErrorServerNotStarted;
// Start the server
ServerTask().Wait();
logger.Info("[vc2opcua] Server started");
exitCode = ExitCode.ErrorServerRunning;
}
catch (Exception ex)
{
Utils.Trace("ServiceResultException:" + ex.Message);
logger.Error(String.Format("[vc2opcua] {0}",ex.ToString()));
exitCode = ExitCode.ErrorServerException;
return;
}
}
public void Stop()
{
using (OpcUaServer _server = server)
{
// Stop status thread
server = null;
status.Wait();
// Stop server and dispose
_server.Stop();
exitCode = ExitCode.Ok;
}
}
public static ExitCode ExitCode { get => exitCode; }
private static void CertificateValidator_CertificateValidation(CertificateValidator validator, CertificateValidationEventArgs e)
{
if (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted)
{
e.Accept = autoAccept;
if (autoAccept)
{
Debug.WriteLine("Accepted Certificate: {0}", e.Certificate.Subject);
}
else
{
Debug.WriteLine("Rejected Certificate: {0}", e.Certificate.Subject);
}
}
}
private async Task ServerTask()
{
//ApplicationInstance.MessageDlg = new ApplicationMessageDlg();
ApplicationInstance application = new ApplicationInstance
{
ApplicationName = "Vc2OpcUa Server",
ApplicationType = ApplicationType.Server,
ConfigSectionName = "Vc2OpcUaServer"
};
// load the application configuration.
ApplicationConfiguration config = await application.LoadApplicationConfiguration(false);
// check the application certificate.
bool haveAppCertificate = await application.CheckApplicationInstanceCertificate(false, 0);
if (!haveAppCertificate)
{
throw new Exception("Application instance certificate invalid!");
}
if (!config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
{
config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
}
// start the server.
server = new OpcUaServer();
await application.Start(server);
// start the status thread
status = Task.Run(new System.Action(StatusThread));
// print notification on session events
server.CurrentInstance.SessionManager.SessionActivated += EventStatus;
server.CurrentInstance.SessionManager.SessionClosing += EventStatus;
server.CurrentInstance.SessionManager.SessionCreated += EventStatus;
}
private void EventStatus(Session session, SessionEventReason reason)
{
lastEventTime = DateTime.UtcNow;
PrintSessionStatus(session, reason.ToString());
}
void PrintSessionStatus(Session session, string reason, bool lastContact = false)
{
lock (session.DiagnosticsLock)
{
string item = String.Format("{0,9}:{1,20}:", reason, session.SessionDiagnostics.SessionName);
if (lastContact)
{
item += String.Format("Last Event:{0:HH:mm:ss}", session.SessionDiagnostics.ClientLastContactTime.ToLocalTime());
}
else
{
if (session.Identity != null)
{
item += String.Format(":{0,20}", session.Identity.DisplayName);
}
item += String.Format(":{0}", session.Id);
}
Debug.WriteLine(item);
}
}
private async void StatusThread()
{
while (server != null)
{
if (DateTime.UtcNow - lastEventTime > TimeSpan.FromMilliseconds(6000))
{
IList<Session> sessions = server.CurrentInstance.SessionManager.GetSessions();
for (int ii = 0; ii < sessions.Count; ii++)
{
Session session = sessions[ii];
PrintSessionStatus(session, "-Status-", true);
}
lastEventTime = DateTime.UtcNow;
}
await Task.Delay(1000);
}
}
}
public enum ExitCode : int
{
Ok = 0,
ErrorServerNotStarted = 0x80,
ErrorServerRunning = 0x81,
ErrorServerException = 0x82,
ErrorInvalidCommandLine = 0x100
};
/// <summary>
/// A class which implements an instance of a UA server.
/// </summary>
public partial class OpcUaServer : StandardServer
{
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#region Overridden Methods
/// <summary>
/// Initializes the server before it starts up.
/// </summary>
/// <remarks>
/// This method is called before any startup processing occurs. The sub-class may update the
/// configuration object or do any other application specific startup tasks.
/// </remarks>
protected override void OnServerStarting(ApplicationConfiguration configuration)
{
Debug.WriteLine("The server is starting.");
base.OnServerStarting(configuration);
}
/// <summary>
/// Cleans up before the server shuts down.
/// </summary>
/// <remarks>
/// This method is called before any shutdown processing occurs.
/// </remarks>
protected override void OnServerStopping()
{
Debug.WriteLine("The Server is stopping.");
base.OnServerStopping();
}
/// <summary>
/// Creates the node managers for the server.
/// </summary>
/// <remarks>
/// This method allows the sub-class create any additional node managers which it uses. The SDK
/// always creates a CoreNodeManager which handles the built-in nodes defined by the specification.
/// Any additional NodeManagers are expected to handle application specific nodes.
///
/// Applications with small address spaces do not need to create their own NodeManagers and can add any
/// application specific nodes to the CoreNodeManager. Applications should use custom NodeManagers when
/// the structure of the address space is stored in another system or when the address space is too large
/// to keep in memory.
/// </remarks>
protected override MasterNodeManager CreateMasterNodeManager(IServerInternal server, ApplicationConfiguration configuration)
{
Debug.WriteLine("Creating the Node Managers.");
List<INodeManager> nodeManagers = new List<INodeManager>();
Vc2OpcUaBridge bridge = new Vc2OpcUaBridge(server, configuration);
// create the custom node manager.
nodeManagers.Add(bridge.nodeManager);
// create master node manager.
return new MasterNodeManager(server, configuration, null, nodeManagers.ToArray());
}
/// <summary>
/// Loads the non-configurable properties for the application.
/// </summary>
/// <remarks>
/// These properties are exposed by the server but cannot be changed by administrators.
/// </remarks>
protected override ServerProperties LoadServerProperties()
{
ServerProperties properties = new ServerProperties
{
ManufacturerName = "OPC Foundation",
ProductName = "OPC UA SDK Samples",
ProductUri = "http://opcfoundation.org/UA/Samples/v1.0",
SoftwareVersion = Utils.GetAssemblySoftwareVersion(),
BuildNumber = Utils.GetAssemblyBuildNumber(),
BuildDate = Utils.GetAssemblyTimestamp()
};
return properties;
}
/// <summary>
/// Initializes the address space after the NodeManagers have started.
/// </summary>
/// <remarks>
/// This method can be used to create any initialization that requires access to node managers.
/// </remarks>
protected override void OnNodeManagerStarted(IServerInternal server)
{
logger.Info("[vc2opcua] Node managers have started");
// allow base class processing to happen first.
base.OnNodeManagerStarted(server);
}
#endregion
}
}