Exemplo n.º 1
0
 @Before
 public void setUp() throws Exception {
   acceptor = new NioSocketAcceptor();
   KeepAliveMessageFactory factory = new ServerFactory();
   KeepAliveFilter filter = new KeepAliveFilter(factory, IdleStatus.BOTH_IDLE);
   acceptor.getFilterChain().addLast("keep-alive", filter);
   acceptor.setHandler(new IoHandlerAdapter());
   acceptor.setDefaultLocalAddress(new InetSocketAddress(0));
   acceptor.bind();
   port = acceptor.getLocalAddress().getPort();
 }
Exemplo n.º 2
0
  public void RunHttp() {
    int port = DEFAULT_PORT;
    try {
      // Create an acceptor
      NioSocketAcceptor acceptor = new NioSocketAcceptor();

      // Create a service configuration
      acceptor
          .getFilterChain()
          .addLast("protocolFilter", new ProtocolCodecFilter(new HttpServerProtocolCodecFactory()));
      acceptor.getFilterChain().addLast("logger", new LoggingFilter());
      acceptor.setHandler(new HttpUCWebHandler());
      acceptor.bind(new InetSocketAddress(port));

      System.out.println("Server now listening uc on port " + port);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Exemplo n.º 3
0
  public void start(final String transCode) throws IOException {
    acceptor = new NioSocketAcceptor();
    DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
    chain.addLast("logger", new LoggingFilter());

    DefaultMessageCodecFactory messageCodecFactory = createMessageCodecFactory();

    DefaultProtocolCodecFactory protocolCodecFactory = new DefaultProtocolCodecFactory();
    DefaultProtocolDecoder protocolDecoder = new DefaultProtocolDecoder();
    protocolDecoder.setMessageCodecFactory(messageCodecFactory);
    protocolCodecFactory.setProtocolDecoder(protocolDecoder);

    DefaultProtocolEncoder encoder = new DefaultProtocolEncoder();
    encoder.setMessageCodecFactory(messageCodecFactory);

    protocolCodecFactory.setProtocolEncoder(encoder);

    chain.addLast("protocol", new ProtocolCodecFilter(protocolCodecFactory));
    acceptor.setHandler(
        new IoHandlerAdapter() {

          @Override
          public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
            logger.error("error.", cause);
            session.close(false);
          }

          @Override
          public void messageReceived(IoSession session, Object message) throws Exception {
            logger.info("handler get message:" + message);

            System.out.println("handler get message:" + message);

            session.write(createResponse(transCode));
            session.close(false);
          }
        });
    // new ProtocolIoHandler()
    // acceptor.setHandler((ProtocolIoHandler)beanFactory.getBean("protocolIoHandler"));
    acceptor.bind(new InetSocketAddress(port));
  }
  @Test
  public void testWriteUsingSocketTransport() throws Exception {
    NioSocketAcceptor acceptor = new NioSocketAcceptor();
    acceptor.setReuseAddress(true);
    SocketAddress address =
        new InetSocketAddress("localhost", AvailablePortFinder.getNextAvailable());

    NioSocketConnector connector = new NioSocketConnector();

    // Generate 4MB of random data
    byte[] data = new byte[4 * 1024 * 1024];
    new Random().nextBytes(data);

    byte[] expectedMd5 = MessageDigest.getInstance("MD5").digest(data);

    M message = createMessage(data);

    SenderHandler sender = new SenderHandler(message);
    ReceiverHandler receiver = new ReceiverHandler(data.length);

    acceptor.setHandler(sender);
    connector.setHandler(receiver);

    acceptor.bind(address);
    connector.connect(address);
    sender.latch.await();
    receiver.latch.await();

    acceptor.dispose();

    assertEquals(data.length, receiver.bytesRead);
    byte[] actualMd5 = receiver.digest.digest();
    assertEquals(expectedMd5.length, actualMd5.length);
    for (int i = 0; i < expectedMd5.length; i++) {
      assertEquals(expectedMd5[i], actualMd5[i]);
    }
  }
Exemplo n.º 5
0
  @Override
  protected void startService() throws Exception {
    if (port == 0) {
      throw new ConfigurationException("Port value not set");
    }
    socketAddress = new InetSocketAddress(port);
    newCodecFactory();
    addListeners();

    acceptor = new NioSocketAcceptor();
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(codecFactory));

    if (timeout == -1) {
      keepAliveHandler = newKeepAliveHandler();
      if (keepAliveHandler != null) {
        KeepAliveFilter kaf =
            new KeepAliveFilter(
                keepAliveHandler.getKeepAliveMessageFactory(),
                IdleStatus.READER_IDLE,
                keepAliveHandler.getKeepAliveRequestTimeoutHandler(),
                keepAliveHandler.getRequestInterval(),
                keepAliveHandler.getRequestTimeout());

        acceptor.getFilterChain().addLast("keepalive", kaf);
      }
    } else if (timeout > 0) {
      acceptor.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, timeout);
    }
    acceptor.setBacklog(100);
    acceptor.setReuseAddress(true);
    acceptor.setDefaultLocalAddress(socketAddress);
    acceptor.setHandler(this);
    acceptor.getSessionConfig().setReadBufferSize(2048);
    preBindAcceptorHook(acceptor);
    acceptor.bind();
  }
Exemplo n.º 6
0
 public void connect() throws IOException {
   NioSocketAcceptor acceptor = new NioSocketAcceptor();
   SocketSessionConfig config = acceptor.getSessionConfig();
   // config.setKeepAlive(true);
   // config.setReuseAddress(true);
   // config.setTcpNoDelay(true);
   config.setMinReadBufferSize(1024 * 10); // 10k
   config.setMaxReadBufferSize(1024 * 1024 * 10); // 10M
   acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(codecFactory));
   // config.setUseReadOperation(true);
   if (filters != null && !filters.isEmpty()) {
     for (String key : filters.keySet()) acceptor.getFilterChain().addLast(key, filters.get(key));
   }
   acceptor.setHandler(handler);
   InetSocketAddress address = new InetSocketAddress(serverPort);
   acceptor.bind(address);
   logger.info("server start success,serverPort:" + address);
 }
Exemplo n.º 7
0
 public String getCountersAsString() {
   StringBuffer sb = new StringBuffer();
   sb.append("connected=").append(acceptor.getManagedSessionCount());
   return sb.toString();
 }
Exemplo n.º 8
0
 @Override
 protected void stopService() throws Exception {
   acceptor.unbind();
   acceptor.dispose();
 }
Exemplo n.º 9
0
 public void endServer() {
   if (acceptor != null) {
     acceptor.dispose();
   }
 }
Exemplo n.º 10
0
 @After
 public void tearDown() throws Exception {
   acceptor.unbind();
   acceptor.dispose();
 }