tboy501
asked on
How to implement an ARQ stop and Wait Protocol
I have this code to turn in next 3 days it is simply supposed to be a sender A transmitting packets to a reciever B which in turn acknowledges it( or nack) it uses checksums and sequence and ack nos for error control. there is an upper layer wch hands the packet down to A, A sends the packet thru a lower layer to B, and B in turn hands an error free packet back to the upper layer.The problem is that we've been given part of the code to folow and our final code must continue from there in other words our program must model around the given format, wch i ve found very difficult to understand. Pls i REALLY NEED HELP FAST!
PROJECT 1
In your report, Please include following in your report:
-flowchart of your implementation
-brief description of the functions that you write
-an output which demonstrates at least 10 successfull frame transmissions A to B.
-printout of your section of the source code (only portion that you write, not the whole source code).
-Please do not use compiler specific instructions.
---------------
Overview
In this laboratory programming assignment, you will be writing the sending and receiving transport-level code for implementing a simple reliable data transfer protocol. There are two versions of this lab, the Alternating-Bit-Protocol version and the  Go-Back-N version. This lab should be fun since your implementation will differ very little from what would be required in a real-world situation.
Since you probably don't have standalone machines (with an OS that you can modify), your code will have to execute in a simulated hardware/software environment. However, the programming interface provided to your routines, i.e., the code that would call your entities from above and from below is very close to what is done in an actual UNIX environment. (Indeed, the software interfaces described in this programming assignment are much more realistic that the infinite loop senders and receivers that many texts describe). Stopping/starting of timers are also simulated, and timer interrupts will cause your timer handling routine to be activated.
The routines you will write
The procedures you will write are for the sending entity (A) and the receiving entity (B). Only unidirectional transfer of data (from A to B) is required. Of course, the B side will have to send packets to A to acknowledge (positively or negatively) receipt of data. Your routines are to be implemented in the form of the procedures described below. These procedures will be called by (and will call) procedures that I have written which emulate a network environment. The overall structure of the environment is shown in Figure Lab.3-1 (structure of the emulated environment):
The unit of data passed between the upper layers and your protocols is a message, which is declared as:
struct msg {
 char data[20];
 };
This declaration, and all other data structure and emulator routines, as well as stub routines (i.e., those you are to complete) are in the file, prog2.c, described later. Your sending entity will thus receive data in 20-byte chunks from layer5; your receiving entity should deliver 20-byte chunks of correctly received data to layer5 at the receiving side.
The unit of data passed between your routines and the network layer is the packet, which is declared as:
struct pkt {
  int seqnum;
  int acknum;
  int checksum;
  char payload[20];
  };
Your routines will fill in the payload field from the message data passed down from layer5. The other packet fields will be used by your protocols to insure reliable delivery, as we've seen in class.
The routines you will write are detailed below. As noted above, such procedures in real-life would be part of the operating system, and would be called by other procedures in the operating system.
  * A_output(message), where message is a structure of type msg, containing data to be sent to the B-side. This routine will be called whenever the upper layer at the sending side (A) has a message to send. It is the job of your protocol to insure that the data in such a message is delivered in-order, and correctly, to the receiving side upper layer.
  * A_input(packet), where packet is a structure of type pkt. This routine will be called whenever a packet sent from the B-side (i.e., as a result of a tolayer3() being done by a B-side procedure) arrives at the A-side. packet is the (possibly corrupted) packet sent from the B-side.
  * A_timerinterrupt()  This routine will be called when A's timer expires (thus generating a timer interrupt). You'll probably want to use this routine to control the retransmission of packets. See starttimer() and stoptimer() below for how the timer is started and stopped.
  * A_init() This routine will be called once, before any of your other A-side routines are called. It can be used to do any required initialization.
  * B_input(packet),where packet is a structure of type pkt. This routine will be called whenever a packet sent from the A-side (i.e., as a result of a tolayer3() being done by a A-side procedure) arrives at the B-side. packet is the (possibly corrupted) packet sent from the A-side.
  * B_init() This routine will be called once, before any of your other B-side routines are called. It can be used to do any required initialization.
