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;

// The constructor now creates an active SNMP agent instance using the class member 'context'.

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

    public void exec()
    {
         addObject(loadAppUptime.toString(), new Date().getTime() - d.getTime());
         while(true) {
              updateObject(loadAppUptime.toString(), new Date().getTime() - d.getTime(), "1");
              while(Rate() < PacketsPerSecond) {
                   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");
        }
    }
}