Sample UDP Server

We will begin by looking at the code found in the org.apache.mina.example.udp package. To keep life simple, we shall concentrate on MINA related constructs only.

To construct the server, we shall have to do the following:

  1. Create a Datagram Socket to listen for incoming Client requests (See MemoryMonitor.java)
  2. Create an IoHandler to handle the MINA framework generated events (See MemoryMonitorHandler.java)

Here is the first snippet that addresses Point# 1:

NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
acceptor.setHandler(new MemoryMonitorHandler(this));

Here, we create a NioDatagramAcceptor to listen for incoming Client requests, and set the IoHandler.The variable 'PORT' is just an int. The next step is to add a logging filter to the filter chain that this DatagramAcceptor will use. LoggingFilter is a very nice way to see MINA in Action. It generate log statements at various stages, providing an insight into how MINA works.

DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
chain.addLast("logger", new LoggingFilter());

Next we get into some more specific code for the UDP traffic. We will set the acceptor to reuse the address

DatagramSessionConfig dcfg = acceptor.getSessionConfig();
dcfg.setReuseAddress(true);acceptor.bind(new InetSocketAddress(PORT));

Of course the last thing that is required here is to call bind().

IoHandler implementation

There are three major events of interest for our Server Implementation

  • Session Created
  • Message Received
  • Session Closed

Lets look at each of them in detail

Session Created Event

@Override
public void sessionCreated(IoSession session) throws Exception {
    SocketAddress remoteAddress = session.getRemoteAddress();
    server.addClient(remoteAddress);
}

In the session creation event, we just call addClient() function, which internally adds a Tab to the UI

Message Received Event

@Override
public void messageReceived(IoSession session, Object message) throws Exception {
    if (message instanceof IoBuffer) {
        IoBuffer buffer = (IoBuffer) message;
        SocketAddress remoteAddress = session.getRemoteAddress();
        server.recvUpdate(remoteAddress, buffer.getLong());
    }
 }

In the message received event, we just dump the data received in the message. Applications that need to send responses, can process message and write the responses onto session in this function.

Session Closed Event

@Override
public void sessionClosed(IoSession session) throws Exception {
    System.out.println("Session closed...");
    SocketAddress remoteAddress = session.getRemoteAddress();
    server.removeClient(remoteAddress);
}

In the Session Closed, event we just remove the Client tab from the UI