/**
   * Publish / send a message to an MQTT server
   *
   * @param topicName the name of the topic to publish to
   * @param qos the quality of service to delivery the message at (0,1,2)
   * @param payload the set of bytes to send to the MQTT server
   * @throws MqttException
   */
  public void publish(String topicName, int qos, byte[] payload) throws MqttException {

    // Connect to the MQTT server
    // issue a non-blocking connect and then use the token to wait until the
    // connect completes. An exception is thrown if connect fails.
    log("Connecting to " + brokerUrl + " with client ID " + client.getClientId());
    IMqttToken conToken = client.connect(conOpt, null, null);
    conToken.waitForCompletion();
    log("Connected");

    String time = new Timestamp(System.currentTimeMillis()).toString();
    log("Publishing at: " + time + " to topic \"" + topicName + "\" qos " + qos);

    // Construct the message to send
    MqttMessage message = new MqttMessage(payload);
    message.setQos(qos);

    // Send the message to the server, control is returned as soon
    // as the MQTT client has accepted to deliver the message.
    // Use the delivery token to wait until the message has been
    // delivered
    IMqttDeliveryToken pubToken = client.publish(topicName, message, null, null);
    pubToken.waitForCompletion();
    log("Published");

    // Disconnect the client
    // Issue the disconnect and then use a token to wait until
    // the disconnect completes.
    log("Disconnecting");
    IMqttToken discToken = client.disconnect(null, null);
    discToken.waitForCompletion();
    log("Disconnected");
  }
Exemplo n.º 2
0
  private boolean sendInfectionMessage(boolean entering) {
    if (mqttClient != null && mqttClient.isConnected()) {
      try {
        JSONArray a = gameDatabase.findAllVirusDataAsJson();
        JSONObject o = new JSONObject();
        if (entering) o.put("entering", currentTopic);
        o.put("sender", clientId);
        o.put("viruses", a);
        String payload = o.toString();
        Log.d(LOG_TAG, "publishing to " + currentTopic + ": " + payload);
        IMqttDeliveryToken token =
            mqttClient.publish(currentTopic, new MqttMessage(payload.getBytes()));
        token.setActionCallback(
            new IMqttActionListener() {
              @Override
              public void onSuccess(IMqttToken asyncActionToken) {
                sendEnterMessage = false;
              }

              @Override
              public void onFailure(IMqttToken asyncActionToken, Throwable exception) {}
            });
        return true;
      } catch (MqttException e) {
        //
      } catch (JSONException e) {
        Log.e(LOG_TAG, "JSONException", e);
      }
    }
    return false;
  }
Exemplo n.º 3
0
 @Override
 public void deliveryComplete(IMqttDeliveryToken topic) {
   log.info(
       "Published topic: '"
           + topic.getTopics()[0]
           + "' successfully to client: '"
           + topic.getClient().getClientId()
           + "'");
 }
 /** @see MqttCallback#deliveryComplete(IMqttDeliveryToken) */
 public void deliveryComplete(IMqttDeliveryToken token) {
   // Called when a message has been delivered to the
   // server. The token passed in here is the same one
   // that was passed to or returned from the original call to publish.
   // This allows applications to perform asynchronous
   // delivery without blocking until delivery completes.
   //
   // This sample demonstrates asynchronous deliver and
   // uses the token.waitForCompletion() call in the main thread which
   // blocks until the delivery has completed.
   // Additionally the deliveryComplete method will be called if
   // the callback is set on the client
   //
   // If the connection to the server breaks before delivery has completed
   // delivery of a message will complete after the client has re-connected.
   // The getPendinTokens method will provide tokens for any messages
   // that are still to be delivered.
   try {
     log("Delivery complete callback: Publish Completed " + token.getMessage());
   } catch (Exception ex) {
     log("Exception in delivery complete callback" + ex);
   }
 }
Exemplo n.º 5
0
 public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
   log.info("message delivered .. : " + iMqttDeliveryToken.toString());
 }
 @Override
 public void deliveryComplete(IMqttDeliveryToken token) {
   LOG.log(Level.INFO, "Delivery complete of message id:{0}", token.getMessageId());
 }
Exemplo n.º 7
0
 /**
  * A completed deliver does not guarantee that the message is received by the service because
  * devices send messages with Quality of Service (QoS) 0. <br>
  * The message count represents the number of messages that were sent by the device without an
  * error on from the perspective of the device.
  *
  * @param token MQTT delivery token
  */
 public void deliveryComplete(IMqttDeliveryToken token) {
   final String METHOD = "deliveryComplete";
   LoggerUtility.fine(CLASS_NAME, METHOD, "token " + token.getMessageId());
   messageCount++;
 }