Java 类org.jboss.netty.channel.ChannelFuture 实例源码

项目:HeliosStreams    文件:Netty3JSONResponse.java   
/**
 * Sends this response to all the passed channels as a {@link TextWebSocketFrame}
 * @param listener A channel future listener to attach to each channel future. Ignored if null.
 * @param channels The channels to send this response to
 * @return An array of the futures for the write of this response to each channel written to
 */
public ChannelFuture[] send(ChannelFutureListener listener, Channel...channels) {
    if(channels!=null && channels.length>0) {
        Set<ChannelFuture> futures = new HashSet<ChannelFuture>(channels.length);
        if(opCode==null) {
            opCode = "ok";
        }
        TextWebSocketFrame frame = new TextWebSocketFrame(this.toChannelBuffer());
        for(Channel channel: channels) {
            if(channel!=null && channel.isWritable()) {
                ChannelFuture cf = Channels.future(channel);
                if(listener!=null) cf.addListener(listener);
                channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, cf, frame, channel.getRemoteAddress()));
                futures.add(cf);
            }
        }
        return futures.toArray(new ChannelFuture[futures.size()]);
    }       
    return EMPTY_CHANNEL_FUTURE_ARR;
}
项目:EatDubbo    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:dubbo2    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:https-github.com-apache-zookeeper    文件:NettyServerCnxnFactory.java   
@Override
public void channelConnected(ChannelHandlerContext ctx,
        ChannelStateEvent e) throws Exception
{
    if (LOG.isTraceEnabled()) {
        LOG.trace("Channel connected " + e);
    }

    NettyServerCnxn cnxn = new NettyServerCnxn(ctx.getChannel(),
            zkServer, NettyServerCnxnFactory.this);
    ctx.setAttachment(cnxn);

    if (secure) {
        SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);
        ChannelFuture handshakeFuture = sslHandler.handshake();
        handshakeFuture.addListener(new CertificateVerifier(sslHandler, cnxn));
    } else {
        allChannels.add(ctx.getChannel());
        addCnxn(cnxn);
    }
}
项目:dubbox-hystrix    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:hadoop    文件:TestShuffleHandler.java   
/**
 * Validate shuffle connection and input/output metrics.
 *
 * @throws Exception exception
 */
