Exemplo n.º 1
0
  /**
   * Dispatch a serialized command to the BackEnd and get back a serialized response. Mutual
   * exclusion with itself to preserve dispatching order
   */
  public synchronized byte[] dispatch(byte[] payload, boolean flush) throws ICPException {
    // Note that we don't even try to dispatch packets while the
    // device is not reachable to preserve dispatching order.
    // If dispatching succeeded in fact this command would overcome
    // any postponed command waiting to be flushed.
    if (myDisconnectionManager.isReachable()) {
      // The following check preserves dispatching order when the
      // device has just reconnected but flushing has not started yet
      if (waitingForFlush && !flush) {
        throw new ICPException("Upsetting dispatching order");
      }
      waitingForFlush = false;

      int sid = outCnt;
      outCnt = (outCnt + 1) & 0x0f;
      myLogger.log(Logger.FINE, "Issuing outgoing command " + sid);
      try {
        JICPPacket pkt =
            new JICPPacket(JICPProtocol.COMMAND_TYPE, JICPProtocol.DEFAULT_INFO, payload);
        pkt.setSessionID((byte) sid);
        pkt = deliver(pkt);
        myLogger.log(Logger.FINE, "Response received " + pkt.getSessionID());
        if (pkt.getType() == JICPProtocol.ERROR_TYPE) {
          // Communication OK, but there was a JICP error on the peer
          throw new ICPException(new String(pkt.getData()));
        }
        return pkt.getData();
      } catch (IOException ioe) {
        // Can't reach the BackEnd. Assume we are unreachable
        myLogger.log(Logger.WARNING, "IOException on output connection", ioe);
        myDisconnectionManager.setUnreachable(false);
        throw new ICPException("Dispatching error.", ioe);
      }
    } else {
      throw new ICPException("Unreachable");
    }
  }