Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SNMP4J- where is processPdu called in the program?

843790Sep 3 2010
Hello Everyone,



I’ve made the simplest possible agent just to check if it can respond to the PDUs received from the manager. Although I’ve written the following code with the help of the SNMP4J tutorials, I have my doubt as to where is the processPdu function called in the program? I’ve written it fine, but I don’t see it being called. Thus, where is it that the PDU sent from the manager is actually received? Following is my code and the errors it generates. Please tell me where I’m going wrong. Also, since i'm working on Eclipse IDE, it seems quite confusing to me how to execute manager and agent together in order to create a communication session( just da way v do in socket programming). it seems that i can only run either of them at a time..



public class SNMP4J_AGENT extends Thread implements CommandResponder

{



/**

* @param args

*/

// Processes incoming Pdu

public synchronized void processPdu(CommandResponderEvent e)

{

System.out.println("Command");

ScopedPDU command;

if ( e.getPDU() instanceof ScopedPDU)

{

command = (ScopedPDU)e.getPDU();

}

else

{

command = (ScopedPDU)e.getPDU();

}

if (command != null)

{

//send reply

ScopedPDU cmd = (ScopedPDU)e.getPDU();

cmd.setType(ScopedPDU.RESPONSE);

cmd.setRequestID(command.getRequestID());

StatusInformation st=new StatusInformation(new VariableBinding(),

e.getStateReference().getContextName(),

e.getStateReference().getContextEngineID(),

new Integer32( e.getStateReference().getSecurityLevel()) );



StatusInformation statusInformation = new StatusInformation();

StateReference ref = e.getStateReference();

System.out.println(ref);

try

{

e.getMessageDispatcher().returnResponsePdu(e.getMessageProcessingModel(),

e.getSecurityModel(),

e.getSecurityName(),

e.getSecurityLevel(),

command,

e.getMaxSizeResponsePDU(),

ref,

st);

System.out.println("Response sent to manager....");

}

catch (MessageException e1)

{

e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates

}



}



}

@Override

public void run()

{

//InetAddress hostIp;

//int port_, version;

try

{

//Sets the snmp listener to port

TransportMapping transport = new DefaultUdpTransportMapping(new UdpAddress("udp:127.0.0.1/161"));

Snmp snmp = new Snmp(transport); //snmp.addCommandResponder(this);

MPv3 mpv3 = (MPv3)snmp.getMessageProcessingModel(MessageProcessingModel.MPv3);

System.out.println("in try");



//if (version == 3)

{

byte[] localEngineID =((MPv3)snmp.getMessageProcessingModel(MessageProcessingModel.MPv3)).createLocalEngineID();

USM usm = new USM(SecurityProtocols.getInstance(),

new OctetString(localEngineID),0);

SecurityModels.getInstance().addSecurityModel(usm);

snmp.setLocalEngine(localEngineID, 0, 0);



// Add the configured user to the USM

snmp.getUSM().addUser(new OctetString("MD5DES"),

new UsmUser(new OctetString("MD5DES"),

AuthMD5.ID,

new OctetString("MD5DESUserAuthPassword"),

PrivDES.ID ,

new OctetString("MD5DESUserPrivPassword")));

System.out.println("USM: "+ snmp.getUSM());



}

snmp.addCommandResponder(this);

//snmp.listen();

// md.addCommandResponder(this);

System.out.println("transportBinding..");

transport.listen();



// transport.listen();

System.out.print("SNMP Listener init: ");

Address a = transport.getListenAddress();

System.out.println(a.toString());

//snmp.processPdu(this);

/* try

{

snmp.processPdu(arg0);

}

catch(Exception ex)

{

ex.getMessage();

}*/

snmp.close();

}

catch (IOException e)

{

System.err.print("ERROR!!!!! "+e);

}

try

{

this.wait();

}

catch (InterruptedException ex)

{

Thread.currentThread().interrupt();

}





}



public static void main(String[] args)throws Exception

{

// TODO Auto-generated method stub



new SNMP4J_AGENT().start();

}



}



Errors:

Exception in thread "Thread-0" java.lang.IllegalArgumentException: udp:127.0.0.1/161

at org.snmp4j.smi.UdpAddress.<init>(UdpAddress.java:48)

at SNMP4J_AGENT.run(SNMP4J_AGENT.java:100)





Thanx a lot…..



Regards,



Nidhi Lohat,

Software Trainee,

Eminent Networks Pvt. Ltd.(HFCL Group)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 1 2010
Added on Sep 3 2010
0 comments
781 views