コード例 #1
0
  protected boolean handleWriting(
      GatheringByteChannel gatheringByteChannel, ChannelContext channelContext) {

    Datagram datagram = channelContext.getWritingDatagram();

    try {
      if (datagram.writeTo(gatheringByteChannel)) {
        channelContext.setWritingDatagram(null);

        EnumSet<CompletionType> completionTypes = datagram.completionTypes;

        if (completionTypes != null) {
          if (completionTypes.contains(CompletionType.SUBMITTED)) {
            CompletionHandler<Object> completeHandler = datagram.completionHandler;

            completeHandler.submitted(datagram.attachment);
          }
        }

        return true;
      } else {
        return false;
      }
    } catch (IOException ioe) {
      RegistrationReference registrationReference = channelContext.getRegistrationReference();

      registrationReference.cancelRegistration();

      CompletionHandler<Object> completionHandler = datagram.completionHandler;

      if (completionHandler != null) {
        completionHandler.failed(datagram.attachment, ioe);
      }

      if (_log.isDebugEnabled()) {
        _log.debug("Broken write channel, unregister " + registrationReference, ioe);
      } else if (_log.isInfoEnabled()) {
        _log.info("Broken write channel, unregister " + registrationReference);
      }

      return false;
    }
  }
コード例 #2
0
  protected void handleReading(
      ScatteringByteChannel scatteringByteChannel, ChannelContext channelContext) {

    Datagram datagram = channelContext.getReadingDatagram();

    if (datagram == null) {
      datagram = Datagram.createReceiveDatagram();

      channelContext.setReadingDatagram(datagram);
    }

    try {
      if (datagram.readFrom(scatteringByteChannel)) {
        channelContext.setReadingDatagram(Datagram.createReceiveDatagram());

        if (datagram.isAckResponse()) {
          Datagram requestDatagram = removeResponseWaitingDatagram(datagram);

          if (requestDatagram == null) {
            if (_log.isWarnEnabled()) {
              _log.warn("Dropped ownerless ACK response " + datagram);
            }
          } else {
            CompletionHandler<Object> completionHandler = requestDatagram.completionHandler;

            completionHandler.delivered(requestDatagram.attachment);
          }
        } else if (datagram.isResponse()) {
          Datagram requestDatagram = removeResponseWaitingDatagram(datagram);

          if (requestDatagram == null) {
            if (_log.isWarnEnabled()) {
              _log.warn("Dropped ownerless response " + datagram);
            }
          } else {
            EnumSet<CompletionType> completionTypes = requestDatagram.completionTypes;

            if (completionTypes.contains(CompletionType.REPLIED)) {
              CompletionHandler<Object> completionHandler = requestDatagram.completionHandler;

              completionHandler.replied(requestDatagram.attachment, datagram);
            } else if (_log.isWarnEnabled()) {
              _log.warn("Dropped unconcerned response " + datagram);
            }
          }
        } else {
          if (datagram.isAckRequest()) {
            Datagram ackResponseDatagram =
                Datagram.createACKResponseDatagram(datagram.getSequenceId());

            doSendDatagram(channelContext.getRegistrationReference(), ackResponseDatagram);
          }

          int index = datagram.getType() & 0xFF;

          DatagramReceiveHandler datagramReceiveHandler =
              datagramReceiveHandlersReference.get()[index];

          if (datagramReceiveHandler == null) {
            if (_log.isWarnEnabled()) {
              _log.warn("Dropped ownerless request " + datagram);
            }
          } else {
            try {
              datagramReceiveHandler.receive(channelContext.getRegistrationReference(), datagram);
            } catch (Throwable t) {
              _log.error("Unable to dispatch", t);
            }
          }
        }
      }
    } catch (IOException ioe) {
      RegistrationReference registrationReference = channelContext.getRegistrationReference();

      registrationReference.cancelRegistration();

      if (_log.isDebugEnabled()) {
        _log.debug("Broken read channel, unregister " + registrationReference, ioe);
      } else if (_log.isInfoEnabled()) {
        _log.info("Broken read channel, unregister " + registrationReference);
      }
    }
  }