package avnmp.java.tutorial.SnmpInstr;

import java.util.*;
import magician.Node.*;
import snmp.*;
import snmp.agent.*;
import avnmp.java.lp.SnmpInterface;

public class LoadGenSnmp extends LoadSnmpBase implements SnmpInterface
{

    long PacketsPerSecond = 20;

    Date d = new Date();

    long numPkts = 0;

    transient private AppCommunicator context;

    final static String experimental = new String("1.3.6.1.3");
    final static String active = new String(experimental+".75");
    final static String loadApp = new String(active+".3");
    final static StringBuffer loadAppPackets = new StringBuffer(loadApp +".1");
    final static StringBuffer loadAppUptime = new StringBuffer(loadApp +".2");

    public LoadGenSnmp()
    {
         super();
         context = new AppCommunicator();
    }

    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();
    }

// The active packet now has a few new lines of code in order to implement the SNMP agent. The new
// lines are highlighted below.

    public void exec()
    {

         // The next line creates the SNMP Object for the given identifier and provides an initial value
         // for the object.

         addObject(loadAppUptime.toString(), new Date().getTime() - d.getTime());
         while(true)
         {
              // The next line, which is continuously called from within the while-loop,  updates the
              // the value of the SNMP Object Identifier.
              updateObject(loadAppUptime.toString(), new Date().getTime() - d.getTime(), "1");
              while(Rate() < PacketsPerSecond)
             {
                   // The next line, which is continuously called from within the while-loop,  updates the
                   // the value of the SNMP Object Identifier.
                   updateObject(loadAppUptime.toString(), new Date().getTime() - d.getTime(), "1");
                   LoadPacket lp = new LoadPacket(milliSeconds());
                   ++numPkts;
              }
         try {
               SPThread.currentSPThread().sleep(250);
         } catch (InterruptedException ex)
         {}
      }
    }

    private void updateObject(String oid, int value, String index) {
        try {
            context.updateSubAgent(oid, value, index);
        } catch (InvalidIdException iie) {
            System.err.println("UpdateSA: Object " + oid + " not found");
        }
    }

    private void updateObject(String oid, String value, String index) {
        try {
            context.updateSubAgent(oid, value, index);
        } catch (InvalidIdException iie) {
            System.err.println("UpdateSA: Object " + oid + " not found");
        }
    }

    private void updateObject(String oid, long value, String index) {
        try {
            context.updateSubAgent(oid, value, index);
        } catch (InvalidIdException iie) {
            System.err.println("UpdateSA: Object " + oid + " not found");
        }
    }

    private void addObject(String oid, int value) {
        try {
            context.addSubAgent(oid, value);
        } catch (DuplicateEntryException dee) {
            System.err.println("AddAgent: Object " + oid + " already exists");
        }
    }

    private void addObject(String oid, long value) {
        try {
            context.addSubAgent(oid, value);
        } catch (DuplicateEntryException dee) {
            System.err.println("AddAgent: Object " + oid + " already exists");
        }
    }

    private void addObject(String oid, String value) {
        try {
            context.addSubAgent(oid, value);
        } catch (DuplicateEntryException dee) {
            System.err.println("AddAgent: Object " + oid + " already exists");
        }
    }
}