package avnmp.java.tutorial.SnmpInstr;

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

public class LoadPacketSnmp extends LoadSnmpBase implements SnmpInterface
{
    long uptime = 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 LoadPacketSnmp(long ut) {
         uptime = ut;
    }

    synchronized public void exec() {
            String NodeName = GetNodeName();
            context = new AppCommunicator();

            if(!NodeName.equals(Source_Address)) {
                  setObject(loadAppUptime.toString(), uptime, "0");
             }

            incrObject(loadAppPackets.toString(), (long) 1, "0");

            if(NodeName.equals(Destination_Address)) {
                  halt();
            }
    }

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

// See the addObject methods in LoadGenAppSnmp. These methods are
// exactly the same.

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

    synchronized private void setObject(String oid, long setVal, String idx)
   {
        context.startTrans();
        try {
                context.getLongSubAgent(oid+"."+idx);
        } catch (InvalidIdException iie) {
                try {
                        context.addSubAgent(oid+"."+idx, setVal);
                } catch(DuplicateEntryException dee) {
                        System.err.println("LoadPacket DuplicateEntryException: "+dee);
                }
        }
        try {
                context.updateSubAgent(oid, (long) setVal, idx);
        } catch(InvalidIdException iie) {
                System.err.println("LoadPacket InvalidIdException: "+iie);
        }
        context.commitTrans();
    }

    synchronized private void incrObject(String oid, long addme, String idx) {
         long val;

        context.startTrans();
        try {
                val = context.getLongSubAgent(oid+"."+idx);
        } catch (InvalidIdException iie) {
                val = (long) 0;
                try {
                        context.addSubAgent(oid+"."+idx, val);
                } catch(DuplicateEntryException dee) {
                        System.err.println("LoadPacket DuplicateEntryException: "+dee);
                }
        }
        try {
                context.updateSubAgent(oid, (long) val+addme, idx);
        } catch(InvalidIdException iie) {
                System.err.println("LoadPacket InvalidIdException: "+iie);
        }
        context.commitTrans();
    }
}