void loopingTest(boolean mark_) {
   // try reset() and then mark(), turn off looping if both failed
   try {
     try {
       reader.reset();
       return;
     } catch (Exception e_) {
       if (mark_) {
         if (!reader.markSupported()) reader = new BufferedReader(reader);
         reader.mark(1 << 20);
         return;
       }
       drcl.Debug.error(e_ + "\n");
     }
   } catch (Exception e_) {
     drcl.Debug.error(e_ + "\n");
   }
   drcl.Debug.error("Turn off the looping flag.\n");
   setLoopEnabled(false);
 }
Example #2
0
  /**
   * Returns a {@link TrafficComponent} instance for the given traffic model. The component returned
   * may be a {@link TrafficSourceComponent} or a {@link TrafficShaperComponent}, depending on the
   * nature of the traffic model.
   */
  public static TrafficComponent getTrafficComponent(TrafficModel traffic_) {
    if (traffic_ instanceof traffic_PeakRate) {
      if (traffic_ instanceof traffic_FixedPoints)
        return new tsFixedPoints((traffic_FixedPoints) traffic_);
      else return new tsPeakRate((traffic_PeakRate) traffic_);
    }
    if (traffic_ instanceof traffic_TokenBucket)
      return new TrafficShaperComponent(new tsTokenBucket((traffic_TokenBucket) traffic_));
    if (traffic_ instanceof traffic_CDSmooth)
      return new TrafficShaperComponent(new tsCDSmooth((traffic_CDSmooth) traffic_));
    if (traffic_ instanceof traffic_RTSmooth)
      return new TrafficShaperComponent(new tsRTSmooth((traffic_RTSmooth) traffic_));
    if (traffic_ instanceof traffic_PacketTrain)
      return new tsPacketTrain((traffic_PacketTrain) traffic_);
    if (traffic_ instanceof traffic_ExpOnOff) return new tsExpOnOff((traffic_ExpOnOff) traffic_);
    if (traffic_ instanceof traffic_ParetoOnOff)
      return new tsParetoOnOff((traffic_ParetoOnOff) traffic_);
    if (traffic_ instanceof traffic_Periodic)
      return new TrafficShaperComponent(new tsPeriodic((traffic_Periodic) traffic_));
    if (traffic_ instanceof traffic_OnOff) return new tsOnOff((traffic_OnOff) traffic_);

    Class class_ = null;
    try {
      String className_ = traffic_.getClass().getName();
      // suppose component is in the same package as the traffic model
      // class.  So className is in the form of
      // "<PackageName>.traffic_XYZ", convert it to
      // "<PackageName>.tsXYZ"
      int index_ = className_.lastIndexOf(".");
      String packageName_ = index_ >= 0 ? className_.substring(0, index_ + 1) : "";
      className_ = index_ >= 0 ? className_.substring(index_ + 1) : className_;

      className_ = packageName_ + "ts" + className_.substring(8);
      class_ = Class.forName(className_);
      TrafficComponent tc_ = null;
      if (TrafficShaper.class.isAssignableFrom(class_))
        tc_ = new TrafficShaperComponent((TrafficShaper) class_.newInstance());
      else tc_ = (TrafficComponent) class_.newInstance();
      tc_.setTrafficModel(traffic_);
      return tc_;
    } catch (ClassNotFoundException e_) {
      drcl.Debug.error("Cannot find traffic component for " + traffic_.getClass());
    } catch (InstantiationException e_) {
      drcl.Debug.error(e_.toString());
    } catch (IllegalAccessException e_) {
      drcl.Debug.error(e_.toString());
    }
    return null;
  }
Example #3
0
  /** Returns a {@link TrafficShaper} instance for the given traffic model. */
  public static TrafficShaper getTrafficShaper(TrafficModel traffic_) {
    // FIXME: hard-coded
    if (traffic_ instanceof traffic_TokenBucket)
      return new tsTokenBucket((traffic_TokenBucket) traffic_);
    if (traffic_ instanceof traffic_CDSmooth) return new tsCDSmooth((traffic_CDSmooth) traffic_);
    if (traffic_ instanceof traffic_RTSmooth) return new tsRTSmooth((traffic_RTSmooth) traffic_);
    if (traffic_ instanceof traffic_Periodic) return new tsPeriodic((traffic_Periodic) traffic_);

    try {
      String className_ = traffic_.getClass().getName();
      // suppose className is in the form of "traffic_XYZ",
      //   convert it to "tsXYZ"
      Class class_ = Class.forName("ts" + className_.substring(8));
      TrafficShaper ts_ = (TrafficShaper) class_.newInstance();
      ts_.setTrafficModel(traffic_);
      return ts_;
    } catch (Exception e_) {
      drcl.Debug.error("Cannot find traffic shaper class for " + traffic_.getClass());
      return null;
    }
  }
Example #4
0
  /** Delay the starting until the time specified later(second). */
  public void start() {
    // if (port != null) System.out.println(this + " starts: " + data);
    // runtime.startRegister(this);

    if (aruntime.isSuspend()) return;

    if (isAlive()) {
      // __wakeUp();
      synchronized (this) {
        lastsleepon = this;
        lastwakeupthread = Thread.currentThread();

        try {
          this.notify();
        } catch (Exception e_) {
          drcl.Debug.error(this, "start()| " + e_, false);
        }
      }
    } else {
      // the thread is newly created
      setState(State_INACTIVE);
      super.start();
    }
  }
