
/*********************************************************
* Program:   UDPEcho v 1.7  
*
* File Last update:  12/5/2017
*
* Summary:  This is an implementation of a UDP version of ping.
*           It has the initial solution to mode 2- window mode.
*
* Usage :   client  
*  serverIP  IPV4 server name of dotted quad address i
*          examples: localhost,  koala4.cs.clemson.edu, 192.168.1.5
*  serverPort :  server's port:    can be [0,65535] but all numbers not availale. Stick to [5000, 6000] 
*  messageSize = amount of data added to each data msg
*  iterations  : number of iterations....0 implies forever
*  debugFlag :  
*  mode:  values 0,1,2 specifies ECHO/RTT, CBR, or WINDOW modes.
*  Last parameter depends on mode
*             [mode 0] [<Iteration Delay (usecs)>] :  value 0 will have the client send back to back with no delays
*                                                     (note, value 0 currently not supported) 
*             [mode 1] [targetSendRate]
*             [mode 2] [maxW]  : max window.  The actual window is:  W=min(cwnd,maxW).
*                                              A maxW setting of 0implies infinity...or no limit
*
*   If debugFlag && 0x80 is true, creates RTT.dat
*
*
* Usage :    server 
*             <Server Port> 
*             [<Debug Flag (default : 129)>]\n", 
*             drop rate -   a number [0 1.0] specifies an artificial drop rate
*
*   If debugFlag && 0x80 is true, creates EchoServer.dat
*
* Usage examples - assume running client and server on same host
*        ./server 5000 129 0.10     will use port 5000, creates EchoServer.dat and adds a random packet drop
*                                   emulation of 0.10 (rate) or 10%.
*
*        ./client localhost 5000 1000 0 130 0 1000000     Echo/RTT mode, msg size 1000 bytes, runs forever
                                                          creates RTT.dat and shows each sample, 
*                                                         generates a  sample each 1 second.
                                                          Note default timeout is 2 seconds.

*        ./client localhost 5000 1000 7 130 2  20     Window mode, msg size 1000 bytes  run for 7  iterations
                                                      which means it sends exactly 7 messages and once they are ack'ed, the
                                                      program terminates.
                                                      Creates RTT.dat, maxW is 1. So....this runs the mode in a very 
*                                                       inefficient stop and wait mode.

*        ./client localhost 5000 1000 0 130 2  0     Window mode, msg size 1000 bytes  run forever,
                                                     Creates RTT.dat, maxW is 0 which means it is assumed to be  infinite. 
                                                     This means that the client will send at a  window that is the current cwnd.
*
*********************************************************/



*********************************************************

client documentation

//Core variables to handle the window controlled transmission of messages
unsigned int cwnd=1;  //congestion window
unsigned int currentWindow=0;       //latest window as of the last call to getWindow
int maxW = -1;




//Maintains the next seq number to use
unsigned int seqNumber = 1;
//Holds the seq number of a received message
unsigned int RxSeqNumber = 0;

//Note the following might not be needed....might be removed eventually
unsigned int largestSeqSent = 0; //at sendto, =max(seqNumber,largestSeqSent)
unsigned int largestAckRxed = 0; //at recvfrom,set if the RxSeqnumber just recved is larger
unsigned int lastSeqNumberAcked = 0; //the msg seq number last ACKed


unsigned int sequenceNumberTimed
//holds the seq number that is being timed or aNULL_SEQ_NUMBER.
//If the latter, next transmission should start the timeout and set this
sequenceNumberTimed = seqNumber;


unsigned int numberDupACKs=0;      //number of RxSeqNumbers that are NOT > largestAckRxed


state related to the sliding window

unsigned int leftEdge=0;    //lowest seq number not yet ack'ed
                            //same as lastSeqNumberAcked+1

unsigned int rightEdge=0;   //highest seq number not yet ack'ed
                            //same as largestSeqSent

unsigned int snd_next=0; //seq number to send next.  

This should be a number in the range [leftEdge, rightEdge].  
If the client is in a loop that sends as maany msgs permitted, 
the snd_next would normally be equal to the RightEdge+1



