private ChannelFuture sendFile(ChannelHandlerContext ctx, Channel ch, FileChunk file)
      throws IOException {
    RandomAccessFile raf;
    try {
      raf = new RandomAccessFile(file.getFile(), "r");
    } catch (FileNotFoundException fnfe) {
      return null;
    }

    ChannelFuture writeFuture;
    if (ch.getPipeline().get(SslHandler.class) != null) {
      // Cannot use zero-copy with HTTPS.
      writeFuture = ch.write(new ChunkedFile(raf, file.startOffset(), file.length(), 8192));
    } else {
      // No encryption - use zero-copy.
      final FileRegion region =
          new DefaultFileRegion(raf.getChannel(), file.startOffset(), file.length());
      writeFuture = ch.write(region);
      writeFuture.addListener(
          new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
              region.releaseExternalResources();
            }
          });
    }

    return writeFuture;
  }
  public void run() {
    // Configure the client.
    ClientBootstrap bootstrap =
        new ClientBootstrap(
            new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

    // Set up the event pipeline factory.
    bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count));

    // Make a new connection.
    ChannelFuture connectFuture = bootstrap.connect(new InetSocketAddress(host, port));

    // Wait until the connection is made successfully.
    Channel channel = connectFuture.awaitUninterruptibly().getChannel();

    // Get the handler instance to retrieve the answer.
    FactorialClientHandler handler = (FactorialClientHandler) channel.getPipeline().getLast();

    // Print out the answer.
    System.err.format("Factorial of %,d is: %,d", count, handler.getFactorial());

    // Shut down all thread pools to exit.
    bootstrap.releaseExternalResources();
  }
 /**
  * {@inheritDoc}
  *
  * @see
  *     org.helios.apmrouter.jmx.mbeanserver.MBeanServerConnectionAdmin#closeMBeanServerConnection()
  */
 @Override
 public void closeMBeanServerConnection() {
   channel.getPipeline().remove(getClass().getSimpleName());
   if (listener != null) {
     listener.onClose();
   }
 }
  @SuppressWarnings("unchecked")
  @Override
  public SMTPClientFuture<FutureResult<me.normanmaurer.niosmtp.transport.FutureResult.Void>>
      startTLS() {
    if (!isEncrypted()) {
      final SMTPClientFutureImpl<FutureResult<me.normanmaurer.niosmtp.transport.FutureResult.Void>>
          future =
              new SMTPClientFutureImpl<
                  FutureResult<me.normanmaurer.niosmtp.transport.FutureResult.Void>>(false);

      SslHandler sslHandler = new SslHandler(engine, false);
      channel.getPipeline().addFirst(SSL_HANDLER_KEY, sslHandler);
      sslHandler
          .handshake()
          .addListener(
              new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture cfuture) throws Exception {
                  if (cfuture.isSuccess()) {
                    future.setResult(FutureResult.createVoid());
                  } else {
                    future.setResult(FutureResult.create(cfuture.getCause()));
                  }
                }
              });

      return future;
    } else {
      return new ReadySMTPClientFuture<
          FutureResult<me.normanmaurer.niosmtp.transport.FutureResult.Void>>(
          this, FutureResult.create(STARTTLS_EXCEPTION));
    }
  }
Example #5
0
  public void sendRequest(MetaInfo meta) {
    System.out.println(Thread.currentThread().getId() + " start sendRequest");
    URI uri = null;
    try {
      System.out.println(meta.getParams());
      uri = new URI(meta.getUrl());
    } catch (URISyntaxException e) {
      e.printStackTrace(); // To change body of catch statement use File | Settings | File
      // Templates.
    }
    String host = uri.getHost();
    int port = 80;

    HttpRequest request =
        new DefaultHttpRequest(
            HttpVersion.HTTP_1_1, HttpMethod.valueOf(meta.getMethod()), uri.toASCIIString());
    meta.buildHttpRequestHeader(request);

    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    Channel channel = future.getChannel();
    channel.getPipeline().addLast("handler", new DownloaderHandler());
    GlobalVar.metaInfoVar.set(channel, meta);

    future.addListener(new ConnectOk(request));
    channel.getCloseFuture().awaitUninterruptibly().addListener(new ConnectClose());
    System.out.println(Thread.currentThread().getId() + " end sendRequest");
  }
 /** Close the connection. */
 public synchronized void close() {
   if (!closed && channel != null) {
     ConnectionWatchdog watchdog = channel.getPipeline().get(ConnectionWatchdog.class);
     watchdog.setReconnect(false);
     closed = true;
     channel.close();
   }
 }
