[SNMP4J] where to call processPdu from in the agent?

Nidhi Lohat lohat_nidhi at yahoo.com
Fri Sep 3 07:56:01 CEST 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..
 
publicclassSNMP4J_AGENT extendsThread implementsCommandResponder
{
 
      /**
       *@paramargs
       */
      // Processes incoming Pdu
    publicsynchronizedvoidprocessPdu(CommandResponderEvent e) 
    {
        System.out.println("Command");
        ScopedPDU command;
        if( e.getPDU() instanceofScopedPDU) 
        {
            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(newVariableBinding(),
                                 e.getStateReference().getContextName(),
                                 e.getStateReference().getContextEngineID(),
                                 newInteger32( 
e.getStateReference().getSecurityLevel()) );
 
            StatusInformation statusInformation = newStatusInformation();
            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
    publicvoidrun() 
      {
            //InetAddress hostIp;
            //int port_, version;
          try
        {
           //Sets the snmp listener to port
           TransportMapping transport = 
newDefaultUdpTransportMapping(newUdpAddress("udp:127.0.0.1/161"));
           Snmp snmp = newSnmp(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 = newUSM(SecurityProtocols.getInstance(),
                               newOctetString(localEngineID),0);
             SecurityModels.getInstance().addSecurityModel(usm);
             snmp.setLocalEngine(localEngineID, 0, 0);
             
             // Add the configured user to the USM
             snmp.getUSM().addUser(newOctetString("MD5DES"),
                                    newUsmUser(newOctetString("MD5DES"), 
                                                                    AuthMD5.ID,
                                                
newOctetString("MD5DESUserAuthPassword"),
                                                                        
PrivDES.ID,
                                                
newOctetString("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();
          }
 
      
    }
 
      publicstaticvoidmain(String[] args)throwsException
      {
            // TODOAuto-generated method stub
            
          newSNMP4J_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)


      


More information about the SNMP4J mailing list