node-opcua-server 2.71.0 → 2.72.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/base_server.d.ts +110 -110
  2. package/dist/base_server.js +473 -473
  3. package/dist/factory.d.ts +12 -12
  4. package/dist/factory.js +23 -23
  5. package/dist/history_server_capabilities.d.ts +35 -35
  6. package/dist/history_server_capabilities.js +43 -43
  7. package/dist/i_channel_data.d.ts +13 -13
  8. package/dist/i_channel_data.js +2 -2
  9. package/dist/i_register_server_manager.d.ts +16 -16
  10. package/dist/i_register_server_manager.js +2 -2
  11. package/dist/i_server_side_publish_engine.d.ts +36 -36
  12. package/dist/i_server_side_publish_engine.js +49 -49
  13. package/dist/i_socket_data.d.ts +11 -11
  14. package/dist/i_socket_data.js +2 -2
  15. package/dist/index.d.ts +15 -15
  16. package/dist/index.js +31 -31
  17. package/dist/monitored_item.d.ts +177 -177
  18. package/dist/monitored_item.js +998 -998
  19. package/dist/node_sampler.d.ts +3 -3
  20. package/dist/node_sampler.js +75 -75
  21. package/dist/opcua_server.d.ts +650 -647
  22. package/dist/opcua_server.js +2392 -2385
  23. package/dist/opcua_server.js.map +1 -1
  24. package/dist/queue.d.ts +11 -11
  25. package/dist/queue.js +71 -71
  26. package/dist/register_server_manager.d.ts +96 -96
  27. package/dist/register_server_manager.js +584 -584
  28. package/dist/register_server_manager_hidden.d.ts +17 -17
  29. package/dist/register_server_manager_hidden.js +27 -27
  30. package/dist/register_server_manager_mdns_only.d.ts +22 -22
  31. package/dist/register_server_manager_mdns_only.js +55 -55
  32. package/dist/server_capabilities.d.ts +148 -61
  33. package/dist/server_capabilities.js +91 -108
  34. package/dist/server_capabilities.js.map +1 -1
  35. package/dist/server_end_point.d.ts +183 -183
  36. package/dist/server_end_point.js +817 -816
  37. package/dist/server_end_point.js.map +1 -1
  38. package/dist/server_engine.d.ts +317 -316
  39. package/dist/server_engine.js +1716 -1688
  40. package/dist/server_engine.js.map +1 -1
  41. package/dist/server_publish_engine.d.ts +112 -112
  42. package/dist/server_publish_engine.js +530 -530
  43. package/dist/server_publish_engine_for_orphan_subscriptions.d.ts +16 -16
  44. package/dist/server_publish_engine_for_orphan_subscriptions.js +49 -49
  45. package/dist/server_session.d.ts +181 -181
  46. package/dist/server_session.js +738 -738
  47. package/dist/server_session.js.map +1 -1
  48. package/dist/server_subscription.d.ts +415 -395
  49. package/dist/server_subscription.js +1334 -1316
  50. package/dist/server_subscription.js.map +1 -1
  51. package/dist/sessions_compatible_for_transfer.d.ts +2 -2
  52. package/dist/sessions_compatible_for_transfer.js +36 -36
  53. package/dist/user_manager.d.ts +32 -32
  54. package/dist/user_manager.js +74 -74
  55. package/dist/user_manager_ua.d.ts +3 -3
  56. package/dist/user_manager_ua.js +39 -39
  57. package/dist/validate_filter.d.ts +5 -5
  58. package/dist/validate_filter.js +60 -60
  59. package/package.json +26 -26
  60. package/source/opcua_server.ts +27 -16
  61. package/source/server_capabilities.ts +178 -91
  62. package/source/server_end_point.ts +9 -8
  63. package/source/server_engine.ts +30 -1
  64. package/source/server_session.ts +1 -0
  65. package/source/server_subscription.ts +46 -5