Software Interfaces
The procedures described above are the ones that you will write. I have written the following routines which can be called by your routines:
  * starttimer(calling_entity, increment) , where calling_entity is either 0 (for starting the A-side timer) or 1 (for starting the B side timer), and increment is a float value indicating the amount of time that will pass before the timer interrupts. A's timer should only be started (or stopped) by A-side routines, and similarly for the B-side timer. To give you an idea of the appropriate increment value to use: a packet sent into the network takes an average of 5 time units to arrive at the other side when there are no other messages in the medium.
  * stoptimer(calling_entity), where calling_entity is either 0 (for stopping the A-side timer) or 1 (for stopping the B side timer).
  * tolayer3(calling_entity,pa cket), where calling_entity is either 0 (for the A-side send) or 1 (for the B side send), and packet is a structure of type pkt. Calling this routine will cause the packet to be sent into the network, destined for the other entity.
  * tolayer5(calling_entity,me ssage), where calling_entity is either 0 (for A-side delivery to layer 5) or 1 (for B-side delivery to layer 5), and message is a structure of type msg. With unidirectional data transfer, you would only be calling this with calling_entity equal to 1 (delivery to the B-side). Calling this routine will cause data to be passed up to layer 5.
The simulated network environment
A call to procedure tolayer3() sends packets into the medium (i.e., into the network layer). Your procedures A_input() and B_input() are called when a packet is to be delivered from the medium to your protocol layer.
The medium is capable of corrupting and losing packets. It will not reorder packets. When you compile your procedures and my procedures together and run the resulting program, you will be asked to specify values regarding the simulated network environment:
  * Number of messages to simulate. My emulator (and your routines) will stop as soon as this number of messages have been passed down from layer 5, regardless of whether or not all of the messages have been correctly delivered. Thus, you need not worry about undelivered or unACK'ed messages still in your sender when the emulator stops. Note that if you set this value to 1, your program will terminate immediately, before the message is delivered to the other side. Thus, this value should always be greater than 1.
  * Loss. You are asked to specify a packet loss probability. A value of 0.1 would mean that one in ten packets (on average) are lost.
  * Corruption. You are asked to specify a packet loss probability. A value of 0.2 would mean that one in five packets (on average) are corrupted. Note that the contents of payload, sequence, ack, or checksum fields can be corrupted. Your checksum should thus include the data, sequence, and ack fields.
  * Tracing. Setting a tracing value of 1 or 2 will print out useful information about what is going on inside the emulation (e.g., what's happening to packets and timers). A tracing value of 0 will turn this off. A tracing value greater than 2 will display all sorts of odd messages that are for my own emulator-debugging purposes. A tracing value of 2 may be helpful to you in debugging your code. You should keep in mind that real implementors do not have underlying networks that provide such nice information about what is going to happen to their packets!
  * Average time between messages from sender's layer5. You can set this value to any non-zero, positive value. Note that the smaller the value you choose, the faster packets will be be arriving to your sender.
The Alternating-Bit-Protocol Version of this lab.
You are to write the procedures, A_output(),A_input(),A_tim erinterrup t(),A_init (),B_input (), and B_init() which together will implement a stop-and-wait (i.e., the alternating bit protocol, which we referred to as rdt3.0 in the text) unidirectional transfer of data from the A-side to the B-side. Your protocol should use both ACK and NACK messages.
You should choose a very large value for the average time between messages from sender's layer5, so that your sender is never called while it still has an outstanding, unacknowledged message it is trying to send to the receiver. I'd suggest you choose a value of 1000. You should also perform a check in your sender to make sure that when A_output() is called, there is no message currently in transit. If there is, you can simply ignore (drop) the data being passed to the A_output() routine.
You should put your procedures in a file called prog2.c. You will need the initial version of this file, containing the emulation routines we have writen for you, and the stubs for your procedures. You can obtain this program from http://gaia.cs.umass.edu/kurose/transport/prog2.c. or local copy if link does not work(local copy is better to start).
This lab can be completed on any machine supporting C. It makes no use of UNIX features. (You can simply  copy the prog2.c file to whatever machine and OS you choose).
We recommend that you should hand in a code listing, a design document, and sample output. For your sample output, your procedures might print out a message whenever an event occurs at your sender or receiver (a message/packet arrival, or a timer interrupt) as well as any action taken in response. You might want to hand in output for a run up to the point (approximately) when 10 messages have been ACK'ed correctly at the receiver, a loss probability of 0.1, and a corruption probability of 0.3, and a trace level of 2. You might want to annotate your printout with a colored pen showing how your protocol correctly recovered from packet loss and corruption.
Make sure you read the "helpful hints" for this lab following the description of the Go_Back-N version of this lab.
# START SIMPLE. Set the probabilities of loss and corruption to zero and test out your routines. Better yet, design and implement your procedures for the case of no loss and no corruption, and get them working first. Then handle the case of one of these probabilities being non-zero, and then finally both being non-zero.
# Debugging. We'd recommend that you set the tracing level to 2 and put LOTS of printf's in your code while your debugging your procedures.
# Random Numbers. The emulator generates packet loss and errors using a random number generator. Our past experience is that random number generators can vary widely from one machine to another. You may need to modify the random number generation code in the emulator we have suplied you. Our emulation routines have a test to see if the random number generator on your machine will work with our code. If you get an error message:
  It is likely that random number generation on your machine is different from what this emulator expects. Please take a look at the routine jimsrand() in the emulator code. Sorry.
then you'll know you'll need to look at how random numbers are generated in the routine jimsrand(); see the comments in that routine.
PROJECT 1
In your report, Please include following in your report:
-flowchart of your implementation
-brief description of the functions that you write
-an output which demonstrates at least 10 successfull frame transmissions A to B.
-printout of your section of the source code (only portion that you write, not the whole source code).
-Please do not use compiler specific instructions.
---------------
Overview
In this laboratory programming assignment, you will be writing the sending and receiving transport-level code for implementing a simple reliable data transfer protocol. There are two versions of this lab, the Alternating-Bit-Protocol version and the  Go-Back-N version. This lab should be fun since your implementation will differ very little from what would be required in a real-world situation.
Since you probably don't have standalone machines (with an OS that you can modify), your code will have to execute in a simulated hardware/software environment. However, the programming interface provided to your routines, i.e., the code that would call your entities from above and from below is very close to what is done in an actual UNIX environment. (Indeed, the software interfaces described in this programming assignment are much more realistic that the infinite loop senders and receivers that many texts describe). Stopping/starting of timers are also simulated, and timer interrupts will cause your timer handling routine to be activated.
The routines you will write
The procedures you will write are for the sending entity (A) and the receiving entity (B). Only unidirectional transfer of data (from A to B) is required. Of course, the B side will have to send packets to A to acknowledge (positively or negatively) receipt of data. Your routines are to be implemented in the form of the procedures described below. These procedures will be called by (and will call) procedures that I have written which emulate a network environment. The overall structure of the environment is shown in Figure Lab.3-1 (structure of the emulated environment):
The unit of data passed between the upper layers and your protocols is a message, which is declared as:
struct msg {
 char data[20];
 };
This declaration, and all other data structure and emulator routines, as well as stub routines (i.e., those you are to complete) are in the file, prog2.c, described later. Your sending entity will thus receive data in 20-byte chunks from layer5; your receiving entity should deliver 20-byte chunks of correctly received data to layer5 at the receiving side.
The unit of data passed between your routines and the network layer is the packet, which is declared as:
struct pkt {
  int seqnum;
  int acknum;
  int checksum;
  char payload[20];
  };
Your routines will fill in the payload field from the message data passed down from layer5. The other packet fields will be used by your protocols to insure reliable delivery, as we've seen in class.
The routines you will write are detailed below. As noted above, such procedures in real-life would be part of the operating system, and would be called by other procedures in the operating system.
  * A_output(message), where message is a structure of type msg, containing data to be sent to the B-side. This routine will be called whenever the upper layer at the sending side (A) has a message to send. It is the job of your protocol to insure that the data in such a message is delivered in-order, and correctly, to the receiving side upper layer.
  * A_input(packet), where packet is a structure of type pkt. This routine will be called whenever a packet sent from the B-side (i.e., as a result of a tolayer3() being done by a B-side procedure) arrives at the A-side. packet is the (possibly corrupted) packet sent from the B-side.
  * A_timerinterrupt()  This routine will be called when A's timer expires (thus generating a timer interrupt). You'll probably want to use this routine to control the retransmission of packets. See starttimer() and stoptimer() below for how the timer is started and stopped.
  * A_init() This routine will be called once, before any of your other A-side routines are called. It can be used to do any required initialization.
  * B_input(packet),where packet is a structure of type pkt. This routine will be called whenever a packet sent from the A-side (i.e., as a result of a tolayer3() being done by a A-side procedure) arrives at the B-side. packet is the (possibly corrupted) packet sent from the A-side.
  * B_init() This routine will be called once, before any of your other B-side routines are called. It can be used to do any required initialization.
Software Interfaces
The procedures described above are the ones that you will write. I have written the following routines which can be called by your routines:
  * starttimer(calling_entity,
  * stoptimer(calling_entity),
  * tolayer3(calling_entity,pa
  * tolayer5(calling_entity,me
The simulated network environment
A call to procedure tolayer3() sends packets into the medium (i.e., into the network layer). Your procedures A_input() and B_input() are called when a packet is to be delivered from the medium to your protocol layer.
The medium is capable of corrupting and losing packets. It will not reorder packets. When you compile your procedures and my procedures together and run the resulting program, you will be asked to specify values regarding the simulated network environment:
  * Number of messages to simulate. My emulator (and your routines) will stop as soon as this number of messages have been passed down from layer 5, regardless of whether or not all of the messages have been correctly delivered. Thus, you need not worry about undelivered or unACK'ed messages still in your sender when the emulator stops. Note that if you set this value to 1, your program will terminate immediately, before the message is delivered to the other side. Thus, this value should always be greater than 1.
  * Loss. You are asked to specify a packet loss probability. A value of 0.1 would mean that one in ten packets (on average) are lost.
  * Corruption. You are asked to specify a packet loss probability. A value of 0.2 would mean that one in five packets (on average) are corrupted. Note that the contents of payload, sequence, ack, or checksum fields can be corrupted. Your checksum should thus include the data, sequence, and ack fields.
  * Tracing. Setting a tracing value of 1 or 2 will print out useful information about what is going on inside the emulation (e.g., what's happening to packets and timers). A tracing value of 0 will turn this off. A tracing value greater than 2 will display all sorts of odd messages that are for my own emulator-debugging purposes. A tracing value of 2 may be helpful to you in debugging your code. You should keep in mind that real implementors do not have underlying networks that provide such nice information about what is going to happen to their packets!
  * Average time between messages from sender's layer5. You can set this value to any non-zero, positive value. Note that the smaller the value you choose, the faster packets will be be arriving to your sender.
The Alternating-Bit-Protocol Version of this lab.
You are to write the procedures, A_output(),A_input(),A_tim
You should choose a very large value for the average time between messages from sender's layer5, so that your sender is never called while it still has an outstanding, unacknowledged message it is trying to send to the receiver. I'd suggest you choose a value of 1000. You should also perform a check in your sender to make sure that when A_output() is called, there is no message currently in transit. If there is, you can simply ignore (drop) the data being passed to the A_output() routine.
You should put your procedures in a file called prog2.c. You will need the initial version of this file, containing the emulation routines we have writen for you, and the stubs for your procedures. You can obtain this program from http://gaia.cs.umass.edu/kurose/transport/prog2.c. or local copy if link does not work(local copy is better to start).
This lab can be completed on any machine supporting C. It makes no use of UNIX features. (You can simply  copy the prog2.c file to whatever machine and OS you choose).
We recommend that you should hand in a code listing, a design document, and sample output. For your sample output, your procedures might print out a message whenever an event occurs at your sender or receiver (a message/packet arrival, or a timer interrupt) as well as any action taken in response. You might want to hand in output for a run up to the point (approximately) when 10 messages have been ACK'ed correctly at the receiver, a loss probability of 0.1, and a corruption probability of 0.3, and a trace level of 2. You might want to annotate your printout with a colored pen showing how your protocol correctly recovered from packet loss and corruption.
Make sure you read the "helpful hints" for this lab following the description of the Go_Back-N version of this lab.
# START SIMPLE. Set the probabilities of loss and corruption to zero and test out your routines. Better yet, design and implement your procedures for the case of no loss and no corruption, and get them working first. Then handle the case of one of these probabilities being non-zero, and then finally both being non-zero.
# Debugging. We'd recommend that you set the tracing level to 2 and put LOTS of printf's in your code while your debugging your procedures.
# Random Numbers. The emulator generates packet loss and errors using a random number generator. Our past experience is that random number generators can vary widely from one machine to another. You may need to modify the random number generation code in the emulator we have suplied you. Our emulation routines have a test to see if the random number generator on your machine will work with our code. If you get an error message:
  It is likely that random number generation on your machine is different from what this emulator expects. Please take a look at the routine jimsrand() in the emulator code. Sorry.
then you'll know you'll need to look at how random numbers are generated in the routine jimsrand(); see the comments in that routine.
#include <stdio.h>
#include <stdlib.h>
/* ******************************************************************
ALTERNATING BIT AND GO-BACK-N NETWORK EMULATOR: VERSION 1.1 J.F.Kurose
This code should be used for PA2, unidirectional or bidirectional
data transfer protocols (from A to B. Bidirectional transfer of data
is for extra credit and is not required). Network properties:
- one way network delay averages five time units (longer if there
are other messages in the channel for GBN), but can be larger
- packets can be corrupted (either the header or the data portion)
or lost, according to user-defined probabilities
- packets will be delivered in the order in which they were sent
(although some can be lost).
**********************************************************************/
#define BIDIRECTIONAL 0 /* change to 1 if you're doing extra credit */
/* and write a routine called B_output */
/* a "msg" is the data unit passed from layer 5 (teachers code) to layer */
/* 4 (students' code). It contains the data (characters) to be delivered */
/* to layer 5 via the students transport level protocol entities. */
struct msg {
char data[20];
};
/* a packet is the data unit passed from layer 4 (students code) to layer */
/* 3 (teachers code). Note the pre-defined packet structure, which all */
/* students must follow. */
struct pkt {
int seqnum;
int acknum;
int checksum;
char payload[20];
};
/********* STUDENTS WRITE THE NEXT SEVEN ROUTINES *********/
int seq_A;
struct pkt pkt_A2B;
/* called from layer 5, passed the data to be sent to other side */
A_output(message)
struct msg message;
{
int i;
int checksumA=0;
printf("Now we are in A_output, seq_a= %d\n", seq_A);
seq_A++ ;
printf("now transfering message to packet\n");
for ( i=0; i< 20; i++)
{
pkt_A2B.payload[i]=message.data[i];
checksumA+=(int)(pkt_A2B.payload[i]);
printf("%c", pkt_A2B.payload[i]);
}
printf("\n character %c is in ascii %d check sum is %d transfer complete\n", pkt_A2B.payload[0], pkt_A2B.payload[0], checksumA);
}
B_output(message) /* need be completed only for extra credit */
struct msg message;
{
/*do nothing */
}
/* called from layer 3, when a packet arrives for layer 4 */
A_input(packet)
struct pkt packet;
{
/* stop timer*/
stoptimer(0);
/* check if ack is ok*/
/*check if ack no == send no */
/*if not resent last packet */
/*if yes increment seq_no and exit */
}
/* called when A's timer goes off */
A_timerinterrupt()
{
}
/* the following routine will be called once (only) before any other */
/* entity A routines are called. You can use it to do any initialization */
A_init()
{
}
/* Note that with simplex transfer from a-to-B, there is no B_output() */
/* called from layer 3, when a packet arrives for layer 4 at B*/
B_input(packet)
struct pkt packet;
{
}
/* called when B's timer goes off */
B_timerinterrupt()
{
}
/* the following rouytine will be called once (only) before any other */
/* entity B routines are called. You can use it to do any initialization */
B_init()
{
}
/*****************************************************************
***************** NETWORK EMULATION CODE STARTS BELOW ***********
The code below emulates the layer 3 and below network environment:
- emulates the tranmission and delivery (possibly with bit-level corruption
and packet loss) of packets across the layer 3/4 interface
- handles the starting/stopping of a timer, and generates timer
interrupts (resulting in calling students timer handler).
- generates message to be sent (passed from later 5 to 4)
THERE IS NOT REASON THAT ANY STUDENT SHOULD HAVE TO READ OR UNDERSTAND
THE CODE BELOW. YOU SHOLD NOT TOUCH, OR REFERENCE (in your code) ANY
OF THE DATA STRUCTURES BELOW. If you're interested in how I designed
the emulator, you're welcome to look at the code - but again, you should have
to, and you defeinitely should not have to modify
******************************************************************/
struct event {
float evtime; /* event time */
int evtype; /* event type code */
int eventity; /* entity where event occurs */
struct pkt *pktptr; /* ptr to packet (if any) assoc w/ this event */
struct event *prev;
struct event *next;
};
struct event *evlist = NULL; /* the event list */
/* possible events: */
#define TIMER_INTERRUPT 0
#define FROM_LAYER5 1
#define FROM_LAYER3 2
#define OFF 0
#define ON 1
#define A 0
#define B 1
int TRACE = 1; /* for my debugging */
int nsim = 0; /* number of messages from 5 to 4 so far */
int nsimmax = 0; /* number of msgs to generate, then stop */
float time = 0.000;
float lossprob; /* probability that a packet is dropped */
float corruptprob; /* probability that one bit is packet is flipped */
float lambda; /* arrival rate of messages from layer 5 */
int ntolayer3; /* number sent into layer 3 */
int nlost; /* number lost in media */
int ncorrupt; /* number corrupted by media*/
main()
{
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);
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:
printf(" Simulator terminated at time %f\n after sending %d msgs from layer5\n",time,nsim);
}
init() /* 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("%d",&nsimmax);
printf("Enter packet loss probability [enter 0.0 for no loss]:");
scanf("%f",&lossprob);
printf("Enter packet corruption probability [0.0 for no corruption]:");
scanf("%f",&corruptprob);
printf("Enter average time between messages from sender's layer5 [ > 0.0]:");
scanf("%f",&lambda);
printf("Enter TRACE:");
scanf("%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 = 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");
exit(0);
}
ntolayer3 = 0;
nlost = 0;
ncorrupt = 0;
time=0.0; /* initialize time to 0.0 */
generate_next_arrival(); /* initialize event list */
}
/****************************************************************************/
/* jimsrand(): return a float in range [0,1]. The routine below is used to */
/* isolate all random number generation in one location. We assume that the*/
/* system-supplied rand() function return an int in therange [0,mmm] */
/****************************************************************************/
float jimsrand()
{
double mmm = (double) RAND_MAX ;//2147483647; /* largest int - MACHINE DEPENDENT!!!!!!!! */
float x; /* individual students may need to change mmm */
x = rand()/mmm; /* x should be uniform in [0,1] */
return(x);
}
/********************* EVENT HANDLINE ROUTINES *******/
/* The next set of routines handle the event list */
/*****************************************************/
generate_next_arrival()
{
double x,log(),ceil();
struct event *evptr;
// char *malloc();
float ttime;
int tempint;
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);
}
insertevent(p)
struct event *p;
{
struct event *q,*qold;
if (TRACE>2) {
printf(" INSERTEVENT: time is %lf\n",time);
printf(" INSERTEVENT: future time will be %lf\n",p->evtime);
}
q = evlist; /* q points to header of list in which p struct inserted */
if (q==NULL) { /* list is empty */
evlist=p;
p->next=NULL;
p->prev=NULL;
}
else {
for (qold = q; q !=NULL && p->evtime > q->evtime; q=q->next)
qold=q;
if (q==NULL) { /* end of list */
qold->next = p;
p->prev = qold;
p->next = NULL;
}
else if (q==evlist) { /* front of list */
p->next=evlist;
p->prev=NULL;
p->next->prev=p;
evlist = p;
}
else { /* middle of list */
p->next=q;
p->prev=q->prev;
q->prev->next=p;
q->prev=p;
}
}
}
printevlist()
{
struct event *q;
int i;
printf("--------------\nEvent List Follows:\n");
for(q = evlist; q!=NULL; q=q->next) {
printf("Event time: %f, type: %d entity: %d\n",q->evtime,q->evtype,q->eventity);
}
printf("--------------\n");
}
/********************** Student-callable ROUTINES ***********************/
/* called by students routine to cancel a previously-started timer */
stoptimer(AorB)
int AorB; /* A or B is trying to stop timer */
{
struct event *q,*qold;
if (TRACE>2)
printf(" STOP TIMER: stopping timer at %f\n",time);
/* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
for (q=evlist; q!=NULL ; q = q->next)
if ( (q->evtype==TIMER_INTERRUPT && q->eventity==AorB) ) {
/* remove this event */
if (q->next==NULL && q->prev==NULL)
evlist=NULL; /* remove first and only event on list */
else if (q->next==NULL) /* end of list - there is one in front */
q->prev->next = NULL;
else if (q==evlist) { /* front of list - there must be event after */
q->next->prev=NULL;
evlist = q->next;
}
else { /* middle of list */
q->next->prev = q->prev;
q->prev->next = q->next;
}
free(q);
return;
}
printf("Warning: unable to cancel your timer. It wasn't running.\n");
}
starttimer(AorB,increment)
int AorB; /* A or B is trying to stop timer */
float increment;
{
struct event *q;
struct event *evptr;
// char *malloc();
if (TRACE>2)
printf(" START TIMER: starting timer at %f\n",time);
/* be nice: check to see if timer is already started, if so, then warn */
/* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
for (q=evlist; q!=NULL ; q = q->next)
if ( (q->evtype==TIMER_INTERRUPT && q->eventity==AorB) ) {
printf("Warning: attempt to start a timer that is already started\n");
return;
}
/* create future event for when timer goes off */
evptr = (struct event *)malloc(sizeof(struct event));
evptr->evtime = time + increment;
evptr->evtype = TIMER_INTERRUPT;
evptr->eventity = AorB;
insertevent(evptr);
}
/************************** TOLAYER3 ***************/
/* 1A25A48A252A */
tolayer3(AorB,packet)
int AorB; /* A or B is trying to stop timer */
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;
/* 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);
}
tolayer5(AorB,datasent)
int AorB;
char datasent[20];
{
int i;
if (TRACE>2) {
printf(" TOLAYER5: data received: ");
for (i=0; i<20; i++)
printf("%c",datasent[i]);
printf("\n");
}
}
project.program.2.rdt.spacer.gif
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
im still waiting for your comments!
ASKER
a
Hi Tboy501,
How did you go with this? Did you eventually find a solution? I'm doing the same practical
How did you go with this? Did you eventually find a solution? I'm doing the same practical
ASKER
Open in new window