package avnmp.java.tutorial.SnmpInstr;

import java.util.*;
import magician.Node.*;
import snmp.*;
import snmp.agent.*;

public class LoadGenSnmp extends LoadSnmpBase implements SnmpInterface
{

    long PacketsPerSecond = 20;

    Date d = new Date();

    long numPkts = 0;

    transient private AppCommunicator context;

// The following lines of code define the SNMP Object Identifiers for this application.
// The Object Identifiers are a tree with two leaves: one for the number of packets
// transmitted and another for the total execution time.

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

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