/**
   * Constructs an instance of the sample client wrapper
   *
   * @param brokerUrl the url to connect to
   * @param clientId the client id to connect with
   * @param cleanSession clear state at end of connection or not (durable or non-durable
   *     subscriptions)
   * @param quietMode whether debug should be printed to standard out
   * @param userName the username to connect with
   * @param password the password for the user
   * @throws MqttException
   */
  public SampleAsyncWait(
      String brokerUrl,
      String clientId,
      boolean cleanSession,
      boolean quietMode,
      String userName,
      String password)
      throws MqttException {
    this.brokerUrl = brokerUrl;
    this.quietMode = quietMode;
    this.clean = cleanSession;
    this.userName = userName;
    this.password = password;
    // This sample stores in a temporary directory... where messages temporarily
    // stored until the message has been delivered to the server.
    // ..a real application ought to store them somewhere
    // where they are not likely to get deleted or tampered with
    String tmpDir = System.getProperty("java.io.tmpdir");
    MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir);

    try {
      // Construct the connection options object that contains connection parameters
      // such as cleanSession and LWT
      conOpt = new MqttConnectOptions();
      conOpt.setCleanSession(clean);
      if (password != null) {
        conOpt.setPassword(this.password.toCharArray());
      }
      if (userName != null) {
        conOpt.setUserName(this.userName);
      }

      // Construct a non-blocking MQTT client instance
      client = new MqttAsyncClient(this.brokerUrl, clientId, dataStore);

      // Set this wrapper as the callback handler
      client.setCallback(this);

    } catch (MqttException e) {
      e.printStackTrace();
      log("Unable to set up client: " + e.toString());
      System.exit(1);
    }
  }
  public boolean connect(Properties props) {

    brokerUrl = props.getProperty("brokerurl", null);
    if (brokerUrl == null) {
      throw new RuntimeException("Could not find parameter brokerur");
    }

    Random rnd = new Random();
    clientId =
        props.getProperty(
            "clientid",
            System.getenv("mqtt.clientid") != null
                ? System.getenv("mqtt.clientid")
                : "mqtt.loadgen." + rnd.nextInt(100000));
    if (clientId == null || clientId.length() == 0) {
      try {
        clientId =
            NetworkInterface.getNetworkInterfaces().hasMoreElements()
                ? NetworkInterface.getNetworkInterfaces()
                    .nextElement()
                    .getInetAddresses()
                    .nextElement()
                    .getHostName()
                : "noop";
      } catch (Exception ex) {
        LOG.log(Level.SEVERE, "Could not retrieve hostname", ex);
      }
    }

    // This sample stores in a temporary directory... where messages temporarily
    // stored until the message has been delivered to the server.
    // ..a real application ought to store them somewhere
    // where they are not likely to get deleted or tampered with
    File tmpDir = createTempDir();
    MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir.getAbsolutePath());
    LOG.log(
        Level.INFO, "Using directory {0} for temporary message storage", tmpDir.getAbsolutePath());

    try {
      // Construct the connection options object that contains connection parameters
      // such as cleanSession and LWT
      conOpt = new MqttConnectOptions();
      conOpt.setCleanSession(Boolean.parseBoolean(props.getProperty("cleansession", "true")));
      conOpt.setConnectionTimeout(Integer.parseInt(props.getProperty("connectiontimeout", "100")));
      conOpt.setKeepAliveInterval(Integer.parseInt(props.getProperty("keepaliveinterval", "100")));
      conOpt.setSocketFactory(
          SslUtil.getSocketFactory(
              props.getProperty("cafile"),
              props.getProperty("cert"),
              props.getProperty("privkey"),
              props.getProperty("password", "dummy")));

      // Construct an MQTT blocking mode client
      client = new MqttClient(this.brokerUrl, clientId, dataStore);

      // Wait max 10sec for a blocking call
      client.setTimeToWait(10000);

      // Set this wrapper as the callback handler
      client.setCallback(this);

      // Connect to the MQTT server
      LOG.log(
          Level.INFO,
          "Connecting to {0} with client ID {1}",
          new Object[] {brokerUrl, client.getClientId()});
      client.connect(conOpt);
      LOG.info("Connected");

      connected = true;

      return connected;

    } catch (MqttException e) {
      LOG.log(Level.WARNING, "Unable to set up client: " + e.toString(), e);
    } catch (Exception ex) {
      LOG.log(Level.SEVERE, "Could not create connection: " + ex.getLocalizedMessage(), ex);
    }
    return false;
  }