public void read(Buffer buf) {
    synchronized (buffers) {
      while (buffers.size() == 0 && notDone) {
        try {
          buffers.wait();
        } catch (InterruptedException ex) {
          ex.printStackTrace();
        }
      }

      if (buffers.size() > 0) {
        Buffer newBuf = (Buffer) buffers.get(0);
        int[] newData = (int[]) newBuf.getData();
        buf.setData(newData);
        buf.setLength(newBuf.getLength());
        buf.setOffset(0);
        buf.setFormat(vFormat);
        buf.setFlags(Buffer.FLAG_KEY_FRAME | Buffer.FLAG_NO_DROP);
        buffers.remove(0);
        // System.out.println("Removing buffer: size = " + buffers.size());
      } else {
        buf.setEOM(true);
        buf.setOffset(0);
        buf.setLength(0);
        synchronized (buffers) {
          buffers.notify();
        }
      }
    }
  }
Exemplo n.º 2
1
  public Message send(Message query) {
    int q, r;
    Message best = null;
    byte rcode;

    for (q = 0; q < 20; q++) {
      boolean ok = false;
      for (r = 0; r < resolvers.length; r++) ok |= sendTo(query, r, q);
      if (!ok) break;
      Message m = null;
      synchronized (queue) {
        try {
          queue.wait((quantum + 1) * 1000);
        } catch (InterruptedException e) {
          System.out.println("interrupted");
        }
        if (queue.size() == 0) continue;
        m = (Message) queue.firstElement();
        queue.removeElementAt(0);
        Integer I = (Integer) queue.firstElement();
        queue.removeElementAt(0);
        r = I.intValue();
      }
      if (m == null) invalid[r] = true;
      else {
        rcode = m.getHeader().getRcode();
        if (rcode == Rcode.NOERROR) return m;
        else {
          if (best == null) best = m;
          invalid[r] = true;
        }
      }
    }
    return best;
  }
  public void waitForDone() {

    synchronized (buffers) {
      notDone = false;
      while (buffers.size() != 0) {
        try {
          buffers.wait();
        } catch (InterruptedException ex) {
        }
      }
      buffers.notify();
    }
    finished = true;
  }
Exemplo n.º 4
0
  /**
   * Pings initial members. Removes self before returning vector of initial members. Uses IP
   * multicast or gossiping, depending on parameters.
   */
  void findInitialMembers() {
    PingRsp ping_rsp;

    synchronized (initial_mbrs) {
      findInitialMbrsAttempts++; // GemStoneAddition

      initial_mbrs.removeAllElements();
      initial_mbrs_received = false;
      gms.passDown(new Event(Event.FIND_INITIAL_MBRS));

      // the initial_mbrs_received flag is needed when passDown() is executed on the same thread, so
      // when
      // it returns, a response might actually have been received (even though the initial_mbrs
      // might still be empty)
      while /*GemStoneAddition*/ (initial_mbrs_received == false) {
        try {
          initial_mbrs
              .wait(); // gms.join_timeout+1000); // GemStoneAddition bug #34274 - don't wait
                       // forever
        } catch (InterruptedException e) { // GemStoneAddition
          Thread.currentThread().interrupt();
          initial_mbrs_received = true; // suppress any late updates
          // process whatever he have now.  Note that since we are
          // stilled synchronized on initial_mbrs, asynchronous updates
          // won't come in before we return a result.
        }
      }

      for (int i = 0; i < initial_mbrs.size(); i++) {
        ping_rsp = (PingRsp) initial_mbrs.elementAt(i);
        if (ping_rsp.own_addr != null
            && gms.local_addr != null
            && ping_rsp.own_addr.equals(gms.local_addr)) {
          initial_mbrs.removeElementAt(i);
          break;
        }
      }
      if (findInitialMbrsAttempts > 2) {
        // GemStoneAddition: bug #50785 - if we have made some rounds using
        // best-guess coordinator candidates from other processes that have
        // yet to join the group then we stop trusting this gossip
        // because it can just ping-pong around between members that are
        // still trying to join.  See PingWaiter.getPossibleCoordinator().
        List<PingRsp> removals = new ArrayList<PingRsp>(initial_mbrs.size());
        for (int i = 0; i < initial_mbrs.size(); i++) {
          ping_rsp = (PingRsp) initial_mbrs.elementAt(i);
          if (ping_rsp.own_addr != null
              && !ping_rsp.isServer()
              && !ping_rsp.getCoordAddress().equals(ping_rsp.own_addr)) {
            removals.add(ping_rsp);
          }
        }
        initial_mbrs.removeAll(removals);
      }
    }
  }
  private void doTransferLocationData() {
    while (true) {
      if (networkConnectivityExecutor.networkConnectivity.getNetStatus()
          == NetworkConnectivity.NET_WORK_ENABLED) {
        // Network is usable now.Try to send data to server from queue.
        // Keep the poped top data just for rolling back in case of sending failed.
        synchronized (dataQueue) {
          if (dataQueue.size() > 0) {
            // dataToSend = dataQueue.get(0);
            dataToSend = dataQueue.remove(0);
          } else {
            dataToSend = null;
          }
        }

        if (dataToSend != null) {
          // Sending location data to server.
          int responseStatus = dataTransferService.sendData(dataToSend);

          if (responseStatus == DataTransferService.SEND_SUCCESSED) {
            // Sending data successed.
            MyLog.i("[Send] " + dataToSend + " Successed");
            // Waiting for the notification from adding new location data till time out.
            waitToResendTillTimeOut();
          } else if (responseStatus == DataTransferService.SEND_FAILED) {
            // Sending data failed.
            MyLog.i("[Send] " + dataToSend + " Failed");
            // Rolling back: put the data back to the top of the queue.
            synchronized (dataQueue) {
              dataQueue.add(0, dataToSend);

              MyLog.i("[RollBack] " + dataToSend + " Successed");
              // Waiting for the notification from adding new location data till time out.
              try {
                dataQueue.wait(SEND_DURATION);
              } catch (InterruptedException e) {
                e.printStackTrace();

                MyLog.w("Interrupted while waiting for sending location data");
              }
            }
          }
        } else {
          MyLog.i("[Send] No data to send");
          // Waiting for the notification from adding new location data till time out.
          waitToResendTillTimeOut();
        }
      } else {
        // Network is not usable now.
        waitToResendTillTimeOut();
      }
    }
  }
  private void waitToResendTillTimeOut() {
    // Waiting for the notification from adding new location data till time out.
    synchronized (dataQueue) {
      try {
        dataQueue.wait(SEND_DURATION);
      } catch (InterruptedException e) {
        e.printStackTrace();

        MyLog.w("Interrupted while waiting for sending location data.");
      }
    }
  }