Example #5
0
  /** Standard Thread.run(). */
  public /*final*/ void run() {
    if (Thread.currentThread() != this) {
      throw new WorkerThreadException(
          "not run() by the owning thread.  Current thread:"
              + Thread.currentThread()
              + "---owning thread:"
              + this);
    }

    try {
      if (aruntime.debug && aruntime.isDebugEnabledAt(ARuntime.Debug_THREAD))
        aruntime.println(ARuntime.Debug_THREAD, this, "THREAD STARTS");

      for (; ; ) {

        // Note:
        // getTask() and recycle() must happen atomically because
        // during the time after this thread cannot get more tasks
        // from runtime.getTask() and before it goes to recycle() to
        // return workforce, another thread may produce a new task in
        // the ready queue (without this thread knowing it) and that
        // thread goes to the waiting state.  Due to this racing,
        // the simulation could be stalled as no thread is actively
        // running to execute the new task in the ready queue.
        synchronized (aruntime) {
          if (mainContext == null || mainContext == DUMMY_CONTEXT) {
            // grab a task from worker pool
            nextTask = GETTING_TASK;
            // XXX: temporary for debug
            nextTask = aruntime.getTask(mainContext == null);
            if (nextTask != null) {
              // so null wont override DUMMY_CONTEXT
              mainContext = nextTask;
              nextTask = null;
            } else {
              setState(State_RECYCLING);
              sleepOn = this;
              aruntime.recycle(this);
              // return workforce in recycle()
            }
          }
        }

        if (state == State_RECYCLING) {
          setState(State_INACTIVE);
          // BOOKMARK1:
          // At this point, other threads may assign a new task
          // to this thread and start this thread, so check if
          // mainContext is being assigned within
          // the following synchronized claus
          synchronized (this) {
            if (mainContext == DUMMY_CONTEXT) mainContext = null;

            if (mainContext == null) {
              __sleepOn(this, State_INACTIVE, State_INACTIVE);
              continue;
            } else sleepOn = null;
          }
        }

        if (runtime.resetting) {
          throw new WorkerThreadInterruptedException();
        } else if (aruntime.isSuspend()) {
          // go into sleep if runtime is suspended
          synchronized (this) {
            __sleepOn(this, State_PREACTIVE, State_ACTIVE);
          }
        }

        if (mainContext.threadGroup != null) {
          if (mainContext instanceof TaskNotify) {
            // mainContext is TaskNotify:
            // the object to be notified on is in "data"
            synchronized (mainContext.data) {
              if (aruntime.debug && aruntime.isDebugEnabledAt(ARuntime.Debug_THREAD))
                aruntime.println(
                    ARuntime.Debug_THREAD,
                    this,
                    "EXECUTING notify:" + mainContext.data + "," + System.currentTimeMillis());
              mainContext.data.notify();
              aruntime.nthreadsWaiting--;
            }
            mainContext = null;
            // workforce transfered to the waked up thread
            continue;
          } else if (getThreadGroup() != mainContext.threadGroup) {
            aruntime.immediatelyStart(mainContext);
            mainContext = null;
            // workforce transfered to new thread
            continue;
          }
        }

        if (aruntime.debug && aruntime.isDebugEnabledAt(ARuntime.Debug_THREAD))
          aruntime.println(ARuntime.Debug_THREAD, this, "EXECUTING:" + mainContext);

        setState(State_ACTIVE);

        // runtime.runCheck(this);
        String method_ = "process()";
        currentContext = mainContext;
        returnPort = currentContext.returnPort;
        // from server port, new context
        try {
          mainContext.execute(this);
        } catch (NullPointerException e_) {
          if (runtime == null) return;
          e_.printStackTrace();
          drcl.Debug.error(info());
        } catch (Exception e_) {
          e_.printStackTrace();
          drcl.Debug.error(info());
        } catch (SendReceiveException e_) {
          e_.printStackTrace();
          drcl.Debug.error(info());
        }
        currentContext.port = null;
        currentContext.data = null;

        finishing();
        mainContext = DUMMY_CONTEXT;
        // to maintain the ownership of workforce

        if (!RECYCLING) {
          aruntime.remove(this);
          break;
        }

        if (nextTask != null) {
          // error checking
          if (mainContext != null && mainContext != DUMMY_CONTEXT)
            drcl.Debug.systemFatalError("task assigned to occupied thread:" + this);
          mainContext = nextTask;
          nextTask = null;
        }
      } // for(;;)
      drcl.Debug.systemFatalError("Unexpected finish at " + this);
    } catch (WorkerThreadInterruptedException e_) {
      // if (!runtime.resetting) {
      //  e_.printStackTrace();
      //  drcl.Debug.systemFatalError(
      //    "AWorkerThread terminates abnormally, " + this
      //      + "\nManager: " + runtime.diag());
      // }
      // Will releaseAllLocks()/cancelAllWaits() clean up the lock
      //   structure in components?
      // Not if the thread comes across multiple components...
      if (mainContext != null && mainContext.port != null) releaseAllLocks(mainContext.port.host);
      aruntime.remove(this);
    } catch (NullPointerException e_) {
      if (runtime != null) {
        e_.printStackTrace();
        drcl.Debug.error(info());
      }
    }
    runtime = null; // become an orphan
    aruntime = null;
  }