package avnmp.java.tutorial.BuildingApplications;


import java.util.*;


public class LoadGenAppBasic extends LoadGenAppBasicBase
{

    // This variable will keep count of the total number of packets generated. See the highlighted
    // line in the exec method below to see when it is incremented as each packet is generated. Also
    // see the Rate() method below where numPkts is used.

    long numPkts = 0;


    long PacketsPerSecond = 20;


    const Date d = new Date();


    public LoadGenAppBasic()
    {
    }


    private long milliSeconds() {
          return new Date().getTime() - d.getTime();
   }


    private long Seconds()
   {
         long s = milliSeconds() / (long) 1000;

         if(s == 0)
              return 1;
         if(s < 0) {
              System.err.println("Time less than zero.");
              halt();
        }
        return s;
    }


    private double Rate() {
         return (double) numPkts / (double) Seconds();
    }


    public void exec()
    {
           while(Rate() < PacketsPerSecond)
           {

           System.out.println("Packets/Second: "+ Rate());
           LoadPacket lp = new LoadPacket();
           lp.SendForProcessing("AN-5");
           numPkts++;
          }

         try {
               SPThread.currentSPThread().sleep(250);
         } catch (InterruptedException ex)
         {}
     }
}