Exemplo n.º 7
0
  /** Invoked by the VM when it is exiting. */
  public void run() {
    synchronized (processes) {
      Enumeration e = processes.elements();
      while (e.hasMoreElements()) {
        ((Process) e.nextElement()).destroy();
      }

      try {
        // wait for all processes to finish
        processes.wait();
      } catch (InterruptedException interrupt) {
        // ignore
      }
    }
  }
Exemplo n.º 8
0
  /*
   * Returns first tuple found which matches given template. Otherwise,
   * returns null.
   */
  private Tuple findTuple(TupleTemplate t, boolean remove, boolean block) {
    // System.out.println("Handling request for template "+t.toString());

    Vector<Tuple> vals = tuples.get(generateKey(t));

    while (vals == null) {
      if (block) {
        synchronized (tuples) {
          try {
            tuples.wait();
          } catch (InterruptedException e) {
            return null;
          }
        }
      } else {
        return null;
      }

      vals = tuples.get(generateKey(t));
    }

    synchronized (vals) {
      Tuple result = null;

      for (; ; ) {
        for (Tuple i : vals) {
          if (t.matches(i)) {
            result = i;

            if (remove) vals.remove(result);

            return result;
          }
        }

        if (block) {
          try {
            vals.wait(1000);
          } catch (InterruptedException e) {
            return null;
          }
        } else {
          return result;
        }
      }
    }
  }
Exemplo n.º 9
0
 private MessageJ waitForResponse() {
   synchronized (response) {
     while (response.size() == 0) {
       try {
         response.wait();
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   if (response.size() > 0) {
     return (MessageJ) response.remove(0);
   } else {
     log("BUG: Recursive call to waitForResponse()...");
     return waitForResponse();
   }
 }
 public int receive(byte[] buf, int off, int len, int waitMillis) throws IOException {
   synchronized (receiveQueue) {
     if (receiveQueue.isEmpty()) {
       try {
         receiveQueue.wait(waitMillis);
       } catch (InterruptedException e) {
         // TODO Keep waiting until full wait expired?
       }
       if (receiveQueue.isEmpty()) {
         return -1;
       }
     }
     DatagramPacket packet = (DatagramPacket) receiveQueue.remove(0);
     int copyLength = Math.min(len, packet.getLength());
     System.arraycopy(packet.getData(), packet.getOffset(), buf, off, copyLength);
     return copyLength;
   }
 }
Exemplo n.º 11
0
  byte[] deQueue() throws IOException {
    synchronized (packets) {
      while (packets.size() == 0) {
        if (connectionClosed)
          throw (IOException)
              new IOException("The connection is closed.").initCause(tm.getReasonClosedCause());

        try {
          packets.wait();
        } catch (InterruptedException ign) {
          Thread.currentThread().interrupt();
        }
      }
      /* This sequence works with J2ME */
      byte[] res = (byte[]) packets.firstElement();
      packets.removeElementAt(0);
      return res;
    }
  }
Exemplo n.º 12
0
  public void run() {
    Object o;

    while (!quit) {
      o = null;

      try {
        while (counter > 0) {
          Thread.sleep(300);
          counter--;
        }
      } catch (InterruptedException e) {
      }

      synchronized (queue) {
        if (queue.size() > 0) {
          /*
          				//#debug info
          				System.out.println("queue firstElement tooltip: " + queue.firstElement());

              			for(int i = 0; i < queue.size() ; i++ )
              			{
              				//#debug info
              				System.out.println("queue[" + i + "] tooltip: " + queue.elementAt(i));
              			}

          				//#debug info
          				System.out.println("queue lastElement tooltip: " + queue.lastElement());
          */
          o = queue.lastElement();
          queue.removeAllElements();
        } else {
          try {
            queue.wait();
          } catch (InterruptedException e) {
          }
        }
      }

      if (o != null) {

        boolean setTooltip = false;

        if (_cutomTableItem.getClass() == CustomTableItem.class) {
          CustomImageItem customImageItem = (CustomImageItem) _tooltipObject;
          if ((_cutomTableItem.getFocusedItem() == customImageItem)
              && _cutomTableItem.isFocused
              && _cutomTableItem._workingForm.isActive()) {
            setTooltip = true;
          }
        }

        if (setTooltip == true) {
          // #style .centeredCell
          tooltipAlert.setString(o.toString());
          tooltipAlert.setTimeout(4000);

          if (!tooltipAlert.isShown()) {
            Utility.show(Display.getDisplay(ABSALive.getInstance()), tooltipAlert);
          }
        }
      }
    }
  }