@Override
  public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    // Suspend incoming traffic until connected to the remote host.
    final Channel inboundChannel = e.getChannel();
    inboundChannel.setReadable(false);

    // Start the connection attempt.
    ClientBootstrap cb = new ClientBootstrap(cf);
    cb.getPipeline().addLast("handler", new OutboundHandler(e.getChannel()));
    ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort));

    outboundChannel = f.getChannel();
    f.addListener(
        new ChannelFutureListener() {
          public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
              // Connection attempt succeeded:
              // Begin to accept incoming traffic.
              inboundChannel.setReadable(true);
            } else {
              // Close the connection if the connection attempt has failed.
              inboundChannel.close();
            }
          }
        });
  }
Ejemplo n.º 2
0
 @Override
 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   AbstractNettyServer.ALL_CHANNELS.add(e.getChannel());
   LOG.debug(
       "Added Channel with id: {} as the {}th open channel",
       e.getChannel().getId(),
       CHANNEL_COUNTER.incrementAndGet());
 }
Ejemplo n.º 3
0
  /**
   * Attention please,
   *
   * @see
   *     org.jboss.netty.channel.SimpleChannelUpstreamHandler#channelDisconnected(org.jboss.netty.channel.ChannelHandlerContext,
   *     org.jboss.netty.channel.ChannelStateEvent)
   */
  @Override
  public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    LOG.info(
        "Receive channelDisconnected to {}, channel = {}", client.getRemoteAddr(), e.getChannel());
    // ctx.sendUpstream(e);
    super.channelDisconnected(ctx, e);

    client.disconnectChannel(e.getChannel());
  }
Ejemplo n.º 4
0
 @Override
 public void channelDisconnected(final ChannelHandlerContext ctx, final ChannelStateEvent e)
     throws Exception {
   chan = null;
   super.channelDisconnected(ctx, e); // Let the ReplayingDecoder cleanup.
   cleanup(e.getChannel());
 }
Ejemplo n.º 5
0
  @Override
  public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {

    // Make sure the handshake future is notified when a connection has
    // been closed during handshake.
    synchronized (handshakeLock) {
      if (handshaking) {
        handshakeFuture.setFailure(new ClosedChannelException());
      }
    }

    try {
      super.channelDisconnected(ctx, e);
    } finally {
      unwrap(ctx, e.getChannel(), ChannelBuffers.EMPTY_BUFFER, 0, 0);
      engine.closeOutbound();
      if (!sentCloseNotify.get() && handshaken) {
        try {
          engine.closeInbound();
        } catch (SSLException ex) {
          logger.debug("Failed to clean up SSLEngine.", ex);
        }
      }
    }
  }
Ejemplo n.º 6
0
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

      final NioSocketChannel ch = (NioSocketChannel) e.getChannel();
      NioWorker worker = ch.getWorker();

      // Choose a handler
      final HandlerHolder handler = handlerManager.chooseHandler(worker);

      if (handler == null) {
        // Ignore
        return;
      }

      VertxInternal.instance.executeOnContext(
          handler.contextID,
          new Runnable() {
            public void run() {
              VertxInternal.instance.setContextID(handler.contextID);
              NetSocket sock = new NetSocket(ch, handler.contextID, Thread.currentThread());
              socketMap.put(ch, sock);
              handler.handler.handle(sock);
            }
          });
    }
Ejemplo n.º 7
0
 @Override
 public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   Channel channel = e.getChannel();
   logger.info("channel disconnected at :{}", channel);
   streamClientListener.onDisconnected(channel);
   super.channelDisconnected(ctx, e);
 }
Ejemplo n.º 8
0
  /** 向server端发送消息 */
  @Override
  public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) {
    System.out.println("client start..");

    String message = "[" + name + "]baicai AAA";
    ChannelBuffer channelBuffer = ChannelBuffers.wrappedBuffer(message.getBytes());
    event.getChannel().write(channelBuffer);
  }
Ejemplo n.º 9
0
 @Override
 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   String welcome =
       "Welcome to Onion Router!\nYou're node name is "
           + GlobalVars.nodeName()
           + "\nType each command with a trailing semicolon\nType 'help;' to see the commands\n\n>";
   TerminalWrite.write(e.getChannel(), welcome);
 }
