System.out.println("connected client " + endpoint.clientIdentifier());
System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, " + "clean session = " + endpoint.isCleanSession());
System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, clean session = " + endpoint.isCleanSession());
/** * @return the client identifier as provided by the remote MQTT client */ public String clientIdentifier() { if (cached_0 != null) { return cached_0; } String ret = delegate.clientIdentifier(); cached_0 = ret; return ret; }
/** * @return the client identifier as provided by the remote MQTT client */ public String clientIdentifier() { if (cached_2 != null) { return cached_2; } String ret = delegate.clientIdentifier(); cached_2 = ret; return ret; }
/** * @return the client identifier as provided by the remote MQTT client */ public String clientIdentifier() { if (cached_2 != null) { return cached_2; } String ret = delegate.clientIdentifier(); cached_2 = ret; return ret; }
/** * AMQP bridge identifier * * @return */ public String id() { // just the MQTT client identifier return this.mqttEndpoint.clientIdentifier(); }
/** * Handler for incoming MQTT DISCONNECT message * * @param v */ private void disconnectHandler(Void v) { LOG.info("DISCONNECT from MQTT client {}", this.mqttEndpoint.clientIdentifier()); this.detachForced = false; }
/** * Handler for connection closed by remote MQTT client * * @param v */ private void closeHandler(Void v) { LOG.info("Close from MQTT client {} at {}", this.mqttEndpoint.clientIdentifier(), this.remoteAddress); close(); }
/** * Handler for incoming MQTT PUBACK message * * @param messageId message identifier */ private void pubackHandler(int messageId) { LOG.info("PUBACK [{}] from MQTT client {}", messageId, this.mqttEndpoint.clientIdentifier()); // a PUBLISH message with QoS 1 was sent to remote MQTT client (not settled yet at source) // now PUBACK is received so it's time to settle this.rcvEndpoint.settle(messageId); }
/** * Handler for incoming MQTT PUBCOMP message * * @param messageId message identifier */ private void pubcompHandler(int messageId) { LOG.info("PUBCOMP [{}] from MQTT client {}", messageId, this.mqttEndpoint.clientIdentifier()); // a PUBLISH message with QoS 2 was sent to remote MQTT client (not settled yet at source) // then PUBREC was received. The corresponding PUBREL was sent (after PUBLISH settlement at source) // and now the PUBCOMP was received so it's time to settle this.rcvEndpoint.settle(messageId); }
/** * Handler for incoming MQTT PUBREL message * * @param messageId message identifier */ private void pubrelHandler(int messageId) { LOG.info("PUBREL [{}] from MQTT client {}", messageId, this.mqttEndpoint.clientIdentifier()); // a PUBLISH message with QoS 2 was received from remote MQTT client, PUBREC was already sent // as reply, now that PUBREL is coming it's time to settle and reply with PUBCOMP this.pubEndpoint.settle(messageId); this.mqttEndpoint.publishComplete(messageId); LOG.info("PUBCOMP [{}] to MQTT client {}", messageId, this.mqttEndpoint.clientIdentifier()); }
/** * Invoked when a client sends its <em>CONNECT</em> packet and client authentication has been disabled by setting * the {@linkplain MqttProtocolAdapterProperties#isAuthenticationRequired() authentication required} configuration * property to {@code false}. * <p> * Registers a close handler on the endpoint which invokes {@link #close(MqttEndpoint, Device, CommandHandler)}. Registers a publish * handler on the endpoint which invokes {@link #onPublishedMessage(MqttContext)} for each message being published * by the client. Accepts the connection request. * * @param endpoint The MQTT endpoint representing the client. */ private Future<Device> handleEndpointConnectionWithoutAuthentication(final MqttEndpoint endpoint) { registerHandlers(endpoint, null); LOG.debug("unauthenticated device [clientId: {}] connected", endpoint.clientIdentifier()); return Future.succeededFuture(); }
/** * Handler disconnection with remote AMQP container * * @param connection current ProtonConnection instance */ private void handleRemoteDisconnect(ProtonConnection connection) { LOG.info("AMQP disconnection with {}", connection.getRemoteContainer()); connection.disconnect(); try { this.mqttEndpoint.close(); } catch (IllegalStateException e) { LOG.warn("MQTT endpoint for client {} already closed", this.mqttEndpoint.clientIdentifier()); } }
/** * Handler for incoming MQTT PUBREC message * * @param messageId message identifier */ private void pubrecHandler(int messageId) { LOG.info("PUBREC [{}] from MQTT client {}", messageId, this.mqttEndpoint.clientIdentifier()); AmqpPubrelMessage amqpPubrelMessage = new AmqpPubrelMessage(messageId); this.pubEndpoint.publish(amqpPubrelMessage, done -> { if (done.succeeded()) { this.rcvEndpoint.settle(messageId); } }); }
/** * Handler for incoming AMQP_PUBREL message * * @param pubrel AMQP_PUBREL message */ private void pubrelHandler(AmqpPubrelMessage pubrel) { this.mqttEndpoint.publishRelease((int) pubrel.messageId()); LOG.info("PUBREL [{}] to MQTT client {}", pubrel.messageId(), this.mqttEndpoint.clientIdentifier()); }
/** * Handle connection closed with remote AMQP container * * @param connection current ProtonConnection instance * @param result result of remote connection closing */ private void handleRemoteConnectionClose(ProtonConnection connection, AsyncResult<ProtonConnection> result) { // NOTE : the connection parameter is needed because Vert.x doesn't provide the ProtonConnection // instance when the operation ends with errors (so exception). We need the instance for closing. if (result.succeeded()) { LOG.info("AMQP connection closed with {}", connection.getRemoteContainer()); } else { LOG.info("AMQP connection closed with {} with error", connection.getRemoteContainer(), result.cause()); } connection.close(); try { this.mqttEndpoint.close(); } catch (IllegalStateException e) { LOG.warn("MQTT endpoint for client {} already closed", this.mqttEndpoint.clientIdentifier()); } }
/** * Setup all AMQP endpoints */ private void setupAmqpEndpoits() { // NOTE : Last Will and Testament Service endpoint is opened only if MQTT client provides will information // The receiver on the unique client publish address will be opened only after // connection is established (and CONNACK sent to the MQTT client) // setup and open AMQP endpoint for receiving on unique client control/publish addresses ProtonReceiver receiverControl = this.connection.createReceiver(String.format(AmqpReceiverEndpoint.CLIENT_CONTROL_ENDPOINT_TEMPLATE, this.mqttEndpoint.clientIdentifier())); ProtonReceiver receiverPublish = this.connection.createReceiver(String.format(AmqpReceiverEndpoint.CLIENT_PUBLISH_ENDPOINT_TEMPLATE, this.mqttEndpoint.clientIdentifier())); this.rcvEndpoint = new AmqpReceiverEndpoint(new AmqpReceiver(receiverControl, receiverPublish)); // setup and open AMQP endpoint to Subscription Service ProtonSender ssSender = this.connection.createSender(AmqpSubscriptionServiceEndpoint.SUBSCRIPTION_SERVICE_ENDPOINT); this.ssEndpoint = new AmqpSubscriptionServiceEndpoint(ssSender); // setup and open AMQP endpoint for publishing ProtonSender senderPubrel = this.connection.createSender(String.format(AmqpPublishEndpoint.AMQP_CLIENT_PUBREL_ENDPOINT_TEMPLATE, this.mqttEndpoint.clientIdentifier())); this.pubEndpoint = new AmqpPublishEndpoint(senderPubrel); this.rcvEndpoint.openControl(); this.ssEndpoint.open(); this.pubEndpoint.open(); }
/** * Invoked when a client sends its <em>CONNECT</em> packet and client authentication has been disabled by setting * the {@linkplain ProtocolAdapterProperties#isAuthenticationRequired() authentication required} configuration * property to {@code false}. * <p> * Registers a close handler on the endpoint which invokes {@link #close(MqttEndpoint, Device)}. Registers a publish * handler on the endpoint which invokes {@link #onPublishedMessage(MqttContext)} for each message being published * by the client. Accepts the connection request. * * @param endpoint The MQTT endpoint representing the client. */ private Future<Device> handleEndpointConnectionWithoutAuthentication(final MqttEndpoint endpoint) { endpoint.closeHandler(v -> close(endpoint, null)); endpoint.publishHandler(message -> handlePublishedMessage(new MqttContext(message, endpoint))); endpoint.subscribeHandler(subscribeMsg -> onSubscribe(endpoint, null, subscribeMsg)); endpoint.unsubscribeHandler(unsubscribeMsg -> onUnsubscribe(endpoint, null, unsubscribeMsg)); LOG.debug("unauthenticated device [clientId: {}] connected", endpoint.clientIdentifier()); metrics.incrementUnauthenticatedConnections(); return Future.succeededFuture(); }
private Span newSpan(final String operationName, final MqttEndpoint endpoint, final Device authenticatedDevice) { final Span span = tracer.buildSpan(operationName) .ignoreActiveSpan() .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER) .withTag(Tags.COMPONENT.getKey(), getTypeName()) .withTag(TracingHelper.TAG_CLIENT_ID.getKey(), endpoint.clientIdentifier()) .withTag(TracingHelper.TAG_AUTHENTICATED.getKey(), authenticatedDevice != null) .start(); if (authenticatedDevice != null) { span.setTag(MessageHelper.APP_PROPERTY_TENANT_ID, authenticatedDevice.getTenantId()); span.setTag(MessageHelper.APP_PROPERTY_DEVICE_ID, authenticatedDevice.getDeviceId()); } return span; }