Example #7
0
 protected void setupNewChannel(RpcChannel rpcChannel) {
   Channel channel = rpcChannel.getChannel();
   RpcMessageHandler handler = (RpcMessageHandler) channel.getPipeline().get("handler");
   handler.setChannel(rpcChannel);
   rpcChannel.setServiceMap(Collections.unmodifiableMap(services));
   if (newChannelCallback != null) {
     newChannelCallback.run(rpcChannel);
   }
 }
Example #8
0
  private <T> Response<T> sendRequest(
      RequestType type,
      SlaveContext slaveContext,
      Serializer serializer,
      Deserializer<T> deserializer) {
    // TODO Refactor, break into smaller methods
    Triplet<Channel, ChannelBuffer, ByteBuffer> channelContext = null;
    try {
      // Send 'em over the wire
      channelContext = getChannel();
      Channel channel = channelContext.first();
      ChannelBuffer buffer = channelContext.second();
      buffer.clear();
      buffer = new ChunkingChannelBuffer(buffer, channel, MAX_FRAME_LENGTH);
      buffer.writeByte(type.ordinal());
      if (type.includesSlaveContext()) {
        writeSlaveContext(buffer, slaveContext);
      }
      serializer.write(buffer, channelContext.third());
      if (buffer.writerIndex() > 0) {
        channel.write(buffer);
      }

      // Read the response
      @SuppressWarnings("unchecked")
      BlockingReadHandler<ChannelBuffer> reader =
          (BlockingReadHandler<ChannelBuffer>) channel.getPipeline().get("blockingHandler");
      final Triplet<Channel, ChannelBuffer, ByteBuffer> finalChannelContext = channelContext;
      DechunkingChannelBuffer dechunkingBuffer =
          new DechunkingChannelBuffer(reader) {
            @Override
            protected ChannelBuffer readNext() {
              ChannelBuffer result = super.readNext();
              if (result == null) {
                channelPool.dispose(finalChannelContext);
                throw new HaCommunicationException("Channel has been closed");
              }
              return result;
            }
          };
      T response = deserializer.read(dechunkingBuffer);
      TransactionStream txStreams =
          type.includesSlaveContext()
              ? readTransactionStreams(dechunkingBuffer)
              : TransactionStream.EMPTY;
      return new Response<T>(response, txStreams);
    } catch (ClosedChannelException e) {
      channelPool.dispose(channelContext);
      throw new HaCommunicationException(e);
    } catch (IOException e) {
      throw new HaCommunicationException(e);
    } catch (InterruptedException e) {
      throw new HaCommunicationException(e);
    } catch (Exception e) {
      throw new HaCommunicationException(e);
    }
  }