Ejemplo n.º 10
0
 @Override
 public void channelClosed(final ChannelHandlerContext ctx, final ChannelStateEvent e) {
   chan = null;
   // No need to call super.channelClosed() because we already called
   // super.channelDisconnected().  If we get here without getting a
   // DISCONNECTED event, then we were never connected in the first place so
   // the ReplayingDecoder has nothing to cleanup.
   cleanup(e.getChannel());
 }
Ejemplo n.º 11
0
 @Override
 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent channelStateEvent)
     throws Exception {
   if (LOG.isTraceEnabled()) {
     LOG.trace("Channel open: {}", ctx.getChannel());
   }
   // to keep track of open sockets
   producer.getAllChannels().add(channelStateEvent.getChannel());
 }
Ejemplo n.º 12
0
 @Override
 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   lock.acquire();
   try {
     channelSetter.setChannel(e.getChannel());
   } finally {
     lock.release();
   }
 }
Ejemplo n.º 13
0
  @Override
  public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    super.channelClosed(ctx, e);

    if (!ctx.getChannel().isOpen()) {
      tryToFinishOffChannel(ctx.getChannel());
    }

    channelGroup.remove(e.getChannel());
  }
Ejemplo n.º 14
0
  private void closeOutboundAndChannel(
      final ChannelHandlerContext context, final ChannelStateEvent e) throws SSLException {
    if (!e.getChannel().isConnected()) {
      context.sendDownstream(e);
      return;
    }

    unwrap(context, e.getChannel(), ChannelBuffers.EMPTY_BUFFER, 0, 0);
    if (!engine.isInboundDone()) {
      if (sentCloseNotify.compareAndSet(false, true)) {
        engine.closeOutbound();
        ChannelFuture closeNotifyFuture = wrapNonAppData(context, e.getChannel());
        closeNotifyFuture.addListener(new ClosingChannelFutureListener(context, e));
        return;
      }
    }

    context.sendDownstream(e);
  }
Ejemplo n.º 15
0
  @Override
  public void channelConnected(final ChannelHandlerContext ctx, final ChannelStateEvent e) {
    final Channel chan = e.getChannel();
    ChannelBuffer header = connectionHeaderPreamble();
    header.writerIndex(RPC_HEADER.length);
    Channels.write(chan, header);

    secureRpcHelper = new SecureRpcHelper(this);
    secureRpcHelper.sendHello(chan);
  }
Ejemplo n.º 16
0
 @Override
 public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e)
     throws Exception {
   // If inboundChannel is not saturated anymore, continue accepting
   // the incoming traffic from the outboundChannel.
   synchronized (trafficLock) {
     if (e.getChannel().isWritable()) {
       outboundChannel.setReadable(true);
     }
   }
 }
 private void sendNumbers(ChannelStateEvent e) {
   Channel channel = e.getChannel();
   while (channel.isWritable()) {
     if (i <= count) {
       channel.write(Integer.valueOf(i));
       i++;
     } else {
       break;
     }
   }
 }
Ejemplo n.º 18
0
  /** Sometime when connect one bad channel which isn't writable, it will call this function */
  @Override
  public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) {
    // register the newly established channel
    Channel channel = event.getChannel();
    LOG.info(
        "connection established to :{}, local port:{}",
        client.getRemoteAddr(),
        channel.getLocalAddress());

    client.handleResponse();
  }
 @Override
 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   Channel channel = e.getChannel();
   System.out.println(channel.getRemoteAddress().toString());
   System.out.println("channelConnected...");
   String msg = "welcome rookiefly...";
   ChannelBuffer buffer = ChannelBuffers.buffer(1024);
   buffer.writeBytes(msg.getBytes());
   channel.write(buffer);
   ctx.sendUpstream(e);
 }
Ejemplo n.º 20
0
 @Override
 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   // Событие вызывается при подключении клиента. Я создаю здесь Worker игрока — объект, который
   // занимается обработкой данных игрока
   // непостредственно.
   // Я передаю ему канал игрока (функция e.getChannel()), чтобы он мог в него посылать пакеты
   worker = workerClass.newInstance();
   worker.setChannel(e.getChannel());
   worker.setClientHandler(this);
   // worker = new InterserverWorker(this, e.getChannel());
   // log.info("channelConnected");
 }
