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