summaryrefslogtreecommitdiff
path: root/java/common/src/test/java/org/apache/mina/SocketIOTest/IOWriterServer.java
blob: 423e98c67b99e539a851d5ef8a422329bf6feab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.apache.mina.SocketIOTest;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.IoFilterChain;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.SimpleByteBufferAllocator;
import org.apache.mina.filter.ReadThrottleFilterBuilder;
import org.apache.mina.filter.WriteBufferLimitFilterBuilder;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;

/** Tests MINA socket performance. This acceptor simply reads data from the network and writes it back again. */
public class IOWriterServer
{
    private static final Logger _logger = LoggerFactory.getLogger(IOWriterServer.class);

    static public int _PORT = 9999;

    private static final String DEFAULT_READ_BUFFER = "262144";
    private static final String DEFAULT_WRITE_BUFFER = "262144";


    private static class TestHandler extends IoHandlerAdapter
    {
        private int _sentCount = 0;

        private int _bytesSent = 0;

        private int _receivedCount = 0;

        public void sessionCreated(IoSession ioSession) throws java.lang.Exception
        {
            IoFilterChain chain = ioSession.getFilterChain();

            ReadThrottleFilterBuilder readfilter = new ReadThrottleFilterBuilder();
            readfilter.setMaximumConnectionBufferSize(Integer.parseInt(System.getProperty("qpid.read.buffer.limit", DEFAULT_READ_BUFFER)));
            readfilter.attach(chain);

            WriteBufferLimitFilterBuilder writefilter = new WriteBufferLimitFilterBuilder();

            writefilter.setMaximumConnectionBufferSize(Integer.parseInt(System.getProperty("qpid.write.buffer.limit", DEFAULT_WRITE_BUFFER)));

            writefilter.attach(chain);

        }

        public void messageReceived(IoSession session, Object message) throws Exception
        {
            ((ByteBuffer) message).acquire();
            session.write(message);

            if (_logger.isDebugEnabled())
            {
                _bytesSent += ((ByteBuffer) message).remaining();

                _sentCount++;

                if (_sentCount % 1000 == 0)
                {
                    _logger.debug("Bytes sent: " + _bytesSent);
                }
            }
        }

        public void messageSent(IoSession session, Object message) throws Exception
        {
            if (_logger.isDebugEnabled())
            {
                ++_receivedCount;

                if (_receivedCount % 1000 == 0)
                {
                    _logger.debug("Receieved count " + _receivedCount);
                }
            }
        }

        public void exceptionCaught(IoSession session, Throwable cause) throws Exception
        {
            _logger.error("Error: " + cause, cause);
        }
    }

    public void startAcceptor() throws IOException
    {
        IoAcceptor acceptor;
        if (Boolean.getBoolean("multinio"))
        {
            _logger.warn("Using MultiThread NIO");
            acceptor = new org.apache.mina.transport.socket.nio.MultiThreadSocketAcceptor();
        }
        else
        {
            _logger.warn("Using MINA NIO");
            acceptor = new org.apache.mina.transport.socket.nio.SocketAcceptor();
        }


        SocketSessionConfig sc = (SocketSessionConfig) acceptor.getDefaultConfig().getSessionConfig();
        sc.setTcpNoDelay(true);
        sc.setSendBufferSize(32768);
        sc.setReceiveBufferSize(32768);

        ByteBuffer.setAllocator(new SimpleByteBufferAllocator());

        //The old mina style
//        acceptor.setLocalAddress(new InetSocketAddress(_PORT));
//        acceptor.setHandler(new TestHandler());
//        acceptor.bind();
        acceptor.bind(new InetSocketAddress(_PORT), new TestHandler());

        _logger.info("Bound on port " + _PORT + ":" + _logger.isDebugEnabled());
        _logger.debug("debug on");
    }

    public static void main(String[] args) throws IOException
    {

        if (args.length > 0)
        {
            try
            {
                _PORT = Integer.parseInt(args[0]);
            }
            catch (NumberFormatException e)
            {
                //IGNORE so use default port 9999;
            }
        }

        IOWriterServer a = new IOWriterServer();
        a.startAcceptor();
    }
}