Ejemplo n.º 21
0
 @Override
 @LogMessageDoc(
     message = "New switch connection from {ip address}",
     explanation = "A new switch has connected from the " + "specified IP address")
 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   log.debug(
       "channelConnected on OFChannelHandler {}",
       String.format("%08x", System.identityHashCode(this)));
   counters.switchConnected.increment();
   channel = e.getChannel();
   log.info("New switch connection from {}", channel.getRemoteAddress());
   setState(new WaitHelloState());
 }
Ejemplo n.º 22
0
 @Override
 public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e)
     throws Exception {
   final NioSocketChannel ch = (NioSocketChannel) e.getChannel();
   final ClientConnection conn = connectionMap.get(ch);
   runOnCorrectThread(
       ch,
       new Runnable() {
         public void run() {
           conn.handleInterestedOpsChanged();
         }
       });
 }
Ejemplo n.º 23
0
 @Override
 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
   final NioSocketChannel ch = (NioSocketChannel) e.getChannel();
   final ClientConnection conn = connectionMap.remove(ch);
   if (conn != null) {
     runOnCorrectThread(
         ch,
         new Runnable() {
           public void run() {
             conn.handleClosed();
           }
         });
   }
 }
Ejemplo n.º 24
0
 @Override
 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
   final NioSocketChannel ch = (NioSocketChannel) e.getChannel();
   final NetSocket sock = socketMap.remove(ch);
   if (sock != null) {
     VertxInternal.instance.executeOnContext(
         sock.getContextID(),
         new Runnable() {
           public void run() {
             sock.handleClosed();
           }
         });
   }
 }
Ejemplo n.º 25
0
 @Override
 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
   final NioSocketChannel ch = (NioSocketChannel) e.getChannel();
   final DefaultNetSocket sock = socketMap.remove(ch);
   if (sock != null) {
     sock.getContext()
         .execute(
             new Runnable() {
               public void run() {
                 sock.handleClosed();
               }
             });
   }
 }
Ejemplo n.º 26
0
  @Override
  public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
    Channel ch = e.getChannel();

    String date = new Date().toString();
    ChannelBuffer time = ChannelBuffers.copiedBuffer(date + "\n", Charset.forName("UTF-8"));

    ChannelFuture f = ch.write(time);

    f.addListener(
        new ChannelFutureListener() {
          public void operationComplete(ChannelFuture future) {
            future.getChannel().close();
          }
        });
  }
Ejemplo n.º 27
0
 @Override
 public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e)
     throws Exception {
   final NioSocketChannel ch = (NioSocketChannel) e.getChannel();
   final DefaultNetSocket sock = socketMap.get(ch);
   ChannelState state = e.getState();
   if (state == ChannelState.INTEREST_OPS) {
     sock.getContext()
         .execute(
             new Runnable() {
               public void run() {
                 sock.handleInterestedOpsChanged();
               }
             });
   }
 }
Ejemplo n.º 28
0
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

      final NioSocketChannel ch = (NioSocketChannel) e.getChannel();
      NioWorker worker = ch.getWorker();

      // Choose a handler
      final HandlerHolder handler = handlerManager.chooseHandler(worker);

      if (handler == null) {
        // Ignore
        return;
      }

      if (tcpHelper.isSSL()) {
        SslHandler sslHandler = (SslHandler) ch.getPipeline().get("ssl");

        ChannelFuture fut = sslHandler.handshake();
        fut.addListener(
            new ChannelFutureListener() {

              public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if (channelFuture.isSuccess()) {
                  connected(ch, handler);
                } else {
                  log.error(
                      "Client from origin "
                          + ch.getRemoteAddress()
                          + " failed to connect over ssl");
                }
              }
            });

      } else {
        connected(ch, handler);
      }
    }
Ejemplo n.º 29
0
 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   myAllOpenChannels.add(e.getChannel());
   super.channelOpen(ctx, e);
 }
 @Override
 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   channel = e.getChannel();
 }