[SNMP4J] DefaultUdpTransportMapping class and ListenThread

Vivi Zhang vzhang at anuesystems.com
Mon Feb 2 20:52:11 CET 2009


Hi,

I am looking at ways to tuning my application with embedded SNMP agent 
with SNMP4J and SNMP4J-Agent.

I have two questions about the implementation of  inner class ListernThread.
Q1) Line 315 
*   try {
      socket.receive(packet);
   }
   catch (InterruptedIOException iiox) {
         if (iiox.bytesTransferred <= 0) {
         continue;
   }*
   If we see SocketTimeoutException since there is no packet to read, 
can we put the thread to a small break before it goes for another 
socket.receive() call? 
   The suggestion is intended to ease some pressure on the CPU and 
network resource.  Do you see any potential drawback from this change?
 
Q2) Line 307 *DatagramPacket packet = new DatagramPacket(buf, buf.length,
                                             udpAddress.getInetAddress(),
                                             udpAddress.getPort());**
        *A DatagramPacket object is created repeatedly within the while 
loop. What is the drawback if we move the stmt outiside while loop.  The 
logic  behind this suggestion is avoiding object creating repeatedly 
withing the loop.

Thanks.

Vivi

P.S.

I am using SNMP4J 1.9.3c.

Following is a section of code I cut from DefaultUdpTransportMapping.java.
============================================================================

while (!stop) {
*  DatagramPacket packet = new DatagramPacket(buf, buf.length,
                                             udpAddress.getInetAddress(),
                                             udpAddress.getPort());*
  try {
    try {
      socket.receive(packet);
    }
    catch (InterruptedIOException iiox) {
      if (iiox.bytesTransferred <= 0) {
     *   continue;*
      }
    }
    if (logger.isDebugEnabled()) {
      logger.debug("Received message from "+packet.getAddress()+"/"+
                   packet.getPort()+
                   " with length "+packet.getLength()+": "+
                   new OctetString(packet.getData(), 0,
                                   packet.getLength()).toHexString());
    }
    ByteBuffer bis;
    // If messages are processed asynchronously (i.e. multi-threaded)
    // then we have to copy the buffer's content here!
    if (isAsyncMsgProcessingSupported()) {
      byte[] bytes = new byte[packet.getLength()];
      System.arraycopy(packet.getData(), 0, bytes, 0, bytes.length);
      bis = ByteBuffer.wrap(bytes);
    }
    else {
      bis = ByteBuffer.wrap(packet.getData());
    }
    fireProcessMessage(new UdpAddress(packet.getAddress(),
                                      packet.getPort()), bis);
  }
  catch (SocketTimeoutException stex) {
    // ignore
  }
  catch (PortUnreachableException purex) {
    synchronized (DefaultUdpTransportMapping.this) {
      listener = null;
    }
    logger.error(purex);
    if (logger.isDebugEnabled()) {
      purex.printStackTrace();
    }
    if (SNMP4JSettings.isFowardRuntimeExceptions()) {
      throw new RuntimeException(purex);
    }
    break;
  }
  catch (SocketException soex) {
    logger.error("Socket for transport mapping "+toString()+
                 " error: "+soex.getMessage(), soex);
    stop = true;
  }
  catch (IOException iox) {
    logger.warn(iox);
    if (logger.isDebugEnabled()) {
      iox.printStackTrace();
    }
    if (SNMP4JSettings.isFowardRuntimeExceptions()) {
      throw new RuntimeException(iox);
    }
  }
}
synchronized (DefaultUdpTransportMapping.this) {
  listener = null;
  stop = true;
  if (socket != null) {
    socket.close();
  }
}



More information about the SNMP4J mailing list