Skip to content

Commit

Permalink
Issue508 (#583)
Browse files Browse the repository at this point in the history
* improve sender_link debugging

* improve debug logging
  • Loading branch information
Yoseph Maguire authored Jul 16, 2019
1 parent 78e9c60 commit 575304d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 5 additions & 1 deletion common/transport/amqp/src/sender_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ export class SenderLink extends EventEmitter implements AmqpLink {
let error = this._indicatedError; // This could be undefined.
this._indicatedError = undefined;
this._senderCloseOccurred = true;
debug('in sender attached state - close event for ' + context.sender.name + ' already indicated error is: ' + this._getErrorName(error));
if (error) {
debug('in sender attached state - close event for ' + context.sender.name + ' already indicated error is: ' + this._getErrorName(error));
} else {
debug('in sender attached state - close event for ' + context.sender.name);
}
/*Codes_SRS_NODE_AMQP_SENDER_LINK_16_016: [If an error happened that caused the link to be detached, the sender link shall call emit an `error` event with that error/] */
if (error) {
context.container.emit('azure-iot-amqp-base:error-indicated', error);
Expand Down
31 changes: 29 additions & 2 deletions device/core/src/device_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Stream } from 'stream';
import * as dbg from 'debug';
const debug = dbg('azure-iot-device:InternalClient');
const debug = dbg('azure-iot-device:DeviceClient');

import { AuthenticationProvider, RetryOperation, ConnectionString, results, Callback, ErrorCallback, callbackToPromise } from 'azure-iot-common';
import { InternalClient, DeviceTransport } from './internal_client';
Expand Down Expand Up @@ -46,9 +46,13 @@ export class Client extends InternalClient {
this.on('removeListener', (eventName) => {
if (eventName === 'message' && this.listeners('message').length === 0) {
/*Codes_SRS_NODE_DEVICE_CLIENT_16_005: [The client shall stop listening for messages from the service whenever the last listener unsubscribes from the `message` event.]*/
debug('in removeListener, disabling C2D.');
this._disableC2D((err) => {
if (err) {
debug('in removeListener, error disabling C2D.');
this.emit('error', err);
} else {
debug('removeListener successfully disabled C2D.');
}
});
}
Expand All @@ -57,9 +61,13 @@ export class Client extends InternalClient {
this.on('newListener', (eventName) => {
if (eventName === 'message') {
/*Codes_SRS_NODE_DEVICE_CLIENT_16_004: [The client shall start listening for messages from the service whenever there is a listener subscribed to the `message` event.]*/
debug('in newListener, enabling C2D.');
this._enableC2D((err) => {
if (err) {
debug('in newListener, error enabling C2D.');
this.emit('error', err);
} else {
debug('in newListener, successfully enabled C2D');
}
});
}
Expand All @@ -74,16 +82,23 @@ export class Client extends InternalClient {
this._deviceDisconnectHandler = (err) => {
debug('transport disconnect event: ' + (err ? err.toString() : 'no error'));
if (err && this._retryPolicy.shouldRetry(err)) {
debug('reconnect policy specifies a reconnect on error');
/*Codes_SRS_NODE_DEVICE_CLIENT_16_097: [If the transport emits a `disconnect` event while the client is subscribed to c2d messages the retry policy shall be used to reconnect and re-enable the feature using the transport `enableC2D` method.]*/
debug('_c2dEnabled is: ' + this._c2dEnabled);
if (this._c2dEnabled) {
this._c2dEnabled = false;
debug('re-enabling C2D link');
debug('re-enabling C2D link.');
this._enableC2D((err) => {
if (err) {
/*Codes_SRS_NODE_DEVICE_CLIENT_16_102: [If the retry policy fails to reestablish the C2D functionality a `disconnect` event shall be emitted with a `results.Disconnected` object.]*/
debug('error on _enableC2D in _deviceDisconnectHandler. Failed to reestablish C2D functionality.');
this.emit('disconnect', new results.Disconnected(err));
} else {
debug('_deviceDisconnectHandler has enabled C2D');
}
});
} else {
debug('During _deviceDisconnectHandler, _c2dEnabled is false');
}
}
};
Expand Down Expand Up @@ -152,30 +167,42 @@ export class Client extends InternalClient {
}

private _enableC2D(callback: (err?: Error) => void): void {
debug('_c2dEnabled is: ' + this._c2dEnabled);
if (!this._c2dEnabled) {
debug('enabling C2D');
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
this._transport.enableC2D(opCallback);
}, (err) => {
if (!err) {
debug('enabled C2D. Setting this._c2dEnabled to true.');
this._c2dEnabled = true;
} else {
debug('Error while enabling C2D.');
}
callback(err);
});
} else {
debug('this._c2dEnable is true. Not enabling C2D.');
callback();
}
}

private _disableC2D(callback: (err?: Error) => void): void {
debug('_c2dEnabled is: ' + this._c2dEnabled);
if (this._c2dEnabled) {
debug('disabling C2D');
this._transport.disableC2D((err) => {
if (!err) {
debug('disabled C2D. Setting this._c2dEnabled to false.');
this._c2dEnabled = false;
} else {
debug('Error while disabling C2D.');
}
callback(err);
});
} else {
debug('this._c2dEnable is false. Not disabling C2D.');
callback();
}
}
Expand Down

0 comments on commit 575304d

Please sign in to comment.