node-opcua-client 2.72.2 → 2.73.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.
@@ -278,7 +278,7 @@ function createUserNameIdentityToken(
278
278
 
279
279
  // istanbul ignore next
280
280
  if (!cryptoFactory) {
281
- throw new Error(" Unsupported security Policy "+ securityPolicy.toString());
281
+ throw new Error(" Unsupported security Policy " + securityPolicy.toString());
282
282
  }
283
283
 
284
284
  identityToken = new UserNameIdentityToken({
@@ -411,7 +411,7 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
411
411
 
412
412
  this._addSession(session as ClientSessionImpl);
413
413
 
414
- this._activateSession(session as ClientSessionImpl, (err1: Error | null, session2?: ClientSessionImpl) => {
414
+ this._activateSession(session as ClientSessionImpl, (err1: Error | null, session2?: ClientSessionImpl) => {
415
415
  callback(err1, session2);
416
416
  });
417
417
  }
@@ -861,13 +861,17 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
861
861
  this.performMessageTransaction(request, (err: Error | null, response?: Response) => {
862
862
  /* istanbul ignore next */
863
863
  if (err) {
864
- // we could have an invalid state here or a connection error
865
- errorLog("error: ", err.message, " retrying in ... 5 secondes");
866
- setTimeout(() => {
867
- errorLog(" .... now retrying");
868
- this.__createSession_step3(session, callback);
869
- }, 5 * 1000);
870
- return;
864
+ console.log("__createSession_step3 has failed", err.message);
865
+ return callback(err);
866
+
867
+ // //xxxxxx qdqsdqsdqsdqsd
868
+ // // we could have an invalid state here or a connection error
869
+ // errorLog("error: ", err.message, " retrying in ... 5 secondes");
870
+ // setTimeout(() => {
871
+ // errorLog(" .... now retrying");
872
+ // this.__createSession_step3(session, callback);
873
+ // }, 5 * 1000);
874
+ // return;
871
875
  }
872
876
 
873
877
  /* istanbul ignore next */
@@ -1039,7 +1043,7 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
1039
1043
  session.lastResponseReceivedTime = new Date();
1040
1044
  if (this.keepSessionAlive) {
1041
1045
  session.startKeepAliveManager();
1042
- }
1046
+ }
1043
1047
  return callback(null, session);
1044
1048
  } else {
1045
1049
  // restore client
@@ -10,12 +10,12 @@ import { checkDebugFlag, make_debugLog, make_errorLog, make_warningLog } from "n
10
10
  import { TransferSubscriptionsRequest, TransferSubscriptionsResponse } from "node-opcua-service-subscription";
11
11
  import { CallbackT, StatusCodes } from "node-opcua-status-code";
12
12
  import { ErrorCallback } from "node-opcua-status-code";
13
+ import { CloseSessionRequest } from "node-opcua-types";
13
14
 
14
15
  import { SubscriptionId } from "./client_session";
15
16
  import { ClientSessionImpl, Reconnectable } from "./private/client_session_impl";
16
17
  import { ClientSubscriptionImpl } from "./private/client_subscription_impl";
17
18
  import { IClientBase } from "./private/i_private_client";
18
- import { OPCUAClientImpl } from "./private/opcua_client_impl";
19
19
 
20
20
  const debugLog = make_debugLog(__filename);
21
21
  const doDebug = checkDebugFlag(__filename);
@@ -133,7 +133,7 @@ function _ask_for_subscription_republish(session: ClientSessionImpl, callback: (
133
133
  });
134
134
  }
135
135
 
136
- function create_session_and_repeat_if_failed(
136
+ function create_session_and_repeat_if_failed_Old(
137
137
  client: IClientBase,
138
138
  session: ClientSessionImpl,
139
139
  callback: CallbackT<ClientSessionImpl>
@@ -145,21 +145,53 @@ function create_session_and_repeat_if_failed(
145
145
  // create new session, based on old session,
146
146
  // so we can reuse subscriptions data
147
147
  client.__createSession_step2(session, (err: Error | null, session1?: ClientSessionImpl) => {
148
- debugLog(chalk.bgWhite.cyan(" => creating a new session (based on old session data).... Done"));
148
+ if (err && session.hasBeenClosed()) {
149
+ return callback(new Error("Cannot complete subscription republish due to session termination"));
150
+ }
149
151
  if (!err && session1) {
150
152
  const newSession = session1;
151
153
  assert(session === session1, "session should have been recycled");
152
154
  callback(err, newSession);
153
155
  return;
154
156
  } else {
157
+ debugLog(chalk.bgWhite.cyan(" => creating a new session (based on old session data).... Done"));
155
158
  setTimeout(() => {
156
159
  create_session_and_repeat_if_failed(client, session, callback);
157
160
  }, 1000);
158
161
  return;
159
162
  }
160
- callback(err);
161
163
  });
162
164
  }
165
+
166
+ function create_session_and_repeat_if_failed(
167
+ client: IClientBase,
168
+ session: ClientSessionImpl,
169
+ callback: CallbackT<ClientSessionImpl>
170
+ ) {
171
+ if (session.hasBeenClosed()) {
172
+ debugLog("Cannot complete subscription republish due to session terminatio");
173
+ return callback(new Error("Cannot complete subscription republish due to session termination"));
174
+ }
175
+ debugLog(chalk.bgWhite.red(" => creating a new session ...."));
176
+ // create new session, based on old session,
177
+ // so we can reuse subscriptions data
178
+ client.__createSession_step2(session, (err: Error | null, session1?: ClientSessionImpl) => {
179
+ if (err && session.hasBeenClosed()) {
180
+ debugLog("Cannot complete subscription republish due to session terminatio");
181
+ return callback(new Error("Cannot complete subscription republish due to session termination"));
182
+ }
183
+ if (!err && session1) {
184
+ const newSession = session1;
185
+ assert(session === session1, "session should have been recycled");
186
+ callback(err, newSession);
187
+ return;
188
+ } else {
189
+ debugLog("Cannot complete subscription republish err = ", err?.message);
190
+ callback(err);
191
+ }
192
+ });
193
+ }
194
+
163
195
  function repair_client_session_by_recreating_a_new_session(
164
196
  client: IClientBase,
165
197
  session: ClientSessionImpl,
@@ -207,7 +239,25 @@ function repair_client_session_by_recreating_a_new_session(
207
239
 
208
240
  client._activateSession(newSession, (err: Error | null, session1?: ClientSessionImpl) => {
209
241
  debugLog(chalk.bgWhite.cyan(" => activating a new session .... Done err=", err ? err.message : "null"));
210
- innerCallback(err ? err : undefined);
242
+ if (err) {
243
+ debugLog(
244
+ chalk.bgWhite.cyan(
245
+ "reactivation of the new session has failed: let be smart and close it before failing this repair attempt"
246
+ )
247
+ );
248
+ // but just on the server side, not on the client side
249
+ const closeSessionRequest = new CloseSessionRequest({
250
+ deleteSubscriptions: true
251
+ });
252
+ session.performMessageTransaction(closeSessionRequest, (err2?: Error | null) => {
253
+ if (err2) {
254
+ warningLog("closing session", err2.message);
255
+ }
256
+ innerCallback(err);
257
+ });
258
+ } else {
259
+ innerCallback(err ? err : undefined);
260
+ }
211
261
  });
212
262
  },
213
263
 
@@ -238,8 +288,8 @@ function repair_client_session_by_recreating_a_new_session(
238
288
  subscriptionsToTransfer,
239
289
  (err: Error | null, transferSubscriptionsResponse?: TransferSubscriptionsResponse) => {
240
290
  if (err) {
241
- debugLog(chalk.bgCyan("Warning TransferSubscription has failed " + err.message));
242
- debugLog(chalk.bgCyan("May be the server is not supporting this feature"));
291
+ warningLog(chalk.bgCyan("Warning TransferSubscription has failed " + err.message));
292
+ warningLog(chalk.bgCyan("May be the server is not supporting this feature"));
243
293
  // when transfer subscription has failed, we have no other choice but
244
294
  // recreate the subscriptions on the server side
245
295
  return innerCallback();
@@ -303,7 +353,7 @@ function repair_client_session_by_recreating_a_new_session(
303
353
 
304
354
  (subscription as ClientSubscriptionImpl).recreateSubscriptionAndMonitoredItem((err1?: Error) => {
305
355
  if (err1) {
306
- debugLog("_recreateSubscription failed !");
356
+ debugLog("_recreateSubscription failed !" + err1.message);
307
357
  }
308
358
 
309
359
  debugLog(
@@ -356,7 +406,7 @@ function repair_client_session_by_recreating_a_new_session(
356
406
 
357
407
  function _repair_client_session(client: IClientBase, session: ClientSessionImpl, callback: (err?: Error) => void): void {
358
408
  const callback2 = (err2?: Error) => {
359
- debugLog("Session is repaired ", err2 ? err2.message : "<no error>", session.sessionId.toString());
409
+ debugLog("Session repair completed with err: ", err2 ? err2.message : "<no error>", session.sessionId.toString());
360
410
  session.emit("session_repaired");
361
411
  callback(err2);
362
412
  };
@@ -404,10 +454,13 @@ export function repair_client_session(client: IClientBase, session: ClientSessio
404
454
  _repair_client_session(client, session, (err) => {
405
455
  privateSession._reconnecting.reconnecting = false;
406
456
  if (err) {
407
- errorLog(chalk.red("SESSION RESTORED HAS FAILED! retrying"), err.message, session.sessionId.toString());
408
- return _repair_client_session(client, session, callback);
457
+ errorLog(chalk.red("session restoration has failed! err ="), err.message, session.sessionId.toString(), " => Let's retry");
458
+ setTimeout(() => {
459
+ _repair_client_session(client, session, callback);
460
+ }, 2000);
461
+ return;
409
462
  }
410
- debugLog(chalk.yellow("SESSION RESTORED"), session.sessionId.toString());
463
+ debugLog(chalk.yellow("session has been restored"), session.sessionId.toString());
411
464
  session.emit("session_restored");
412
465
  const otherCallbacks = privateSession._reconnecting.pendingCallbacks;
413
466
  privateSession._reconnecting.pendingCallbacks = [];
@@ -28,7 +28,7 @@ export function findServers(
28
28
  discoveryServerEndpointUri: string,
29
29
  callback?: (err: Error | null, result?: FindServerResults) => void
30
30
  ): any {
31
- const client = new ClientBaseImpl({});
31
+ const client = new ClientBaseImpl({ connectionStrategy: { maxRetry: 3 } });
32
32
 
33
33
  let servers: ApplicationDescription[] = [];
34
34
  let endpoints: EndpointDescription[] = [];
@@ -77,7 +77,7 @@ export function findServersOnNetwork(
77
77
  discoveryServerEndpointUri: string,
78
78
  callback?: (err: Error | null, servers?: ServerOnNetwork[]) => void
79
79
  ): any {
80
- const client = new ClientBaseImpl({});
80
+ const client = new ClientBaseImpl({ connectionStrategy: { maxRetry: 3 } });
81
81
 
82
82
  client.connect(discoveryServerEndpointUri, (err?: Error) => {
83
83
  if (!err) {