@Test (timeout = 10000)
public void testShuffleMetrics() throws Exception {
  MetricsSystem ms = new MetricsSystemImpl();
  ShuffleHandler sh = new ShuffleHandler(ms);
  ChannelFuture cf = make(stub(ChannelFuture.class).
      returning(true, false).from.isSuccess());

  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(1*MiB);
  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(2*MiB);

  checkShuffleMetrics(ms, 3*MiB, 0 , 0, 2);

  sh.metrics.operationComplete(cf);
  sh.metrics.operationComplete(cf);

  checkShuffleMetrics(ms, 3*MiB, 1, 1, 0);
}
项目:hadoop    文件:SimpleTcpClient.java   
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

  bootstrap.setOption("tcpNoDelay", true);
  bootstrap.setOption("keepAlive", true);

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
项目:iTAP-controller    文件:RPCService.java   
@Override
public void operationComplete(ChannelFuture cf) throws Exception {
    if (!cf.isSuccess()) {
        synchronized (connections) {
            NodeConnection c = connections.remove(node.getNodeId());
            if (c != null) c.nuke();
            cf.getChannel().close();
        }

        String message = "[unknown error]";
        if (cf.isCancelled()) message = "Timed out on connect";
        if (cf.getCause() != null) message = cf.getCause().getMessage();
        logger.debug("[{}->{}] Could not connect to RPC " +
                     "node: {}", 
                     new Object[]{syncManager.getLocalNodeId(), 
                                  node.getNodeId(), 
                                  message});
    } else {
        logger.trace("[{}->{}] Channel future successful", 
                     syncManager.getLocalNodeId(), 
                     node.getNodeId());
    }
}
项目:iTAP-controller    文件:Bootstrap.java   
public boolean bootstrap(HostAndPort seed, 
                         Node localNode) throws SyncException {
    this.localNode = localNode;
    succeeded = false;
    SocketAddress sa =
            new InetSocketAddress(seed.getHostText(), seed.getPort());
    ChannelFuture future = bootstrap.connect(sa);
    future.awaitUninterruptibly();
    if (!future.isSuccess()) {
        logger.debug("Could not connect to " + seed, future.getCause());
        return false;
    }
    Channel channel = future.getChannel();
    logger.debug("[{}] Connected to {}", 
                 localNode != null ? localNode.getNodeId() : null,
                 seed);

    try {
        channel.getCloseFuture().await();
    } catch (InterruptedException e) {
        logger.debug("Interrupted while waiting for bootstrap");
        return succeeded;
    }
    return succeeded;
}
项目:iTAP-controller    文件:RemoteSyncManager.java   
protected boolean connect(String hostname, int port) {
    ready = false;
    if (channel == null || !channel.isConnected()) {
        SocketAddress sa =
                new InetSocketAddress(hostname, port);
        ChannelFuture future = clientBootstrap.connect(sa);
        future.awaitUninterruptibly();
        if (!future.isSuccess()) {
            logger.error("Could not connect to " + hostname + 
                         ":" + port, future.getCause());
            return false;
        }
        channel = future.getChannel();
    }
    while (!ready && channel != null && channel.isConnected()) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) { }
    }
    if (!ready || channel == null || !channel.isConnected()) {
        logger.warn("Timed out connecting to {}:{}", hostname, port);
        return false;
    }
    logger.debug("Connected to {}:{}", hostname, port);
    return true;
}
项目:dubbocloud    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:aliyun-oss-hadoop-fs    文件:TestShuffleHandler.java   
/**
 * Validate shuffle connection and input/output metrics.
 *
 * @throws Exception exception
 */
