int main(int argc, char **argv)
{
	struct event *eventptr;
	struct msg  msg2give;
	struct pkt  pkt2give;

	int i,j;
	char c;

	int opt;
	int seed;

	//Check for number of arguments
	if(argc != 5){
		fprintf(stderr, "Missing arguments\n");
		printf("Usage: %s -s SEED -w WINDOWSIZE\n", argv[0]);
		return -1;
	}

	/*
	 * Parse the arguments
	 * http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
	 */
	while((opt = getopt(argc, argv,"s:w:")) != -1){
		switch (opt){
		case 's':   if(!isNumber(optarg)){
			fprintf(stderr, "Invalid value for -s\n");
			return -1;
		}
		seed = atoi(optarg);
		break;

		case 'w':   if(!isNumber(optarg)){
			fprintf(stderr, "Invalid value for -w\n");
			return -1;
		}
		WINSIZE = atoi(optarg);
		break;

		case '?':   break;

		default:    printf("Usage: %s -s SEED -w WINDOWSIZE\n", argv[0]);
		return -1;
		}
	}

	init(seed);
	A_init();
	B_init();

	while (1) {
		eventptr = evlist;            /* get next event to simulate */
		if (eventptr==NULL)
			goto terminate;
		evlist = evlist->next;        /* remove this event from event list */
		if (evlist!=NULL)
			evlist->prev=NULL;
		if (TRACE>=2) {
			printf("\nEVENT time: %f,",eventptr->evtime);
			printf("  type: %d",eventptr->evtype);
			if (eventptr->evtype==0)
				printf(", timerinterrupt  ");
			else if (eventptr->evtype==1)
				printf(", fromlayer5 ");
			else
				printf(", fromlayer3 ");
			printf(" entity: %d\n",eventptr->eventity);
		}
		time_local = eventptr->evtime;        /* update time to next event time */
		if (nsim==nsimmax)
			break;                        /* all done with simulation */
		if (eventptr->evtype == FROM_LAYER5 ) {
			generate_next_arrival();   /* set up future arrival */
			/* fill in msg to give with string of same letter */
			j = nsim % 26;
			for (i=0; i<20; i++)
				msg2give.data[i] = 97 + j;
			if (TRACE>2) {
				printf("          MAINLOOP: data given to student: ");
				for (i=0; i<20; i++)
					printf("%c", msg2give.data[i]);
				printf("\n");
			}
			nsim++;
			if (eventptr->eventity == A)
				A_output(msg2give);
			else
				B_output(msg2give);
		}
		else if (eventptr->evtype ==  FROM_LAYER3) {
			pkt2give.seqnum = eventptr->pktptr->seqnum;
			pkt2give.acknum = eventptr->pktptr->acknum;
			pkt2give.checksum = eventptr->pktptr->checksum;
			for (i=0; i<20; i++)
				pkt2give.payload[i] = eventptr->pktptr->payload[i];
			if (eventptr->eventity ==A)      /* deliver packet by calling */
				A_input(pkt2give);            /* appropriate entity */
			else
				B_input(pkt2give);
			free(eventptr->pktptr);          /* free the memory for packet */
		}
		else if (eventptr->evtype ==  TIMER_INTERRUPT) {
			if (eventptr->eventity == A)
				A_timerinterrupt();
			else
				B_timerinterrupt();
		}
		else  {
			printf("INTERNAL PANIC: unknown event type \n");
		}
		free(eventptr);
	}

	terminate:
	//Do NOT change any of the following printfs
	printf(" Simulator terminated at time %f\n after sending %d msgs from layer5\n",time_local,
nsim);

	printf("\n");
	printf("Protocol: GBN\n");
	printf("[PA2]%d packets sent from the Application Layer of Sender A[/PA2]\n", A_application);
	printf("[PA2]%d packets sent from the Transport Layer of Sender A[/PA2]\n", A_transport);
	printf("[PA2]%d packets received at the Transport layer of Receiver B[/PA2]\n", B_transport);
	printf("[PA2]%d packets received at the Application layer of Receiver B[/PA2]\n", 
B_application);
	printf("[PA2]Total time: %f time units[/PA2]\n", time_local);
	printf("[PA2]Throughput: %f packets/time units[/PA2]\n", B_application/time_local);
	return 0;
}
Exemplo n.º 2
0
int main( int argc, char *argv[] )     {
    struct event *eventptr;
    struct msg   msg2give;
    struct pkt   pkt2give;
   
    int          currentEntity;
    int          i,j;
  
//  Do our initialization and any requested by student's Transport Layer 
    CallingArgc = argc;
    CallingArgv = argv;
    init( );
    A_init();
    B_init();
   
    /* ***********************************************************************
      We loop here forever.  During these actions we get an event off the
      event header.  We analyze the action to be performed and go do it.
    *************************************************************************/
    while (TRUE) {
        eventptr = evlist;        // get next event to simulate from Q head 
        if (eventptr==NULL)       //  IF nothing to simulate, we're done  
            break;

        evlist = evlist->next;    // remove this event from event list 
        if (evlist != NULL)       //   and sort out forward and back ptrs
            evlist->prev=NULL;

        // Update simulation time to event time  - by definition, the
        // simulation time is the time of the last event.
        CurrentSimTime = eventptr->evtime;        
        currentEntity = eventptr->eventity;
        // If we've delivered the required number of messages, then we've
        // fullfilled our task and are done.
        if ( NumMsgs4To5 >= MaxMsgsToSimulate )
            break;                            // all done with simulation 
        
        /* *******************************************************************
          Here we've gotten a request to hand data from layer 5 to layer 4.
            Generate the date we want to give to the student.                 
        *********************************************************************/
        if ( eventptr->evtype == FROM_LAYER5 ) {
            // Use the sequence number as starter for the message string
            j = GeneratingSeqNum[currentEntity]++;
            // printf( "%X  %d %d\n", &eventptr, currentEntity, j );
            GetMessageString( currentEntity, j, &(msg2give.data[0]) );
            /*  Always print trace so we know it matches the receive  */
            if ( TraceLevel >= 0 )  {    
                //printf("%c: ", &(EntityLetter[currentEntity]) );
                if ( currentEntity == AEntity )   
                    printf("A: " );
                else
                    printf("B: " );
                printf(" %11.4f,", eventptr->evtime);

                printf("  Layer 5 to 4  Message = ");
                for (i=0; i<MESSAGE_LENGTH; i++) 
                    printf("%c", msg2give.data[i]);
                printf("\n");
            }
            // The simulation will actually end when the requested number of
            // messages has been received back at layer 5.  But there could be
            // many packets in the system, and that total arrival could take
            // a long time.  We want to make sure we down't overwhelm the
            // student code with messages that won't ever be delivered anyway.
            //
            if ( NumMsgs5To4 <= MaxMsgsToSimulate + 3 ) { 
                GenerateNextArrival();           // set up future arrival 
                NumMsgs5To4++;                   // # msgs from layer 5 to 4 
            }
            if ( currentEntity == AEntity )  // Pass the data to layer 4 here 
                A_output(msg2give);  
            else
                B_output(msg2give);  
        }                              /* END of event is from layer 5 */

        /* *******************************************************************
          This is a request to hand data from layer 3 up to student layer 4 
        *********************************************************************/
        else if (eventptr->evtype ==  FROM_LAYER3 ) {
            pkt2give.seqnum   = eventptr->pktptr->seqnum;
            pkt2give.acknum   = eventptr->pktptr->acknum;
            pkt2give.checksum = eventptr->pktptr->checksum;
            for ( i = 0; i < MESSAGE_LENGTH; i++)  
                pkt2give.payload[i] = eventptr->pktptr->payload[i];

            if ( TraceLevel >= 2 )  {     /* Print out trace info if requested */
                if ( eventptr->eventity == AEntity )   
                    printf("A: " );
                else
                    printf("B: " );
                printf(" %11.4f,", eventptr->evtime);

                printf("  Layer 3 to 4  ");
                printf( "Seq/Ack/Check = %d/%d/%d:  ",
                        eventptr->pktptr->seqnum, eventptr->pktptr->acknum,
                        eventptr->pktptr->checksum );
                for (i=0; i<MESSAGE_LENGTH; i++) 
                    printf("%c", eventptr->pktptr->payload[i] );
                printf("\n");
            }
            if (eventptr->eventity == AEntity)   /* deliver packet by calling */
                   A_input(pkt2give);            /* appropriate entity */
            else
                   B_input(pkt2give);
            free(eventptr->pktptr);          /* free the memory for packet */
        }                             /* END of event is from layer 3 */

        /* *******************************************************************
              This is a request for a timer interrupt 
        *********************************************************************/
        else if (eventptr->evtype ==  TIMER_INTERRUPT) {
            if ( TraceLevel >= 2 )  {     /* Print out trace info if requested */
                if ( eventptr->eventity == AEntity )   
                    printf("A: " );
                else
                    printf("B: " );
                printf(" %f,", eventptr->evtime);
                printf("  Timer Interrupt\n");
            }
            if (eventptr->eventity == AEntity) 
                A_timerinterrupt();
            else
                B_timerinterrupt();
        }                             /* END of event is interrupt request */
        else  {
            printf("INTERNAL PANIC: unknown event type \n");
        }
        free(eventptr);
    }                       // End of while

terminate:
    printf("\n\nSimulator terminated at time %f\n after receiving %d msgs at layer5\n",
                  CurrentSimTime, NumMsgs4To5 );
    printf( "Simulator Analysis:\n");
    printf( "  Number of messages sent from 5 to 4: %d\n", NumMsgs5To4 );
    printf( "  Number of messages received at Layer 5, side A: %d\n",
                  ExpectedSeqNum[0]  );
    printf( "  Number of messages received at Layer 5, side B: %d\n",
                  ExpectedSeqNum[1]  );
    printf( "  Number of messages incorrectly received at layer 5: %d\n", 
                  NumMsgs5To4WithErr );
    printf( "  Number of packets entering the network: %d\n", NumMsgs4To3 );
    printf( "  Average number of packets already in network: %6.3f\n",
               (double)NumSimutaneousMsgs /  (double)NumMsgs4To3 );
    printf( "  Number of packets that the network lost: %d\n", NumMsgsLost );
    printf( "  Number of packets that the network corrupted: %d\n", 
                   NumMsgsCorrupt );
    printf( "  Number of packets that the network put out of order: %d\n", 
                   NumMsgsOutOfOrder );
    return 0;
}
Exemplo n.º 3
0
int _tmain(int argc, _TCHAR* argv[])
{
   struct event *eventptr;
   struct msg  msg2give;
   struct pkt  pkt2give;
   
   int i,j;
//   char c; 
  
   init();
   A_init();
   B_init();
   
   while (1) {
        eventptr = evlist;            /* get next event to simulate */
        if (eventptr==NULL)
           goto terminate;
        evlist = evlist->next;        /* remove this event from event list */
        if (evlist!=NULL)
           evlist->prev=NULL;
        if (TRACE>=2) {
           printf("\nEVENT time: %f,",eventptr->evtime);
           printf("  type: %d",eventptr->evtype);
           if (eventptr->evtype==0)
	       printf(", timerinterrupt  ");
             else if (eventptr->evtype==1)
               printf(", fromlayer5 ");
             else
	     printf(", fromlayer3 ");
           printf(" entity: %d\n",eventptr->eventity);
           }
        time = eventptr->evtime;        /* update time to next event time */
        if (nsim==nsimmax)
	  break;                        /* all done with simulation */
        if (eventptr->evtype == FROM_LAYER5 ) {
            generate_next_arrival();   /* set up future arrival */
            /* fill in msg to give with string of same letter */    
            j = nsim % 26; 
            for (i=0; i<20; i++)  
               msg2give.data[i] = 97 + j;
            if (TRACE>2) {
               printf("          MAINLOOP: data given to student: ");
                 for (i=0; i<20; i++) 
                  printf("%c", msg2give.data[i]);
               printf("\n");
	     }
            nsim++;
            if (eventptr->eventity == A) 
               A_output(msg2give);  
             else
               B_output(msg2give);  
            }
          else if (eventptr->evtype ==  FROM_LAYER3) {
            pkt2give.seqnum = eventptr->pktptr->seqnum;
            pkt2give.acknum = eventptr->pktptr->acknum;
            pkt2give.checksum = eventptr->pktptr->checksum;
            for (i=0; i<20; i++)  
                pkt2give.payload[i] = eventptr->pktptr->payload[i];
	    if (eventptr->eventity ==A)      /* deliver packet by calling */
   	       A_input(pkt2give);            /* appropriate entity */
            else
   	       B_input(pkt2give);
	    delete(eventptr->pktptr);          /* free the memory for packet */
            }
          else if (eventptr->evtype ==  TIMER_INTERRUPT) {
            if (eventptr->eventity == A) 
	       A_timerinterrupt();
             else
	       B_timerinterrupt();
             }
          else  {
	     printf("INTERNAL PANIC: unknown event type \n");
             }
        delete(eventptr);
        }

terminate:
   printf(" Simulator terminated at time %f\n after sending %d msgs from layer5\n",time,nsim);
   return 0;
}