View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.udp.perf;
21  
22  import java.io.IOException;
23  import java.net.InetSocketAddress;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  import org.apache.mina.core.service.IoHandlerAdapter;
27  import org.apache.mina.core.session.IdleStatus;
28  import org.apache.mina.core.session.IoSession;
29  import org.apache.mina.transport.socket.DatagramSessionConfig;
30  import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
31  
32  /**
33   * An UDP server used for performance tests.
34   * 
35   * It does nothing fancy, except receiving the messages, and counting the number of
36   * received messages.
37   * 
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public class UdpServer extends IoHandlerAdapter {
41      /** The listening port (check that it's not already in use) */
42      public static final int PORT = 18567;
43  
44      /** The number of message to receive */
45      public static final int MAX_RECEIVED = 100000;
46  
47      /** The starting point, set when we receive the first message */
48      private static long t0;
49  
50      /** A counter incremented for every recieved message */
51      private AtomicInteger nbReceived = new AtomicInteger(0);
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
58          cause.printStackTrace();
59          session.close(true);
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public void messageReceived(IoSession session, Object message) throws Exception {
67  
68          int nb = nbReceived.incrementAndGet();
69  
70          if (nb == 1) {
71              t0 = System.currentTimeMillis();
72          }
73  
74          if (nb == MAX_RECEIVED) {
75              long t1 = System.currentTimeMillis();
76              System.out.println("-------------> end " + (t1 - t0));
77          }
78  
79          if (nb % 10000 == 0) {
80              System.out.println("Received " + nb + " messages");
81          }
82  
83          // If we want to test the write operation, uncomment this line
84          session.write(message);
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void sessionClosed(IoSession session) throws Exception {
92          System.out.println("Session closed...");
93  
94          // Reinitialize the counter and expose the number of received messages
95          System.out.println("Nb message received : " + nbReceived.get());
96          nbReceived.set(0);
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public void sessionCreated(IoSession session) throws Exception {
104         System.out.println("Session created...");
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
112         System.out.println("Session idle...");
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public void sessionOpened(IoSession session) throws Exception {
120         System.out.println("Session Opened...");
121     }
122 
123     /**
124      * Create the UDP server
125      */
126     public UdpServer() throws IOException {
127         NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
128         acceptor.setHandler(this);
129 
130         // The logger, if needed. Commented atm
131         //DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
132         //chain.addLast("logger", new LoggingFilter());
133 
134         acceptor.bind(new InetSocketAddress(PORT));
135 
136         System.out.println("Server started...");
137     }
138 
139     /**
140      * The entry point.
141      */
142     public static void main(String[] args) throws IOException {
143         new UdpServer();
144     }
145 }