@Test (timeout = 10000)
public void testShuffleMetrics() throws Exception {
  MetricsSystem ms = new MetricsSystemImpl();
  ShuffleHandler sh = new ShuffleHandler(ms);
  ChannelFuture cf = make(stub(ChannelFuture.class).
      returning(true, false).from.isSuccess());

  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(1*MiB);
  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(2*MiB);

  checkShuffleMetrics(ms, 3*MiB, 0 , 0, 2);

  sh.metrics.operationComplete(cf);
  sh.metrics.operationComplete(cf);

  checkShuffleMetrics(ms, 3*MiB, 1, 1, 0);
}
项目:aliyun-oss-hadoop-fs    文件:TestShuffleHandler.java   
public ChannelFuture createMockChannelFuture(Channel mockCh,
    final List<ShuffleHandler.ReduceMapFileCount> listenerList) {
  final ChannelFuture mockFuture = Mockito.mock(ChannelFuture.class);
  Mockito.when(mockFuture.getChannel()).thenReturn(mockCh);
  Mockito.doReturn(true).when(mockFuture).isSuccess();
  Mockito.doAnswer(new Answer() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      //Add ReduceMapFileCount listener to a list
      if (invocation.getArguments()[0].getClass() ==
          ShuffleHandler.ReduceMapFileCount.class)
        listenerList.add((ShuffleHandler.ReduceMapFileCount)
            invocation.getArguments()[0]);
      return null;
    }
  }).when(mockFuture).addListener(Mockito.any(
      ShuffleHandler.ReduceMapFileCount.class));
  return mockFuture;
}
项目:aliyun-oss-hadoop-fs    文件:SimpleTcpClient.java   
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

  bootstrap.setOption("tcpNoDelay", true);
  bootstrap.setOption("keepAlive", true);

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
项目:dubbos    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:bigstreams    文件:ServerUtil.java   
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
        throws Exception {
    super.messageReceived(ctx, e);

    System.out.println("-------- Server  Channel messageRecieved "
            + System.currentTimeMillis());

    if (induceError.get()) {
        System.out
                .println("Inducing Error in Server messageReceived method");
        throw new IOException("Induced error ");
    }

    MessageEventBag bag = new MessageEventBag();
    bag.setBytes(e);
    bagList.add(bag);

    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    buffer.writeInt(200);

    ChannelFuture future = e.getChannel().write(buffer);

    future.addListener(ChannelFutureListener.CLOSE);

}
项目:bigstreams    文件:ServerUtil.java   
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
        throws Exception {
    System.out.println("Server Exception Caught");
    e.getCause().printStackTrace();

    /**
     * Very important to respond here.
     * The agent will always be listening for some kind of feedback.
     */
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    buffer.writeInt(500);

    ChannelFuture future = e.getChannel().write(buffer);

    future.addListener(ChannelFutureListener.CLOSE);

}
项目:QoS-floodlight    文件:RPCService.java   
@Override
public void operationComplete(ChannelFuture cf) throws Exception {
    if (!cf.isSuccess()) {
        synchronized (connections) {
            NodeConnection c = connections.remove(node.getNodeId());
            if (c != null) c.nuke();
            cf.getChannel().close();
        }

        String message = "[unknown error]";
        if (cf.isCancelled()) message = "Timed out on connect";
        if (cf.getCause() != null) message = cf.getCause().getMessage();
        logger.debug("[{}->{}] Could not connect to RPC " +
                     "node: {}", 
                     new Object[]{syncManager.getLocalNodeId(), 
                                  node.getNodeId(), 
                                  message});
    } else {
        logger.trace("[{}->{}] Channel future successful", 
                     syncManager.getLocalNodeId(), 
                     node.getNodeId());
    }
}
项目:QoS-floodlight    文件:Bootstrap.java   
public boolean bootstrap(HostAndPort seed, 
                         Node localNode) throws SyncException {
    this.localNode = localNode;
    succeeded = false;
    SocketAddress sa =
            new InetSocketAddress(seed.getHostText(), seed.getPort());
    ChannelFuture future = bootstrap.connect(sa);
    future.awaitUninterruptibly();
    if (!future.isSuccess()) {
        logger.debug("Could not connect to " + seed, future.getCause());
        return false;
    }
    Channel channel = future.getChannel();
    logger.debug("[{}] Connected to {}", 
                 localNode != null ? localNode.getNodeId() : null,
                 seed);

    try {
        channel.getCloseFuture().await();
    } catch (InterruptedException e) {
        logger.debug("Interrupted while waiting for bootstrap");
        return succeeded;
    }
    return succeeded;
}
项目:QoS-floodlight    文件:RemoteSyncManager.java   
protected boolean connect(String hostname, int port) {
    ready = false;
    if (channel == null || !channel.isConnected()) {
        SocketAddress sa =
                new InetSocketAddress(hostname, port);
        ChannelFuture future = clientBootstrap.connect(sa);
        future.awaitUninterruptibly();
        if (!future.isSuccess()) {
            logger.error("Could not connect to " + hostname + 
                         ":" + port, future.getCause());
            return false;
        }
        channel = future.getChannel();
    }
    while (!ready && channel != null && channel.isConnected()) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) { }
    }
    if (!ready || channel == null || !channel.isConnected()) {
        logger.warn("Timed out connecting to {}:{}", hostname, port);
        return false;
    }
    logger.debug("Connected to {}:{}", hostname, port);
    return true;
}
项目:dubbo-comments    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        //FIXME  sent为true的话   要等待数据写完才返回,失败抛出异常   add by jileng
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            // 写超时了,难道不能取消这次写操作?
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:dubbox    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:dubbo    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:jahhan    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:big-c    文件:TestShuffleHandler.java   
/**
 * Validate shuffle connection and input/output metrics.
 *
 * @throws Exception exception
 */