Example #9
0
 /**
  * Shutdown this client and close all open connections. The client should be discarded after
  * calling shutdown.
  */
 public void shutdown() {
   for (Channel c : channels) {
     ChannelPipeline pipeline = c.getPipeline();
     RedisAsyncConnection<?, ?> connection = pipeline.get(RedisAsyncConnection.class);
     connection.close();
   }
   ChannelGroupFuture future = channels.close();
   future.awaitUninterruptibly();
   bootstrap.releaseExternalResources();
 }
 private void addHandler(SimpleChannelUpstreamHandler handler) {
   ChannelPipeline cp = channel.getPipeline();
   int count = futureCount.incrementAndGet();
   String oldHandler = "futureHandler" + (count - 1);
   if (count == 1 || cp.get(oldHandler) == null) {
     cp.addBefore("callback", "futureHandler" + count, handler);
   } else {
     cp.addBefore(oldHandler, "futureHandler" + count, handler);
   }
 }
 private void close(Channel channel) {
   try {
     channel
         .getPipeline()
         .getContext(NettyAsyncHttpProvider.class)
         .setAttachment(new NettyAsyncHttpProvider.DiscardEvent());
     channel.close();
   } catch (Throwable t) {
     // noop
   }
 }
 private ChannelFuture sendFile(ChannelHandlerContext ctx, Channel ch, FileChunk file)
     throws IOException {
   RandomAccessFile spill;
   try {
     spill = new RandomAccessFile(file.getFile(), "r");
   } catch (FileNotFoundException e) {
     LOG.info(file.getFile() + " not found");
     return null;
   }
   ChannelFuture writeFuture;
   if (ch.getPipeline().get(SslHandler.class) == null) {
     final FadvisedFileRegion partition =
         new FadvisedFileRegion(
             spill,
             file.startOffset,
             file.length(),
             manageOsCache,
             readaheadLength,
             readaheadPool,
             file.getFile().getAbsolutePath());
     writeFuture = ch.write(partition);
     writeFuture.addListener(
         new ChannelFutureListener() {
           // TODO error handling; distinguish IO/connection failures,
           //      attribute to appropriate spill output
           @Override
           public void operationComplete(ChannelFuture future) {
             partition.releaseExternalResources();
           }
         });
   } else {
     // HTTPS cannot be done with zero copy.
     final FadvisedChunkedFile chunk =
         new FadvisedChunkedFile(
             spill,
             file.startOffset,
             file.length,
             sslFileBufferSize,
             manageOsCache,
             readaheadLength,
             readaheadPool,
             file.getFile().getAbsolutePath());
     writeFuture = ch.write(chunk);
   }
   metrics.shuffleConnections.incr();
   metrics.shuffleOutputBytes.incr(file.length); // optimistic
   return writeFuture;
 }
  protected void setResponseProcessor(
      HttpResponseProcessor responseProcessor, SendRequestResultListener l)
      throws DatabusException {
    _handler.setResponseProcessor(responseProcessor);
    _handler.setRequestListener(l);

    /*
    if (null != _channel)
    {
      ChannelPipeline channelPipeline = _channel.getPipeline();
      channelPipeline.replace("handler", "handler", _handler);
    }
    */
    assert (_channel != null);
    assert (_channel.getPipeline().get("handler") != null);
  }
  public NettySMTPClientSession(
      Channel channel,
      Logger logger,
      SMTPClientConfig config,
      SMTPDeliveryMode mode,
      SSLEngine engine) {
    super(
        logger,
        config,
        mode,
        (InetSocketAddress) channel.getLocalAddress(),
        (InetSocketAddress) channel.getRemoteAddress());
    this.channel = channel;
    channel
        .getPipeline()
        .addBefore(IDLE_HANDLER_KEY, "callback", new CloseHandler(closeFuture, logger));

    this.engine = engine;
  }
 /**
  * Creates a new AgentMBeanServerConnectionFactory
  *
  * @param builder The AgentMBeanServerConnectionFactory builder
  */
 protected AgentMBeanServerConnectionFactory(Builder builder) {
   this.channel = builder.channel;
   this.remoteAddress =
       builder.remoteAddress == null ? this.channel.getRemoteAddress() : builder.remoteAddress;
   this.timeout = builder.timeout;
   this.listener = builder.listener;
   this.domain = builder.domain;
   if ("DefaultDomain".equals(domain)) {
     domainInfoData = new byte[] {0};
   } else {
     byte[] domainBytes = domain.getBytes();
     domainInfoData = new byte[domainBytes.length + 1];
     domainInfoData[0] = (byte) domainBytes.length;
     System.arraycopy(domainBytes, 0, domainInfoData, 1, domainBytes.length);
   }
   if (channel.getPipeline().get(getClass().getSimpleName()) == null) {
     this.channel.getPipeline().addFirst(getClass().getSimpleName(), responseHandler);
     //			LoggingHandler logg = new LoggingHandler(InternalLogLevel.ERROR, true);
     //			this.channel.getPipeline().addFirst("logger", logg);
   }
 }
    public void run(Timeout timeout) {
      if (isClosed.get()) return;
      Object attachment =
          channel.getPipeline().getContext(NettyAsyncHttpProvider.class).getAttachment();
      if (attachment != null) {
        if (NettyResponseFuture.class.isAssignableFrom(attachment.getClass())) {
          NettyResponseFuture<?> future = (NettyResponseFuture<?>) attachment;

          if (!future.isDone() && !future.isCancelled()) {
            log.warn("Future not in appropriate state {}", future);
            return;
          }
        }
      }

      if (activeChannels.remove(channel)) {
        log.debug("Channel idle. Expiring {}", channel);
        close(channel);
      }
      timeout.cancel();
    }
  /** {@inheritDoc} */
  public boolean offer(String uri, Channel channel) {
    if (!provider.getConfig().isSslConnectionPoolEnabled() && uri.startsWith("https")) {
      return false;
    }

    log.debug("Adding uri: {} for channel {}", uri, channel);
    channel
        .getPipeline()
        .getContext(NettyAsyncHttpProvider.class)
        .setAttachment(new NettyAsyncHttpProvider.DiscardEvent());

    ConcurrentLinkedQueue<Channel> pooledConnectionForHost = connectionsPool.get(uri);
    if (pooledConnectionForHost == null) {
      ConcurrentLinkedQueue<Channel> newPool = new ConcurrentLinkedQueue<Channel>();
      connectionsPool.putIfAbsent(uri, newPool);
      pooledConnectionForHost = connectionsPool.get(uri);
    }

    boolean added;
    int size = pooledConnectionForHost.size();
    if (config.getMaxConnectionPerHost() == -1 || size < config.getMaxConnectionPerHost()) {
      added = pooledConnectionForHost.add(channel);
      if (added) {
        Timeout t =
            timer.newTimeout(
                new IdleRunner(channel, pooledConnectionForHost),
                config.getIdleConnectionInPoolTimeoutInMs(),
                TimeUnit.MILLISECONDS);
        trackedIdleConnections.put(channel, t);
        log.debug("ConnectionsPool increment totalConnections {}", trackedIdleConnections.size());
      }
    } else {
      log.debug("Maximum connections per hosts reached {}", config.getMaxConnectionPerHost());
      added = false;
    }
    return added;
  }
  @Test
  public void testSslEcho() throws Throwable {
    ServerBootstrap sb =
        new ServerBootstrap(newServerSocketChannelFactory(Executors.newCachedThreadPool()));
    ClientBootstrap cb =
        new ClientBootstrap(newClientSocketChannelFactory(Executors.newCachedThreadPool()));

    EchoHandler sh = new EchoHandler(true);
    EchoHandler ch = new EchoHandler(false);

    SSLEngine sse = SecureChatSslContextFactory.getServerContext().createSSLEngine();
    SSLEngine cse = SecureChatSslContextFactory.getClientContext().createSSLEngine();
    sse.setUseClientMode(false);
    cse.setUseClientMode(true);

    // Workaround for blocking I/O transport write-write dead lock.
    sb.setOption("receiveBufferSize", 1048576);
    sb.setOption("receiveBufferSize", 1048576);

    sb.getPipeline().addFirst("ssl", new SslHandler(sse));
    sb.getPipeline().addLast("handler", sh);
    cb.getPipeline().addFirst("ssl", new SslHandler(cse));
    cb.getPipeline().addLast("handler", ch);
    ExecutorService eventExecutor = null;
    if (isExecutorRequired()) {
      eventExecutor = new OrderedMemoryAwareThreadPoolExecutor(16, 0, 0);
      sb.getPipeline().addFirst("executor", new ExecutionHandler(eventExecutor));
      cb.getPipeline().addFirst("executor", new ExecutionHandler(eventExecutor));
    }

    Channel sc = sb.bind(new InetSocketAddress(0));
    int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();

    ChannelFuture ccf = cb.connect(new InetSocketAddress(TestUtil.getLocalHost(), port));
    ccf.awaitUninterruptibly();
    if (!ccf.isSuccess()) {
      logger.error("Connection attempt failed", ccf.getCause());
      sc.close().awaitUninterruptibly();
    }
    assertTrue(ccf.isSuccess());

    Channel cc = ccf.getChannel();
    ChannelFuture hf = cc.getPipeline().get(SslHandler.class).handshake();
    hf.awaitUninterruptibly();
    if (!hf.isSuccess()) {
      logger.error("Handshake failed", hf.getCause());
      sh.channel.close().awaitUninterruptibly();
      ch.channel.close().awaitUninterruptibly();
      sc.close().awaitUninterruptibly();
    }

    assertTrue(hf.isSuccess());

    for (int i = 0; i < data.length; ) {
      int length = Math.min(random.nextInt(1024 * 64), data.length - i);
      cc.write(ChannelBuffers.wrappedBuffer(data, i, length));
      i += length;
    }

    while (ch.counter < data.length) {
      if (sh.exception.get() != null) {
        break;
      }
      if (ch.exception.get() != null) {
        break;
      }

      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        // Ignore.
      }
    }

    while (sh.counter < data.length) {
      if (sh.exception.get() != null) {
        break;
      }
      if (ch.exception.get() != null) {
        break;
      }

      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        // Ignore.
      }
    }

    sh.channel.close().awaitUninterruptibly();
    ch.channel.close().awaitUninterruptibly();
    sc.close().awaitUninterruptibly();
    cb.shutdown();
    sb.shutdown();
    cb.releaseExternalResources();
    sb.releaseExternalResources();

    if (eventExecutor != null) {
      eventExecutor.shutdown();
    }
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
      throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
      throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
      throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
      throw ch.exception.get();
    }
  }
 @Override
 public boolean isEncrypted() {
   return channel.getPipeline().get(SslHandler.class) != null;
 }
  /**
   * {@inheritDoc}
   *
   * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method,
   *     java.lang.Object[])
   */
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (MBeanServerConnection.class != method.getDeclaringClass()) {
      return method.invoke(Modifier.isStatic(method.getModifiers()) ? null : this, args);
    }
    if (channel.getPipeline().get(getClass().getSimpleName()) == null) {
      throw new IOException("This MBeanServerConnection has been closed", new Throwable());
    }
    // SimpleLogger.debug("MBeanServerConnection [", method.getName(), "] Payload Size [",
    // sargs.length+6+4, "]");
    final int reqId = requestId.incrementAndGet();
    if ("addNotificationListener".equals(method.getName())
        && !method.getParameterTypes()[1].equals(ObjectName.class)) {
      NotificationListener listener = (NotificationListener) args[1];
      args[1] = reqId;
      addRegisteredListener(reqId, listener);
    } else if ("removeNotificationListener".equals(method.getName())
        && !method.getParameterTypes()[1].equals(ObjectName.class)) {
      removeRegisteredListener((NotificationListener) args[1]);
      args = new Object[0];
    }
    byte[] sargs = getOutput(args);
    ChannelBuffer cb =
        ChannelBuffers.directBuffer(1 + domainInfoData.length + 4 + 1 + 4 + sargs.length);
    cb.writeByte(OpCode.JMX_REQUEST.op()); // 1
    cb.writeBytes(domainInfoData); // domain data
    cb.writeInt(reqId); // 4
    cb.writeByte(methodToKey.get(method)); // 1
    cb.writeInt(sargs.length); // 4
    cb.writeBytes(sargs); // sargs.length

    if (listener == null) {
      synchTimeoutMap.addListener(
          new TimeoutListener<Integer, CountDownLatch>() {
            @Override
            public void onTimeout(Integer key, CountDownLatch value) {
              if (reqId == key) {
                synchTimeoutMap.remove(key);
                synchTimeoutMap.removeListener(this);
                onSynchronousResponse(
                    reqId,
                    new IOException(
                        "Operation timed out after [" + timeout + "] ms.", new Throwable()));
              }
            }
          });
    } else {
      asynchTimeoutMap.put(reqId, listener, timeout);
      asynchTimeoutMap.addListener(
          new TimeoutListener<Integer, AsynchJMXResponseListener>() {
            @Override
            public void onTimeout(Integer key, AsynchJMXResponseListener value) {
              if (reqId == key) {
                asynchTimeoutMap.remove(key);
                listener.onTimeout(reqId, timeout);
                asynchTimeoutMap.removeListener(this);
              }
            }
          });
    }

    channel
        .write(cb, remoteAddress)
        .addListener(
            new ChannelFutureListener() {
              @Override
              public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                  SimpleLogger.debug("Sent JMX Request to [", remoteAddress, "]");
                } else {
                  SimpleLogger.error(
                      "Failed to send JMX Request to [", remoteAddress, "]", future.getCause());
                }
              }
            });
    if (listener == null) {
      waitForSynchronousResponse(reqId, timeout);
      Object result = synchResultMap.get(reqId);
      if (result != null && result instanceof Throwable) {
        throw (Throwable) result;
      }
      return result;
    }
    return null;
  }
 public ChannelPipeline getPipeline() {
   return channel.getPipeline();
 }
Example #22
0
 protected boolean isSSL() {
   return channel.getPipeline().get(SslHandler.class) != null;
 }
Example #23
0
 public ServerImpl(NetworkSystemImpl system, Channel channel) {
   this.channel = channel;
   metricsSource = (NetMetricSource) channel.getPipeline().get(MetricRecordingHandler.NAME);
   this.networkSystem = system;
   this.time = (EngineTime) CoreRegistry.get(Time.class);
 }