Search in sources :

Example 1 with Server

use of io.moquette.server.Server in project nifi by apache.

the class TestConsumeMQTT method startServer.

private void startServer() throws IOException {
    MQTT_server = new Server();
    final Properties configProps = new Properties();
    configProps.put(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME, "1884");
    configProps.setProperty(PERSISTENT_STORE_PROPERTY_NAME, "./target/moquette_store.mapdb");
    IConfig server_config = new MemoryConfig(configProps);
    MQTT_server.startServer(server_config);
}
Also used : IConfig(io.moquette.server.config.IConfig) Server(io.moquette.server.Server) MemoryConfig(io.moquette.server.config.MemoryConfig) Properties(java.util.Properties)

Example 2 with Server

use of io.moquette.server.Server in project nifi by apache.

the class TestPublishAndSubscribeMqttIntegration method startServer.

private void startServer() throws IOException {
    MQTT_server = new Server();
    final Properties configProps = new Properties();
    configProps.put(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME, "1884");
    configProps.setProperty(PERSISTENT_STORE_PROPERTY_NAME, "./target/moquette_store.mapdb");
    IConfig server_config = new MemoryConfig(configProps);
    MQTT_server.startServer(server_config);
}
Also used : IConfig(io.moquette.server.config.IConfig) Server(io.moquette.server.Server) MemoryConfig(io.moquette.server.config.MemoryConfig) Properties(java.util.Properties)

Example 3 with Server

use of io.moquette.server.Server in project nifi by apache.

the class TestPublishMqttSSL method startServer.

private void startServer() throws IOException {
    MQTT_server = new Server();
    final Properties configProps = new Properties();
    configProps.put(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME, "1884");
    configProps.put(BrokerConstants.SSL_PORT_PROPERTY_NAME, "8883");
    configProps.put(BrokerConstants.JKS_PATH_PROPERTY_NAME, "src/test/resources/localhost-ks.jks");
    configProps.put(BrokerConstants.KEY_STORE_PASSWORD_PROPERTY_NAME, "localtest");
    configProps.put(BrokerConstants.KEY_MANAGER_PASSWORD_PROPERTY_NAME, "localtest");
    configProps.setProperty(PERSISTENT_STORE_PROPERTY_NAME, "./target/moquette_store.mapdb");
    IConfig server_config = new MemoryConfig(configProps);
    MQTT_server.startServer(server_config);
}
Also used : IConfig(io.moquette.server.config.IConfig) Server(io.moquette.server.Server) MemoryConfig(io.moquette.server.config.MemoryConfig) Properties(java.util.Properties) MqttTestUtils.createSslProperties(org.apache.nifi.processors.mqtt.common.MqttTestUtils.createSslProperties)

Example 4 with Server

use of io.moquette.server.Server in project smarthome by eclipse.

the class EmbeddedBrokerServiceImpl method startEmbeddedServer.