@Test (timeout = 10000)
public void testShuffleMetrics() throws Exception {
  MetricsSystem ms = new MetricsSystemImpl();
  ShuffleHandler sh = new ShuffleHandler(ms);
  ChannelFuture cf = make(stub(ChannelFuture.class).
      returning(true, false).from.isSuccess());

  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(1*MiB);
  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(2*MiB);

  checkShuffleMetrics(ms, 3*MiB, 0 , 0, 2);

  sh.metrics.operationComplete(cf);
  sh.metrics.operationComplete(cf);

  checkShuffleMetrics(ms, 3*MiB, 1, 1, 0);
}
项目:big-c    文件:SimpleTcpClient.java   
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

  bootstrap.setOption("tcpNoDelay", true);
  bootstrap.setOption("keepAlive", true);

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
项目:dubbo3    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:fastcatsearch3    文件:TransportChannel.java   
public void sendResponse(Object obj) throws IOException {
    byte type = 0;
    type = TransportOption.setTypeMessage(type);
    byte status = 0;
    status = TransportOption.setResponse(status);
    status = TransportOption.setResponseObject(status);
    byte resType = 0;
    resType = TransportOption.setResponseObject(resType);
    CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry();
    BytesStreamOutput stream = cachedEntry.bytes();
    stream.skip(MessageProtocol.HEADER_SIZE);
    stream.writeGenericValue(obj);
    stream.close();

    ChannelBuffer buffer = stream.bytesReference().toChannelBuffer();
    MessageProtocol.writeHeader(buffer, type, requestId, status);
    ChannelFuture future = channel.write(buffer);
    future.addListener(new TransportModule.CacheFutureListener(cachedEntry));
}
项目:fastcatsearch3    文件:TransportChannel.java   
public void sendResponse(Streamable response) throws IOException {
    byte type = 0;
    type = TransportOption.setTypeMessage(type);
    byte status = 0;
    status = TransportOption.setResponse(status);
    CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry();
    BytesStreamOutput stream = cachedEntry.bytes();
    stream.skip(MessageProtocol.HEADER_SIZE);
    stream.writeString(response.getClass().getName());
    response.writeTo(stream);
    stream.close();

    ChannelBuffer buffer = stream.bytesReference().toChannelBuffer();
    MessageProtocol.writeHeader(buffer, type, requestId, status);
    ChannelFuture future = channel.write(buffer);
    future.addListener(new TransportModule.CacheFutureListener(cachedEntry));
}
项目:Camel    文件:NettyHelper.java   
/**
 * Writes the given body to Netty channel. Will <b>not</b >wait until the body has been written.
 *
 * @param log             logger to use
 * @param channel         the Netty channel
 * @param remoteAddress   the remote address when using UDP
 * @param body            the body to write (send)
 * @param exchange        the exchange
 * @param listener        listener with work to be executed when the operation is complete
 */
public static void writeBodyAsync(Logger log, Channel channel, SocketAddress remoteAddress, Object body,
                                  Exchange exchange, ChannelFutureListener listener) {
    ChannelFuture future;
    if (remoteAddress != null) {
        if (log.isDebugEnabled()) {
            log.debug("Channel: {} remote address: {} writing body: {}", new Object[]{channel, remoteAddress, body});
        }
        future = channel.write(body, remoteAddress);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Channel: {} writing body: {}", new Object[]{channel, body});
        }
        future = channel.write(body);
    }

    if (listener != null) {
        future.addListener(listener);
    }
}
项目:HeliosStreams    文件:Netty3JSONResponse.java   
/**
 * Sends this response to all the passed channels as a {@link TextWebSocketFrame}
 * @param listener A channel future listener to attach to each channel future. Ignored if null.
 * @param channels The channels to send this response to
 * @return An array of the futures for the write of this response to each channel written to
 */
public ChannelFuture[] send(ChannelFutureListener listener, Channel...channels) {
    if(channels!=null && channels.length>0) {
        Set<ChannelFuture> futures = new HashSet<ChannelFuture>(channels.length);
        if(opCode==null) {
            opCode = "ok";
        }
        TextWebSocketFrame frame = new TextWebSocketFrame(this.toChannelBuffer());
        for(Channel channel: channels) {
            if(channel!=null && channel.isWritable()) {
                ChannelFuture cf = Channels.future(channel);
                if(listener!=null) cf.addListener(listener);
                channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, cf, frame, channel.getRemoteAddress()));
                futures.add(cf);
            }
        }
        return futures.toArray(new ChannelFuture[futures.size()]);
    }       
    return EMPTY_CHANNEL_FUTURE_ARR;
}
项目:HeliosStreams    文件:InvocationChannel.java   
/**
 * {@inheritDoc}
 * @see org.jboss.netty.channel.Channel#write(java.lang.Object)
 */
