Exemplo n.º 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;
    }
  }
Exemplo n.º 2
0
  public void run() {
    while (running()) {
      try {
        selector.select();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }

      Set<SelectionKey> acceptKeys = selector.selectedKeys();
      Iterator<SelectionKey> iterator = acceptKeys.iterator();

      while (iterator.hasNext()) {
        SelectionKey key = iterator.next();
        iterator.remove();

        try {
          if (key.isValid() && key.isReadable()) {
            ChannelContext ctx = (ChannelContext) key.attachment();
            ctx.read();
          }
          if (key.isValid() && key.isWritable()) {
            ChannelContext ctx = (ChannelContext) key.attachment();
            ctx.flush();
          }
        } catch (Exception e) {
          log.severe("Terminating connection to - " + key.channel());
          try {
            key.channel().close();
          } catch (IOException e1) {
            e1.printStackTrace();
          }
          key.cancel();
          // throw new RuntimeException(e);
        }
        if (!running()) {
          break;
        }
      }
      configureChannel();
    }
  }
Exemplo n.º 3
0
  @Override
  public void dispatch(String o, final Callback callback) {
    ContainerRoot model = modelService.getCurrentModel().getModel();
    Channel thisChan = (Channel) model.findByPath(context.getPath());
    Set<String> outputPaths = Helper.getRequiredPortsPath(thisChan, context.getNodeName());

    // create a list of destination paths
    Set<String> destPaths = new HashSet<>();
    // process remote paths in order to add _<chanName> to the paths
    // (WsMsgBroker protocol)
    // add processed remote path to dest
    destPaths.addAll(
        channelContext
            .getRemotePortPaths()
            .stream()
            .map(remotePath -> remotePath + "_" + context.getInstanceName())
            .collect(Collectors.toList()));
    // add local connected inputs to dest
    Set<String> providedPaths =
        Helper.getProvidedPortsPath(thisChan, context.getNodeName())
            .stream()
            .map(s -> s + "_" + context.getInstanceName())
            .collect(Collectors.toSet());

    destPaths.addAll(providedPaths);
    // create the array that will store the dest
    String[] dest = new String[destPaths.size()];
    // convert list to array
    destPaths.toArray(dest);

    for (final String outputPath : outputPaths) {
      WSMsgBrokerClient client = this.clients.get(outputPath + "_" + context.getInstanceName());
      if (client != null) {
        if (callback != null) {
          client.send(
              o,
              dest,
              (from, o1) -> {
                CallbackResult result = new CallbackResult();
                result.setPayload(o1.toString());
                result.setOriginChannelPath(context.getPath());
                result.setOriginPortPath(outputPath);
                callback.onSuccess(result);
              });
        } else {
          client.send(o, dest);
        }
      } else {
        createInputClient(outputPath + "_" + context.getInstanceName());
      }
    }
  }
Exemplo n.º 4
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);
      }
    }
  }