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