@Override
public ChannelFuture write(Object message) {
    if(message!=null) {
        if(message instanceof FileRegion) {
            try {
                Pipe pipe = Pipe.open();
                FileRegion fr = (FileRegion)message;

                long bytesToRead = fr.getCount();
                fr.transferTo(pipe.sink(), 0L);
                byte[] content = new byte[(int)bytesToRead];
                pipe.source().read(ByteBuffer.wrap(content));
                channelWrites.add(content);
            } catch (Exception ex) {
                log.error("Failed to read content from pipe", ex);
                channelWrites.add(ex);
            }
        } else {
            channelWrites.add(message);
        }
        log.info("Received Channel Write [{}]  type:[{}]", message, message.getClass().getName());
    }

    return Channels.succeededFuture(this);
}
项目:SecureKeeper    文件:NettyServerCnxnFactory.java   
@Override
public void channelConnected(ChannelHandlerContext ctx,
        ChannelStateEvent e) throws Exception
{
    if (LOG.isTraceEnabled()) {
        LOG.trace("Channel connected " + e);
    }

    NettyServerCnxn cnxn = new NettyServerCnxn(ctx.getChannel(),
            zkServer, NettyServerCnxnFactory.this);
    ctx.setAttachment(cnxn);

    //SECUREKEEPER: Enable ssl only if specified
    //if (secure) {
    if(encryption.equals("ssl")){
        SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);
        ChannelFuture handshakeFuture = sslHandler.handshake();
        handshakeFuture.addListener(new CertificateVerifier(sslHandler, cnxn));
    } else {
        allChannels.add(ctx.getChannel());
        addCnxn(cnxn);
    }
}
项目:dubbo-learning    文件:NettyChannel.java   
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent); //调用父类的send方法进行异常判断

    boolean success = true;
    int timeout = 0;
    try {
        //NIO框架通知执行写操作
        ChannelFuture future = channel.write(message);
        if (sent) {//如果已经发送成功
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        //获得失败原因
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }

    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
项目:nfs-client-java    文件:Connection.java   
/**
 * This attempts to bind to privileged ports, starting with 1023 and working downwards, and returns when the first binding succeeds.
 * 
 * <p>
 * Some NFS servers apparently may require that some requests originate on
 * an Internet port below IPPORT_RESERVED (1024). This is generally not
 * used, though, as the client then has to run as a user authorized for
 * privileged, which is dangerous. It is also not generally needed.
 * </p>
 * 
 * @return
 *         <ul>
 *         <li><code>true</code> if the binding succeeds,</li>
 *         <li><code>false</code> otherwise.</li>
 *         </ul>
 * @throws RpcException If an exception occurs, or if no binding succeeds.
 */
private Channel bindToPrivilegedPort() throws RpcException {
    System.out.println("Attempting to use privileged port.");
    for (int port = 1023; port > 0; --port) {
        try {
            ChannelPipeline pipeline = _clientBootstrap.getPipelineFactory().getPipeline();
            Channel channel = _clientBootstrap.getFactory().newChannel(pipeline);
            channel.getConfig().setOptions(_clientBootstrap.getOptions());
            ChannelFuture bindFuture = channel.bind(new InetSocketAddress(port)).awaitUninterruptibly();
            if (bindFuture.isSuccess()) {
                System.out.println("Success! Bound to port " + port);
                return bindFuture.getChannel();
            }
        } catch (Exception e) {
            String msg = String.format("rpc request bind error for address: %s", 
                    getRemoteAddress());
            throw new RpcException(RpcStatus.NETWORK_ERROR, msg, e);
        }
    }

    throw new RpcException(RpcStatus.LOCAL_BINDING_ERROR, String.format("Cannot bind a port < 1024: %s", getRemoteAddress()));
}
项目:voyage    文件:InvokeFuture.java   
/**
 * 发送请求
 */
public void send() {
    ChannelFuture writeFuture = channel.write(request);
    //阻塞等待,若超时则返回已完成和失败
    boolean ret = writeFuture.awaitUninterruptibly(1000, TimeUnit.MILLISECONDS);
    if (ret && writeFuture.isSuccess()) {
        return;
    } else if(writeFuture.getCause() != null) {
        invokeMap.remove(request.getRequestID());
        throw new RpcException(writeFuture.getCause());
    } else {
        invokeMap.remove(request.getRequestID());
        throw new RpcException("sendRequest error");
    }
}
项目:voyage    文件:NettyRpcConnection.java   
/**
 * 尝试连接
 */
public void connect() {
       ChannelFuture future = bootstrap.connect(inetAddr);
       try{
           boolean ret = future.awaitUninterruptibly(Constants.TIMEOUT_CONNECTION_MILLSECOND, TimeUnit.MILLISECONDS);
           if (ret && future.isSuccess()) {
               Channel newChannel = future.getChannel();
               newChannel.setInterestOps(Channel.OP_READ_WRITE);
               try {
                   // 关闭旧的连接
                   Channel oldChannel = NettyRpcConnection.this.channel;
                   if (oldChannel != null) {
                       logger.info("Close old netty channel {} on create new netty channel {}", oldChannel, newChannel);
                       oldChannel.close();
                   }
               } finally {
                   if (!isConnected()) {
                       try {
                           logger.info("Close new netty channel {}, because the client closed.", newChannel);
                           newChannel.close();
                       } finally {
                        NettyRpcConnection.this.channel = null;
                       }
                   } else {
                    NettyRpcConnection.this.channel = newChannel;
                   }
               }
           } else if (null != future.getCause()) {
            logger.error("connect fail", future.getCause());
            throw new RuntimeException("connect error", future.getCause());
           } else {
            logger.error("connect fail,connstr: "+this.getConnStr());
            throw new RuntimeException("connect error");
           }
       }finally{
           if (! isConnected()) {
               future.cancel();
           }
       }
}
项目:fuck_zookeeper    文件:NettyServerCnxn.java   
private ChannelFuture disableRecvNoWait() {
    throttled = true;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Throttling - disabling recv " + this);
    }
    return channel.setReadable(false);
}
项目:https-github.com-apache-zookeeper    文件:NettyServerCnxnFactory.java   
/**
 * Only allow the connection to stay open if certificate passes auth
 */
