traffic ITraffic {
  void setup (char fn[]) {
	Traffic::setup (SCL_off);
  };
};

#define traceToTIME 1000	/* 1000 nanoseconds per microsecond */

process ITGen {
  ITraffic *ITP;
  long MLength, From, To;
  TIME inputTime = 0;
  TIME arrivalTime = 0, interArrival;
  int numArrivals = 0;
  BIG numInputBits = 0;
  TIME MIT;

  void setup (ITraffic *tp) {
    ITP = tp;
    inputTime = 0;
    arrivalTime = 0;
    numArrivals = 0;
    numInputBits = 0;
	MIT = TTime * ::mit;
  };

  states { FindStart, Wait, Generate};

  perform {

	state FindStart:		/* find beginning of input */
		char line[1024];
		while(strcmp(fgets(line, sizeof(line), stdin), "TRACE\n"))
			cout << line;
		proceed(Wait);

	state Wait:
		if(ASCII) {
			char c;
			interArrival = traceToTIME * CReadInt(stdin);
			MLength = 8 * CReadInt(stdin);	/* input in bytes */
			From = CReadInt(stdin);
			To = CReadInt(stdin);
		} else {
			unsigned short foo[3];
			fread(foo, sizeof(foo), 1, stdin);
			interArrival = traceToTIME * (long)foo[0];
			MLength = 8 * foo[1];
			From = foo[2] >> 8;
			To = foo[2] & 255;
		}
		++numArrivals;
		inputTime += interArrival;
		numInputBits += MLength;

		if(!TraceArrivals)
			interArrival = tRndPoisson(MIT);
		if(!TraceSizes)
			MLength = mle;
		if(!TraceWho)
		{
			From = toss(NNodes);
			To = toss(NNodes);	// don't care if From == To
		}

		arrivalTime += interArrival;
		if(feof(stdin)) terminate();
		Assert (arrivalTime >= Time, "Nonincreasing arrival time");
		Timer->wait (arrivalTime - Time, Generate);

	state Generate:
		if(ITP->genMSG (From, To, MLength))	/* NULL if message was ignored */
		{
			/* we divide by TTime just in an attempt to avoid overflow in the
			** sum, not because it has any physical meaning.  Read: KLUGE.
			*/
			queueSizeCum[From] += queueSize[From] * (Time - prevQueueUpdate[From]) / TTime;
			prevQueueUpdate[From] = Time;
			++queueSize[From];
		}
		proceed Wait;
  };
};

