/** * Event handler for the connection unbound event. Sets the connection state to DISCONNECTED. * @param event The Proton Event object. */ @Override public void onConnectionUnbound(Event event) { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_010: [The function sets the state to closed.] this.state = IotHubConnectionStatus.DISCONNECTED; logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Event handler for reactor final event. Releases the close lock. * If reconnection has been set starts the reconnection by calling openAsync() * @param event Proton Event object */ @Override public void onReactorFinal(Event event) { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_011: [The function shall call countdown on close latch and open latch.] closeLatch.countDown(); openLatch.countDown(); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_012: [The function shall set the reactor member variable to null.] this.reactor = null; logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Delegate the close link call to device operation objects. */ void closeLinks() { logger.LogDebug("Entered in method %s", logger.getMethodName()); for (int i = 0; i < amqpsDeviceOperationsList.size(); i++) { // Codes_SRS_AMQPSESSIONDEVICEOPERATION_12_010: [The function shall call closeLinks on all device operations.] amqpsDeviceOperationsList.get(i).closeLinks(); } logger.LogDebug("Exited from method %s", logger.getMethodName()); }
public void run() { Thread.currentThread().setName(THREAD_NAME); try { // Codes_SRS_IOTHUBRECEIVETASK_11_002: [The function shall poll an IoT Hub for messages, invoke the message callback if one exists, and return one of COMPLETE, ABANDON, or REJECT to the IoT Hub.] this.transport.handleMessage(); } // Codes_SRS_IOTHUBRECEIVETASK_11_004: [The function shall not crash because of an IOException thrown by the transport.] // Codes_SRS_IOTHUBRECEIVETASK_11_005: [The function shall not crash because of any error or exception thrown by the transport.] catch (Throwable e) { logger.LogError(e.toString() + ": " + e.getMessage()); logger.LogDebug("Exception on receiving queued messages to IoT Hub", e); } } }
/** * Event handler for the link flow event. Handles sending a single message. * @param event The Proton Event object. */ @Override public void onLinkFlow(Event event) { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_15_040: [The event handler shall save the remaining link credit.] this.linkCredit = event.getLink().getCredit(); logger.LogDebug("The link credit value is %s, method name is %s", this.linkCredit, logger.getMethodName()); logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Event handler for the link remote open event. This signifies that the * {@link org.apache.qpid.proton.reactor.Reactor} is ready, so we set the connection to CONNECTED. * @param event The Proton Event object. */ @Override public void onLinkRemoteOpen(Event event) { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_052: [The function shall call AmqpsSessionManager.onLinkRemoteOpen with the given link.] if (this.amqpsSessionManager.onLinkRemoteOpen(event)) { this.state = IotHubConnectionStatus.CONNECTED; // Codes_SRS_AMQPSIOTHUBCONNECTION_21_051 [The open latch shall be notified when that the connection has been established.] openLatch.countDown(); } logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * The function to run. */ @Override public void run() { Thread.currentThread().setName(THREAD_NAME); try { // Codes_SRS_AMQPSDEVICEAUTHENTICATIONCBSTOKENRENEWALTASK_12_003: [The function shall call the amqpsSessionDeviceOperation.renewToken.] this.amqpsSessionDeviceOperation.renewToken(); } catch (Throwable e) { logger.LogError(e.toString() + ": " + e.getMessage()); logger.LogDebug("Exception on housekeeping", e); } } }
/** * The function to run. */ @Override public void run() { Thread.currentThread().setName(THREAD_NAME); try { // Codes_SRS_AMQPSDEVICEAUTHENTICATIONCBSSENDTASK_12_003: [The function shall call the amqpsDeviceAuthenticationCBS.sendAuthenticationMessages.] this.amqpsDeviceAuthenticationCBS.sendAuthenticationMessages(); } catch (Throwable e) { logger.LogError(e.toString() + ": " + e.getMessage()); logger.LogDebug("Exception on housekeeping", e); } } }
/** * Event handler for connection bond. * Set the SSL domain and the SSL context. * * @param transport the Proton transport object to work with. */ void onConnectionBound(Transport transport) throws TransportException { logger.LogDebug("Entered in method %s", logger.getMethodName()); if (this.session != null) { // Codes_SRS_AMQPSESSIONMANAGER_12_026: [The function shall call setSslDomain on authentication if the session is not null.] this.amqpsDeviceAuthentication.setSslDomain(transport); } logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Calls the AmqpsSessionManager to find the appropriate convertToProton converter. * * @param message the message to convert. * @return AmqpsConvertToProtonReturnValue containing the status and converted message. * @throws TransportException if conversion fails. */ protected AmqpsConvertToProtonReturnValue convertToProton(com.microsoft.azure.sdk.iot.device.Message message) throws TransportException { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_056: [The function shall call AmqpsSessionManager.convertToProton with the given message.] return this.amqpsSessionManager.convertToProton(message); }
/** * Event handler for the transport error event. This triggers reconnection attempts until successful. * @param event The Proton Event object. */ @Override public void onTransportError(Event event) { logger.LogDebug("Entered in method %s", logger.getMethodName()); this.state = IotHubConnectionStatus.DISCONNECTED; //Codes_SRS_AMQPSIOTHUBCONNECTION_34_060 [If the provided event object's transport holds an error condition object, this function shall report the associated TransportException to this object's listeners.] this.savedException = getTransportExceptionFromEvent(event); this.scheduleReconnection(this.savedException); logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Calls the AmqpsSessionManager to find the appropriate convertFromProton converter. * * @param amqpsMessage the message to convert. * @param deviceClientConfig the configuration to identify the message. * @return AmqpsConvertFromProtonReturnValue containing the status and converted message. * @throws TransportException if conversion fails. */ protected AmqpsConvertFromProtonReturnValue convertFromProton(AmqpsMessage amqpsMessage, DeviceClientConfig deviceClientConfig) throws TransportException { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_056: [*The function shall call AmqpsSessionManager.convertFromProton with the given message. ] return this.amqpsSessionManager.convertFromProton(amqpsMessage, deviceClientConfig); }
/** * Starts the authentication by calling the AmqpsSessionManager. * @throws TransportException if authentication open throws. */ public void authenticate() throws TransportException { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_020: [The function shall do nothing if the authentication is already open.] if (this.amqpsSessionManager.isAuthenticationOpened()) { // Codes_SRS_AMQPSIOTHUBCONNECTION_12_021: [The function shall call AmqpsSessionManager.authenticate.] this.amqpsSessionManager.authenticate(); } logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Opens all the operation links by calling the AmqpsSessionManager. * * @throws TransportException if Proton throws. */ public void openLinks() throws TransportException { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_022: [The function shall do nothing if the authentication is already open.] if (this.amqpsSessionManager.isAuthenticationOpened()) { // Codes_SRS_AMQPSIOTHUBCONNECTION_12_023: [The function shall call AmqpsSessionManager.openDeviceOperationLinks.] this.amqpsSessionManager.openDeviceOperationLinks(); } logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Event handler for the link remote close event. This triggers reconnection attempts until successful. * Both sender and receiver links closing trigger this event, so we only handle one of them, * since the other is redundant. * @param event The Proton Event object. */ @Override public void onLinkRemoteClose(Event event) { logger.LogDebug("Entered in method %s", logger.getMethodName()); this.state = IotHubConnectionStatus.DISCONNECTED; //Codes_SRS_AMQPSIOTHUBCONNECTION_34_061 [If the provided event object's transport holds a remote error condition object, this function shall report the associated TransportException to this object's listeners.] this.savedException = getTransportExceptionFromEvent(event); this.scheduleReconnection(this.savedException); logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Closes receiver and sender link if they are not null */ protected void closeLinks() { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSDEVICEOPERATIONS_12_011: [If the sender link is not null the function shall close it and sets it to null.] if (this.senderLink != null) { this.senderLink.close(); this.senderLink = null; } // Codes_SRS_AMQPSDEVICEOPERATIONS_12_012: [If the receiver link is not null the function shall close it and sets it to null.] if (this.receiverLink != null) { this.receiverLink.close(); this.receiverLink = null; } this.amqpsSendLinkState = AmqpsDeviceOperationLinkState.CLOSED; this.amqpsRecvLinkState = AmqpsDeviceOperationLinkState.CLOSED; logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Start the token renewal process using CBS authentication. * * @throws TransportException throw if Proton operation throws. */ public void renewToken() throws TransportException { logger.LogDebug("Entered in method %s", logger.getMethodName()); if ((this.deviceClientConfig.getAuthenticationType() == DeviceClientConfig.AuthType.SAS_TOKEN) && (this.amqpsAuthenticatorState == AmqpsDeviceAuthenticationState.AUTHENTICATED)) { if (this.deviceClientConfig.getSasTokenAuthentication().isRenewalNecessary()) { logger.LogDebug("Sas token cannot be renewed automatically, so amqp connection will be unauthorized soon, method: %s", logger.getMethodName()); } else { // Codes_SRS_AMQPSESSIONDEVICEOPERATION_12_051: [The function start the authentication with the new token.] authenticate(); } } logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Event handler for the link init event. Sets the proper target address on the link. * @param event The Proton Event object. */ @Override public void onLinkInit(Event event) { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_12_016: [The function shall get the link from the event and call device operation objects with it.] Link link = event.getLink(); try { this.amqpsSessionManager.onLinkInit(link); } catch (TransportException e) { // Codes_SRS_AMQPSIOTHUBCONNECTION_34_067: [If an exception is thrown while executing the callback onLinkInit on the saved amqpsSessionManager, that exception shall be saved.] this.savedException = e; logger.LogError(this.savedException); } logger.LogDebug("Exited from method %s", logger.getMethodName()); }
/** * Private helper for open. * Starts the Proton reactor. */ private void openAsync() throws TransportException { logger.LogDebug("Entered in method %s", logger.getMethodName()); if (this.reactor == null) { // Codes_SRS_AMQPSIOTHUBCONNECTION_12_003: [The constructor shall throw TransportException if the Proton reactor creation failed.] this.reactor = createReactor(); } if (executorService == null) { executorService = Executors.newFixedThreadPool(1); } IotHubReactor iotHubReactor = new IotHubReactor(reactor); ReactorRunner reactorRunner = new ReactorRunner(iotHubReactor, this.listener, this.connectionId); executorService.submit(reactorRunner); logger.LogInfo("Reactor is assigned to executor service, method name is %s ", logger.getMethodName()); }
/** * Event handler for reactor init event. * @param event Proton Event object */ @Override public void onReactorInit(Event event) { logger.LogDebug("Entered in method %s", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_15_033: [The event handler shall set the current handler to handle the connection events.] if(this.useWebSockets) { event.getReactor().connectionToHost(this.chooseHostname(), AMQP_WEB_SOCKET_PORT, this); } else { event.getReactor().connectionToHost(this.chooseHostname(), AMQP_PORT, this); } logger.LogDebug("Exited from method %s", logger.getMethodName()); }