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.tcp.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.nio.NioSocketAcceptor;
30  
31  /**
32   * An TCP server used for performance tests.
33   * 
34   * It does nothing fancy, except receiving the messages, and counting the number of
35   * received messages.
36   * 
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public class TcpServer extends IoHandlerAdapter {
40      /** The listening port (check that it's not already in use) */
41      public static final int PORT = 18567;
42  
43      /** The number of message to receive */
44      public static final int MAX_RECEIVED = 100000;
45  
46      /** The starting point, set when we receive the first message */
47      private static long t0;
48  
49      /** A counter incremented for every recieved message */
50      private AtomicInteger nbReceived = new AtomicInteger(0);
51  
52      /**
53       * {@inheritDoc}
54       */
55      @Override
56      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
57          cause.printStackTrace();
58          session.close(true);
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public void messageReceived(IoSession session, Object message) throws Exception {
66  
67          int nb = nbReceived.incrementAndGet();
68  
69          if (nb == 1) {
70              t0 = System.currentTimeMillis();
71          }
72  
73          if (nb == MAX_RECEIVED) {
74              long t1 = System.currentTimeMillis();
75              System.out.println("-------------> end " + (t1 - t0));
76          }
77  
78          if (nb % 10000 == 0) {
79              System.out.println("Received " + nb + " messages");
80          }
81  
82          // If we want to test the write operation, uncomment this line
83          session.write(message);
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void sessionClosed(IoSession session) throws Exception {
91          System.out.println("Session closed...");
92  
93          // Reinitialize the counter and expose the number of received messages
94          System.out.println("Nb message received : " + nbReceived.get());
95          nbReceived.set(0);
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public void sessionCreated(IoSession session) throws Exception {
103         System.out.println("Session created...");
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
111         System.out.println("Session idle...");
112     }
113 
114     /**
115      * {@inheritDoc}
116      * @param session the current seession
117      * @throws Exception If something went wrong
118      */
119     @Override
120     public void sessionOpened(IoSession session) throws Exception {
121         System.out.println("Session Opened...");
122     }
123 
124     /**
125      * Create the TCP server
126      * 
127      * @throws IOException If something went wrong
128      */
129     public TcpServer() throws IOException {
130         NioSocketAcceptor acceptor = new NioSocketAcceptor();
131         acceptor.setHandler(this);
132 
133         // The logger, if needed. Commented atm
134         //DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
135         //chain.addLast("logger", new LoggingFilter());
136 
137         acceptor.bind(new InetSocketAddress(PORT));
138 
139         System.out.println("Server started...");
140     }
141 
142     /**
143      * The entry point.
144      * 
145      * @param args The arguments
146      * @throws IOException If something went wrong
147      */
148     public static void main(String[] args) throws IOException {
149         new TcpServer();
150     }
151 }