12 using namespace forest;
56 int main(
int argc,
char *argv[]) {
57 ipa_t myIpAdr, rtrIpAdr;
58 int repeatFlag, delta, finTime;
63 sscanf(argv[3],
"%d", &repeatFlag) != 1 ||
64 sscanf(argv[4],
"%d", &delta) != 1 ||
65 sscanf(argv[5],
"%d", &finTime) != 1)
66 fatal(
"usage: fHost myIpAdr routerIpAdr repeatCnt "
69 Host host(myIpAdr,rtrIpAdr);
70 if (!host.
init()) fatal(
"Host:: initialization failure");
71 host.run((repeatFlag ?
true :
false), delta, 1000000*finTime);
77 Host::Host(ipa_t mipa, ipa_t ripa) : myIpAdr(mipa) , rtrIpAdr(ripa) {
82 Host::~Host() {
delete ps; }
96 void Host::run(
bool repeatFlag, uint32_t delta, uint32_t finishTime) {
101 int i, pause, cnt, np;
103 const int MAXEVENTS = 200;
104 struct {
bool sendFlag; uint32_t time;
int pkt;} events[MAXEVENTS];
106 int nRcvd = 0;
int nSent = 0;
108 struct {
int pause, p, cnt, iter; } pkt[nPkts+1];
114 if (px == Null) fatal(
"Host::run: too many packets");
115 if (!readPacket(px,pause,cnt))
break;
116 pkt[np].pause = pause;
119 }
else if (repeatFlag) {
120 if (pause + np < 0) pause = -np;
121 pkt[np].cnt = cnt; pkt[np++].iter = cnt;
131 struct timeval ct, pt;
132 if (gettimeofday(&ct, NULL) < 0)
133 fatal(
"Host::run: gettimeofday failure");
135 now = 0; nextTime = 1000000;
136 while (now <= finishTime) {
146 if (evCnt < MAXEVENTS) {
147 events[evCnt].sendFlag =
false;
148 events[evCnt].time = now;
149 events[evCnt].pkt = px;
157 if (i < np && now >= nextTime) {
161 if (evCnt < MAXEVENTS) {
162 pktx px1 = ps->
clone(pkt[i].p);
163 events[evCnt].sendFlag =
true;
164 events[evCnt].time = now;
165 events[evCnt].pkt = px1;
169 while (i < np && pkt[i].pause < 0) {
170 if (pkt[i].cnt <= 0) {
172 }
else if (pkt[i].iter <= 0) {
173 pkt[i].iter = pkt[i].cnt;
180 if (i < np) nextTime += max(delta,pkt[i].pause);
184 if (gettimeofday(&ct,Null) < 0)
185 fatal(
"Host::run: gettimeofday failure");
186 now += (ct.tv_sec == pt.tv_sec ?
187 ct.tv_usec - pt.tv_usec :
188 ct.tv_usec + (1000000 - pt.tv_usec)
190 if (!didNothing)
continue;
193 if (now + 500 >= nextTime)
continue;
194 struct timespec sleeptime, rem;
195 sleeptime.tv_sec = 0;
196 if (nextTime - (now + 500) < 2500)
197 sleeptime.tv_nsec = (nextTime - (now + 500))*1000;
199 sleeptime.tv_nsec = 2500000;
200 nanosleep(&sleeptime,&rem);
203 if (gettimeofday(&ct,Null) < 0)
204 fatal(
"Host::run: gettimeofday failure");
205 now += (ct.tv_sec == pt.tv_sec ?
206 ct.tv_usec - pt.tv_usec :
207 ct.tv_usec + (1000000 - pt.tv_usec)
212 for (
int i = 0; i < evCnt; i++) {
213 if (events[i].sendFlag)
217 cout << setw(2) << setw(8) << events[i].time <<
"] ";
224 cout << nRcvd <<
" packets received, " << nSent <<
" packets sent, ";
227 bool Host::readPacket(pktx px,
int& pause,
int& cnt) {
237 bool success = p.
read(cin);
242 void Host::send(pktx px) {
247 if (rv == -1) fatal(
"Host::send: failure in sendto");
250 int Host::receive() {
254 pktx px = ps->
alloc();
255 if (px == Null)
return Null;
258 ipa_t remoteIp; ipp_t remotePort;
260 remoteIp, remotePort);
262 if (errno == EWOULDBLOCK) {
263 ps->
free(px);
return Null;
265 fatal(
"Host::receive: error in recvfrom call");