Exemple #1
0
  /**
   * A connection action has been successfully completed, update the connection object associated
   * with the client this action belongs to and then notify the user of success.
   */
  private void connect() {

    Connection c = Connection.getInstance();
    c.changeConnectionStatus(Connection.ConnectionStatus.CONNECTED);

    Notify.toast(context, "Connected", Toast.LENGTH_LONG);
  }
Exemple #2
0
 /**
  * A connect action was unsuccessful, notify the user and update client history
  *
  * @param exception This argument is not used
  */
 private void connect(Throwable exception) {
   Connection c = Connection.getInstance();
   c.changeConnectionStatus(Connection.ConnectionStatus.ERROR);
 }
Exemple #3
0
 /**
  * A disconnect action was unsuccessful, notify user and update client history
  *
  * @param exception This argument is not used
  */
 private void disconnect(Throwable exception) {
   Connection c = Connection.getInstance();
   c.changeConnectionStatus(Connection.ConnectionStatus.DISCONNECTED);
 }
Exemple #4
0
 /**
  * A disconnection action has been successfully completed, update the connection object associated
  * with the client this action belongs to and then notify the user of success.
  */
 private void disconnect() {
   Connection c = Connection.getInstance();
   c.changeConnectionStatus(Connection.ConnectionStatus.DISCONNECTED);
   String actionTaken = context.getString(R.string.toast_disconnected);
   Notify.toast(context, actionTaken, Toast.LENGTH_LONG);
 }
  /**
   * Process data from the connect action
   *
   * @param data the {@link Bundle} returned by the {@link NewConnection} Acitivty
   */
  private void connectAction(Bundle data) {
    MqttConnectOptions conOpt = new MqttConnectOptions();
    /*
     * Mutal Auth connections could do something like this
     *
     *
     * SSLContext context = SSLContext.getDefault();
     * context.init({new CustomX509KeyManager()},null,null); //where CustomX509KeyManager proxies calls to keychain api
     * SSLSocketFactory factory = context.getSSLSocketFactory();
     *
     * MqttConnectOptions options = new MqttConnectOptions();
     * options.setSocketFactory(factory);
     *
     * client.connect(options);
     *
     */

    //    public Properties getSSLSettings() {
    //        final Properties properties = new Properties();
    //        properties.setProperty("com.ibm.ssl.keyStore",
    //            "C:/BKSKeystore/mqttclientkeystore.keystore");
    //        properties.setProperty("com.ibm.ssl.keyStoreType", "BKS");
    //        properties.setProperty("com.ibm.ssl.keyStorePassword", "passphrase");
    //        properties.setProperty("com.ibm.ssl.trustStore",
    //            "C:/BKSKeystore/mqttclienttrust.keystore");
    //        properties.setProperty("com.ibm.ssl.trustStoreType", "BKS");
    //        properties.setProperty("com.ibm.ssl.trustStorePassword", "passphrase ");
    //
    //        return properties;
    //    }

    try {

      SSLContext context;
      KeyStore ts = KeyStore.getInstance("BKS");
      ts.load(getResources().openRawResource(R.raw.test), "123456".toCharArray());
      TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
      tmf.init(ts);
      TrustManager[] tm = tmf.getTrustManagers();
      context = SSLContext.getInstance("TLS");
      context.init(null, tm, null);

      // SocketFactory factory= SSLSocketFactory.getDefault();
      // Socket socket =factory.createSocket("localhost", 10000);
      SocketFactory factory = context.getSocketFactory();
      conOpt.setSocketFactory(factory);

    } catch (Exception e) {
      // TODO: handle exception
    }

    // The basic client information
    String server = (String) data.get(ActivityConstants.server);
    String clientId = (String) data.get(ActivityConstants.clientId);
    int port = Integer.parseInt((String) data.get(ActivityConstants.port));
    boolean cleanSession = (Boolean) data.get(ActivityConstants.cleanSession);

    boolean ssl = (Boolean) data.get(ActivityConstants.ssl);
    String uri = null;
    if (ssl) {
      Log.e("SSLConnection", "Doing an SSL Connect");
      uri = "ssl://";

    } else {
      uri = "tcp://";
    }

    uri = uri + server + ":" + port;

    MqttClientAndroidService client;
    client = Connections.getInstance(this).createClient(this, uri, clientId);
    // create a client handle
    String clientHandle = uri + clientId;

    // last will message
    String message = (String) data.get(ActivityConstants.message);
    String topic = (String) data.get(ActivityConstants.topic);
    Integer qos = (Integer) data.get(ActivityConstants.qos);
    Boolean retained = (Boolean) data.get(ActivityConstants.retained);

    // connection options

    String username = (String) data.get(ActivityConstants.username);

    String password = (String) data.get(ActivityConstants.password);

    int timeout = (Integer) data.get(ActivityConstants.timeout);
    int keepalive = (Integer) data.get(ActivityConstants.keepalive);

    Connection connection = new Connection(clientHandle, clientId, server, port, this, client, ssl);
    arrayAdapter.add(connection);

    connection.registerChangeListener(changeListener);
    // connect client

    String[] actionArgs = new String[1];
    actionArgs[0] = clientId;
    connection.changeConnectionStatus(ConnectionStatus.CONNECTING);

    conOpt.setCleanSession(cleanSession);
    conOpt.setConnectionTimeout(timeout);
    conOpt.setKeepAliveInterval(keepalive);
    if (!username.equals(ActivityConstants.empty)) {
      conOpt.setUserName(username);
    }
    if (!password.equals(ActivityConstants.empty)) {
      conOpt.setPassword(password.toCharArray());
    }

    final ActionListener callback =
        new ActionListener(this, ActionListener.Action.CONNECT, clientHandle, actionArgs);

    boolean doConnect = true;

    if ((!message.equals(ActivityConstants.empty)) || (!topic.equals(ActivityConstants.empty))) {
      // need to make a message since last will is set
      try {
        conOpt.setWill(topic, message.getBytes(), qos.intValue(), retained.booleanValue());
      } catch (Exception e) {
        doConnect = false;
        callback.onFailure(null, e);
      }
    }
    client.setCallback(new MqttCallbackHandler(this, clientHandle));
    connection.addConnectionOptions(conOpt);
    Connections.getInstance(this).addConnection(connection);
    if (doConnect) {
      try {
        client.connect(conOpt, null, callback);
      } catch (MqttException e) {
        Log.e(this.getClass().getCanonicalName(), "MqttException Occured", e);
      }
    }
  }