org.jgroups.blocks

Class ConnectionTable

public class ConnectionTable extends Object implements Runnable

Manages incoming and outgoing TCP connections. For each outgoing message to destination P, if there is not yet a connection for P, one will be created. Subsequent outgoing messages will use this connection. For incoming messages, one server socket is created at startup. For each new incoming client connecting, a new thread from a thread pool is allocated and listens for incoming messages until the socket is closed by the peer.
Sockets/threads with no activity will be killed after some time.

Incoming messages from any of the sockets can be received by setting the message listener.

Author: Bela Ban

Nested Class Summary
interfaceConnectionTable.ConnectionListener
Used to be notified about connection establishment and teardown.
interfaceConnectionTable.Receiver
Used for message reception.
Field Summary
protected Loglog
Constructor Summary
ConnectionTable(int srv_port)
Regular ConnectionTable without expiration of idle connections
ConnectionTable(InetAddress bind_addr, int srv_port)
ConnectionTable(int srv_port, long reaper_interval, long conn_expire_time)
ConnectionTable including a connection reaper.
ConnectionTable(ConnectionTable.Receiver r, InetAddress bind_addr, InetAddress external_addr, int srv_port, int max_port)
Create a ConnectionTable
ConnectionTable(ConnectionTable.Receiver r, InetAddress bind_addr, InetAddress external_addr, int srv_port, int max_port, long reaper_interval, long conn_expire_time)
ConnectionTable including a connection reaper.
Method Summary
voidaddConnectionListener(ConnectionTable.ConnectionListener l)
protected ServerSocketcreateServerSocket(int start_port, int end_port)
Finds first available port starting at start_port and returns server socket.
AddressgetLocalAddress()
intgetNumConnections()
intgetReceiveBufferSize()
intgetSendBufferSize()
intgetSocketConnectionTimeout()
booleangetUseSendQueues()
protected voidinit()
voidreceive(Address sender, byte[] data, int offset, int length)
Calls the receiver callback.
voidremove(Address addr)
Remove addrfrom connection table.
voidremoveConnectionListener(ConnectionTable.ConnectionListener l)
voidrun()
Acceptor thread.
voidsend(Address dest, byte[] data, int offset, int length)
voidsetReceiveBufferSize(int recv_buf_size)
voidsetReceiver(ConnectionTable.Receiver r)
voidsetSendBufferSize(int send_buf_size)
voidsetSocketConnectionTimeout(int sock_conn_timeout)
voidsetUseSendQueues(boolean flag)
voidstart()
voidstop()
Closes all open sockets, the server socket and all threads waiting for incoming messages
StringtoString()

Field Detail

log

protected final Log log

Constructor Detail

ConnectionTable

public ConnectionTable(int srv_port)
Regular ConnectionTable without expiration of idle connections

Parameters: srv_port The port on which the server will listen. If this port is reserved, the next free port will be taken (incrementing srv_port).

ConnectionTable

public ConnectionTable(InetAddress bind_addr, int srv_port)

ConnectionTable

public ConnectionTable(int srv_port, long reaper_interval, long conn_expire_time)
ConnectionTable including a connection reaper. Connections that have been idle for more than conn_expire_time milliseconds will be closed and removed from the connection table. On next access they will be re-created.

Parameters: srv_port The port on which the server will listen reaper_interval Number of milliseconds to wait for reaper between attepts to reap idle connections conn_expire_time Number of milliseconds a connection can be idle (no traffic sent or received until it will be reaped

ConnectionTable

public ConnectionTable(ConnectionTable.Receiver r, InetAddress bind_addr, InetAddress external_addr, int srv_port, int max_port)
Create a ConnectionTable

Parameters: r A reference to a receiver of all messages received by this class. Method receive() will be called. bind_addr The host name or IP address of the interface to which the server socket will bind. This is interesting only in multi-homed systems. If bind_addr is null, the server socket will bind to the first available interface (e.g. /dev/hme0 on Solaris or /dev/eth0 on Linux systems). external_addr The address which will be broadcast to the group (the externally visible address which this host should be contacted on). If external_addr is null, it will default to the same address that the server socket is bound to. srv_port The port to which the server socket will bind to. If this port is reserved, the next free port will be taken (incrementing srv_port). max_port The largest port number that the server socket will be bound to. If max_port < srv_port then there is no limit.

ConnectionTable

public ConnectionTable(ConnectionTable.Receiver r, InetAddress bind_addr, InetAddress external_addr, int srv_port, int max_port, long reaper_interval, long conn_expire_time)
ConnectionTable including a connection reaper. Connections that have been idle for more than conn_expire_time milliseconds will be closed and removed from the connection table. On next access they will be re-created.

Parameters: r The Receiver bind_addr The host name or IP address of the interface to which the server socket will bind. This is interesting only in multi-homed systems. If bind_addr is null, the server socket will bind to the first available interface (e.g. /dev/hme0 on Solaris or /dev/eth0 on Linux systems). external_addr The address which will be broadcast to the group (the externally visible address which this host should be contacted on). If external_addr is null, it will default to the same address that the server socket is bound to. srv_port The port to which the server socket will bind to. If this port is reserved, the next free port will be taken (incrementing srv_port). max_port The largest port number that the server socket will be bound to. If max_port < srv_port then there is no limit. reaper_interval Number of milliseconds to wait for reaper between attepts to reap idle connections conn_expire_time Number of milliseconds a connection can be idle (no traffic sent or received until it will be reaped

Method Detail

addConnectionListener

public void addConnectionListener(ConnectionTable.ConnectionListener l)

createServerSocket

protected ServerSocket createServerSocket(int start_port, int end_port)
Finds first available port starting at start_port and returns server socket. Will not bind to port >end_port. Sets srv_port

getLocalAddress

public Address getLocalAddress()

getNumConnections

public int getNumConnections()

getReceiveBufferSize

public int getReceiveBufferSize()

getSendBufferSize

public int getSendBufferSize()

getSocketConnectionTimeout

public int getSocketConnectionTimeout()

getUseSendQueues

public boolean getUseSendQueues()

init

protected void init()

receive

public void receive(Address sender, byte[] data, int offset, int length)
Calls the receiver callback. We serialize access to this method because it may be called concurrently by several Connection handler threads. Therefore the receiver doesn't need to synchronize.

remove

public void remove(Address addr)
Remove addrfrom connection table. This is typically triggered when a member is suspected.

removeConnectionListener

public void removeConnectionListener(ConnectionTable.ConnectionListener l)

run

public void run()
Acceptor thread. Continuously accept new connections. Create a new thread for each new connection and put it in conns. When the thread should stop, it is interrupted by the thread creator.

send

public void send(Address dest, byte[] data, int offset, int length)

setReceiveBufferSize

public void setReceiveBufferSize(int recv_buf_size)

setReceiver

public void setReceiver(ConnectionTable.Receiver r)

setSendBufferSize

public void setSendBufferSize(int send_buf_size)

setSocketConnectionTimeout

public void setSocketConnectionTimeout(int sock_conn_timeout)

setUseSendQueues

public void setUseSendQueues(boolean flag)

start

public void start()

stop

public void stop()
Closes all open sockets, the server socket and all threads waiting for incoming messages

toString

public String toString()
Copyright ? 1998-2005 Bela Ban. All Rights Reserved.