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();
}
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)
{}
}
}
// The following three methods update the given
SNMP Object Identifier and table index
// with the given value. The second parameter,
which is the value, is used to overload the
// updateObject method with either an int,
String, or long value.
// Note that an exception occurs if the SNMP
Object Identifier is updated with updateSubAgent,
// but has not been initially created with
addSubAgent.
When this occurs, an InvalidIdException
// is thrown.
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");
}
}
}