//===================================================== file = fluid1.c =====
//=  Program to output fluid-flow queue lengths for an input of rates       =
//===========================================================================
//=  Notes:                                                                 =
//=    1) All rates are in bits per second.                                 =
//=    2) Input from input file "in.dat" to stdin.  The file in.dat         =
//=       contains a series of rate values at increments of T seconds.      =
//=       See example below.                                                =
//=    3) Output is queue lengths and is to stdout.                         =
//=    4) Must manually set BUFFER_SIZE (in bits), LINK_RATE (in bits per   =
//=       second), and T (time increment of in.dat records in seconds).     =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=  100                                                                    =
//=  110                                                                    =
//=  120                                                                    =
//=  130                                                                    =
//=  90                                                                     =
//=  70                                                                     =
//=  70                                                                     =
//=  100                                                                    =
//=  110                                                                    =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat", BUFFER_SIZE = 4.00,                 =
//=                 LINK_RATE = 100.00, and T = 0.10)                       =
//=                                                                         =
//=   ---------------------------------------------- fluid1.c -----         =
//=     0.000000  0.000000                                                  =
//=     1.000000  0.000000                                                  =
//=     3.000000  0.000000                                                  =
//=     4.000000  2.000000                                                  =
//=     3.000000  0.000000                                                  =
//=     0.000000  0.000000                                                  =
//=     0.000000  0.000000                                                  =
//=     0.000000  0.000000                                                  =
//=     1.000000  0.000000                                                  =
//=   -------------------------------------------------------------         =
//=-------------------------------------------------------------------------=
//=  Build: gcc fluid1.c, bcc32 fluid1.c, cl fluid1.c                       =
//=-------------------------------------------------------------------------=
//=  Execute: fluid1 < in.dat                                               =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (04/10/01) - Genesis                                      =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf() and feof()
#include <stdlib.h>                // Needed for exit() and atof()

//----- Defines -------------------------------------------------------------
#define BUFFER_SIZE    4.00        // Buffer size in bits
#define LINK_RATE    100.00        // Output rate in bits per second
#define T              0.10        // Increment in seconds for input data

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  char     instring[255];            // Input string
  double   queue_len;                // Current queue length
  double   lost_bits;                // Lost bits on the floor
  double   input_rate;               // Current input rate
  double   fill_rate;                // Current queue fill rate

  // Output the start banner
  printf("---------------------------------------------- fluid1.c -----\n");

  // Initialize the queue length to zero
  queue_len = 0.0;

  // Compute queue lengths until end-of-file
  while(!feof(stdin))
  {
    // Read the next input rate value
    scanf("%s \n", instring);
    input_rate = atof(instring);

    // Compute the fill rate
    fill_rate = input_rate - LINK_RATE;

    // Compute current queue length and lost bits
    lost_bits = 0.0;
    queue_len = queue_len + (T * fill_rate);
    if (queue_len < 0) queue_len = 0.0;
    if (queue_len > BUFFER_SIZE)
    {
      lost_bits = queue_len - BUFFER_SIZE;
      queue_len = BUFFER_SIZE;
    }

    // Output queue length and lost bits
    printf("  %f  %f \n", queue_len, lost_bits);
  }

  // Output the end banner
  printf("-------------------------------------------------------------\n");
}

