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.SocketSessionConfig;
30  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
31  
32  /**
33   * An TCP 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 TcpServer 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          //System.out.println("Message : " + ((IoBuffer) message).getInt());
84  
85          //((IoBuffer) message).flip();
86  
87          // If we want to test the write operation, uncomment this line
88          session.write(message);
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void sessionClosed(IoSession session) throws Exception {
96          System.out.println("Session closed...");
97  
98          // Reinitialize the counter and expose the number of received messages
99          System.out.println("Nb message received : " + nbReceived.get());
100         nbReceived.set(0);
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public void sessionCreated(IoSession session) throws Exception {
108         System.out.println("Session created...");
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
116         System.out.println("Session idle...");
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void sessionOpened(IoSession session) throws Exception {
124         System.out.println("Session Opened...");
125     }
126 
127     /**
128      * Create the TCP server
129      */
130     public TcpServer() throws IOException {
131         NioSocketAcceptor acceptor = new NioSocketAcceptor();
132         acceptor.setHandler(this);
133 
134         // The logger, if needed. Commented atm
135         //DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
136         //chain.addLast("logger", new LoggingFilter());
137 
138         SocketSessionConfig scfg = acceptor.getSessionConfig();
139 
140         acceptor.bind(new InetSocketAddress(PORT));
141 
142         System.out.println("Server started...");
143     }
144 
145     /**
146      * The entry point.
147      */
148     public static void main(String[] args) throws IOException {
149         new TcpServer();
150     }
151 }