Thursday, December 18, 2008

Moved my blog to

Have moved my posts to my own hosting space, can keep in touch at
http://www.ashishpaliwal.com/blog

Thursday, September 11, 2008

SNMP4J Decoder for Apache MINA

Recently started evaluating Apache MINA. So thought about building a Trap Receiver using Apache MINA. Refer to one of my posting on What is Apache MINA?


Design



In this post we shall concentrate on just writing the ProtocolDecoder

The code snippet using SNMP4J library is

public class SNMP4JCodec extends ProtocolDecoderAdapter {

static Logger logger = LoggerFactory.getLogger(SNMP4JCodec.class);

public void decode(IoSession ioSession, IoBuffer ioBuffer,
ProtocolDecoderOutput protocolDecoderOutput) throws Exception {
ByteBuffer pduBuffer = ioBuffer.buf();
// Decode the bytes using SNMP4J API's
PDU pdu = new PDU();
try {
BERInputStream berStream = new BERInputStream(pduBuffer);
BER.MutableByte mutableByte = new BER.MutableByte();
int length = BER.decodeHeader(berStream, mutableByte);
int startPos = (int)berStream.getPosition();

if (mutableByte.getValue() != BER.SEQUENCE) {
String txt = "SNMPv2c PDU must start with a SEQUENCE";
throw new IOException(txt);
}
Integer32 version = new Integer32();
version.decodeBER(berStream);

// decode community string
OctetString securityName = new OctetString();
securityName.decodeBER(berStream);

// decode the remaining PDU
pdu.decodeBER(berStream);
logger.debug("PDU - "+pdu);
} catch (Exception ex) {
ex.printStackTrace();
}
protocolDecoderOutput.write(pdu);
}
}

The code converts ByteBuffer into an SNMP PDU using SNMP4J  library. Similarly, we can use Adventnet or joesnmp to convert the byte stream into SNMP PDU's

Labels: , , , ,

What is Apache MINA?

Apache MINA (Multipurpose Infrastructure for Network Application) is an open source java network application framework. MINA is can be used to create Scalable, high performance Network applications. MINA provides unified API’s for various transports like TCP, UDP, Serial communication, In-VM pipe. It also makes it easy to make an implementation of custom transport type. MINA provides both high-level and low-level network API’s.

Application Architecture




From the figure above, User application interacts with MINA API’s, shielding the User application from Low level IO details. MINA internally uses IO API’s to perform the actual IO functions. This makes it easy for the Users to concentrate on Application logic and leave the IO handling to Apache MINA.

Advantages

  • Unified API’s for various transports (TCP/UDP etc)
  • Provides high/low level API’s
  • Customizable Thread Model
  • Easy Unit Testing using MockObjects
  • Integration with DI frameworks like Spring, Google Guice, picocontainer
  • JMX Manageability

Reference

Apache MINA - Home Page of Apache MINA