Exemplo n.º 1
0
    /** Thread to handle incoming requests. */
    @Override
    public void run() {
      // start message queue
      while (!Thread.interrupted()) {
        try {
          // wait for new message
          Message msg = mQueuedMessageConnection.readMessage();

          mPongFlag = true;
          if (msg.getRequest() == Instruction.PONG) {
            continue;
          }

          // notify listeners
          for (Receiver r : mReceivers) {
            r.OnNewMessage(msg);
          }
        }
        // The thread was interrupted
        catch (InterruptedIOException ex) {
          LOGGER.warning(ex.getMessage());
          mWorkerQueue.add(
              new Runnable() {
                public void run() {
                  _disconnect();
                  if (mConnectEnabled) {
                    _connect();
                  }
                }
              });
          return;
        }
        // The socket was close of any reason
        catch (IOException ex) {
          LOGGER.warning(ex.getMessage());
          mWorkerQueue.add(
              new Runnable() {
                public void run() {
                  _disconnect();
                  if (mConnectEnabled) {
                    _connect();
                  }
                }
              });
          return;
        }
      }
      LOGGER.log(Level.INFO, "[[MessageReceiver THREAD done]]");

      mWorkerQueue.add(
          new Runnable() {
            public void run() {
              _disconnect();
            }
          });
    }
Exemplo n.º 2
0
  /**
   * Sends Message if connected.
   *
   * @param msg
   */
  public void sendMessage(Message msg) {
    if (mQueuedMessageConnection == null) {
      LOGGER.warning(String.format("Message %s could not be send.", msg.getRequest().toString()));
      return;
    }

    try {
      mQueuedMessageConnection.sendMessage(msg);
      mPongFlag = true;
    } catch (IOException ex) {
      LOGGER.log(Level.SEVERE, null, ex);
    }
  }