package avnmp.java.tutorial.BuildingApplications;


import java.util.*;


public class LoadGenAppBasic extends LoadGenAppBasicBase
{
    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;
    }


    // This is the total number of active packets transmitted over the total uptime for the application.

    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)
         {}
     }
}