/** * Get the SSL trust store path. * @return the path. null if not set. */ public String getTrustStore() { return getSslOption("com.ibm.ssl.trustStore"); }
/** * Set the SSL trust store path. * <p> * Only used with "ssl:" serverURL. * Path to trust store file in JKS format. * If not set, the standard JRE and javax.net.ssl system properties * control the SSL behavior. * Generally not required if server has a CA-signed certificate. * @param path the path. null to unset. */ public void setTrustStore(String path) { setSslOption("com.ibm.ssl.trustStore", path); }
/** * Create a connector to the specified server. * <p> * A convenience function. * Connecting to the server occurs after the * topology is submitted for execution. * * @param topology the connector's associated {@code Topology}. * @param url URL of MQTT server. * @param clientId the connector's MQTT clientId. auto-generated if null. */ public MqttStreams(Topology topology, String url, String clientId) { this.topology = topology; MqttConfig config = new MqttConfig(); config.setServerURLs(new String[] {url}); config.setClientId(clientId); connector = new MqttConnector(() -> config); }
private MqttConfig newConfig(String serverURL, String clientId) { MqttConfig config = new MqttConfig(serverURL, clientId); if (authInfo.get("userID") != null) config.setUserName(authInfo.get("userID")); if (authInfo.get("password") != null) config.setPassword(authInfo.get("password").toCharArray()); if (authInfo.get("trustStore") != null) config.setTrustStore(authInfo.get("trustStore")); if (authInfo.get("trustStorePassword") != null) config.setTrustStorePassword(authInfo.get("trustStorePassword").toCharArray()); if (authInfo.get("keyStore") != null) config.setKeyStore(authInfo.get("keyStore")); if (authInfo.get("keyStorePassword") != null) config.setKeyStorePassword(authInfo.get("keyStorePassword").toCharArray()); // if (authInfo.get("keyPassword") != null) // config.setKeyPassword(authInfo.get("keyPassword").toCharArray()); // if (authInfo.get("keyCertificateAlias") != null) // config.setKeyCertificateAlias(authInfo.get("keyCertificateAlias")); return config; }
private MqttClient newClient(MqttConfig config) throws MqttException { String url = config.getServerURLs()[0]; MqttClientPersistence persistence = config.getPersistence(); if (persistence == null) persistence = new MemoryPersistence(); long actionTimeToWaitMillis = config.getActionTimeToWaitMillis(); logger.info("{} server:{} clientId:{} actionTimeToWait:{} persistence:{}", id(), url, clientId, actionTimeToWaitMillis, persistence); MqttClient client = new MqttClient(url, clientId, persistence); client.setTimeToWait(actionTimeToWaitMillis); client.setCallback(new Callback()); return client; }
@Test public void testMultipleServerURL() throws Exception { Topology top = newTopology("testMultipleServerURL"); MsgGenerator mgen = new MsgGenerator(top.getName()); int qos = 0; boolean retain = false; String clientId = newClientId(top.getName()); String topic = getMqttTopics()[0]; List<String> msgs = createMsgs(mgen, topic, getMsg1(), getMsg2()); TStream<String> s = PlumbingStreams.blockingOneShotDelay( top.collection(msgs), PUB_DELAY_MSEC, TimeUnit.MILLISECONDS); // Test server URL selection - supply a bogus 1st URL. String[] serverURLs = new String[] {"tcp://localhost:31999", getServerURI()}; MqttConfig config = newConfig(serverURLs[0], clientId); config.setServerURLs(serverURLs); MqttStreams mqtt = new MqttStreams(top, () -> config); mqtt.publish(s, topic, qos, retain); TStream<String> rcvd = mqtt.subscribe(topic, qos); completeAndValidate(clientId, top, rcvd, mgen, SEC_TIMEOUT, msgs.toArray(new String[0])); }
@Test public void testActionTime() throws Exception { Topology top = newTopology("testActionTime"); MsgGenerator mgen = new MsgGenerator(top.getName()); int qos = 0; boolean retain = false; String clientId = newClientId(top.getName()); String topic = getMqttTopics()[0]; List<String> msgs = createMsgs(mgen, topic, getMsg1(), getMsg2()); TStream<String> s = PlumbingStreams.blockingOneShotDelay( top.collection(msgs), PUB_DELAY_MSEC, TimeUnit.MILLISECONDS); // Test publish(TStream<String>, topic, qos) // Test TStream<String> subscribe(topic, qos) MqttConfig config = newConfig(getServerURI(), clientId); config.setActionTimeToWaitMillis(3*1000); MqttStreams mqtt = new MqttStreams(top, () -> config); mqtt.publish(s, topic, qos, retain); TStream<String> rcvd = mqtt.subscribe(topic, qos); completeAndValidate(clientId, top, rcvd, mgen, SEC_TIMEOUT, msgs.toArray(new String[0])); }
@Test(expected = IllegalStateException.class) public void testMultiSubscribeNeg() throws Exception { Topology top = newTopology("testMultiSubscribeNeg"); int qos = 0; String clientId = newClientId(top.getName()); String topic = getMqttTopics()[0]; // Verify the current behavior of at-most-one subscribe() // for a MqttStreams instance MqttStreams mqtt = new MqttStreams(top, getServerURI(), clientId); mqtt.subscribe(topic, qos); mqtt.subscribe(topic, qos); // should throw }
/** * Subscribe to the MQTT topic(s) and create a stream of tuples of type {@code T}. * @param <T> Tuple type * @param topicFilter the topic(s) to subscribe to. * @param qos the maximum Quality of Service to use. * @param message2Tuple function to convert {@code (topic, payload)} to * a tuple of type {@code T} * @return {@code TStream<T>} */ public <T> TStream<T> subscribe(String topicFilter, int qos, BiFunction<String, byte[], T> message2Tuple) { addSubscribe(); return topology().events(new MqttSubscriber<T>(connector, topicFilter, qos, message2Tuple)); }
@Override public MqttConfig get() { ++getCnt; System.err.println("**** getCnt:"+getCnt); // delay enough to induce multiple retry w/o exceeding test harness timeout if (getCnt == 4) pubConfig.setServerURLs(new String[]{ goodServerURI }); return pubConfig; } });
/** * Publish a {@code TStream<String>} stream's tuples as MQTT messages. * <p> * A convenience function. * The payload of each message is the String tuple's value serialized as UTF-8. * * @param stream Stream to be published. * @param topic the fixed topic. * @param qos the fixed delivery Quality of Service. * @param retain the fixed retain value. * @return TSink sink element representing termination of this stream. */ public TSink<String> publish(TStream<String> stream, String topic, int qos, boolean retain) { return publish(stream, tuple -> topic, tuple -> tuple.getBytes(StandardCharsets.UTF_8), tuple -> qos, tuple -> retain); }
/** * Create a new connector. * * @param config connector configuration. */ public MqttConnector(Supplier<MqttConfig> config) { this.configFn = config; String cid = configFn.get().getClientId(); if (cid == null) cid = MqttClient.generateClientId(); clientId = cid; } @Override
/** * Subscribe to the MQTT topic(s) and create a {@code TStream<String>}. * <p> * A convenience function. * Each message's payload is expected/required to be a UTF-8 encoded string. * Only the converted payload is present the generated tuple. * * @param <T> Tuple type * @param topicFilter the topic(s) to subscribe to. * @param qos the maximum Quality of Service to use. * @return {@code TStream<String>} * @see #publish(TStream, String, int, boolean) */ public <T> TStream<String> subscribe(String topicFilter, int qos) { addSubscribe(); return topology().events(new MqttSubscriber<String>(connector, topicFilter, qos, (topic, payload) -> new String(payload, StandardCharsets.UTF_8))); }
@Override public MqttConfig get() { ++getCnt; System.err.println("**** getCnt:"+getCnt); // delay enough to induce retry w/o exceeding test harness timeout // or missing published tuples if (getCnt == 4) subConfig.setServerURLs(new String[]{ goodServerURI }); return subConfig; } });
/** * Get the SSL trust store path. * @return the path. null if not set. */ public String getKeyStore() { return getSslOption("com.ibm.ssl.keyStore"); }
/** * Set the SSL key store path. * <p> * Only used with "ssl:" serverURL when the server is configured for * client auth. * Path to trust store file in JKS format. * If not set, the standard JRE and javax.net.ssl system properties * control the SSL behavior. * @param path the path. null to unset. */ public void setKeyStore(String path) { setSslOption("com.ibm.ssl.keyStore", path); }
/** * Get the SSL trust store path password. * @return the password. null if not set. */ public char[] getTrustStorePassword() { String s = getSslOption("com.ibm.ssl.trustStorePassword"); return s == null ? null : s.toCharArray(); }
/** * Set the SSL key store password. * <p> * Required if the key store path is set. * * @param password the password. null to unset. */ public void setKeyStorePassword(char[] password) { setSslOption("com.ibm.ssl.keyStorePassword", new String(password)); }
/** * Get the SSL key store path password. * @return the password. null if not set. */ public char[] getKeyStorePassword() { String s = getSslOption("com.ibm.ssl.keyStorePassword"); return s == null ? null : s.toCharArray(); }
/** * Set the SSL trust store password. * <p> * Required if the trust store path is set. * * @param password the password */ public void setTrustStorePassword(char[] password) { setSslOption("com.ibm.ssl.trustStorePassword", new String(password)); }