io.vertx.mqtt.MqttServerOptions Java Examples
The following examples show how to use
io.vertx.mqtt.MqttServerOptions.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractVertxBasedMqttProtocolAdapter.java From hono with Eclipse Public License 2.0 | 6 votes |
private Future<Void> bindSecureMqttServer() { if (isSecurePortEnabled()) { final MqttServerOptions options = new MqttServerOptions(); options .setHost(getConfig().getBindAddress()) .setPort(determineSecurePort()) .setMaxMessageSize(getConfig().getMaxPayloadSize()); addTlsKeyCertOptions(options); addTlsTrustOptions(options); return bindMqttServer(options, server).map(s -> { server = s; return (Void) null; }).recover(t -> { return Future.failedFuture(t); }); } else { return Future.succeededFuture(); } }
Example #2
Source File: AbstractVertxBasedMqttProtocolAdapter.java From hono with Eclipse Public License 2.0 | 6 votes |
private Future<Void> bindInsecureMqttServer() { if (isInsecurePortEnabled()) { final MqttServerOptions options = new MqttServerOptions(); options .setHost(getConfig().getInsecurePortBindAddress()) .setPort(determineInsecurePort()) .setMaxMessageSize(getConfig().getMaxPayloadSize()); return bindMqttServer(options, insecureServer).map(server -> { insecureServer = server; return (Void) null; }).recover(t -> { return Future.failedFuture(t); }); } else { return Future.succeededFuture(); } }
Example #3
Source File: AbstractVertxBasedMqttProtocolAdapter.java From hono with Eclipse Public License 2.0 | 6 votes |
private Future<MqttServer> bindMqttServer(final MqttServerOptions options, final MqttServer mqttServer) { final Promise<MqttServer> result = Promise.promise(); final MqttServer createdMqttServer = mqttServer == null ? MqttServer.create(this.vertx, options) : mqttServer; createdMqttServer .endpointHandler(this::handleEndpointConnection) .listen(done -> { if (done.succeeded()) { log.info("MQTT server running on {}:{}", getConfig().getBindAddress(), createdMqttServer.actualPort()); result.complete(createdMqttServer); } else { log.error("error while starting up MQTT server", done.cause()); result.fail(done.cause()); } }); return result.future(); }
Example #4
Source File: VertxMqttServerProvider.java From jetlinks-community with Apache License 2.0 | 6 votes |
@Nonnull @Override public Mono<VertxMqttServerProperties> createConfig(@Nonnull NetworkProperties properties) { return Mono.defer(() -> { VertxMqttServerProperties config = FastBeanCopier.copy(properties.getConfigurations(), new VertxMqttServerProperties()); config.setId(properties.getId()); config.setOptions(new JSONObject(properties.getConfigurations()).toJavaObject(MqttServerOptions.class)); if (config.isSsl()) { config.getOptions().setSsl(true); return certificateManager.getCertificate(config.getCertId()) .map(VertxKeyCertTrustOptions::new) .doOnNext(config.getOptions()::setKeyCertOptions) .doOnNext(config.getOptions()::setTrustOptions) .thenReturn(config); } return Mono.just(config); }); }
Example #5
Source File: MqttClientSslTest.java From vertx-mqtt with Apache License 2.0 | 6 votes |
@Before public void before(TestContext ctx) { PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions() .setKeyPath("tls/server-key.pem") .setCertPath("tls/server-cert.pem"); MqttServerOptions serverOptions = new MqttServerOptions() .setPort(MQTT_SERVER_TLS_PORT) .setHost(MQTT_SERVER_HOST) .setKeyCertOptions(pemKeyCertOptions) .setSsl(true); server = MqttServer.create(vertx, serverOptions); server.exceptionHandler(t -> context.assertTrue(false)); server.endpointHandler(e -> { log.info("Client connected"); e.disconnectHandler(d -> log.info("Client disconnected")); e.accept(false); }).listen(ctx.asyncAssertSuccess()); }
Example #6
Source File: MqttServerBaseTest.java From vertx-mqtt with Apache License 2.0 | 6 votes |
/** * Setup the needs for starting the MQTT server * * @param context TestContext instance * @param options MQTT server options */ protected void setUp(TestContext context, MqttServerOptions options) { this.vertx = Vertx.vertx(); if (options == null) { this.mqttServer = MqttServer.create(this.vertx); } else { this.mqttServer = MqttServer.create(this.vertx, options); } this.mqttServer.exceptionHandler(err -> { rejection = err; }); Async async = context.async(); this.mqttServer.endpointHandler(endpoint -> endpointHandler(endpoint, context)).listen(context.asyncAssertSuccess(res -> { log.info("MQTT server listening on port " + res.actualPort()); async.complete(); })); // Synchronous start since the proxy might be triggered in method overrides async.awaitSuccess(15000); }
Example #7
Source File: MqttServerClientCertSslTest.java From vertx-mqtt with Apache License 2.0 | 5 votes |
@Before public void before(TestContext context) { MqttServerOptions options = new MqttServerOptions() .setPort(MQTT_SERVER_TLS_PORT) .setKeyCertOptions(Cert.SERVER_PEM_ROOT_CA.get()) .setTrustOptions(Trust.SERVER_PEM_ROOT_CA.get()) .setSsl(true) .setClientAuth(ClientAuth.REQUEST); this.setUp(context, options); }
Example #8
Source File: VertxMqttServerProviderTest.java From jetlinks-community with Apache License 2.0 | 5 votes |
@BeforeAll static void init() { VertxMqttServerProvider mqttServerManager = new VertxMqttServerProvider(id -> Mono.empty(), vertx); VertxMqttServerProperties properties = new VertxMqttServerProperties(); properties.setId("test"); properties.setInstance(4); properties.setOptions(new MqttServerOptions().setPort(1811)); mqttServer = mqttServerManager.createNetwork(properties); }
Example #9
Source File: MqttServerSource.java From smallrye-reactive-messaging with Apache License 2.0 | 5 votes |
private static MqttServerOptions mqttServerOptions(MqttServerConnectorIncomingConfiguration config) { final MqttServerOptions options = new MqttServerOptions(); options.setAutoClientId(config.getAutoGeneratedClientId()); options.setSsl(config.getSsl()); // TODO set KeyCertOptions if SSL, c.f. https://vertx.io/docs/vertx-mqtt/java/#_handling_client_connection_disconnection_with_ssl_tls_support options.setMaxMessageSize(config.getMaxMessageSize()); options.setTimeoutOnConnect(config.getTimeoutOnConnect()); options.setReceiveBufferSize(config.getReceiveBufferSize()); final int defaultPort = options.isSsl() ? DEFAULT_TLS_PORT : DEFAULT_PORT; options.setPort(config.getPort().orElse(defaultPort)); options.setHost(config.getHost()); return options; }
Example #10
Source File: MqttServerConnectionTest.java From vertx-mqtt with Apache License 2.0 | 5 votes |
@Before public void before(TestContext context) { MqttServerOptions options = new MqttServerOptions(); options.setTimeoutOnConnect(MQTT_TIMEOUT_ON_CONNECT); this.setUp(context, options); }
Example #11
Source File: MqttServerMaxMessageSizeTest.java From vertx-mqtt with Apache License 2.0 | 5 votes |
@Before public void before(TestContext context) { MqttServerOptions options = new MqttServerOptions(); options.setMaxMessageSize(MQTT_MAX_MESSAGE_SIZE); this.setUp(context, options); }
Example #12
Source File: MqttServerSslTest.java From vertx-mqtt with Apache License 2.0 | 5 votes |
@Before public void before(TestContext context) { MqttServerOptions options = new MqttServerOptions() .setPort(MQTT_SERVER_TLS_PORT) .setKeyCertOptions(Cert.SERVER_PEM_ROOT_CA.get()) .setSsl(true); // just useful for enabling decryption using Wireshark (which doesn't support default Diffie-Hellmann for key exchange) // options.addEnabledCipherSuite("TLS_RSA_WITH_AES_256_CBC_SHA256"); this.setUp(context, options); }
Example #13
Source File: MqttClientConnectTest.java From vertx-mqtt with Apache License 2.0 | 5 votes |
@Test public void connackNotOk(TestContext context) { Async async = context.async(); Async asyncServer = context.async(); Vertx vertx = Vertx.vertx(); MqttServer server = MqttServer.create(vertx); server.endpointHandler(endpoint -> { endpoint.reject(MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE); }); server.listen(MqttServerOptions.DEFAULT_PORT, context.asyncAssertSuccess(v -> asyncServer.complete())); asyncServer.await(); MqttClient client = MqttClient.create(vertx); client.closeHandler(v -> { // when server replies with "negative" CONNACK, this handler should not be called // the failure is just part of the connectHandler context.fail(); }); client.connect(MqttClientOptions.DEFAULT_PORT, MqttClientOptions.DEFAULT_HOST, c -> { assertTrue(c.failed()); assertTrue(c.cause() instanceof MqttConnectionException); MqttConnectionException connEx = (MqttConnectionException) c.cause(); assertEquals(connEx.code(), MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE); assertFalse(client.isConnected()); async.complete(); }); async.await(); }
Example #14
Source File: MqttServerImpl.java From vertx-mqtt with Apache License 2.0 | 4 votes |
public MqttServerImpl(Vertx vertx, MqttServerOptions options) { this.server = vertx.createNetServer(options); this.options = options; }
Example #15
Source File: MqttServerConnection.java From vertx-mqtt with Apache License 2.0 | 4 votes |
public MqttServerConnection(NetSocketInternal so, MqttServerOptions options) { this.so = so; this.chctx = so.channelHandlerContext(); this.options = options; }
Example #16
Source File: VertxMqttServerExamples.java From vertx-mqtt with Apache License 2.0 | 4 votes |
/** * Example for handling client connection using SSL/TLS * @param vertx */ public void example3(Vertx vertx) { MqttServerOptions options = new MqttServerOptions() .setPort(8883) .setKeyCertOptions(new PemKeyCertOptions() .setKeyPath("./src/test/resources/tls/server-key.pem") .setCertPath("./src/test/resources/tls/server-cert.pem")) .setSsl(true); MqttServer mqttServer = MqttServer.create(vertx, options); mqttServer.endpointHandler(endpoint -> { // shows main connect info System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, clean session = " + endpoint.isCleanSession()); if (endpoint.auth() != null) { System.out.println("[username = " + endpoint.auth().getUsername() + ", password = " + endpoint.auth().getPassword() + "]"); } if (endpoint.will() != null) { System.out.println("[will topic = " + endpoint.will().getWillTopic() + " msg = " + new String(endpoint.will().getWillMessageBytes()) + " QoS = " + endpoint.will().getWillQos() + " isRetain = " + endpoint.will().isWillRetain() + "]"); } System.out.println("[keep alive timeout = " + endpoint.keepAliveTimeSeconds() + "]"); // accept connection from the remote client endpoint.accept(false); }) .listen(ar -> { if (ar.succeeded()) { System.out.println("MQTT server is listening on port " + ar.result().actualPort()); } else { System.out.println("Error on starting the server"); ar.cause().printStackTrace(); } }); }
Example #17
Source File: MqttServerSource.java From smallrye-reactive-messaging with Apache License 2.0 | 4 votes |
MqttServerSource(Vertx vertx, MqttServerConnectorIncomingConfiguration config) { this.broadcast = config.getBroadcast(); final MqttServerOptions options = mqttServerOptions(config); this.mqttServer = MqttServer.create(vertx, options); final BehaviorProcessor<MqttMessage> processor = BehaviorProcessor.create(); mqttServer.exceptionHandler(error -> { log.exceptionThrown(error); processor.onError(error); }); mqttServer.endpointHandler(endpoint -> { log.requestToConnect(endpoint.clientIdentifier(), endpoint.isCleanSession()); if (endpoint.auth() != null) { log.requestToConnectUserName(endpoint.auth().getUsername(), endpoint.auth().getPassword()); } if (endpoint.will() != null) { log.requestToConnectWill(endpoint.will().getWillTopic(), endpoint.will().getWillMessageBytes(), endpoint.will().getWillQos(), endpoint.will().isWillRetain()); } log.requestToConnectKeepAlive(endpoint.keepAliveTimeSeconds()); endpoint.exceptionHandler( error -> log.errorWithClient(endpoint.clientIdentifier(), error)); endpoint.disconnectHandler( v -> log.clientDisconnected(endpoint.clientIdentifier())); endpoint.pingHandler( v -> log.pingReceived(endpoint.clientIdentifier())); endpoint.publishHandler(message -> { log.receivedMessageFromClient(message.payload(), message.qosLevel(), endpoint.clientIdentifier()); processor.onNext(new MqttMessage(message, endpoint.clientIdentifier(), () -> { if (message.qosLevel() == AT_LEAST_ONCE) { log.sendToClient("PUBACK", endpoint.clientIdentifier(), message.messageId()); endpoint.publishAcknowledge(message.messageId()); } else if (message.qosLevel() == EXACTLY_ONCE) { log.sendToClient("PUBREC", endpoint.clientIdentifier(), message.messageId()); endpoint.publishReceived(message.messageId()); } return CompletableFuture.completedFuture(null); })); }); endpoint.publishReleaseHandler(messageId -> { log.sendToClient("PUBCOMP", endpoint.clientIdentifier(), messageId); endpoint.publishComplete(messageId); }); endpoint.subscribeHandler(subscribeMessage -> { log.receivedSubscription(subscribeMessage, endpoint.clientIdentifier()); endpoint.close(); }); // accept connection from the remote client // this implementation doesn't keep track of sessions endpoint.accept(false); }); this.source = ReactiveStreams.fromPublisher(processor .delaySubscription(mqttServer.listen() .onItem().invoke(ignored -> log.serverListeningOn(options.getHost(), mqttServer.actualPort())) .onFailure().invoke(throwable -> log.failedToStart(throwable)) .toMulti() .then(flow -> { if (broadcast) { return flow.broadcast().toAllSubscribers(); } else { return flow; } })) .doOnSubscribe(subscription -> log.newSubscriberAdded(subscription))); }
Example #18
Source File: VertxMqttSslProviderTest.java From jetlinks-community with Apache License 2.0 | 4 votes |
@Test @SneakyThrows public void test() { VertxMqttServerProvider mqttServerManager = new VertxMqttServerProvider(id -> Mono.empty(), vertx); DefaultCertificate serverCert = new DefaultCertificate("server", "test"); DefaultCertificate clientCert = new DefaultCertificate("client", "test"); byte[] serverKs = StreamUtils.copyToByteArray(new ClassPathResource("server.p12").getInputStream()); byte[] clientKs = StreamUtils.copyToByteArray(new ClassPathResource("client.p12").getInputStream()); byte[] trust = StreamUtils.copyToByteArray(new ClassPathResource("trustStore.p12").getInputStream()); serverCert.initPfxKey(serverKs, "endPass").initPfxTrust(trust, "rootPass"); clientCert.initPfxKey(clientKs, "endPass").initPfxTrust(trust, "rootPass"); VertxMqttServerProperties properties = new VertxMqttServerProperties(); properties.setId("test"); properties.setInstance(4); properties.setSsl(true); properties.setOptions(new MqttServerOptions() .setSsl(true) .setKeyCertOptions(new VertxKeyCertTrustOptions(serverCert)) .setTrustOptions(new VertxKeyCertTrustOptions(serverCert)) .setPort(1888)); mqttServer = mqttServerManager.createNetwork(properties); MqttClientProperties propertiesClient = new MqttClientProperties(); propertiesClient.setHost("127.0.0.1"); propertiesClient.setPort(1888); propertiesClient.setOptions(new MqttClientOptions() .setSsl(true) .setKeyCertOptions(new VertxKeyCertTrustOptions(clientCert)) .setTrustOptions(new VertxKeyCertTrustOptions(clientCert))); MqttClientProvider provider = new MqttClientProvider(id -> Mono.empty(), vertx); VertxMqttClient client = provider.createNetwork(propertiesClient); mqttServer.handleConnection() .map(MqttConnection::getClientId) .doOnNext(System.out::println) .take(1) .as(StepVerifier::create) .expectNextCount(1) .verifyComplete(); }