public void operationComplete(ChannelFuture future)
        throws SSLPeerUnverifiedException {
    if (future.isSuccess()) {
        LOG.debug("Successful handshake with session 0x{}",
                Long.toHexString(cnxn.sessionId));
        SSLEngine eng = sslHandler.getEngine();
        SSLSession session = eng.getSession();
        cnxn.setClientCertificateChain(session.getPeerCertificates());

        String authProviderProp
                = System.getProperty(ZKConfig.SSL_AUTHPROVIDER, "x509");

        X509AuthenticationProvider authProvider =
                (X509AuthenticationProvider)
                        ProviderRegistry.getProvider(authProviderProp);

        if (authProvider == null) {
            LOG.error("Auth provider not found: {}", authProviderProp);
            cnxn.close();
            return;
        }

        if (KeeperException.Code.OK !=
                authProvider.handleAuthentication(cnxn, null)) {
            LOG.error("Authentication failed for session 0x{}",
                    Long.toHexString(cnxn.sessionId));
            cnxn.close();
            return;
        }

        allChannels.add(future.getChannel());
        addCnxn(cnxn);
    } else {
        LOG.error("Unsuccessful handshake with session 0x{}",
                Long.toHexString(cnxn.sessionId));
        cnxn.close();
    }
}
项目:https-github.com-apache-zookeeper    文件:NettyServerCnxn.java   
private ChannelFuture disableRecvNoWait() {
    throttled = true;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Throttling - disabling recv " + this);
    }
    return channel.setReadable(false);
}