(Supplement: as the manual is constantly being updated, it may change. The last update date is. It is strongly recommended that you read the original document)
This time, we will start with the code in the org. Apache. Mina. example. UDP package. To keep it simple, we will focus only on the sections related to Mina.
To build a UDP Service, we need to complete the following two points: 1. create a datagram datasync socket to listen to incoming client requests (see the code memorymonitor in the package. java) 2. create an iohandler to process the events generated by the Mina framework (see the code memorymonitorhandler in the package. java) below is the code (memorymonitor. java) segment:
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();acceptor.setHandler(new MemoryMonitorHandler(this));
Here, we create a NiO datagram receiver niodatagramacceptor object to pass in client requests, and then set the Handler through sethandler. The next step is to add a LOG filter to the filter chain of the receiver. The LOG filter can be used to observe the running process of Mina. By generating logs of different levels, provides an in-depth understanding of the Mina operating mechanism.
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();chain.addLast("logger", new LoggingFilter());
Next, let's take a look at some specific Code related to UDP transmission. Here we set the receiver reuse address. The last thing we need to do is to bind it to a port-where port is an int type variable:
DatagramSessionConfig dcfg = acceptor.getSessionConfig();dcfg.setReuseAddress(true);acceptor.bind(new InetSocketAddress(PORT));
Next, let's look at "iohandler implementation ":
The UPD server has three most important event methods:
- Session Creation
- Message receiving
- Session closed
Next, let's take a closer look at the specific implementation of each step:
Session creation event:
@Overridepublic void sessionCreated(IoSession session) throws Exception { SocketAddress remoteAddress = session.getRemoteAddress(); server.addClient(remoteAddress);}
The Code only calls the addclient () method. This operation only adds a tab representing the client tab on the UI of the server.
Message Receiving Event:
@Overridepublic 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 this event, it is mainly to dump the received message data and provide response processing. This method completes message processing and writes the response to the session and returns it to the client.
Session Close event:
@Overridepublic void sessionClosed(IoSession session) throws Exception { System.out.println("Session closed..."); SocketAddress remoteAddress = session.getRemoteAddress(); server.removeClient(remoteAddress);}
The close event only removes the tab that represents the client connection from the server UI.