helpful routines: 

int getWindow(unsigned int cwnd, int maxW);

int updateState(double curTime, int opCode);

int getStateInfo(double curTime, int opCode);


//These are the opcodes used in either updateState or getStateInfo
#define  NEXT_EXPECTED_ACK_EVENT 1
#define  NEW_ACK_EVENT 2
#define  DUPLICATE_ACK_EVENT 3
#define  UNEXPECTED_ACK_EVENT 4

#define  TIMEOUT_EVENT  5

#define  GET_WINDOW 10 
#define  GET_LEFT_EDGE 11
#define  GET_RIGHT_EDGE 12
#define  GET_SEND_NEXT  13
#define  GET_NUMBER_IN_FLIGHT 14
#define  GET_NUMBER_CAN_SEND 15
#define  GET_ERROR_RECOVERY_STATE 16

#define  UNKNOWN_OPCODE -1


//The client goes to DETECTED state upon timeout or first dup
//                   moves to active once it's retransmitted
//                   moves to comlete once retransmit is ACKed
//                   moves back to NO_ERROR_RECOVERY once it sends the seq number after the retrnasmitted.
//possible states of clients errorRecoveryState 
#define  NO_ERROR_RECOVERY 0
#define  ERROR_RECOVERY_DETECTED  1
#define  ERROR_RECOVERY_ACTIVE 2
#define  ERROR_RECOVERY_COMPLETE 3
// NOTE: A TCP modification could be 
//   goes to detected on timeout or first dup
//   goes to active once it retransmits all segments it thinks were dropped
//   goes to complete after it ggets the acks of all retransmissions
//   toes back to NO_ERROR after once it sends first segment that is 'new' and not a part of the prior loss epsisode
#define  NO_ERROR_RECOVERY 0
#define  ERROR_RECOVERY_DETECTED  1
#define  ERROR_RECOVERY_ACTIVE 2
#define  ERROR_RECOVERY_COMPLETE 3


#define NULL_SEQ_NUMBER 0


CLient loop:
while (1)


Design of window state


int getWindow(unsigned int cwnd, int maxW);
      returns current window based on innput params

     
int updateState(double curTime, int opCode);
    called for two reasons:
         First, each time an ACK arrives.  Returns
            specific things depending on the opCode.
         Second, called to request the value of something
    A return of FAILURE implies something is wrong.



int sendDataMessages(double curTime, int sock, struct sockaddr_in *serverAddress, int numberMsgs, unsigned int startingSeqNum, void *dataBuffer,  uint32_t dataSize);
       Still under development....but meant to send a window of data msg's




EXAMPLE:

let's say we start out sending 8 messages
let's set maxW=100


________________
RTT period 1
________________
seqNumber 1
largestSeqSent = seqNumber; 
lastSeqNumberAcked = 0; //the msg seq number last ACKed
currentWindow=1

allowedToSend = currentWindow - updateState(curTime,GET_NUMBER_IN_FLIGHT);


snd_next  1
leftEdge=1
rightEdge=1

Send 1 message (seqNumber 1)

________________
RTT period 2
________________

There are three possibilities depending on the received RxSeqNumber 



CASE 1 NEW ACK
if ( (RxSeqNumber-1) == largestSeqSent)
{
  //New ACK
  cwnd++
  currentwindow= getWindow(cwnd,  maxW); 
  allowedToSend = currentWindow -updateState(curTime,GET_NUMBER_IN_FLIGHT);
}

CASE 1 NEW ACK
if ((RxSeqNumber-1) < largestSeqSent)
{
   //Duplicate ACK
}

if ((RxSeqNumber-1) > largestSeqSent)
{
    //An error 
}

  But I'll do the two iterations separately...
    cwnd=2, currentWindow=2
    allowed to send = 2
    rightEdge = rightEdge + allowedToSend (3)
    leftEdge++;  (2)

      send 2
      
  { 
    move window forward
        current
    send next msg
  }



RTT period 3 


To move the window forward....we do this once msg at a time, looping to 
    do multiple times as needed.

  cwnd,  currentWindow









server documentation