@@ -1,739 +1,739 @@
1
- "use strict";
2
- /**
3
- * @module node-opcua-server
4
- */
5
- // tslint:disable:no-console
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.ServerSession = void 0;
8
- const crypto = require("crypto");
9
- const events_1 = require("events");
10
- const node_opcua_address_space_1 = require("node-opcua-address-space");
11
- const node_opcua_assert_1 = require("node-opcua-assert");
12
- const node_opcua_basic_types_1 = require("node-opcua-basic-types");
13
- const node_opcua_common_1 = require("node-opcua-common");
14
- const node_opcua_data_model_1 = require("node-opcua-data-model");
15
- const node_opcua_debug_1 = require("node-opcua-debug");
16
- const node_opcua_nodeid_1 = require("node-opcua-nodeid");
17
- const node_opcua_object_registry_1 = require("node-opcua-object-registry");
18
- const node_opcua_status_code_1 = require("node-opcua-status-code");
19
- const node_opcua_utils_1 = require("node-opcua-utils");
20
- const node_opcua_utils_2 = require("node-opcua-utils");
21
- const server_publish_engine_1 = require("./server_publish_engine");
22
- const server_subscription_1 = require("./server_subscription");
23
- const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
24
- const errorLog = (0, node_opcua_debug_1.make_errorLog)(__filename);
25
- const doDebug = (0, node_opcua_debug_1.checkDebugFlag)(__filename);
26
- const theWatchDog = new node_opcua_utils_1.WatchDog();
27
- const registeredNodeNameSpace = 9999;
28
- function compareSessionId(sessionDiagnostics1, sessionDiagnostics2) {
29
- return sessionDiagnostics1.sessionId.toString() === sessionDiagnostics2.sessionId.toString();
30
- }
31
- function on_channel_abort() {
32
- debugLog("ON CHANNEL ABORT ON SESSION!!!");
33
- /**
34
- * @event channel_aborted
35
- */
36
- this.emit("channel_aborted");
37
- }
38
- /**
39
- *
40
- * A Server session object.
41
- *
42
- * **from OPCUA Spec 1.02:**
43
- *
44
- * * Sessions are created to be independent of the underlying communications connection. Therefore, if a communication
45
- * connection fails, the Session is not immediately affected. The exact mechanism to recover from an underlying
46
- * communication connection error depends on the SecureChannel mapping as described in Part 6.
47
- *
48
- * * Sessions are terminated by the Server automatically if the Client fails to issue a Service request on the Session
49
- * within the timeout period negotiated by the Server in the CreateSession Service response. This protects the Server
50
- * against Client failures and against situations where a failed underlying connection cannot be re-established.
51
- *
52
- * * Clients shall be prepared to submit requests in a timely manner to prevent the Session from closing automatically.
53
- *
54
- * * Clients may explicitly terminate Sessions using the CloseSession Service.
55
- *
56
- * * When a Session is terminated, all outstanding requests on the Session are aborted and BadSessionClosed StatusCodes
57
- * are returned to the Client. In addition, the Server deletes the entry for the Client from its
58
- * SessionDiagnosticsArray Variable and notifies any other Clients who were subscribed to this entry.
59
- *
60
- */
61
- class ServerSession extends events_1.EventEmitter {
62
- constructor(parent, sessionTimeout) {
63
- super();
64
- this.__status = "";
65
- this.sessionName = "";
66
- this.keepAlive = node_opcua_utils_1.WatchDog.emptyKeepAlive;
67
- this.parent = parent; // SessionEngine
68
- ServerSession.registry.register(this);
69
- (0, node_opcua_assert_1.assert)(isFinite(sessionTimeout));
70
- (0, node_opcua_assert_1.assert)(sessionTimeout >= 0, " sessionTimeout");
71
- this.sessionTimeout = sessionTimeout;
72
- const authenticationTokenBuf = crypto.randomBytes(16);
73
- this.authenticationToken = new node_opcua_nodeid_1.NodeId(node_opcua_nodeid_1.NodeIdType.BYTESTRING, authenticationTokenBuf);
74
- // the sessionId
75
- const ownNamespaceIndex = 1; // addressSpace.getOwnNamespace().index;
76
- this.nodeId = new node_opcua_nodeid_1.NodeId(node_opcua_nodeid_1.NodeIdType.GUID, (0, node_opcua_basic_types_1.randomGuid)(), ownNamespaceIndex);
77
- (0, node_opcua_assert_1.assert)(this.authenticationToken instanceof node_opcua_nodeid_1.NodeId);
78
- (0, node_opcua_assert_1.assert)(this.nodeId instanceof node_opcua_nodeid_1.NodeId);
79
- this._cumulatedSubscriptionCount = 0;
80
- this.publishEngine = new server_publish_engine_1.ServerSidePublishEngine({
81
- maxPublishRequestInQueue: ServerSession.maxPublishRequestInQueue
82
- });
83
- this.publishEngine.setMaxListeners(100);
84
- theWatchDog.addSubscriber(this, this.sessionTimeout);
85
- this.__status = "new";
86
- /**
87
- * the continuation point manager for this session
88
- * @property continuationPointManager
89
- * @type {ContinuationPointManager}
90
- */
91
- this.continuationPointManager = new node_opcua_address_space_1.ContinuationPointManager();
92
- /**
93
- * @property creationDate
94
- * @type {Date}
95
- */
96
- this.creationDate = new Date();
97
- this._registeredNodesCounter = 0;
98
- this._registeredNodes = {};
99
- this._registeredNodesInv = {};
100
- }
101
- getSessionId() {
102
- return this.nodeId;
103
- }
104
- getEndpointDescription() {
105
- return this.endpoint;
106
- }
107
- dispose() {
108
- debugLog("ServerSession#dispose()");
109
- (0, node_opcua_assert_1.assert)(!this.sessionObject, " sessionObject has not been cleared !");
110
- this.parent = null;
111
- this.authenticationToken = new node_opcua_nodeid_1.NodeId();
112
- if (this.publishEngine) {
113
- this.publishEngine.dispose();
114
- this.publishEngine = null;
115
- }
116
- this._sessionDiagnostics = undefined;
117
- this._registeredNodesCounter = 0;
118
- this._registeredNodes = null;
119
- this._registeredNodesInv = null;
120
- this.continuationPointManager = null;
121
- this.removeAllListeners();
122
- this.__status = "disposed";
123
- ServerSession.registry.unregister(this);
124
- }
125
- get clientConnectionTime() {
126
- return this.creationDate;
127
- }
128
- /**
129
- * return the number of milisecond since last session transaction occurs from client
130
- * the first transaction is the creation of the session
131
- */
132
- get clientLastContactTime() {
133
- const lastSeen = this._watchDogData ? this._watchDogData.lastSeen : node_opcua_basic_types_1.minOPCUADate.getTime();
134
- return node_opcua_utils_1.WatchDog.lastSeenToDuration(lastSeen);
135
- }
136
- get status() {
137
- return this.__status;
138
- }
139
- set status(value) {
140
- if (value === "active") {
141
- this._createSessionObjectInAddressSpace();
142
- }
143
- this.__status = value;
144
- }
145
- get addressSpace() {
146
- if (this.parent && this.parent.addressSpace) {
147
- return this.parent.addressSpace;
148
- }
149
- return null;
150
- }
151
- get currentPublishRequestInQueue() {
152
- return this.publishEngine ? this.publishEngine.pendingPublishRequestCount : 0;
153
- }
154
- updateClientLastContactTime() {
155
- if (this._sessionDiagnostics && this._sessionDiagnostics.clientLastContactTime) {
156
- const currentTime = new Date();
157
- // do not record all ticks as this may be overwhelming,
158
- if (currentTime.getTime() - 250 >= this._sessionDiagnostics.clientLastContactTime.getTime()) {
159
- this._sessionDiagnostics.clientLastContactTime = currentTime;
160
- }
161
- }
162
- }
163
- /**
164
- * @method onClientSeen
165
- * required for watch dog
166
- * @param currentTime {DateTime}
167
- * @private
168
- */
169
- onClientSeen() {
170
- this.updateClientLastContactTime();
171
- if (this._sessionDiagnostics) {
172
- // see https://opcfoundation-onlineapplications.org/mantis/view.php?id=4111
173
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, "currentMonitoredItemsCount"));
174
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, "currentSubscriptionsCount"));
175
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, "currentPublishRequestsInQueue"));
176
- // note : https://opcfoundation-onlineapplications.org/mantis/view.php?id=4111
177
- // sessionDiagnostics extension object uses a different spelling
178
- // here with an S !!!!
179
- if (this._sessionDiagnostics.currentMonitoredItemsCount !== this.currentMonitoredItemCount) {
180
- this._sessionDiagnostics.currentMonitoredItemsCount = this.currentMonitoredItemCount;
181
- }
182
- if (this._sessionDiagnostics.currentSubscriptionsCount !== this.currentSubscriptionCount) {
183
- this._sessionDiagnostics.currentSubscriptionsCount = this.currentSubscriptionCount;
184
- }
185
- if (this._sessionDiagnostics.currentPublishRequestsInQueue !== this.currentPublishRequestInQueue) {
186
- this._sessionDiagnostics.currentPublishRequestsInQueue = this.currentPublishRequestInQueue;
187
- }
188
- }
189
- }
190
- incrementTotalRequestCount() {
191
- if (this._sessionDiagnostics && this._sessionDiagnostics.totalRequestCount) {
192
- this._sessionDiagnostics.totalRequestCount.totalCount += 1;
193
- }
194
- }
195
- incrementRequestTotalCounter(counterName) {
196
- if (this._sessionDiagnostics) {
197
- const propName = (0, node_opcua_utils_2.lowerFirstLetter)(counterName + "Count");
198
- // istanbul ignore next
199
- if (!Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, propName)) {
200
- errorLog("incrementRequestTotalCounter: cannot find", propName);
201
- // xx return;
202
- }
203
- else {
204
- this._sessionDiagnostics[propName].totalCount += 1;
205
- }
206
- }
207
- }
208
- incrementRequestErrorCounter(counterName) {
209
- var _a;
210
- (_a = this.parent) === null || _a === void 0 ? void 0 : _a.incrementRejectedRequestsCount();
211
- if (this._sessionDiagnostics) {
212
- const propName = (0, node_opcua_utils_2.lowerFirstLetter)(counterName + "Count");
213
- // istanbul ignore next
214
- if (!Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, propName)) {
215
- errorLog("incrementRequestErrorCounter: cannot find", propName);
216
- // xx return;
217
- }
218
- else {
219
- this._sessionDiagnostics[propName].errorCount += 1;
220
- }
221
- }
222
- }
223
- /**
224
- * returns rootFolder.objects.server.serverDiagnostics.sessionsDiagnosticsSummary.sessionDiagnosticsArray
225
- */
226
- getSessionDiagnosticsArray() {
227
- const server = this.addressSpace.rootFolder.objects.server;
228
- return server.serverDiagnostics.sessionsDiagnosticsSummary.sessionDiagnosticsArray;
229
- }
230
- /**
231
- * returns rootFolder.objects.server.serverDiagnostics.sessionsDiagnosticsSummary.sessionSecurityDiagnosticsArray
232
- */
233
- getSessionSecurityDiagnosticsArray() {
234
- const server = this.addressSpace.rootFolder.objects.server;
235
- return server.serverDiagnostics.sessionsDiagnosticsSummary.sessionSecurityDiagnosticsArray;
236
- }
237
- /**
238
- * number of active subscriptions
239
- */
240
- get currentSubscriptionCount() {
241
- return this.publishEngine ? this.publishEngine.subscriptionCount : 0;
242
- }
243
- /**
244
- * number of subscriptions ever created since this object is live
245
- */
246
- get cumulatedSubscriptionCount() {
247
- return this._cumulatedSubscriptionCount;
248
- }
249
- /**
250
- * number of monitored items
251
- */
252
- get currentMonitoredItemCount() {
253
- return this.publishEngine ? this.publishEngine.currentMonitoredItemCount : 0;
254
- }
255
- /**
256
- * retrieve an existing subscription by subscriptionId
257
- * @method getSubscription
258
- * @param subscriptionId {Number}
259
- * @return {Subscription}
260
- */
261
- getSubscription(subscriptionId) {
262
- const subscription = this.publishEngine.getSubscriptionById(subscriptionId);
263
- if (subscription && subscription.state === server_subscription_1.SubscriptionState.CLOSED) {
264
- // subscription is CLOSED but has not been notified yet
265
- // it should be considered as excluded
266
- return null;
267
- }
268
- (0, node_opcua_assert_1.assert)(!subscription || subscription.state !== server_subscription_1.SubscriptionState.CLOSED, "CLOSED subscription shall not be managed by publish engine anymore");
269
- return subscription;
270
- }
271
- /**
272
- * @method deleteSubscription
273
- * @param subscriptionId {Number}
274
- * @return {StatusCode}
275
- */
276
- deleteSubscription(subscriptionId) {
277
- const subscription = this.getSubscription(subscriptionId);
278
- if (!subscription) {
279
- return node_opcua_status_code_1.StatusCodes.BadSubscriptionIdInvalid;
280
- }
281
- // xx this.publishEngine.remove_subscription(subscription);
282
- subscription.terminate();
283
- if (this.currentSubscriptionCount === 0) {
284
- const local_publishEngine = this.publishEngine;
285
- local_publishEngine.cancelPendingPublishRequest();
286
- }
287
- return node_opcua_status_code_1.StatusCodes.Good;
288
- }
289
- /**
290
- * close a ServerSession, this will also delete the subscriptions if the flag is set.
291
- *
292
- * Spec extract:
293
- *
294
- * If a Client invokes the CloseSession Service then all Subscriptions associated with the Session are also deleted
295
- * if the deleteSubscriptions flag is set to TRUE. If a Server terminates a Session for any other reason,
296
- * Subscriptions associated with the Session, are not deleted. Each Subscription has its own lifetime to protect
297
- * against data loss in the case of a Session termination. In these cases, the Subscription can be reassigned to
298
- * another Client before its lifetime expires.
299
- *
300
- * @method close
301
- * @param deleteSubscriptions : should we delete subscription ?
302
- * @param [reason = "CloseSession"] the reason for closing the session
303
- * (shall be "Timeout", "Terminated" or "CloseSession")
304
- *
305
- */
306
- close(deleteSubscriptions, reason) {
307
- debugLog(" closing session deleteSubscriptions = ", deleteSubscriptions);
308
- if (this.publishEngine) {
309
- this.publishEngine.onSessionClose();
310
- }
311
- theWatchDog.removeSubscriber(this);
312
- // --------------- delete associated subscriptions ---------------------
313
- if (!deleteSubscriptions && this.currentSubscriptionCount !== 0) {
314
- // I don't know what to do yet if deleteSubscriptions is false
315
- errorLog("TO DO : Closing session without deleting subscription not yet implemented");
316
- // to do: Put subscriptions in safe place for future transfer if any
317
- }
318
- this._deleteSubscriptions();
319
- (0, node_opcua_assert_1.assert)(this.currentSubscriptionCount === 0);
320
- // Post-Conditions
321
- (0, node_opcua_assert_1.assert)(this.currentSubscriptionCount === 0);
322
- this.status = "closed";
323
- this._detach_channel();
324
- /**
325
- * @event session_closed
326
- * @param deleteSubscriptions {Boolean}
327
- * @param reason {String}
328
- */
329
- this.emit("session_closed", this, deleteSubscriptions, reason);
330
- // ---------------- shut down publish engine
331
- if (this.publishEngine) {
332
- // remove subscription
333
- this.publishEngine.shutdown();
334
- (0, node_opcua_assert_1.assert)(this.publishEngine.subscriptionCount === 0);
335
- this.publishEngine.dispose();
336
- this.publishEngine = null;
337
- }
338
- this._removeSessionObjectFromAddressSpace();
339
- (0, node_opcua_assert_1.assert)(!this.sessionDiagnostics, "ServerSession#_removeSessionObjectFromAddressSpace must be called");
340
- (0, node_opcua_assert_1.assert)(!this.sessionObject, "ServerSession#_removeSessionObjectFromAddressSpace must be called");
341
- }
342
- registerNode(nodeId) {
343
- (0, node_opcua_assert_1.assert)(nodeId instanceof node_opcua_nodeid_1.NodeId);
344
- if (nodeId.namespace === 0 && nodeId.identifierType === node_opcua_nodeid_1.NodeIdType.NUMERIC) {
345
- return nodeId;
346
- }
347
- const key = nodeId.toString();
348
- const registeredNode = this._registeredNodes[key];
349
- if (registeredNode) {
350
- // already registered
351
- return registeredNode;
352
- }
353
- const node = this.addressSpace.findNode(nodeId);
354
- if (!node) {
355
- return nodeId;
356
- }
357
- this._registeredNodesCounter += 1;
358
- const aliasNodeId = (0, node_opcua_nodeid_1.makeNodeId)(this._registeredNodesCounter, registeredNodeNameSpace);
359
- this._registeredNodes[key] = aliasNodeId;
360
- this._registeredNodesInv[aliasNodeId.toString()] = node;
361
- return aliasNodeId;
362
- }
363
- unRegisterNode(aliasNodeId) {
364
- (0, node_opcua_assert_1.assert)(aliasNodeId instanceof node_opcua_nodeid_1.NodeId);
365
- if (aliasNodeId.namespace !== registeredNodeNameSpace) {
366
- return; // not a registered Node
367
- }
368
- const node = this._registeredNodesInv[aliasNodeId.toString()];
369
- if (!node) {
370
- return;
371
- }
372
- this._registeredNodesInv[aliasNodeId.toString()] = null;
373
- this._registeredNodes[node.nodeId.toString()] = null;
374
- }
375
- resolveRegisteredNode(aliasNodeId) {
376
- if (aliasNodeId.namespace !== registeredNodeNameSpace) {
377
- return aliasNodeId; // not a registered Node
378
- }
379
- const node = this._registeredNodesInv[aliasNodeId.toString()];
380
- if (!node) {
381
- return aliasNodeId;
382
- }
383
- return node.nodeId;
384
- }
385
- /**
386
- * true if the underlying channel has been closed or aborted...
387
- */
388
- get aborted() {
389
- if (!this.channel) {
390
- return true;
391
- }
392
- return this.channel.aborted;
393
- }
394
- createSubscription(parameters) {
395
- const subscription = this.parent._createSubscriptionOnSession(this, parameters);
396
- (0, node_opcua_assert_1.assert)(!Object.prototype.hasOwnProperty.call(parameters, "id"));
397
- this.assignSubscription(subscription);
398
- (0, node_opcua_assert_1.assert)(subscription.$session === this);
399
- (0, node_opcua_assert_1.assert)(subscription.sessionId instanceof node_opcua_nodeid_1.NodeId);
400
- (0, node_opcua_assert_1.assert)((0, node_opcua_nodeid_1.sameNodeId)(subscription.sessionId, this.nodeId));
401
- return subscription;
402
- }
403
- _attach_channel(channel) {
404
- (0, node_opcua_assert_1.assert)(this.nonce && this.nonce instanceof Buffer);
405
- this.channel = channel;
406
- this.channelId = channel.channelId;
407
- const key = this.authenticationToken.toString();
408
- (0, node_opcua_assert_1.assert)(!Object.prototype.hasOwnProperty.call(channel.sessionTokens, key), "channel has already a session");
409
- channel.sessionTokens[key] = this;
410
- // when channel is aborting
411
- this.channel_abort_event_handler = on_channel_abort.bind(this);
412
- channel.on("abort", this.channel_abort_event_handler);
413
- }
414
- _detach_channel() {
415
- const channel = this.channel;
416
- // istanbul ignore next
417
- if (!channel) {
418
- return;
419
- // already detached !
420
- // throw new Error("expecting a valid channel");
421
- }
422
- (0, node_opcua_assert_1.assert)(this.nonce && this.nonce instanceof Buffer);
423
- (0, node_opcua_assert_1.assert)(this.authenticationToken);
424
- const key = this.authenticationToken.toString();
425
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(channel.sessionTokens, key));
426
- (0, node_opcua_assert_1.assert)(this.channel);
427
- (0, node_opcua_assert_1.assert)(typeof this.channel_abort_event_handler === "function");
428
- channel.removeListener("abort", this.channel_abort_event_handler);
429
- delete channel.sessionTokens[key];
430
- this.channel = undefined;
431
- this.channelId = undefined;
432
- }
433
- _exposeSubscriptionDiagnostics(subscription) {
434
- debugLog("ServerSession#_exposeSubscriptionDiagnostics");
435
- (0, node_opcua_assert_1.assert)(subscription.$session === this);
436
- const subscriptionDiagnosticsArray = this._getSubscriptionDiagnosticsArray();
437
- const subscriptionDiagnostics = subscription.subscriptionDiagnostics;
438
- (0, node_opcua_assert_1.assert)(subscriptionDiagnostics.$subscription === subscription);
439
- if (subscriptionDiagnostics && subscriptionDiagnosticsArray) {
440
- // subscription.id,"on session", session.nodeId.toString());
441
- (0, node_opcua_address_space_1.addElement)(subscriptionDiagnostics, subscriptionDiagnosticsArray);
442
- }
443
- }
444
- _unexposeSubscriptionDiagnostics(subscription) {
445
- const subscriptionDiagnosticsArray = this._getSubscriptionDiagnosticsArray();
446
- const subscriptionDiagnostics = subscription.subscriptionDiagnostics;
447
- (0, node_opcua_assert_1.assert)(subscriptionDiagnostics instanceof node_opcua_common_1.SubscriptionDiagnosticsDataType);
448
- if (subscriptionDiagnostics && subscriptionDiagnosticsArray) {
449
- // subscription.id,"on session", session.nodeId.toString());
450
- (0, node_opcua_address_space_1.removeElement)(subscriptionDiagnosticsArray, subscriptionDiagnostics);
451
- }
452
- debugLog("ServerSession#_unexposeSubscriptionDiagnostics");
453
- }
454
- /**
455
- * @method watchdogReset
456
- * used as a callback for the Watchdog
457
- * @private
458
- */
459
- watchdogReset() {
460
- debugLog("Session#watchdogReset: the server session has expired and must be removed from the server");
461
- // the server session has expired and must be removed from the server
462
- this.emit("timeout");
463
- }
464
- _createSessionObjectInAddressSpace() {
465
- if (this.sessionObject) {
466
- return;
467
- }
468
- (0, node_opcua_assert_1.assert)(!this.sessionObject, "ServerSession#_createSessionObjectInAddressSpace already called ?");
469
- this.sessionObject = null;
470
- if (!this.addressSpace) {
471
- debugLog("ServerSession#_createSessionObjectInAddressSpace : no addressSpace");
472
- return; // no addressSpace
473
- }
474
- const root = this.addressSpace.rootFolder;
475
- (0, node_opcua_assert_1.assert)(root, "expecting a root object");
476
- if (!root.objects) {
477
- debugLog("ServerSession#_createSessionObjectInAddressSpace : no object folder");
478
- return false;
479
- }
480
- if (!root.objects.server) {
481
- debugLog("ServerSession#_createSessionObjectInAddressSpace : no server object");
482
- return false;
483
- }
484
- // self.addressSpace.findNode(makeNodeId(ObjectIds.Server_ServerDiagnostics));
485
- const serverDiagnosticsNode = root.objects.server.serverDiagnostics;
486
- if (!serverDiagnosticsNode || !serverDiagnosticsNode.sessionsDiagnosticsSummary) {
487
- debugLog("ServerSession#_createSessionObjectInAddressSpace :" + " no serverDiagnostics.sessionsDiagnosticsSummary");
488
- return false;
489
- }
490
- const sessionDiagnosticsObjectType = this.addressSpace.findObjectType("SessionDiagnosticsObjectType");
491
- const sessionDiagnosticsDataType = this.addressSpace.findDataType("SessionDiagnosticsDataType");
492
- const sessionDiagnosticsVariableType = this.addressSpace.findVariableType("SessionDiagnosticsVariableType");
493
- const sessionSecurityDiagnosticsDataType = this.addressSpace.findDataType("SessionSecurityDiagnosticsDataType");
494
- const sessionSecurityDiagnosticsType = this.addressSpace.findVariableType("SessionSecurityDiagnosticsType");
495
- const namespace = this.addressSpace.getOwnNamespace();
496
- function createSessionDiagnosticsStuff() {
497
- if (sessionDiagnosticsDataType && sessionDiagnosticsVariableType) {
498
- // the extension object
499
- this._sessionDiagnostics = this.addressSpace.constructExtensionObject(sessionDiagnosticsDataType, {});
500
- this._sessionDiagnostics.$session = this;
501
- // install property getter on property that are unlikely to change
502
- if (this.parent.clientDescription) {
503
- this._sessionDiagnostics.clientDescription = this.parent.clientDescription;
504
- }
505
- Object.defineProperty(this._sessionDiagnostics, "clientConnectionTime", {
506
- get() {
507
- return this.$session.clientConnectionTime;
508
- }
509
- });
510
- Object.defineProperty(this._sessionDiagnostics, "actualSessionTimeout", {
511
- get() {
512
- var _a;
513
- return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.sessionTimeout;
514
- }
515
- });
516
- Object.defineProperty(this._sessionDiagnostics, "sessionId", {
517
- get() {
518
- var _a;
519
- return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.nodeId;
520
- }
521
- });
522
- Object.defineProperty(this._sessionDiagnostics, "sessionName", {
523
- get() {
524
- var _a;
525
- return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.sessionName.toString();
526
- }
527
- });
528
- this.sessionDiagnostics = sessionDiagnosticsVariableType.instantiate({
529
- browseName: new node_opcua_data_model_1.QualifiedName({ name: "SessionDiagnostics", namespaceIndex: 0 }),
530
- componentOf: this.sessionObject,
531
- extensionObject: this._sessionDiagnostics,
532
- minimumSamplingInterval: 2000 // 2 seconds
533
- });
534
- this._sessionDiagnostics = this.sessionDiagnostics.$extensionObject;
535
- (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.$session === this);
536
- const sessionDiagnosticsArray = this.getSessionDiagnosticsArray();
537
- // add sessionDiagnostics into sessionDiagnosticsArray
538
- (0, node_opcua_address_space_1.addElement)(this._sessionDiagnostics, sessionDiagnosticsArray);
539
- }
540
- }
541
- function createSessionSecurityDiagnosticsStuff() {
542
- if (sessionSecurityDiagnosticsDataType && sessionSecurityDiagnosticsType) {
543
- // the extension object
544
- this._sessionSecurityDiagnostics = this.addressSpace.constructExtensionObject(sessionSecurityDiagnosticsDataType, {});
545
- this._sessionSecurityDiagnostics.$session = this;
546
- /*
547
- sessionId: NodeId;
548
- clientUserIdOfSession: UAString;
549
- clientUserIdHistory: UAString[] | null;
550
- authenticationMechanism: UAString;
551
- encoding: UAString;
552
- transportProtocol: UAString;
553
- securityMode: MessageSecurityMode;
554
- securityPolicyUri: UAString;
555
- clientCertificate: ByteString;
556
- */
557
- Object.defineProperty(this._sessionSecurityDiagnostics, "sessionId", {
558
- get() {
559
- var _a;
560
- return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.nodeId;
561
- }
562
- });
563
- Object.defineProperty(this._sessionSecurityDiagnostics, "clientUserIdOfSession", {
564
- get() {
565
- return ""; // UAString // TO DO : implement
566
- }
567
- });
568
- Object.defineProperty(this._sessionSecurityDiagnostics, "clientUserIdHistory", {
569
- get() {
570
- return []; // UAString[] | null
571
- }
572
- });
573
- Object.defineProperty(this._sessionSecurityDiagnostics, "authenticationMechanism", {
574
- get() {
575
- return "";
576
- }
577
- });
578
- Object.defineProperty(this._sessionSecurityDiagnostics, "encoding", {
579
- get() {
580
- return "";
581
- }
582
- });
583
- Object.defineProperty(this._sessionSecurityDiagnostics, "transportProtocol", {
584
- get() {
585
- return "opc.tcp";
586
- }
587
- });
588
- Object.defineProperty(this._sessionSecurityDiagnostics, "securityMode", {
589
- get() {
590
- var _a;
591
- const session = this.$session;
592
- return (_a = session === null || session === void 0 ? void 0 : session.channel) === null || _a === void 0 ? void 0 : _a.securityMode;
593
- }
594
- });
595
- Object.defineProperty(this._sessionSecurityDiagnostics, "securityPolicyUri", {
596
- get() {
597
- var _a;
598
- const session = this.$session;
599
- return (_a = session === null || session === void 0 ? void 0 : session.channel) === null || _a === void 0 ? void 0 : _a.securityPolicy;
600
- }
601
- });
602
- Object.defineProperty(this._sessionSecurityDiagnostics, "clientCertificate", {
603
- get() {
604
- const session = this.$session;
605
- return session === null || session === void 0 ? void 0 : session.channel.clientCertificate;
606
- }
607
- });
608
- this.sessionSecurityDiagnostics = sessionSecurityDiagnosticsType.instantiate({
609
- browseName: new node_opcua_data_model_1.QualifiedName({ name: "SessionSecurityDiagnostics", namespaceIndex: 0 }),
610
- componentOf: this.sessionObject,
611
- extensionObject: this._sessionSecurityDiagnostics,
612
- minimumSamplingInterval: 2000 // 2 seconds
613
- });
614
- (0, node_opcua_address_space_1.ensureObjectIsSecure)(this.sessionSecurityDiagnostics);
615
- this._sessionSecurityDiagnostics = this.sessionSecurityDiagnostics
616
- .$extensionObject;
617
- (0, node_opcua_assert_1.assert)(this._sessionSecurityDiagnostics.$session === this);
618
- const sessionSecurityDiagnosticsArray = this.getSessionSecurityDiagnosticsArray();
619
- // add sessionDiagnostics into sessionDiagnosticsArray
620
- const node = (0, node_opcua_address_space_1.addElement)(this._sessionSecurityDiagnostics, sessionSecurityDiagnosticsArray);
621
- (0, node_opcua_address_space_1.ensureObjectIsSecure)(node);
622
- }
623
- }
624
- function createSessionDiagnosticSummaryUAObject() {
625
- const references = [];
626
- if (sessionDiagnosticsObjectType) {
627
- references.push({
628
- isForward: true,
629
- nodeId: sessionDiagnosticsObjectType,
630
- referenceType: "HasTypeDefinition"
631
- });
632
- }
633
- this.sessionObject = namespace.createNode({
634
- browseName: this.sessionName || "Session-" + this.nodeId.toString(),
635
- componentOf: serverDiagnosticsNode.sessionsDiagnosticsSummary,
636
- nodeClass: node_opcua_data_model_1.NodeClass.Object,
637
- nodeId: this.nodeId,
638
- references,
639
- typeDefinition: sessionDiagnosticsObjectType
640
- });
641
- createSessionDiagnosticsStuff.call(this);
642
- createSessionSecurityDiagnosticsStuff.call(this);
643
- }
644
- function createSubscriptionDiagnosticsArray() {
645
- const subscriptionDiagnosticsArrayType = this.addressSpace.findVariableType("SubscriptionDiagnosticsArrayType");
646
- (0, node_opcua_assert_1.assert)(subscriptionDiagnosticsArrayType.nodeId.toString() === "ns=0;i=2171");
647
- this.subscriptionDiagnosticsArray = (0, node_opcua_address_space_1.createExtObjArrayNode)(this.sessionObject, {
648
- browseName: { namespaceIndex: 0, name: "SubscriptionDiagnosticsArray" },
649
- complexVariableType: "SubscriptionDiagnosticsArrayType",
650
- indexPropertyName: "subscriptionId",
651
- minimumSamplingInterval: 2000,
652
- variableType: "SubscriptionDiagnosticsType"
653
- });
654
- }
655
- createSessionDiagnosticSummaryUAObject.call(this);
656
- createSubscriptionDiagnosticsArray.call(this);
657
- return this.sessionObject;
658
- }
659
- /**
660
- *
661
- * @private
662
- */
663
- _removeSessionObjectFromAddressSpace() {
664
- // todo : dump session statistics in a file or somewhere for deeper diagnostic analysis on closed session
665
- if (!this.addressSpace) {
666
- return;
667
- }
668
- if (this.sessionDiagnostics) {
669
- const sessionDiagnosticsArray = this.getSessionDiagnosticsArray();
670
- (0, node_opcua_address_space_1.removeElement)(sessionDiagnosticsArray, this.sessionDiagnostics.$extensionObject);
671
- this.addressSpace.deleteNode(this.sessionDiagnostics);
672
- (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.$session === this);
673
- this._sessionDiagnostics.$session = null;
674
- this._sessionDiagnostics = undefined;
675
- this.sessionDiagnostics = undefined;
676
- }
677
- if (this.sessionSecurityDiagnostics) {
678
- const sessionSecurityDiagnosticsArray = this.getSessionSecurityDiagnosticsArray();
679
- (0, node_opcua_address_space_1.removeElement)(sessionSecurityDiagnosticsArray, this.sessionSecurityDiagnostics.$extensionObject);
680
- this.addressSpace.deleteNode(this.sessionSecurityDiagnostics);
681
- (0, node_opcua_assert_1.assert)(this._sessionSecurityDiagnostics.$session === this);
682
- this._sessionSecurityDiagnostics.$session = null;
683
- this._sessionSecurityDiagnostics = undefined;
684
- this.sessionSecurityDiagnostics = undefined;
685
- }
686
- if (this.sessionObject) {
687
- this.addressSpace.deleteNode(this.sessionObject);
688
- this.sessionObject = null;
689
- }
690
- }
691
- /**
692
- *
693
- * @private
694
- */
695
- _getSubscriptionDiagnosticsArray() {
696
- if (!this.addressSpace) {
697
- if (doDebug) {
698
- console.warn("ServerSession#_getSubscriptionDiagnosticsArray : no addressSpace");
699
- }
700
- return null; // no addressSpace
701
- }
702
- const subscriptionDiagnosticsArray = this.subscriptionDiagnosticsArray;
703
- if (!subscriptionDiagnosticsArray) {
704
- return null; // no subscriptionDiagnosticsArray
705
- }
706
- (0, node_opcua_assert_1.assert)(subscriptionDiagnosticsArray.browseName.toString() === "SubscriptionDiagnosticsArray");
707
- return subscriptionDiagnosticsArray;
708
- }
709
- assignSubscription(subscription) {
710
- (0, node_opcua_assert_1.assert)(!subscription.$session);
711
- (0, node_opcua_assert_1.assert)(this.nodeId instanceof node_opcua_nodeid_1.NodeId);
712
- subscription.$session = this;
713
- subscription.sessionId = this.nodeId;
714
- this._cumulatedSubscriptionCount += 1;
715
- // Notify the owner that a new subscription has been created
716
- // @event new_subscription
717
- // @param {Subscription} subscription
718
- this.emit("new_subscription", subscription);
719
- // add subscription diagnostics to SubscriptionDiagnosticsArray
720
- this._exposeSubscriptionDiagnostics(subscription);
721
- subscription.once("terminated", () => {
722
- // Notify the owner that a new subscription has been terminated
723
- // @event subscription_terminated
724
- // @param {Subscription} subscription
725
- this.emit("subscription_terminated", subscription);
726
- });
727
- }
728
- _deleteSubscriptions() {
729
- (0, node_opcua_assert_1.assert)(this.publishEngine);
730
- const subscriptions = this.publishEngine.subscriptions;
731
- for (const subscription of subscriptions) {
732
- this.deleteSubscription(subscription.id);
733
- }
734
- }
735
- }
736
- exports.ServerSession = ServerSession;
737
- ServerSession.registry = new node_opcua_object_registry_1.ObjectRegistry();
738
- ServerSession.maxPublishRequestInQueue = 100;
1
+ "use strict";
2
+ /**
3
+ * @module node-opcua-server
4
+ */
5
+ // tslint:disable:no-console
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.ServerSession = void 0;
8
+ const crypto = require("crypto");
9
+ const events_1 = require("events");
10
+ const node_opcua_address_space_1 = require("node-opcua-address-space");
11
+ const node_opcua_assert_1 = require("node-opcua-assert");
12
+ const node_opcua_basic_types_1 = require("node-opcua-basic-types");
13
+ const node_opcua_common_1 = require("node-opcua-common");
14
+ const node_opcua_data_model_1 = require("node-opcua-data-model");
15
+ const node_opcua_debug_1 = require("node-opcua-debug");
16
+ const node_opcua_nodeid_1 = require("node-opcua-nodeid");
17
+ const node_opcua_object_registry_1 = require("node-opcua-object-registry");
18
+ const node_opcua_status_code_1 = require("node-opcua-status-code");
19
+ const node_opcua_utils_1 = require("node-opcua-utils");
20
+ const node_opcua_utils_2 = require("node-opcua-utils");
21
+ const server_publish_engine_1 = require("./server_publish_engine");
22
+ const server_subscription_1 = require("./server_subscription");
23
+ const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
24
+ const errorLog = (0, node_opcua_debug_1.make_errorLog)(__filename);
25
+ const doDebug = (0, node_opcua_debug_1.checkDebugFlag)(__filename);
26
+ const theWatchDog = new node_opcua_utils_1.WatchDog();
27
+ const registeredNodeNameSpace = 9999;
28
+ function compareSessionId(sessionDiagnostics1, sessionDiagnostics2) {
29
+ return sessionDiagnostics1.sessionId.toString() === sessionDiagnostics2.sessionId.toString();
30
+ }
31
+ function on_channel_abort() {
32
+ debugLog("ON CHANNEL ABORT ON SESSION!!!");
33
+ /**
34
+ * @event channel_aborted
35
+ */
36
+ this.emit("channel_aborted");
37
+ }
38
+ /**
39
+ *
40
+ * A Server session object.
41
+ *
42
+ * **from OPCUA Spec 1.02:**
43
+ *
44
+ * * Sessions are created to be independent of the underlying communications connection. Therefore, if a communication
45
+ * connection fails, the Session is not immediately affected. The exact mechanism to recover from an underlying
46
+ * communication connection error depends on the SecureChannel mapping as described in Part 6.
47
+ *
48
+ * * Sessions are terminated by the Server automatically if the Client fails to issue a Service request on the Session
49
+ * within the timeout period negotiated by the Server in the CreateSession Service response. This protects the Server
50
+ * against Client failures and against situations where a failed underlying connection cannot be re-established.
51
+ *
52
+ * * Clients shall be prepared to submit requests in a timely manner to prevent the Session from closing automatically.
53
+ *
54
+ * * Clients may explicitly terminate Sessions using the CloseSession Service.
55
+ *
56
+ * * When a Session is terminated, all outstanding requests on the Session are aborted and BadSessionClosed StatusCodes
57
+ * are returned to the Client. In addition, the Server deletes the entry for the Client from its
58
+ * SessionDiagnosticsArray Variable and notifies any other Clients who were subscribed to this entry.
59
+ *
60
+ */
61
+ class ServerSession extends events_1.EventEmitter {
62
+ constructor(parent, sessionTimeout) {
63
+ super();
64
+ this.__status = "";
65
+ this.sessionName = "";
66
+ this.keepAlive = node_opcua_utils_1.WatchDog.emptyKeepAlive;
67
+ this.parent = parent; // SessionEngine
68
+ ServerSession.registry.register(this);
69
+ (0, node_opcua_assert_1.assert)(isFinite(sessionTimeout));
70
+ (0, node_opcua_assert_1.assert)(sessionTimeout >= 0, " sessionTimeout");
71
+ this.sessionTimeout = sessionTimeout;
72
+ const authenticationTokenBuf = crypto.randomBytes(16);
73
+ this.authenticationToken = new node_opcua_nodeid_1.NodeId(node_opcua_nodeid_1.NodeIdType.BYTESTRING, authenticationTokenBuf);
74
+ // the sessionId
75
+ const ownNamespaceIndex = 1; // addressSpace.getOwnNamespace().index;
76
+ this.nodeId = new node_opcua_nodeid_1.NodeId(node_opcua_nodeid_1.NodeIdType.GUID, (0, node_opcua_basic_types_1.randomGuid)(), ownNamespaceIndex);
77
+ (0, node_opcua_assert_1.assert)(this.authenticationToken instanceof node_opcua_nodeid_1.NodeId);
78
+ (0, node_opcua_assert_1.assert)(this.nodeId instanceof node_opcua_nodeid_1.NodeId);
79
+ this._cumulatedSubscriptionCount = 0;
80
+ this.publishEngine = new server_publish_engine_1.ServerSidePublishEngine({
81
+ maxPublishRequestInQueue: ServerSession.maxPublishRequestInQueue
82
+ });
83
+ this.publishEngine.setMaxListeners(100);
84
+ theWatchDog.addSubscriber(this, this.sessionTimeout);
85
+ this.__status = "new";
86
+ /**
87
+ * the continuation point manager for this session
88
+ * @property continuationPointManager
89
+ * @type {ContinuationPointManager}
90
+ */
91
+ this.continuationPointManager = new node_opcua_address_space_1.ContinuationPointManager();
92
+ /**
93
+ * @property creationDate
94
+ * @type {Date}
95
+ */
96
+ this.creationDate = new Date();
97
+ this._registeredNodesCounter = 0;
98
+ this._registeredNodes = {};
99
+ this._registeredNodesInv = {};
100
+ }
101
+ getSessionId() {
102
+ return this.nodeId;
103
+ }
104
+ getEndpointDescription() {
105
+ return this.endpoint;
106
+ }
107
+ dispose() {
108
+ debugLog("ServerSession#dispose()");
109
+ (0, node_opcua_assert_1.assert)(!this.sessionObject, " sessionObject has not been cleared !");
110
+ this.parent = null;
111
+ this.authenticationToken = new node_opcua_nodeid_1.NodeId();
112
+ if (this.publishEngine) {
113
+ this.publishEngine.dispose();
114
+ this.publishEngine = null;
115
+ }
116
+ this._sessionDiagnostics = undefined;
117
+ this._registeredNodesCounter = 0;
118
+ this._registeredNodes = null;
119
+ this._registeredNodesInv = null;
120
+ this.continuationPointManager = null;
121
+ this.removeAllListeners();
122
+ this.__status = "disposed";
123
+ ServerSession.registry.unregister(this);
124
+ }
125
+ get clientConnectionTime() {
126
+ return this.creationDate;
127
+ }
128
+ /**
129
+ * return the number of milisecond since last session transaction occurs from client
130
+ * the first transaction is the creation of the session
131
+ */
132
+ get clientLastContactTime() {
133
+ const lastSeen = this._watchDogData ? this._watchDogData.lastSeen : node_opcua_basic_types_1.minOPCUADate.getTime();
134
+ return node_opcua_utils_1.WatchDog.lastSeenToDuration(lastSeen);
135
+ }
136
+ get status() {
137
+ return this.__status;
138
+ }
139
+ set status(value) {
140
+ if (value === "active") {
141
+ this._createSessionObjectInAddressSpace();
142
+ }
143
+ this.__status = value;
144
+ }
145
+ get addressSpace() {
146
+ if (this.parent && this.parent.addressSpace) {
147
+ return this.parent.addressSpace;
148
+ }
149
+ return null;
150
+ }
151
+ get currentPublishRequestInQueue() {
152
+ return this.publishEngine ? this.publishEngine.pendingPublishRequestCount : 0;
153
+ }
154
+ updateClientLastContactTime() {
155
+ if (this._sessionDiagnostics && this._sessionDiagnostics.clientLastContactTime) {
156
+ const currentTime = new Date();
157
+ // do not record all ticks as this may be overwhelming,
158
+ if (currentTime.getTime() - 250 >= this._sessionDiagnostics.clientLastContactTime.getTime()) {
159
+ this._sessionDiagnostics.clientLastContactTime = currentTime;
160
+ }
161
+ }
162
+ }
163
+ /**
164
+ * @method onClientSeen
165
+ * required for watch dog
166
+ * @param currentTime {DateTime}
167
+ * @private
168
+ */
169
+ onClientSeen() {
170
+ this.updateClientLastContactTime();
171
+ if (this._sessionDiagnostics) {
172
+ // see https://opcfoundation-onlineapplications.org/mantis/view.php?id=4111
173
+ (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, "currentMonitoredItemsCount"));
174
+ (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, "currentSubscriptionsCount"));
175
+ (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, "currentPublishRequestsInQueue"));
176
+ // note : https://opcfoundation-onlineapplications.org/mantis/view.php?id=4111
177
+ // sessionDiagnostics extension object uses a different spelling
178
+ // here with an S !!!!
179
+ if (this._sessionDiagnostics.currentMonitoredItemsCount !== this.currentMonitoredItemCount) {
180
+ this._sessionDiagnostics.currentMonitoredItemsCount = this.currentMonitoredItemCount;
181
+ }
182
+ if (this._sessionDiagnostics.currentSubscriptionsCount !== this.currentSubscriptionCount) {
183
+ this._sessionDiagnostics.currentSubscriptionsCount = this.currentSubscriptionCount;
184
+ }
185
+ if (this._sessionDiagnostics.currentPublishRequestsInQueue !== this.currentPublishRequestInQueue) {
186
+ this._sessionDiagnostics.currentPublishRequestsInQueue = this.currentPublishRequestInQueue;
187
+ }
188
+ }
189
+ }
190
+ incrementTotalRequestCount() {
191
+ if (this._sessionDiagnostics && this._sessionDiagnostics.totalRequestCount) {
192
+ this._sessionDiagnostics.totalRequestCount.totalCount += 1;
193
+ }
194
+ }
195
+ incrementRequestTotalCounter(counterName) {
196
+ if (this._sessionDiagnostics) {
197
+ const propName = (0, node_opcua_utils_2.lowerFirstLetter)(counterName + "Count");
198
+ // istanbul ignore next
199
+ if (!Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, propName)) {
200
+ errorLog("incrementRequestTotalCounter: cannot find", propName);
201
+ // xx return;
202
+ }
203
+ else {
204
+ this._sessionDiagnostics[propName].totalCount += 1;
205
+ }
206
+ }
207
+ }
208
+ incrementRequestErrorCounter(counterName) {
209
+ var _a;
210
+ (_a = this.parent) === null || _a === void 0 ? void 0 : _a.incrementRejectedRequestsCount();
211
+ if (this._sessionDiagnostics) {
212
+ const propName = (0, node_opcua_utils_2.lowerFirstLetter)(counterName + "Count");
213
+ // istanbul ignore next
214
+ if (!Object.prototype.hasOwnProperty.call(this._sessionDiagnostics, propName)) {
215
+ errorLog("incrementRequestErrorCounter: cannot find", propName);
216
+ // xx return;
217
+ }
218
+ else {
219
+ this._sessionDiagnostics[propName].errorCount += 1;
220
+ }
221
+ }
222
+ }
223
+ /**
224
+ * returns rootFolder.objects.server.serverDiagnostics.sessionsDiagnosticsSummary.sessionDiagnosticsArray
225
+ */
226
+ getSessionDiagnosticsArray() {
227
+ const server = this.addressSpace.rootFolder.objects.server;
228
+ return server.serverDiagnostics.sessionsDiagnosticsSummary.sessionDiagnosticsArray;
229
+ }
230
+ /**
231
+ * returns rootFolder.objects.server.serverDiagnostics.sessionsDiagnosticsSummary.sessionSecurityDiagnosticsArray
232
+ */
233
+ getSessionSecurityDiagnosticsArray() {
234
+ const server = this.addressSpace.rootFolder.objects.server;
235
+ return server.serverDiagnostics.sessionsDiagnosticsSummary.sessionSecurityDiagnosticsArray;
236
+ }
237
+ /**
238
+ * number of active subscriptions
239
+ */
240
+ get currentSubscriptionCount() {
241
+ return this.publishEngine ? this.publishEngine.subscriptionCount : 0;
242
+ }
243
+ /**
244
+ * number of subscriptions ever created since this object is live
245
+ */
246
+ get cumulatedSubscriptionCount() {
247
+ return this._cumulatedSubscriptionCount;
248
+ }
249
+ /**
250
+ * number of monitored items
251
+ */
252
+ get currentMonitoredItemCount() {
253
+ return this.publishEngine ? this.publishEngine.currentMonitoredItemCount : 0;
254
+ }
255
+ /**
256
+ * retrieve an existing subscription by subscriptionId
257
+ * @method getSubscription
258
+ * @param subscriptionId {Number}
259
+ * @return {Subscription}
260
+ */
261
+ getSubscription(subscriptionId) {
262
+ const subscription = this.publishEngine.getSubscriptionById(subscriptionId);
263
+ if (subscription && subscription.state === server_subscription_1.SubscriptionState.CLOSED) {
264
+ // subscription is CLOSED but has not been notified yet
265
+ // it should be considered as excluded
266
+ return null;
267
+ }
268
+ (0, node_opcua_assert_1.assert)(!subscription || subscription.state !== server_subscription_1.SubscriptionState.CLOSED, "CLOSED subscription shall not be managed by publish engine anymore");
269
+ return subscription;
270
+ }
271
+ /**
272
+ * @method deleteSubscription
273
+ * @param subscriptionId {Number}
274
+ * @return {StatusCode}
275
+ */
276
+ deleteSubscription(subscriptionId) {
277
+ const subscription = this.getSubscription(subscriptionId);
278
+ if (!subscription) {
279
+ return node_opcua_status_code_1.StatusCodes.BadSubscriptionIdInvalid;
280
+ }
281
+ // xx this.publishEngine.remove_subscription(subscription);
282
+ subscription.terminate();
283
+ if (this.currentSubscriptionCount === 0) {
284
+ const local_publishEngine = this.publishEngine;
285
+ local_publishEngine.cancelPendingPublishRequest();
286
+ }
287
+ return node_opcua_status_code_1.StatusCodes.Good;
288
+ }
289
+ /**
290
+ * close a ServerSession, this will also delete the subscriptions if the flag is set.
291
+ *
292
+ * Spec extract:
293
+ *
294
+ * If a Client invokes the CloseSession Service then all Subscriptions associated with the Session are also deleted
295
+ * if the deleteSubscriptions flag is set to TRUE. If a Server terminates a Session for any other reason,
296
+ * Subscriptions associated with the Session, are not deleted. Each Subscription has its own lifetime to protect
297
+ * against data loss in the case of a Session termination. In these cases, the Subscription can be reassigned to
298
+ * another Client before its lifetime expires.
299
+ *
300
+ * @method close
301
+ * @param deleteSubscriptions : should we delete subscription ?
302
+ * @param [reason = "CloseSession"] the reason for closing the session
303
+ * (shall be "Timeout", "Terminated" or "CloseSession")
304
+ *
305
+ */
306
+ close(deleteSubscriptions, reason) {
307
+ debugLog(" closing session deleteSubscriptions = ", deleteSubscriptions);
308
+ if (this.publishEngine) {
309
+ this.publishEngine.onSessionClose();
310
+ }
311
+ theWatchDog.removeSubscriber(this);
312
+ // --------------- delete associated subscriptions ---------------------
313
+ if (!deleteSubscriptions && this.currentSubscriptionCount !== 0) {
314
+ // I don't know what to do yet if deleteSubscriptions is false
315
+ errorLog("TO DO : Closing session without deleting subscription not yet implemented");
316
+ // to do: Put subscriptions in safe place for future transfer if any
317
+ }
318
+ this._deleteSubscriptions();
319
+ (0, node_opcua_assert_1.assert)(this.currentSubscriptionCount === 0);
320
+ // Post-Conditions
321
+ (0, node_opcua_assert_1.assert)(this.currentSubscriptionCount === 0);
322
+ this.status = "closed";
323
+ this._detach_channel();
324
+ /**
325
+ * @event session_closed
326
+ * @param deleteSubscriptions {Boolean}
327
+ * @param reason {String}
328
+ */
329
+ this.emit("session_closed", this, deleteSubscriptions, reason);
330
+ // ---------------- shut down publish engine
331
+ if (this.publishEngine) {
332
+ // remove subscription
333
+ this.publishEngine.shutdown();
334
+ (0, node_opcua_assert_1.assert)(this.publishEngine.subscriptionCount === 0);
335
+ this.publishEngine.dispose();
336
+ this.publishEngine = null;
337
+ }
338
+ this._removeSessionObjectFromAddressSpace();
339
+ (0, node_opcua_assert_1.assert)(!this.sessionDiagnostics, "ServerSession#_removeSessionObjectFromAddressSpace must be called");
340
+ (0, node_opcua_assert_1.assert)(!this.sessionObject, "ServerSession#_removeSessionObjectFromAddressSpace must be called");
341
+ }
342
+ registerNode(nodeId) {
343
+ (0, node_opcua_assert_1.assert)(nodeId instanceof node_opcua_nodeid_1.NodeId);
344
+ if (nodeId.namespace === 0 && nodeId.identifierType === node_opcua_nodeid_1.NodeIdType.NUMERIC) {
345
+ return nodeId;
346
+ }
347
+ const key = nodeId.toString();
348
+ const registeredNode = this._registeredNodes[key];
349
+ if (registeredNode) {
350
+ // already registered
351
+ return registeredNode;
352
+ }
353
+ const node = this.addressSpace.findNode(nodeId);
354
+ if (!node) {
355
+ return nodeId;
356
+ }
357
+ this._registeredNodesCounter += 1;
358
+ const aliasNodeId = (0, node_opcua_nodeid_1.makeNodeId)(this._registeredNodesCounter, registeredNodeNameSpace);
359
+ this._registeredNodes[key] = aliasNodeId;
360
+ this._registeredNodesInv[aliasNodeId.toString()] = node;
361
+ return aliasNodeId;
362
+ }
363
+ unRegisterNode(aliasNodeId) {
364
+ (0, node_opcua_assert_1.assert)(aliasNodeId instanceof node_opcua_nodeid_1.NodeId);
365
+ if (aliasNodeId.namespace !== registeredNodeNameSpace) {
366
+ return; // not a registered Node
367
+ }
368
+ const node = this._registeredNodesInv[aliasNodeId.toString()];
369
+ if (!node) {
370
+ return;
371
+ }
372
+ this._registeredNodesInv[aliasNodeId.toString()] = null;
373
+ this._registeredNodes[node.nodeId.toString()] = null;
374
+ }
375
+ resolveRegisteredNode(aliasNodeId) {
376
+ if (aliasNodeId.namespace !== registeredNodeNameSpace) {
377
+ return aliasNodeId; // not a registered Node
378
+ }
379
+ const node = this._registeredNodesInv[aliasNodeId.toString()];
380
+ if (!node) {
381
+ return aliasNodeId;
382
+ }
383
+ return node.nodeId;
384
+ }
385
+ /**
386
+ * true if the underlying channel has been closed or aborted...
387
+ */
388
+ get aborted() {
389
+ if (!this.channel) {
390
+ return true;
391
+ }
392
+ return this.channel.aborted;
393
+ }
394
+ createSubscription(parameters) {
395
+ const subscription = this.parent._createSubscriptionOnSession(this, parameters);
396
+ (0, node_opcua_assert_1.assert)(!Object.prototype.hasOwnProperty.call(parameters, "id"));
397
+ this.assignSubscription(subscription);
398
+ (0, node_opcua_assert_1.assert)(subscription.$session === this);
399
+ (0, node_opcua_assert_1.assert)(subscription.sessionId instanceof node_opcua_nodeid_1.NodeId);
400
+ (0, node_opcua_assert_1.assert)((0, node_opcua_nodeid_1.sameNodeId)(subscription.sessionId, this.nodeId));
401
+ return subscription;
402
+ }
403
+ _attach_channel(channel) {
404
+ (0, node_opcua_assert_1.assert)(this.nonce && this.nonce instanceof Buffer);
405
+ this.channel = channel;
406
+ this.channelId = channel.channelId;
407
+ const key = this.authenticationToken.toString();
408
+ (0, node_opcua_assert_1.assert)(!Object.prototype.hasOwnProperty.call(channel.sessionTokens, key), "channel has already a session");
409
+ channel.sessionTokens[key] = this;
410
+ // when channel is aborting
411
+ this.channel_abort_event_handler = on_channel_abort.bind(this);
412
+ channel.on("abort", this.channel_abort_event_handler);
413
+ }
414
+ _detach_channel() {
415
+ const channel = this.channel;
416
+ // istanbul ignore next
417
+ if (!channel) {
418
+ return;
419
+ // already detached !
420
+ // throw new Error("expecting a valid channel");
421
+ }
422
+ (0, node_opcua_assert_1.assert)(this.nonce && this.nonce instanceof Buffer);
423
+ (0, node_opcua_assert_1.assert)(this.authenticationToken);
424
+ const key = this.authenticationToken.toString();
425
+ (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(channel.sessionTokens, key));
426
+ (0, node_opcua_assert_1.assert)(this.channel);
427
+ (0, node_opcua_assert_1.assert)(typeof this.channel_abort_event_handler === "function");
428
+ channel.removeListener("abort", this.channel_abort_event_handler);
429
+ delete channel.sessionTokens[key];
430
+ this.channel = undefined;
431
+ this.channelId = undefined;
432
+ }
433
+ _exposeSubscriptionDiagnostics(subscription) {
434
+ debugLog("ServerSession#_exposeSubscriptionDiagnostics");
435
+ (0, node_opcua_assert_1.assert)(subscription.$session === this);
436
+ const subscriptionDiagnosticsArray = this._getSubscriptionDiagnosticsArray();
437
+ const subscriptionDiagnostics = subscription.subscriptionDiagnostics;
438
+ (0, node_opcua_assert_1.assert)(subscriptionDiagnostics.$subscription === subscription);
439
+ if (subscriptionDiagnostics && subscriptionDiagnosticsArray) {
440
+ // subscription.id,"on session", session.nodeId.toString());
441
+ (0, node_opcua_address_space_1.addElement)(subscriptionDiagnostics, subscriptionDiagnosticsArray);
442
+ }
443
+ }
444
+ _unexposeSubscriptionDiagnostics(subscription) {
445
+ const subscriptionDiagnosticsArray = this._getSubscriptionDiagnosticsArray();
446
+ const subscriptionDiagnostics = subscription.subscriptionDiagnostics;
447
+ (0, node_opcua_assert_1.assert)(subscriptionDiagnostics instanceof node_opcua_common_1.SubscriptionDiagnosticsDataType);
448
+ if (subscriptionDiagnostics && subscriptionDiagnosticsArray) {
449
+ // subscription.id,"on session", session.nodeId.toString());
450
+ (0, node_opcua_address_space_1.removeElement)(subscriptionDiagnosticsArray, subscriptionDiagnostics);
451
+ }
452
+ debugLog("ServerSession#_unexposeSubscriptionDiagnostics");
453
+ }
454
+ /**
455
+ * @method watchdogReset
456
+ * used as a callback for the Watchdog
457
+ * @private
458
+ */
459
+ watchdogReset() {
460
+ debugLog("Session#watchdogReset: the server session has expired and must be removed from the server");
461
+ // the server session has expired and must be removed from the server
462
+ this.emit("timeout");
463
+ }
464
+ _createSessionObjectInAddressSpace() {
465
+ if (this.sessionObject) {
466
+ return;
467
+ }
468
+ (0, node_opcua_assert_1.assert)(!this.sessionObject, "ServerSession#_createSessionObjectInAddressSpace already called ?");
469
+ this.sessionObject = null;
470
+ if (!this.addressSpace) {
471
+ debugLog("ServerSession#_createSessionObjectInAddressSpace : no addressSpace");
472
+ return; // no addressSpace
473
+ }
474
+ const root = this.addressSpace.rootFolder;
475
+ (0, node_opcua_assert_1.assert)(root, "expecting a root object");
476
+ if (!root.objects) {
477
+ debugLog("ServerSession#_createSessionObjectInAddressSpace : no object folder");
478
+ return false;
479
+ }
480
+ if (!root.objects.server) {
481
+ debugLog("ServerSession#_createSessionObjectInAddressSpace : no server object");
482
+ return false;
483
+ }
484
+ // self.addressSpace.findNode(makeNodeId(ObjectIds.Server_ServerDiagnostics));
485
+ const serverDiagnosticsNode = root.objects.server.serverDiagnostics;
486
+ if (!serverDiagnosticsNode || !serverDiagnosticsNode.sessionsDiagnosticsSummary) {
487
+ debugLog("ServerSession#_createSessionObjectInAddressSpace :" + " no serverDiagnostics.sessionsDiagnosticsSummary");
488
+ return false;
489
+ }
490
+ const sessionDiagnosticsObjectType = this.addressSpace.findObjectType("SessionDiagnosticsObjectType");
491
+ const sessionDiagnosticsDataType = this.addressSpace.findDataType("SessionDiagnosticsDataType");
492
+ const sessionDiagnosticsVariableType = this.addressSpace.findVariableType("SessionDiagnosticsVariableType");
493
+ const sessionSecurityDiagnosticsDataType = this.addressSpace.findDataType("SessionSecurityDiagnosticsDataType");
494
+ const sessionSecurityDiagnosticsType = this.addressSpace.findVariableType("SessionSecurityDiagnosticsType");
495
+ const namespace = this.addressSpace.getOwnNamespace();
496
+ function createSessionDiagnosticsStuff() {
497
+ if (sessionDiagnosticsDataType && sessionDiagnosticsVariableType) {
498
+ // the extension object
499
+ this._sessionDiagnostics = this.addressSpace.constructExtensionObject(sessionDiagnosticsDataType, {});
500
+ this._sessionDiagnostics.$session = this;
501
+ // install property getter on property that are unlikely to change
502
+ if (this.parent.clientDescription) {
503
+ this._sessionDiagnostics.clientDescription = this.parent.clientDescription;
504
+ }
505
+ Object.defineProperty(this._sessionDiagnostics, "clientConnectionTime", {
506
+ get() {
507
+ return this.$session.clientConnectionTime;
508
+ }
509
+ });
510
+ Object.defineProperty(this._sessionDiagnostics, "actualSessionTimeout", {
511
+ get() {
512
+ var _a;
513
+ return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.sessionTimeout;
514
+ }
515
+ });
516
+ Object.defineProperty(this._sessionDiagnostics, "sessionId", {
517
+ get() {
518
+ var _a;
519
+ return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.nodeId;
520
+ }
521
+ });
522
+ Object.defineProperty(this._sessionDiagnostics, "sessionName", {
523
+ get() {
524
+ var _a;
525
+ return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.sessionName.toString();
526
+ }
527
+ });
528
+ this.sessionDiagnostics = sessionDiagnosticsVariableType.instantiate({
529
+ browseName: new node_opcua_data_model_1.QualifiedName({ name: "SessionDiagnostics", namespaceIndex: 0 }),
530
+ componentOf: this.sessionObject,
531
+ extensionObject: this._sessionDiagnostics,
532
+ minimumSamplingInterval: 2000 // 2 seconds
533
+ });
534
+ this._sessionDiagnostics = this.sessionDiagnostics.$extensionObject;
535
+ (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.$session === this);
536
+ const sessionDiagnosticsArray = this.getSessionDiagnosticsArray();
537
+ // add sessionDiagnostics into sessionDiagnosticsArray
538
+ (0, node_opcua_address_space_1.addElement)(this._sessionDiagnostics, sessionDiagnosticsArray);
539
+ }
540
+ }
541
+ function createSessionSecurityDiagnosticsStuff() {
542
+ if (sessionSecurityDiagnosticsDataType && sessionSecurityDiagnosticsType) {
543
+ // the extension object
544
+ this._sessionSecurityDiagnostics = this.addressSpace.constructExtensionObject(sessionSecurityDiagnosticsDataType, {});
545
+ this._sessionSecurityDiagnostics.$session = this;
546
+ /*
547
+ sessionId: NodeId;
548
+ clientUserIdOfSession: UAString;
549
+ clientUserIdHistory: UAString[] | null;
550
+ authenticationMechanism: UAString;
551
+ encoding: UAString;
552
+ transportProtocol: UAString;
553
+ securityMode: MessageSecurityMode;
554
+ securityPolicyUri: UAString;
555
+ clientCertificate: ByteString;
556
+ */
557
+ Object.defineProperty(this._sessionSecurityDiagnostics, "sessionId", {
558
+ get() {
559
+ var _a;
560
+ return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.nodeId;
561
+ }
562
+ });
563
+ Object.defineProperty(this._sessionSecurityDiagnostics, "clientUserIdOfSession", {
564
+ get() {
565
+ return ""; // UAString // TO DO : implement
566
+ }
567
+ });
568
+ Object.defineProperty(this._sessionSecurityDiagnostics, "clientUserIdHistory", {
569
+ get() {
570
+ return []; // UAString[] | null
571
+ }
572
+ });
573
+ Object.defineProperty(this._sessionSecurityDiagnostics, "authenticationMechanism", {
574
+ get() {
575
+ return "";
576
+ }
577
+ });
578
+ Object.defineProperty(this._sessionSecurityDiagnostics, "encoding", {
579
+ get() {
580
+ return "";
581
+ }
582
+ });
583
+ Object.defineProperty(this._sessionSecurityDiagnostics, "transportProtocol", {
584
+ get() {
585
+ return "opc.tcp";
586
+ }
587
+ });
588
+ Object.defineProperty(this._sessionSecurityDiagnostics, "securityMode", {
589
+ get() {
590
+ var _a;
591
+ const session = this.$session;
592
+ return (_a = session === null || session === void 0 ? void 0 : session.channel) === null || _a === void 0 ? void 0 : _a.securityMode;
593
+ }
594
+ });
595
+ Object.defineProperty(this._sessionSecurityDiagnostics, "securityPolicyUri", {
596
+ get() {
597
+ var _a;
598
+ const session = this.$session;
599
+ return (_a = session === null || session === void 0 ? void 0 : session.channel) === null || _a === void 0 ? void 0 : _a.securityPolicy;
600
+ }
601
+ });
602
+ Object.defineProperty(this._sessionSecurityDiagnostics, "clientCertificate", {
603
+ get() {
604
+ const session = this.$session;
605
+ return session === null || session === void 0 ? void 0 : session.channel.clientCertificate;
606
+ }
607
+ });
608
+ this.sessionSecurityDiagnostics = sessionSecurityDiagnosticsType.instantiate({
609
+ browseName: new node_opcua_data_model_1.QualifiedName({ name: "SessionSecurityDiagnostics", namespaceIndex: 0 }),
610
+ componentOf: this.sessionObject,
611
+ extensionObject: this._sessionSecurityDiagnostics,
612
+ minimumSamplingInterval: 2000 // 2 seconds
613
+ });
614
+ (0, node_opcua_address_space_1.ensureObjectIsSecure)(this.sessionSecurityDiagnostics);
615
+ this._sessionSecurityDiagnostics = this.sessionSecurityDiagnostics
616
+ .$extensionObject;
617
+ (0, node_opcua_assert_1.assert)(this._sessionSecurityDiagnostics.$session === this);
618
+ const sessionSecurityDiagnosticsArray = this.getSessionSecurityDiagnosticsArray();
619
+ // add sessionDiagnostics into sessionDiagnosticsArray
620
+ const node = (0, node_opcua_address_space_1.addElement)(this._sessionSecurityDiagnostics, sessionSecurityDiagnosticsArray);
621
+ (0, node_opcua_address_space_1.ensureObjectIsSecure)(node);
622
+ }
623
+ }
624
+ function createSessionDiagnosticSummaryUAObject() {
625
+ const references = [];
626
+ if (sessionDiagnosticsObjectType) {
627
+ references.push({
628
+ isForward: true,
629
+ nodeId: sessionDiagnosticsObjectType,
630
+ referenceType: "HasTypeDefinition"
631
+ });
632
+ }
633
+ this.sessionObject = namespace.createNode({
634
+ browseName: this.sessionName || "Session-" + this.nodeId.toString(),
635
+ componentOf: serverDiagnosticsNode.sessionsDiagnosticsSummary,
636
+ nodeClass: node_opcua_data_model_1.NodeClass.Object,
637
+ nodeId: this.nodeId,
638
+ references,
639
+ typeDefinition: sessionDiagnosticsObjectType
640
+ });
641
+ createSessionDiagnosticsStuff.call(this);
642
+ createSessionSecurityDiagnosticsStuff.call(this);
643
+ }
644
+ function createSubscriptionDiagnosticsArray() {
645
+ const subscriptionDiagnosticsArrayType = this.addressSpace.findVariableType("SubscriptionDiagnosticsArrayType");
646
+ (0, node_opcua_assert_1.assert)(subscriptionDiagnosticsArrayType.nodeId.toString() === "ns=0;i=2171");
647
+ this.subscriptionDiagnosticsArray = (0, node_opcua_address_space_1.createExtObjArrayNode)(this.sessionObject, {
648
+ browseName: { namespaceIndex: 0, name: "SubscriptionDiagnosticsArray" },
649
+ complexVariableType: "SubscriptionDiagnosticsArrayType",
650
+ indexPropertyName: "subscriptionId",
651
+ minimumSamplingInterval: 2000,
652
+ variableType: "SubscriptionDiagnosticsType"
653
+ });
654
+ }
655
+ createSessionDiagnosticSummaryUAObject.call(this);
656
+ createSubscriptionDiagnosticsArray.call(this);
657
+ return this.sessionObject;
658
+ }
659
+ /**
660
+ *
661
+ * @private
662
+ */
663
+ _removeSessionObjectFromAddressSpace() {
664
+ // todo : dump session statistics in a file or somewhere for deeper diagnostic analysis on closed session
665
+ if (!this.addressSpace) {
666
+ return;
667
+ }
668
+ if (this.sessionDiagnostics) {
669
+ const sessionDiagnosticsArray = this.getSessionDiagnosticsArray();
670
+ (0, node_opcua_address_space_1.removeElement)(sessionDiagnosticsArray, this.sessionDiagnostics.$extensionObject);
671
+ this.addressSpace.deleteNode(this.sessionDiagnostics);
672
+ (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.$session === this);
673
+ this._sessionDiagnostics.$session = null;
674
+ this._sessionDiagnostics = undefined;
675
+ this.sessionDiagnostics = undefined;
676
+ }
677
+ if (this.sessionSecurityDiagnostics) {
678
+ const sessionSecurityDiagnosticsArray = this.getSessionSecurityDiagnosticsArray();
679
+ (0, node_opcua_address_space_1.removeElement)(sessionSecurityDiagnosticsArray, this.sessionSecurityDiagnostics.$extensionObject);
680
+ this.addressSpace.deleteNode(this.sessionSecurityDiagnostics);
681
+ (0, node_opcua_assert_1.assert)(this._sessionSecurityDiagnostics.$session === this);
682
+ this._sessionSecurityDiagnostics.$session = null;
683
+ this._sessionSecurityDiagnostics = undefined;
684
+ this.sessionSecurityDiagnostics = undefined;
685
+ }
686
+ if (this.sessionObject) {
687
+ this.addressSpace.deleteNode(this.sessionObject);
688
+ this.sessionObject = null;
689
+ }
690
+ }
691
+ /**
692
+ *
693
+ * @private
694
+ */
695
+ _getSubscriptionDiagnosticsArray() {
696
+ if (!this.addressSpace) {
697
+ if (doDebug) {
698
+ console.warn("ServerSession#_getSubscriptionDiagnosticsArray : no addressSpace");
699
+ }
700
+ return null; // no addressSpace
701
+ }
702
+ const subscriptionDiagnosticsArray = this.subscriptionDiagnosticsArray;
703
+ if (!subscriptionDiagnosticsArray) {
704
+ return null; // no subscriptionDiagnosticsArray
705
+ }
706
+ (0, node_opcua_assert_1.assert)(subscriptionDiagnosticsArray.browseName.toString() === "SubscriptionDiagnosticsArray");
707
+ return subscriptionDiagnosticsArray;
708
+ }
709
+ assignSubscription(subscription) {
710
+ (0, node_opcua_assert_1.assert)(!subscription.$session);
711
+ (0, node_opcua_assert_1.assert)(this.nodeId instanceof node_opcua_nodeid_1.NodeId);
712
+ subscription.$session = this;
713
+ subscription.sessionId = this.nodeId;
714
+ this._cumulatedSubscriptionCount += 1;
715
+ // Notify the owner that a new subscription has been created
716
+ // @event new_subscription
717
+ // @param {Subscription} subscription
718
+ this.emit("new_subscription", subscription);
719
+ // add subscription diagnostics to SubscriptionDiagnosticsArray
720
+ this._exposeSubscriptionDiagnostics(subscription);
721
+ subscription.once("terminated", () => {
722
+ // Notify the owner that a new subscription has been terminated
723
+ // @event subscription_terminated
724
+ // @param {Subscription} subscription
725
+ this.emit("subscription_terminated", subscription);
726
+ });
727
+ }
728
+ _deleteSubscriptions() {
729
+ (0, node_opcua_assert_1.assert)(this.publishEngine);
730
+ const subscriptions = this.publishEngine.subscriptions;
731
+ for (const subscription of subscriptions) {
732
+ this.deleteSubscription(subscription.id);
733
+ }
734
+ }
735
+ }
736
+ exports.ServerSession = ServerSession;
737
+ ServerSession.registry = new node_opcua_object_registry_1.ObjectRegistry();
738
+ ServerSession.maxPublishRequestInQueue = 100;
739
739
  //# sourceMappingURL=server_session.js.map