@Override
public void startEmbeddedServer(@Nullable Integer portParam, boolean secure, @Nullable String username, @Nullable String password, String persistenceFilenameParam) throws IOException {
    String persistenceFilename = persistenceFilenameParam;
    Server server = new Server();
    Properties properties = new Properties();
    // Host and port
    properties.put(BrokerConstants.HOST_PROPERTY_NAME, "0.0.0.0");
    int port;
    if (secure) {
        port = (portParam == null) ? port = 8883 : portParam;
        properties.put(BrokerConstants.SSL_PORT_PROPERTY_NAME, Integer.toString(port));
        properties.put(BrokerConstants.PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
        properties.put(BrokerConstants.KEY_MANAGER_PASSWORD_PROPERTY_NAME, "esheshesh");
    } else {
        port = (portParam == null) ? port = 1883 : portParam;
        // with SSL_PORT_PROPERTY_NAME set, netty tries to evaluate the SSL context and shuts down immediately.
        // properties.put(BrokerConstants.SSL_PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
        properties.put(BrokerConstants.PORT_PROPERTY_NAME, Integer.toString(port));
    }
    // Authentication
    io.moquette.spi.security.IAuthenticator authentificator = null;
    if (username != null && password != null && username.length() > 0 && password.length() > 0) {
        properties.put(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, false);
        properties.put(BrokerConstants.AUTHENTICATOR_CLASS_NAME, MqttEmbeddedBrokerUserAuthenticator.class.getName());
        authentificator = new MqttEmbeddedBrokerUserAuthenticator(username, password.getBytes());
    } else {
        properties.put(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, true);
    }
    // Persistence: If not set, an in-memory database is used.
    if (!persistenceFilename.isEmpty()) {
        if (!Paths.get(persistenceFilename).isAbsolute()) {
            persistenceFilename = Paths.get(ConfigConstants.getUserDataFolder()).toAbsolutePath().resolve(persistenceFilename).toString();
        }
        properties.put(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, persistenceFilename);
    }
    // We may provide ACL functionality at some point as well
    IAuthorizator authorizer = null;
    // Secure connection support
    // TODO wait for NetworkServerTls implementation
    // try {
    // final SSLContext sslContext = networkServerTls.createSSLContext("mqtt");
    // server.startServer(new MemoryConfig(properties), null, () -> sslContext, authentificator, authorizer);
    // } catch (GeneralSecurityException | IOException e) {
    // logger.error("No SSL available", e);
    server.startServer(new MemoryConfig(properties), null, null, authentificator, authorizer);
    // }
    this.server = server;
    metrics.setServer(server);
    ScheduledExecutorService s = new ScheduledThreadPoolExecutor(1);
    detectStart.startBrokerStartedDetection(port, s);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(io.moquette.server.Server) MemoryConfig(io.moquette.server.config.MemoryConfig) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) IAuthorizator(io.moquette.spi.security.IAuthorizator) Properties(java.util.Properties)

Example 5 with Server

use of io.moquette.server.Server in project mist by snuspl.

the class EventReplayUtilsTest method testUtils.

/**
 * This test tests the subscribe, replay, and removeOnCheckpoint APIs of the EventReplayUtils class.
 */
@Test
public void testUtils() throws IOException, MqttException, InterruptedException {
    try {
        final int replayPortNum = 26523;
        final String localAddress = "127.0.0.1";
        final String brokerURI = MqttUtils.BROKER_URI;
        final String topic1 = "source/user/gps";
        final String topic2 = "source/user/heartbeat";
        final String topic3 = "source/user/doorClosed";
        final String payload1 = "{\"message\": \"aaa\", \"start_timestamp\": 1}";
        final String payload2 = "{\"message\": \"bbb\", \"start_timestamp\": 2}";
        final String payload3 = "{\"message\": \"ccc\", \"start_timestamp\": 3}";
        final String payload4 = "{\"message\": \"ddd\", \"start_timestamp\": 4}";
        final String payload5 = "{\"message\": \"eee\", \"start_timestamp\": 5}";
        final String payload6 = "{\"message\": \"eee\", \"start_timestamp\": 6}";
        final MockReplayServer server = new MockReplayServer(replayPortNum);
        final Server mqttBroker = MqttUtils.createMqttBroker();
        final MqttClient pubClient = new MqttClient(brokerURI, "testPubClient");
        final Thread serverThread = new Thread(new Runnable() {

            @Override
            public void run() {
                server.startServer();
            }
        });
        serverThread.start();
        // Wait for the socket to open.
        Thread.sleep(500);
        while (server.isClosed()) {
            Thread.sleep(100);
        }
        Assert.assertTrue(!server.isClosed());
        // Have the local replay server subscribe to topics 1~3.
        EventReplayUtils.subscribe(localAddress, replayPortNum, MqttUtils.HOST, Integer.parseInt(MqttUtils.PORT), topic1);
        EventReplayUtils.subscribe(localAddress, replayPortNum, MqttUtils.HOST, Integer.parseInt(MqttUtils.PORT), topic2);
        EventReplayUtils.subscribe(localAddress, replayPortNum, MqttUtils.HOST, Integer.parseInt(MqttUtils.PORT), topic3);
        // Add mqtt events to the mock server.
        pubClient.connect();
        pubClient.publish(topic1, new MqttMessage(payload1.getBytes()));
        pubClient.publish(topic1, new MqttMessage(payload2.getBytes()));
        pubClient.publish(topic1, new MqttMessage(payload3.getBytes()));
        pubClient.publish(topic2, new MqttMessage(payload4.getBytes()));
        pubClient.publish(topic2, new MqttMessage(payload4.getBytes()));
        pubClient.publish(topic3, new MqttMessage(payload5.getBytes()));
        pubClient.publish(topic2, new MqttMessage(payload6.getBytes()));
        Thread.sleep(1000);
        // Fetch all topic1 events by using the replay method.
        final EventReplayResult result1 = EventReplayUtils.replay(localAddress, replayPortNum, brokerURI, topic1);
        final List<Tuple<Long, MqttMessage>> result1Messages = result1.getMqttMessages();
        Assert.assertEquals(3, result1Messages.size());
        final Tuple<Long, MqttMessage> result1Message1 = result1Messages.get(0);
        Assert.assertEquals(1L, (long) result1Message1.getKey());
        Assert.assertEquals(payload1, result1Message1.getValue().toString());
        final Tuple<Long, MqttMessage> result1Message2 = result1Messages.get(1);
        Assert.assertEquals(2L, (long) result1Message2.getKey());
        Assert.assertEquals(payload2, result1Message2.getValue().toString());
        final Tuple<Long, MqttMessage> result1Message3 = result1Messages.get(2);
        Assert.assertEquals(3L, (long) result1Message3.getKey());
        Assert.assertEquals(payload3, result1Message3.getValue().toString());
        // Fetch all topic2 events by using the replay method.
        final EventReplayResult result2 = EventReplayUtils.replay(localAddress, replayPortNum, brokerURI, topic2);
        final List<Tuple<Long, MqttMessage>> result2Messages = result2.getMqttMessages();
        Assert.assertEquals(3, result2Messages.size());
        final Tuple<Long, MqttMessage> result2Message1 = result2Messages.get(0);
        Assert.assertEquals(4L, (long) result2Message1.getKey());
        Assert.assertEquals(payload4, result2Message1.getValue().toString());
        final Tuple<Long, MqttMessage> result2Message2 = result2Messages.get(1);
        Assert.assertEquals(4L, (long) result2Message2.getKey());
        Assert.assertEquals(payload4, result2Message2.getValue().toString());
        final Tuple<Long, MqttMessage> result2Message3 = result2Messages.get(2);
        Assert.assertEquals(6L, (long) result2Message3.getKey());
        Assert.assertEquals(payload6, result2Message3.getValue().toString());
        // Fetch all topic3 events by using the replay method.
        final EventReplayResult result3 = EventReplayUtils.replay(localAddress, replayPortNum, brokerURI, topic3);
        final List<Tuple<Long, MqttMessage>> result3Messages = result3.getMqttMessages();
        Assert.assertEquals(1, result3Messages.size());
        final Tuple<Long, MqttMessage> result3Message1 = result3Messages.get(0);
        Assert.assertEquals(5L, (long) result3Message1.getKey());
        Assert.assertEquals(payload5, result3Message1.getValue().toString());
        // Fetch all topic2 events with timestamp 3L ~ 5L by using the replay method.
        final EventReplayResult result4 = EventReplayUtils.replay(localAddress, replayPortNum, brokerURI, topic2, 3L, 5L);
        final List<Tuple<Long, MqttMessage>> result4Messages = result4.getMqttMessages();
        Assert.assertEquals(2, result4Messages.size());
        final Tuple<Long, MqttMessage> result4Message1 = result4Messages.get(0);
        Assert.assertEquals(4L, (long) result4Message1.getKey());
        Assert.assertEquals(payload4, result4Message1.getValue().toString());
        final Tuple<Long, MqttMessage> result4Message2 = result4Messages.get(1);
        Assert.assertEquals(4L, (long) result4Message2.getKey());
        Assert.assertEquals(payload4, result4Message2.getValue().toString());
        // Remove all events of topic1 by using the removeOnCheckpoint method.
        EventReplayUtils.removeOnCheckpoint(localAddress, replayPortNum, brokerURI, topic1);
        final EventReplayResult result5 = EventReplayUtils.replay(localAddress, replayPortNum, brokerURI, topic1);
        Assert.assertEquals(0, result5.getMqttMessages().size());
        // Remove events of topic2 with timestamp less than 5 by using the removeOnCheckpoint method.
        EventReplayUtils.removeOnCheckpoint(localAddress, replayPortNum, brokerURI, topic2, 5L);
        final EventReplayResult result6 = EventReplayUtils.replay(localAddress, replayPortNum, brokerURI, topic2);
        final List<Tuple<Long, MqttMessage>> result6Messages = result6.getMqttMessages();
        Assert.assertEquals(1, result6Messages.size());
        final Tuple<Long, MqttMessage> result6Message1 = result6Messages.get(0);
        Assert.assertEquals(6L, (long) result6Message1.getKey());
        Assert.assertEquals(payload6, result6Message1.getValue().toString());
        // Disconnect and close all clients and servers.
        pubClient.disconnect();
        server.closeServer();
        mqttBroker.stopServer();
    } catch (final Exception e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Server(io.moquette.server.Server) MqttException(org.eclipse.paho.client.mqttv3.MqttException) IOException(java.io.IOException) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) Tuple(org.apache.reef.io.Tuple) Test(org.junit.Test)

Aggregations

Server (io.moquette.server.Server)9 Properties (java.util.Properties)7 MemoryConfig (io.moquette.server.config.MemoryConfig)6 IConfig (io.moquette.server.config.IConfig)5 MqttTestUtils.createSslProperties (org.apache.nifi.processors.mqtt.common.MqttTestUtils.createSslProperties)2 Test (org.junit.Test)2 MqttSink (edu.snu.mist.core.sinks.MqttSink)1 NettyTextSink (edu.snu.mist.core.sinks.NettyTextSink)1 Sink (edu.snu.mist.core.sinks.Sink)1 IAuthorizator (io.moquette.spi.security.IAuthorizator)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 Tuple (org.apache.reef.io.Tuple)1 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)1