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.udp.perf;
021
022import java.io.IOException;
023import java.net.InetSocketAddress;
024import java.util.concurrent.atomic.AtomicInteger;
025
026import org.apache.mina.core.service.IoHandlerAdapter;
027import org.apache.mina.core.session.IdleStatus;
028import org.apache.mina.core.session.IoSession;
029import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
030
031/**
032 * An UDP server used for performance tests.
033 * 
034 * It does nothing fancy, except receiving the messages, and counting the number of
035 * received messages.
036 * 
037 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
038 */
039public class UdpServer extends IoHandlerAdapter {
040    /** The listening port (check that it's not already in use) */
041    public static final int PORT = 18567;
042
043    /** The number of message to receive */
044    public static final int MAX_RECEIVED = 100000;
045
046    /** The starting point, set when we receive the first message */
047    private static long t0;
048
049    /** A counter incremented for every recieved message */
050    private AtomicInteger nbReceived = new AtomicInteger(0);
051
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
057        cause.printStackTrace();
058        session.closeNow();
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public void messageReceived(IoSession session, Object message) throws Exception {
066
067        int nb = nbReceived.incrementAndGet();
068
069        if (nb == 1) {
070            t0 = System.currentTimeMillis();
071        }
072
073        if (nb == MAX_RECEIVED) {
074            long t1 = System.currentTimeMillis();
075            System.out.println("-------------> end " + (t1 - t0));
076        }
077
078        if (nb % 10000 == 0) {
079            System.out.println("Received " + nb + " messages");
080        }
081
082        // If we want to test the write operation, uncomment this line
083        session.write(message);
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public void sessionClosed(IoSession session) throws Exception {
091        System.out.println("Session closed...");
092
093        // Reinitialize the counter and expose the number of received messages
094        System.out.println("Nb message received : " + nbReceived.get());
095        nbReceived.set(0);
096    }
097
098    /**
099     * {@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     */
117    @Override
118    public void sessionOpened(IoSession session) throws Exception {
119        System.out.println("Session Opened...");
120    }
121
122    /**
123     * Create the UDP server
124     * 
125     * @throws IOException If something went wrong
126     */
127    public UdpServer() throws IOException {
128        NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
129        acceptor.setHandler(this);
130
131        // The logger, if needed. Commented atm
132        //DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
133        //chain.addLast("logger", new LoggingFilter());
134
135        acceptor.bind(new InetSocketAddress(PORT));
136
137        System.out.println("Server started...");
138    }
139
140    /**
141     * The entry point.
142     * 
143     * @param args The arguments
144     * @throws IOException If something went wrong
145     */
146    public static void main(String[] args) throws IOException {
147        new UdpServer();
148    }
149}