Ejemplo n.º 1
0
 private void incrementCounter(AtomicInteger counter) {
   synchronized (counter) {
     if (counter.incrementAndGet() == 0) {
       counter.notify();
     }
   }
 }
Ejemplo n.º 2
0
  /**
   * Read callback implementation
   *
   * @param rc return code
   * @param ledgerId Ledger identifier
   * @param entryId Entry identifier
   * @param bb Data
   * @param ctx Control object
   */
  public void readEntryComplete(int rc, long ledgerId, long entryId, ByteBuffer bb, Object ctx) {
    if (rc == 0) {
      bb.rewind();

      /*
       * Collect new vote
       */
      if (!votes.containsKey(entryId)) {
        votes.put(entryId, new ArrayList<ByteBuffer>());
      }
      votes.get(entryId).add(bb);

      /*
       * Extract hint
       */

      bb.position(16);
      long hint = bb.getLong();

      LOG.info("Received a response: " + rc + ", " + entryId + ", " + hint);

      if (!hints.containsKey(hint)) {
        hints.put(hint, 0);
      }
      hints.put(hint, hints.get(hint) + 1);

      synchronized (counter) {
        if (counter.incrementAndGet() >= minimum) ;
        counter.notify();
      }
    } else {
      LOG.debug("rc != 0");
    }
  }
  private void serveDataSocket(final Socket client) throws IOException {
    final OutputStream os = client.getOutputStream();
    final InputStream is = client.getInputStream();
    while (!m_stopDataServer.get()) {
      final byte[] request = new byte[DATA_SIZE];
      final ByteBuffer buf = ByteBuffer.wrap(request);
      final ReadableByteChannel channel = Channels.newChannel(is);
      while (buf.hasRemaining()) {
        channel.read(buf);
      }
      m_dataOnServer.set(request);

      m_dataServerMessagesReceived.incrementAndGet();
      synchronized (m_dataServerMessagesReceived) {
        m_dataServerMessagesReceived.notify();
      }
      m_log.debug(
          "Client socket "
              + client.getPort()
              + " has received "
              + m_dataServerMessagesReceived.get()
              + " messages...");
      if (!Arrays.equals(DATA, request)) {
        m_log.error("Bad data received on DATA server!!");
        m_unexpectedDataOnServer.set(true);
      } else {
        m_dataServerMessagesValidated++;
        m_log.debug("Data server has validated " + m_dataServerMessagesValidated + " messages...");
      }

      // Now, write the response and make sure the remote host ultimately
      // gets the raw data back.
      os.write(DATA);
      os.flush();
      m_dataMessagesWrittenFromServer.incrementAndGet();
      synchronized (m_dataMessagesWrittenFromServer) {
        m_dataMessagesWrittenFromServer.notify();
      }
    }
  }
Ejemplo n.º 4
0
 @Override
 public void handle(DiameterMessage message) {
   try {
     doHandle(message);
   } catch (Throwable e) {
     e.printStackTrace();
     _e = e;
   } finally {
     _msgReceived.incrementAndGet();
     synchronized (_msgReceived) {
       _msgReceived.notify();
     }
   }
 }
  private void startServer(final InetSocketAddress serverAddress) throws Exception {
    this.m_dataServerSocket = new ServerSocket(serverAddress.getPort());
    synchronized (m_dataServerStarted) {
      m_dataServerStarted.set(true);
      m_dataServerStarted.notify();
    }

    while (!this.m_dataServerSocket.isClosed()) {
      final Socket client;
      try {
        client = m_dataServerSocket.accept();
        m_dataServerSockets.incrementAndGet();
        synchronized (m_dataServerSockets) {
          m_dataServerSockets.notify();
        }
      } catch (final SocketException se) {
        m_log.debug("Socket closed");
        assertTrue(m_stopDataServer.get());
        // This will happen when the server socket's closed.
        break;
      }
      m_log.debug("Got data socket number: " + m_dataServerSockets);
      final Runnable serverSocketRunner =
          new Runnable() {
            public void run() {
              try {
                serveDataSocket(client);
              } catch (final Throwable t) {
                m_socketServingFailed.set(true);
              }
            }
          };
      final Thread serverSocketThread =
          new Thread(serverSocketRunner, "Server-Socket-Serving-Thread");
      serverSocketThread.setDaemon(true);
      serverSocketThread.start();
    }
  }