001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.example.tcp.perf;
021
022import java.io.IOException;
023import java.net.InetSocketAddress;
024import java.security.GeneralSecurityException;
025import java.util.concurrent.atomic.AtomicInteger;
026
027import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
028import org.apache.mina.core.service.IoHandlerAdapter;
029import org.apache.mina.core.session.IdleStatus;
030import org.apache.mina.core.session.IoSession;
031import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory;
032import org.apache.mina.filter.ssl.SslFilter;
033import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
034
035/**
036 * An TCP SSL server used for performance tests.
037 * 
038 * It does nothing fancy, except receiving the messages, and counting the number of
039 * received messages.
040 * 
041 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
042 */
043public class TcpSslServer extends IoHandlerAdapter {
044    /** The listening port (check that it's not already in use) */
045    public static final int PORT = 18567;
046
047    /** The number of message to receive */
048    public static final int MAX_RECEIVED = 100000;
049
050    /** The starting point, set when we receive the first message */
051    private static long t0;
052
053    /** A counter incremented for every recieved message */
054    private AtomicInteger nbReceived = new AtomicInteger(0);
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
061        cause.printStackTrace();
062        session.closeNow();
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public void messageReceived(IoSession session, Object message) throws Exception {
070
071        int nb = nbReceived.incrementAndGet();
072
073        if (nb == 1) {
074            t0 = System.currentTimeMillis();
075        }
076
077        if (nb == MAX_RECEIVED) {
078            long t1 = System.currentTimeMillis();
079            System.out.println("-------------> end " + (t1 - t0));
080        }
081
082        if (nb % 10000 == 0) {
083            System.out.println("Received " + nb + " messages");
084        }
085
086        // If we want to test the write operation, uncomment this line
087        session.write(message);
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public void sessionClosed(IoSession session) throws Exception {
095        System.out.println("Session closed...");
096
097        // Reinitialize the counter and expose the number of received messages
098        System.out.println("Nb message received : " + nbReceived.get());
099        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        NioSocketAcceptor acceptor = new NioSocketAcceptor();
136        
137        // Inject the SSL filter
138        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
139        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}