Пример #1
0
  /* (non-Javadoc)
   * @see org.eclipse.paho.client.mqttv3.IMqttAsyncClient#subscribe(java.lang.String[], int[], java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)
   */
  public IMqttToken subscribe(
      String[] topicFilters, int[] qos, Object userContext, IMqttActionListener callback)
      throws MqttException {
    final String methodName = "subscribe";

    if (topicFilters.length != qos.length) {
      throw new IllegalArgumentException();
    }

    // remove any message handlers for individual topics
    for (int i = 0; i < topicFilters.length; ++i) {
      this.comms.removeMessageListener(topicFilters[i]);
    }

    String subs = "";
    for (int i = 0; i < topicFilters.length; i++) {
      if (i > 0) {
        subs += ", ";
      }
      subs += "topic=" + topicFilters[i] + " qos=" + qos[i];

      // Check if the topic filter is valid before subscribing
      MqttTopic.validate(topicFilters[i], true /*allow wildcards*/);
    }
    // @TRACE 106=Subscribe topicFilter={0} userContext={1} callback={2}
    log.fine(CLASS_NAME, methodName, "106", new Object[] {subs, userContext, callback});

    MqttToken token = new MqttToken(getClientId());
    token.setActionCallback(callback);
    token.setUserContext(userContext);
    token.internalTok.setTopics(topicFilters);

    MqttSubscribe register = new MqttSubscribe(topicFilters, qos);

    comms.sendNoWait(register, token);
    // @TRACE 109=<
    log.fine(CLASS_NAME, methodName, "109");

    return token;
  }
Пример #2
0
  /* (non-Javadoc)
   * @see org.eclipse.paho.client.mqttv3.IMqttAsyncClient#unsubscribe(java.lang.String[], java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)
   */
  public IMqttToken unsubscribe(
      String[] topicFilters, Object userContext, IMqttActionListener callback)
      throws MqttException {
    final String methodName = "unsubscribe";
    String subs = "";
    for (int i = 0; i < topicFilters.length; i++) {
      if (i > 0) {
        subs += ", ";
      }
      subs += topicFilters[i];

      // Check if the topic filter is valid before unsubscribing
      // Although we already checked when subscribing, but invalid
      // topic filter is meanless for unsubscribing, just prohibit it
      // to reduce unnecessary control packet send to broker.
      MqttTopic.validate(topicFilters[i], true /*allow wildcards*/);
    }

    // @TRACE 107=Unsubscribe topic={0} userContext={1} callback={2}
    log.fine(CLASS_NAME, methodName, "107", new Object[] {subs, userContext, callback});

    // remove message handlers from the list for this client
    for (int i = 0; i < topicFilters.length; ++i) {
      this.comms.removeMessageListener(topicFilters[i]);
    }

    MqttToken token = new MqttToken(getClientId());
    token.setActionCallback(callback);
    token.setUserContext(userContext);
    token.internalTok.setTopics(topicFilters);

    MqttUnsubscribe unregister = new MqttUnsubscribe(topicFilters);

    comms.sendNoWait(unregister, token);
    // @TRACE 110=<
    log.fine(CLASS_NAME, methodName, "110");

    return token;
  }
Пример #3
0
  /* (non-Javadoc)
   * @see org.eclipse.paho.client.mqttv3.IMqttAsyncClient#publish(java.lang.String, org.eclipse.paho.client.mqttv3.MqttMessage, java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)
   */
  public IMqttDeliveryToken publish(
      String topic, MqttMessage message, Object userContext, IMqttActionListener callback)
      throws MqttException, MqttPersistenceException {
    final String methodName = "publish";
    // @TRACE 111=< topic={0} message={1}userContext={1} callback={2}
    log.fine(CLASS_NAME, methodName, "111", new Object[] {topic, userContext, callback});

    // Checks if a topic is valid when publishing a message.
    MqttTopic.validate(topic, false /*wildcards NOT allowed*/);

    MqttDeliveryToken token = new MqttDeliveryToken(getClientId());
    token.setActionCallback(callback);
    token.setUserContext(userContext);
    token.setMessage(message);
    token.internalTok.setTopics(new String[] {topic});

    MqttPublish pubMsg = new MqttPublish(topic, message);
    comms.sendNoWait(pubMsg, token);

    // @TRACE 112=<
    log.fine(CLASS_NAME, methodName, "112");

    return token;
  }