コード例 #1
0
  private static boolean checkPort(final InetSocketAddress remoteAddress) {
    final ClientBootstrap bootstrap =
        new ClientBootstrap(new OioClientSocketChannelFactory(new PooledThreadExecutor()));
    bootstrap.setOption("child.tcpNoDelay", true);

    final AtomicBoolean result = new AtomicBoolean(false);
    final Semaphore semaphore = new Semaphore();
    semaphore.down(); // must call to down() here to ensure that down was called _before_ up()
    bootstrap.setPipeline(
        pipeline(
            new HttpResponseDecoder(),
            new HttpRequestEncoder(),
            new SimpleChannelUpstreamHandler() {
              @Override
              public void messageReceived(ChannelHandlerContext context, MessageEvent e)
                  throws Exception {
                try {
                  if (e.getMessage() instanceof HttpResponse) {
                    HttpResponse response = (HttpResponse) e.getMessage();
                    if (response.getStatus().equals(OK)
                        && response
                            .getContent()
                            .toString(CharsetUtil.US_ASCII)
                            .equals(getApplicationStartTime())) {
                      LOG.info("port check: current OS must be marked as normal");
                      result.set(true);
                    }
                  }
                } finally {
                  semaphore.up();
                }
              }

              @Override
              public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
                  throws Exception {
                try {
                  LOG.error(e.getCause());
                } finally {
                  semaphore.up();
                }
              }
            }));

    ChannelFuture connectFuture = null;
    try {
      connectFuture = bootstrap.connect(remoteAddress);
      if (!waitComplete(connectFuture, "connect")) {
        return false;
      }
      ChannelFuture writeFuture =
          connectFuture
              .getChannel()
              .write(new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, START_TIME_PATH));
      if (!waitComplete(writeFuture, "write")) {
        return false;
      }

      try {
        // yes, 30 seconds. I always get timeout in Linux in Parallels if I set to 2 seconds.
        // In any case all work is done in pooled thread (IDE init time isn't affected)
        if (!semaphore.waitForUnsafe(30000)) {
          LOG.info("port check: semaphore down timeout");
        }
      } catch (InterruptedException e) {
        LOG.info("port check: semaphore interrupted", e);
      }
    } finally {
      if (connectFuture != null) {
        connectFuture.getChannel().close().awaitUninterruptibly();
      }
      bootstrap.releaseExternalResources();
    }
    return result.get();
  }
コード例 #2
0
ファイル: AppTest.java プロジェクト: Turbo87/intellij-plugins
 private void await() throws InterruptedException {
   semaphore.waitForUnsafe();
   if (fail.get()) {
     fail();
   }
 }