private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) {
   String protocol = "ws";
   if (cp.get(SslHandler.class) != null) {
     // SSL in use so use Secure WebSockets
     protocol = "wss";
   }
   return protocol + "://" + req.getHttpHeaders().getHeaderString(HttpHeaders.Names.HOST) + path;
 }
Exemple #2
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();
 }
  /**
   * Reconnect to the remote address that the closed channel was connected to. This creates a new
   * {@link ChannelPipeline} with the same handler instances contained in the old channel's
   * pipeline.
   *
   * @param timeout Timer task handle.
   * @throws Exception when reconnection fails.
   */
  @Override
  public void run(Timeout timeout) throws Exception {
    ChannelPipeline old = channel.pipeline();
    final CommandHandler<?, ?> handler = old.get(CommandHandler.class);
    final RedisAsyncConnection<?, ?> connection = old.get(RedisAsyncConnection.class);

    ChannelFuture connect = null;
    // TODO use better concurrent workaround
    synchronized (bootstrap) {
      connect =
          bootstrap
              .handler(
                  new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                      ch.pipeline().addLast(this, handler, connection);
                    }
                  })
              .connect();
    }
    connect.sync();
  }
  public void checkPipeline(String protocol, ChannelPipeline pipeline, ChannelBuffer buf)
      throws Exception {
    Object tmp = buf.duplicate();

    // Frame decoder
    FrameDecoder frameDecoder = (FrameDecoder) pipeline.get("frameDecoder");
    if (frameDecoder != null) {
      try {
        Method method =
            frameDecoder
                .getClass()
                .getDeclaredMethod(
                    "decode", ChannelHandlerContext.class, Channel.class, ChannelBuffer.class);
        method.setAccessible(true);
        tmp = method.invoke(frameDecoder, null, null, tmp);
      } catch (NoSuchMethodException error) {
        Method method =
            frameDecoder
                .getClass()
                .getSuperclass()
                .getDeclaredMethod(
                    "decode", ChannelHandlerContext.class, Channel.class, ChannelBuffer.class);
        method.setAccessible(true);
        tmp = method.invoke(frameDecoder, null, null, tmp);
      }
    }

    // String decoder
    if (pipeline.get("stringDecoder") != null) {
      StringDecoder stringDecoder = new StringDecoder();
      if (tmp != null) {
        try {
          Method method =
              stringDecoder
                  .getClass()
                  .getDeclaredMethod(
                      "decode", ChannelHandlerContext.class, Channel.class, Object.class);
          method.setAccessible(true);
          tmp = method.invoke(stringDecoder, null, null, tmp);
        } catch (NoSuchMethodException error) {
          Method method =
              stringDecoder
                  .getClass()
                  .getSuperclass()
                  .getDeclaredMethod(
                      "decode", ChannelHandlerContext.class, Channel.class, Object.class);
          method.setAccessible(true);
          tmp = method.invoke(stringDecoder, null, null, tmp);
        }
      }
    }

    // Protocol decoder
    BaseProtocolDecoder protocolDecoder = (BaseProtocolDecoder) pipeline.get("objectDecoder");
    if (tmp != null) {
      try {
        Method method =
            protocolDecoder
                .getClass()
                .getDeclaredMethod(
                    "decode",
                    ChannelHandlerContext.class,
                    Channel.class,
                    SocketAddress.class,
                    Object.class);
        method.setAccessible(true);
        tmp = method.invoke(protocolDecoder, null, null, null, tmp);
      } catch (NoSuchMethodException error) {
        Method method =
            protocolDecoder
                .getClass()
                .getSuperclass()
                .getDeclaredMethod(
                    "decode",
                    ChannelHandlerContext.class,
                    Channel.class,
                    SocketAddress.class,
                    Object.class);
        method.setAccessible(true);
        tmp = method.invoke(protocolDecoder, null, null, null, tmp);
      }
    }

    if (tmp != null) {
      Log.info("Protocol " + protocol + " possible match");
    } else if (showFailed) {
      Log.info("Protocol " + protocol + " no match");
    }
  }
Exemple #5
0
  NetSocket createNetSocket() {
    DefaultNetSocket socket =
        new DefaultNetSocket(vertx, channel, context, server.tcpHelper, false);
    Map<Channel, DefaultNetSocket> connectionMap = new HashMap<Channel, DefaultNetSocket>(1);
    connectionMap.put(channel, socket);

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
    if (compressor != null) {
      pipeline.remove(compressor);
    }

    pipeline.remove("httpDecoder");
    if (pipeline.get("chunkedWriter") != null) {
      pipeline.remove("chunkedWriter");
    }

    channel
        .pipeline()
        .replace(
            "handler",
            "handler",
            new VertxNetHandler(server.vertx, connectionMap) {
              @Override
              public void exceptionCaught(ChannelHandlerContext chctx, Throwable t)
                  throws Exception {
                // remove from the real mapping
                server.connectionMap.remove(channel);
                super.exceptionCaught(chctx, t);
              }

              @Override
              public void channelInactive(ChannelHandlerContext chctx) throws Exception {
                // remove from the real mapping
                server.connectionMap.remove(channel);
                super.channelInactive(chctx);
              }

              @Override
              public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
                if (msg instanceof HttpContent) {
                  ReferenceCountUtil.release(msg);
                  return;
                }
                super.channelRead(chctx, msg);
              }
            });

    // check if the encoder can be removed yet or not.
    if (lastWriteFuture == null) {
      channel.pipeline().remove("httpEncoder");
    } else {
      lastWriteFuture.addListener(
          new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
              channel.pipeline().remove("httpEncoder");
            }
          });
    }
    return socket;
  }