/*
 * sim_router.h
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1996,1993,1991,1990  Arizona Board of Regents
 *
 */

#define TRACE_INOUT(itp, len, t) {                                             \
  (itp)->packetCnt++;                                                          \
  (itp)->byteCnt += (len);                                                     \
  if (t > (itp)->nextTime) {                                                   \
    (itp)->array[(itp)->arrayNum] = ((itp)->sum*(itp)->m_factor) /             \
                                    (itp)->d_factor;                           \
    while ((itp)->nextTime <= t)                                               \
      (itp)->nextTime += (itp)->timeInc, (itp)->arrayNum++;                    \
    if ((itp)->arrayNum >= ARRAY_SIZE) {                                       \
      traceSaveAll();                                                          \
      printf("\nERROR: arrayNum >= ARRAY_SIZE in router, increase timeInc\n"); \
      printf("\n  *****  End of Simulation  *****\n");                         \
      exit(0);                                                                 \
    }                                                                          \
    (itp)->sum = (len);                                                        \
  }                                                                            \
  else                                                                         \
    (itp)->sum += (len);                                                       \
}

#define TRACE_QUEUE(mtp, qcnt, t) {                                            \
  if (t > (mtp)->nextTime) {                                                   \
    (mtp)->maxArray[(mtp)->arrayNum] = (mtp)->max/(mtp)->div;                  \
    (mtp)->minArray[(mtp)->arrayNum] = (mtp)->min/(mtp)->div;                  \
    (mtp)->sum += ((qcnt) + 1)*(t - (mtp)->otime);                             \
    (mtp)->cnt += (t - (mtp)->otime);                                          \
    (mtp)->avgArray[(mtp)->arrayNum] = ((mtp)->sum +                           \
				       (mtp)->cnt/2)/(mtp)->cnt/(mtp)->div;    \
    (mtp)->sum = (mtp)->cnt = 0;                                               \
    (mtp)->otime = t;                                                          \
    while ((mtp)->nextTime <= t)                                               \
      (mtp)->nextTime += (mtp)->timeInc, (mtp)->arrayNum++;                    \
    if ((mtp)->arrayNum >= ARRAY_SIZE) {                                       \
      traceSaveAll();                                                          \
      printf("\nERROR: arrayNum >= ARRAY_SIZE in router, increase timeInc\n"); \
      printf("\n  *****  End of Simulation  *****\n");                         \
      exit(0);                                                                 \
    }                                                                          \
    (mtp)->max = (mtp)->min = (qcnt);                                          \
  }                                                                            \
  else {                                                                       \
    if ((mtp)->max < (qcnt))                                                   \
      (mtp)->max = (qcnt);                                                     \
    if ((mtp)->min > (qcnt))                                                   \
      (mtp)->min = (qcnt);                                                     \
    (mtp)->sum += ((qcnt)+1)*(t - (mtp)->otime);                               \
    (mtp)->cnt += (t - (mtp)->otime);                                          \
    (mtp)->otime = t;                                                          \
  }                                                                            \
} 



