void generate_next_arrival() { double x,log(),ceil(); struct event *evptr; if (TRACE>2) printf(" GENERATE NEXT ARRIVAL: creating new arrival\n"); x = lambda*jimsrand()*2; /* x is uniform on [0,2*lambda] */ /* having mean of lambda */ evptr = (struct event *)malloc(sizeof(struct event)); evptr->evtime = time + x; evptr->evtype = FROM_LAYER5; if (BIDIRECTIONAL && (jimsrand()>0.5) ) evptr->eventity = B; else evptr->eventity = A; insertevent(evptr); }
int init(void) /* initialize the simulator */ { int i; float sum, avg; float jimsrand(); printf("----- Stop and Wait Network Simulator Version 1.1 -------- \n\n"); printf("Enter the number of messages to simulate: "); scanf_s("%d",&nsimmax); printf("Enter packet loss probability [enter 0.0 for no loss]:"); scanf_s("%f",&lossprob); printf("Enter packet corruption probability [0.0 for no corruption]:"); scanf_s("%f",&corruptprob); printf("Enter average time between messages from sender's layer5 [ > 0.0]:"); scanf_s("%f",&lambda); printf("Enter TRACE:"); scanf_s("%d",&TRACE); srand(9999); /* init random number generator */ sum = 0.0; /* test random number generator for students */ for (i=0; i<1000; i++) sum=sum+jimsrand(); /* jimsrand() should be uniform in [0,1] */ avg = (float)(sum/1000.0); if (avg < 0.25 || avg > 0.75) { printf("It is likely that random number generation on your machine\n" ); printf("is different from what this emulator expects. Please take\n"); printf("a look at the routine jimsrand() in the emulator code. Sorry. \n"); return -9; } ntolayer3 = 0; nlost = 0; ncorrupt = 0; time=0.0; /* initialize time to 0.0 */ generate_next_arrival(); /* initialize event list */ return 0; }
/************************** TOLAYER3 ***************/ void tolayer3(int AorB,struct pkt packet) { struct pkt *mypktptr; struct event *evptr,*q; ////char *malloc(); float lastime, x, jimsrand(); int i; ntolayer3++; /* simulate losses: */ if (jimsrand() < lossprob) { nlost++; if (TRACE>0) printf(" TOLAYER3: packet being lost\n"); return; } /* make a copy of the packet student just gave me since he/she may decide */ /* to do something with the packet after we return back to him/her */ mypktptr = (struct pkt *)malloc(sizeof(struct pkt)); mypktptr->seqnum = packet.seqnum; mypktptr->acknum = packet.acknum; mypktptr->checksum = packet.checksum; for (i=0; i<20; i++) mypktptr->payload[i] = packet.payload[i]; if (TRACE>2) { printf(" TOLAYER3: seq: %d, ack %d, check: %d ", mypktptr->seqnum, mypktptr->acknum, mypktptr->checksum); for (i=0; i<20; i++) printf("%c",mypktptr->payload[i]); printf("\n"); } /* create future event for arrival of packet at the other side */ evptr = (struct event *)malloc(sizeof(struct event)); evptr->evtype = FROM_LAYER3; /* packet will pop out from layer3 */ evptr->eventity = (AorB+1) % 2; /* event occurs at other entity */ evptr->pktptr = mypktptr; /* save ptr to my copy of packet */ /* finally, compute the arrival time of packet at the other end. medium can not reorder, so make sure packet arrives between 1 and 10 time units after the latest arrival time of packets currently in the medium on their way to the destination */ lastime = time_local; /* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */ for (q=evlist; q!=NULL ; q = q->next) if ( (q->evtype==FROM_LAYER3 && q->eventity==evptr->eventity) ) lastime = q->evtime; evptr->evtime = lastime + 1 + 9*jimsrand(); /* simulate corruption: */ if (jimsrand() < corruptprob) { ncorrupt++; if ( (x = jimsrand()) < .75) mypktptr->payload[0]='Z'; /* corrupt payload */ else if (x < .875) mypktptr->seqnum = 999999; else mypktptr->acknum = 999999; if (TRACE>0) printf(" TOLAYER3: packet being corrupted\n"); } if (TRACE>2) printf(" TOLAYER3: scheduling arrival on other side\n"); insertevent(evptr); }