/// <summary> /// Send message to a device. The target device must be in the same network as the sender device. /// </summary> /// <param name="deviceId">Target device</param> /// <param name="payload">Message payload</param> /// <exception cref="DisconnectedException">Exception is thrown when the client is accidentally disconnected from the service</exception> /// <exception cref="LoginRequiredException">Exception is thrown when the device is not logged in to the service</exception> /// <exception cref="SendToInvalidException">Error while sending message</exception> /// <exception cref="TimeoutException">Timeout period exceeded</exception> /// <exception cref="TooManyRetriesException">The retry count exceeded</exception> public void SendMessageTo(string deviceId, string payload) { Exception lastException = new TooManyRetriesException(); int retryCount = 0; while (retryCount < _maxRetryCount) { try { _persistentConnectionInternalClient.SendMessageTo(deviceId, payload); RecordHeartbeat(); return; } catch (DisconnectedException ex) { Wait(); Relogin(); lastException = ex; } catch (LoginRequiredException ex) { Wait(); Relogin(); lastException = ex; } catch (TimeoutException ex) { Wait(); lastException = ex; } retryCount++; } throw lastException; }
/// <summary> /// Logs in a device to the service and open a persistent websocket connection /// </summary> /// <param name="deviceId">Unique device identifier</param> /// <param name="apiKey">Api key of the device, or any of the enclosing networks, or the enclosing service</param> /// <exception cref="DisconnectedException">Exception is thrown when the client is accidentally disconnected from the service</exception> /// <exception cref="ConnectedAlreadyException">Exception is thrown when the connection is alrady opened</exception> /// <exception cref="ConnectionFailedException">Exception is thrown when there is some error while connecting</exception> /// <exception cref="TimeoutException">Timeout period exceeded</exception> /// <exception cref="TooManyRetriesException">The retry count exceeded</exception> public void Login(string deviceId, string apiKey) { _isLoggedIn = true; _deviceId = deviceId; _apiKey = apiKey; Exception lastException = new TooManyRetriesException(); int retryCount = 0; while (retryCount < _maxRetryCount) { try { _persistentConnectionInternalClient.Login(_url, deviceId, apiKey); RecordHeartbeat(); return; } catch (DisconnectedException ex) { Wait(); InitializeClient(); lastException = ex; } catch (ConnectedAlreadyException ex) { Wait(); InitializeClient(); lastException = ex; } catch (ConnectionFailedException ex) { Wait(); InitializeClient(); lastException = ex; } catch (TimeoutException ex) { Wait(); InitializeClient(); lastException = ex; } retryCount++; } throw lastException; }
/// <summary> /// Subscribe to be able to receive pushed message from the service. When the device is not supposed to receive messages (only records telemetry data) this method shouldn't be called. /// Then the onMessageReceived action is successfully executed in QoS 1 mode the message is automatically commited. /// /// </summary> /// <param name="subscriptionType">QoS 0 or QoS 1-level subscription</param> /// <param name="onMessageReceived">This action is called whena message is received</param> /// <exception cref="DisconnectedException">Exception is thrown when the client is accidentally disconnected from the service</exception> /// <exception cref="LoginRequiredException">Exception is thrown when the device is not logged in to the service</exception> /// <exception cref="SubscribedAlreadyException">The device is already subscribed for pushed messages</exception> /// <exception cref="TimeoutException">Timeout period exceeded</exception> /// <exception cref="TooManyRetriesException">The retry count exceeded</exception> public void Subscribe(SubscriptionType subscriptionType, Action <PushedMessage> onMessageReceived) { _subscriptionType = subscriptionType; _onMessageReceived = onMessageReceived; Exception lastException = new TooManyRetriesException(); int retryCount = 0; while (retryCount < _maxRetryCount) { try { _persistentConnectionInternalClient.Subscribe(subscriptionType, onMessageReceived); RecordHeartbeat(); return; } catch (DisconnectedException ex) { Wait(); Relogin(); lastException = ex; } catch (LoginRequiredException ex) { Wait(); Relogin(); lastException = ex; } catch (TimeoutException ex) { Wait(); lastException = ex; } catch (SubscribedAlreadyException) { RecordHeartbeat(); return; } retryCount++; } throw lastException; }
public void TooManyRetriesException_MessageConstructor_HasCorrectMessage() { #region Arrange const string message = "Hello, World!"; #endregion #region Act var exception = new TooManyRetriesException(message); #endregion #region Assert Assert.AreEqual(message, exception.Message); #endregion }
public void TooManyRetriesException_InnerExceptionConstructor_HasStandardMessage() { #region Arrange const string message = "A retryable condition did not complete within the maximum number of retries."; #endregion #region Act var exception = new TooManyRetriesException(new Exception()); #endregion #region Assert Assert.AreEqual(message, exception.Message); #endregion }
/// <summary> /// Unsubscribe the device from automatically receiving pushed messages /// </summary> /// <exception cref="DisconnectedException">Exception is thrown when the client is accidentally disconnected from the service</exception> /// <exception cref="LoginRequiredException">Exception is thrown when the device is not logged in to the service</exception> /// <exception cref="UnsubscribeNotsubscribedException">The device was not subscribed so it cannot unsubscribe</exception> /// <exception cref="UnsubscribeInvalidException">Error occured while unsubscribe</exception> /// <exception cref="TimeoutException">Timeout period exceeded</exception> /// <exception cref="TooManyRetriesException">The retry count exceeded</exception> public void Unsubscribe() { Exception lastException = new TooManyRetriesException(); int retryCount = 0; while (retryCount < _maxRetryCount) { try { _persistentConnectionInternalClient.Unsubscribe(); RecordHeartbeat(); return; } catch (TimeoutException ex) { Wait(); lastException = ex; } retryCount++; } throw lastException; }
public void TooManyRetriesException_MessageInnerExceptionConstructor_HasCorrectValues() { #region Arrange const string message = "Hello, World!"; var mockException = new Mock <Exception>(); #endregion #region Act var exception = new TooManyRetriesException(message, mockException.Object); #endregion #region Assert Assert.AreEqual(message, exception.Message); Assert.AreSame(mockException.Object, exception.InnerException); #endregion }