public void run() {
   while (!interrupted()) {
     try {
       if (c.isConnected()) recvCommand();
     } catch (IOException e) {
       System.err.println("ReceiveThread: Connection Object IO error"); // ACTION
       System.exit(1);
     }
   }
 }
示例#2
0
  /**
   * Closes all existing connections
   *
   * @param callbackContext
   */
  private void disconnectAll(CallbackContext callbackContext) {
    // building iterator
    Iterator<Entry<String, Connection>> it = this.pool.entrySet().iterator();

    while (it.hasNext()) {

      // retrieving object
      Map.Entry<String, Connection> pairs = (Entry<String, Connection>) it.next();
      Connection socket = pairs.getValue();

      // checking connection
      if (socket.isConnected()) {
        socket.close();
      }

      // removing from pool
      this.pool.remove(pairs.getKey());
    }

    callbackContext.success("All connections were closed.");
  }
示例#3
0
  /**
   * Send information to target host
   *
   * @param args
   * @param callbackContext
   */
  private void send(JSONArray args, CallbackContext callbackContext) {
    Connection socket;

    // validating parameters
    if (args.length() < 2) {
      callbackContext.error("Missing arguments when calling 'send' action.");
    } else {
      try {
        // retrieving parameters
        String key = args.getString(0);
        String data = args.getString(1);

        // getting socket
        socket = this.pool.get(key);

        // checking if socket was not found and his connectivity
        if (socket == null) {
          callbackContext.error("No connection found with host " + key);

        } else if (!socket.isConnected()) {
          callbackContext.error("Invalid connection with host " + key);

        } else if (data.length() == 0) {
          callbackContext.error("Cannot send empty data to " + key);

        } else {

          // write on output stream
          socket.write(data);

          // ending send process
          callbackContext.success();
        }

      } catch (JSONException e) {
        callbackContext.error("Unexpected error sending information: " + e.getMessage());
      }
    }
  }
示例#4
0
  /**
   * Closes an existing connection
   *
   * @param args
   * @param callbackContext
   */
  private void disconnect(JSONArray args, CallbackContext callbackContext) {
    String key;
    Connection socket;

    // validating parameters
    if (args.length() < 1) {
      callbackContext.error("Missing arguments when calling 'disconnect' action.");
    } else {

      try {
        // preparing parameters
        key = args.getString(0);

        // getting connection from pool
        socket = pool.get(key);

        // closing socket
        if (socket != null) {

          // checking connection
          if (socket.isConnected()) {
            socket.close();
          }

          // removing from pool
          pool.remove(key);
        }

        // ending with success
        callbackContext.success("Disconnected from " + key);

      } catch (JSONException e) {
        callbackContext.error("Invalid parameters for 'connect' action:" + e.getMessage());
      }
    }
  }