Example #1
0
  /**
   * Performs a remote tuple space operation.
   *
   * @param template
   * @param dest
   * @param type
   * @return The matching tuple or null if none was found.
   */
  private Tuple doMonoOp(Tuple template, int dest, int type) {
    AgillaTSReqMsgJ request;
    if (type == REMOVE)
      request = new AgillaTSReqMsgJ(dest, TOS_UART_ADDRESS, BasicOpcodes.OPrinp, template);
    else request = new AgillaTSReqMsgJ(dest, TOS_UART_ADDRESS, BasicOpcodes.OPrrdp, template);

    log("doMonoOp: request = " + request + ", dest = " + dest);

    response.clear();

    try {
      log("doMonoOp: Sent inp or rdp request.");
      // results = null;
      sni.send(request);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }

    TimeoutTimer timer = new TimeoutTimer(response, AGILLA_RTS_TIMEOUT, timerid++);
    MessageJ rMsg = waitForResponse();

    if (rMsg instanceof TimeoutMsgJ && ((TimeoutMsgJ) rMsg).id() == timer.id()) {
      log("doMonoOp: Remote TS operation timed out.");
      return null;
    } else if (rMsg instanceof AgillaTSResMsgJ) {
      AgillaTSResMsgJ results = (AgillaTSResMsgJ) rMsg;
      log("doMonoOp: Got results, " + (results.isSuccess() ? "SUCCESS" : "FAIL"));
      if (results.isSuccess()) return results.getTuple();
      else return null;
    }
    return null;
  }
  @Override
  public void onTimeout(final TimeoutTimer timer) {
    endTimerForProvider(timer.getId(), (LocationListener) timer.getTag());
    updateFromLocationListener();

    Utils.d(TAG, "Location timeout for provider", timer.getId());
  }
  private void endTimer(TimeoutTimer timer) {
    if (timer == null) return;

    Utils.d(TAG, "Ending a timed update from", timer.getId());
    timer.stop(); // this will NOT stop the already running thread
    manager.removeUpdates((LocationListener) timer.getTag());
  }
  private void startTimerForProvider(String provider, LocationListener listener) {
    TimeoutTimer timer = new TimeoutTimer(this, handler, provider);
    timer.setTag(listener);
    timer.start();

    timers.add(timer);
    manager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE, listener);

    Utils.d(TAG, "Getting a timed update from", provider);
  }
 private TimeoutTimer getTimer(String id) {
   int size = timers.size();
   for (int i = 0; i < size; i++) {
     TimeoutTimer timer = timers.get(i);
     if (timer.getId().equals(id)) {
       return timer;
     }
   }
   return null;
 }
Example #6
0
 @Override
 public void run() {
   while (isRunning) {
     int numSeconds = (int) ((Math.random() * 9) + 1);
     //            System.out.println("TimeoutChecker waiting " + numSeconds + " seconds.");
     try {
       Thread.sleep(numSeconds * 1000);
     } catch (InterruptedException ex) {
       Logger.getLogger(TimeoutChecker.class.getName()).log(Level.SEVERE, null, ex);
     }
     totalRunningTime += numSeconds;
     if (timer.isTimeOut()) {
       //                System.out.println("Time is out! Waited " + totalRunningTime + "
       // seconds.");
       fireTimeout();
       isRunning = false;
       try {
         Thread.currentThread().join();
       } catch (InterruptedException ex) {
         Logger.getLogger(TimeoutChecker.class.getName()).log(Level.SEVERE, null, ex);
       }
     }
   }
 }
Example #7
0
 public void restart() {
   timer.restart();
   isRunning = true;
 }
Example #8
0
 public void terminate() {
   isRunning = false;
   timer.stop();
 }