Esempio n. 1
0
 public boolean trySendOnSharedLine(String message, long timeout, TimeUnit unit)
     throws InterruptedException {
   long nanosToLock = unit.toNanos(timeout) - estimatedNanosToSend(message);
   if (!lock.tryLock(nanosToLock, NANOSECONDS)) return false;
   try {
     return sendOnSharedLine(message);
   } finally {
     lock.unlock();
   }
 }
  public void startSession(String id, PrintWriter writer) {
    SlaveBrowser browser = capturedBrowsers.getBrowser(id);
    Lock lock = locks.get(id);
    String sessionId = UUID.randomUUID().toString();

    if (lock.tryLock(sessionId)) {
      writer.write(sessionId);
    } else {
      // session is probably staled
      if ((!browser.isCommandRunning() && browser.peekCommand() == null)
          || ((new Date().getTime() - lock.getLastHeartBeat()) > HEARTBEAT_TIMEOUT)) {
        lock.forceUnlock();
        SlaveBrowser slaveBrowser = capturedBrowsers.getBrowser(id);

        slaveBrowser.clearCommandRunning();
        slaveBrowser.clearResponseQueue();
        filesCache.clear();
        writer.write(lock.tryLock(sessionId) ? sessionId : "FAILED");
      } else {
        writer.write("FAILED");
      }
    }
    writer.flush();
  }