public void testAcceptorFilterChain() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(1024 + 1000);
    IoFilter mockFilter = new MockFilter();
    IoHandler mockHandler = new MockHandler();

    acceptor.getFilterChain().addLast("mock", mockFilter);
    acceptor.setHandler(mockHandler);
    acceptor.bind(new InetSocketAddress(port));

    try {
      connector.setHandler(new IoHandlerAdapter());
      ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", port));
      future.awaitUninterruptibly();

      WriteFuture writeFuture = future.getSession().write(IoBuffer.allocate(16).putInt(0).flip());
      writeFuture.awaitUninterruptibly();
      Assert.assertTrue(writeFuture.isWritten());

      future.getSession().close(true);

      for (int i = 0; i < 30; i++) {
        if (result.length() == 2) {
          break;
        }
        Thread.sleep(100);
      }

      Assert.assertEquals("FH", result);
    } finally {
      acceptor.unbind();
    }
  }
  @Override
  public boolean send(String data) {
    // Check for and disconnect slow consumers.
    if (maxScheduledWriteRequests > 0
        && ioSession.getScheduledWriteMessages() >= maxScheduledWriteRequests) {
      Session qfjSession = (Session) ioSession.getAttribute(SessionConnector.QF_SESSION);
      try {
        qfjSession.disconnect("Slow consumer", true);
      } catch (IOException e) {
      }
      return false;
    }

    // The data is written asynchronously in a MINA thread
    WriteFuture future = ioSession.write(data);
    if (synchronousWrites) {
      try {
        if (!future.awaitUninterruptibly(synchronousWriteTimeout)) {
          log.error("Synchronous write timed out after " + synchronousWriteTimeout + "ms");
          return false;
        }
      } catch (RuntimeException e) {
        log.error("Synchronous write failed: " + e.getMessage());
        return false;
      }
    }
    return true;
  }
Example #3
0
  public void sendSample() throws InterruptedException {
    IoSession session = rs232connector.getReceiverSession();
    // SendOnePacket(session, pkt, nc, 1000);
    CHYCAOutMessage pkt = new CHYCAOutMessage();
    WriteFuture wf;
    byte bt[] = pkt.getContent();
    bt[0] = 0;

    pkt.setContent(bt);
    wf = session.write(pkt); // 发送消息
    wf.awaitUninterruptibly();
    Thread.sleep(1000);
  }
Example #4
0
    @Override
    public void run() {
      // TODO Auto-generated method stub
      super.run();
      if (iosession != null) {
        try {
          byte[] p;
          if (ng != null) {
            if ("十六进制".equals(sendtype)) {
              p = Utils.getByteArray(ng);
            } else {
              p = ng.getBytes("GBK");
            }
          } else {
            if ("十六进制".equals(sendtype)) {
              p = Utils.getByteArray(msg);
            } else {
              p = msg.getBytes("GBK");
            }
          }

          IoBuffer ib = IoBuffer.wrap(p, 0, p.length);
          Log.i("msg", "send" + p.length + "--" + ib.array().length);
          WriteFuture future = iosession.write(ib);
          ;
          future.awaitUninterruptibly();
          if (ng != null) {
            Message mg = handler.obtainMessage();
            mg.what = 4;
            mg.obj = "发送成功";
            handler.sendMessage(mg);
          }
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          Message mg = handler.obtainMessage();
          mg.what = 4;
          mg.obj = "发送框输入数据格式错误 十六进制模式请使用空格隔开!";
          handler.sendMessage(mg);
        }
      }
    }