summaryrefslogtreecommitdiff
path: root/java/newclient/src/main/java/org/apache/qpid/nclient/transport/TCPConnection.java
blob: 734aa68a9df385bbd4b72d7998f52578cdef0b54 (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
package org.apache.qpid.nclient.transport;

import org.apache.log4j.Logger;
import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoConnector;
import org.apache.mina.common.SimpleByteBufferAllocator;
import org.apache.mina.transport.socket.nio.SocketConnector;
import org.apache.mina.transport.socket.nio.SocketConnectorConfig;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;
import org.apache.qpid.nclient.config.ClientConfiguration;
import org.apache.qpid.nclient.core.AMQPException;
import org.apache.qpid.nclient.core.DefaultPhaseContext;
import org.apache.qpid.nclient.core.Phase;
import org.apache.qpid.nclient.core.PhaseContext;
import org.apache.qpid.nclient.core.PhaseFactory;
import org.apache.qpid.nclient.core.QpidConstants;
import org.apache.qpid.pool.ReadWriteThreadModel;

public class TCPConnection implements TransportConnection
{
    private static final Logger _logger = Logger.getLogger(TCPConnection.class);
    private BrokerDetails _brokerDetails;
    private IoConnector _ioConnector;
    private Phase _phase;  
    private PhaseContext _ctx;
    
    protected TCPConnection(ConnectionURL url, PhaseContext ctx)
    {
	_brokerDetails = url.getBrokerDetails(0);
	_ctx = ctx;
	
	ByteBuffer.setUseDirectBuffers(ClientConfiguration.get().getBoolean(QpidConstants.ENABLE_DIRECT_BUFFERS));

        // the MINA default is currently to use the pooled allocator although this may change in future
        // once more testing of the performance of the simple allocator has been done
        if (ClientConfiguration.get().getBoolean(QpidConstants.ENABLE_POOLED_ALLOCATOR))
        {
            // Not sure what the original code wanted use :)
        }
        else
        {
            _logger.info("Using SimpleByteBufferAllocator");
            ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
        }

        _ioConnector = new SocketConnector();
        SocketConnectorConfig cfg = (SocketConnectorConfig) _ioConnector.getDefaultConfig();

        // if we do not use our own thread model we get the MINA default which is to use
        // its own leader-follower model
        if (ClientConfiguration.get().getBoolean(QpidConstants.USE_SHARED_READ_WRITE_POOL))
        {
            cfg.setThreadModel(ReadWriteThreadModel.getInstance());
        }

        SocketSessionConfig scfg = (SocketSessionConfig) cfg.getSessionConfig();
        scfg.setTcpNoDelay(ClientConfiguration.get().getBoolean(QpidConstants.TCP_NO_DELAY));
        scfg.setSendBufferSize(ClientConfiguration.get().getInt(QpidConstants.SEND_BUFFER_SIZE_IN_KB)*1024);
        scfg.setReceiveBufferSize(ClientConfiguration.get().getInt(QpidConstants.RECEIVE_BUFFER_SIZE_IN_KB)*1024);
    }

    // Returns the phase pipe
    public Phase connect() throws AMQPException
    {		
	_ctx.setProperty(QpidConstants.AMQP_BROKER_DETAILS,_brokerDetails);
	_ctx.setProperty(QpidConstants.MINA_IO_CONNECTOR,_ioConnector);
	
	_phase = PhaseFactory.createPhasePipe(_ctx);
	_phase.start();
	
	return _phase;
    }

    public void close() throws AMQPException
    {
	
    }
    
    public Phase getPhasePipe()
    {
	return _phase;
    }
}