Package org.jgroups.blocks.cs
Class BaseServer
- java.lang.Object
-
- org.jgroups.blocks.cs.BaseServer
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,ConnectionListener
- Direct Known Subclasses:
NioBaseServer,TcpBaseServer
public abstract class BaseServer extends java.lang.Object implements java.io.Closeable, ConnectionListener
Abstract class for a server handling sending, receiving and connection management.- Since:
- 3.6.5
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classBaseServer.Reaper
-
Field Summary
Fields Modifier and Type Field Description protected java.net.InetAddressclient_bind_addrprotected intclient_bind_portprotected longconn_expire_timeprotected java.util.List<ConnectionListener>conn_listenersprotected java.util.Map<Address,Connection>connsprotected booleandefer_client_bindingprotected ThreadFactoryfactoryprotected intlingerprotected Addresslocal_addrprotected Loglogprotected intmax_lengthprotected BaseServer.Reaperreaperprotected longreaperIntervalprotected Receiverreceiverprotected intrecv_buf_sizeprotected java.util.concurrent.atomic.AtomicBooleanrunningprotected intsend_buf_sizeprotected intsock_conn_timeoutprotected java.util.concurrent.locks.Locksock_creation_lockprotected SocketFactorysocket_factoryprotected booleantcp_nodelayprotected TimeServicetime_serviceprotected booleanuse_peer_connections
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseServer(ThreadFactory f, SocketFactory sf)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddConnection(Address peer_addr, Connection conn)voidaddConnectionListener(ConnectionListener cml)voidclearConnections()Used only for testing !java.net.InetAddressclientBindAddress()BaseServerclientBindAddress(java.net.InetAddress addr)intclientBindPort()BaseServerclientBindPort(int port)voidclose()voidcloseConnection(Connection conn)voidconnectionClosed(Connection conn)voidconnectionEstablished(Connection conn)booleanconnectionEstablishedTo(Address address)longconnExpireTime()BaseServerconnExpireTimeout(long t)protected abstract ConnectioncreateConnection(Address dest)Creates a new connection object to target dest, but doesn't yet connect itbooleandeferClientBinding()BaseServerdeferClientBinding(boolean defer)protected static java.lang.Stringexplanation(boolean connection_existed, boolean replace)ConnectiongetConnection(Address dest)Creates a new connection to dest, or returns an existing oneintgetMaxLength()intgetNumConnections()intgetNumOpenConnections()booleanhasConnection(Address address)intlinger()BaseServerlinger(int linger)AddresslocalAddress()protected static AddresslocalAddress(java.net.InetAddress bind_addr, int local_port, java.net.InetAddress external_addr, int external_port)Loglog()BaseServerlog(Log the_log)voidnotifyConnectionClosed(Connection conn)voidnotifyConnectionEstablished(Connection conn)java.lang.StringprintConnections()longreaperInterval()BaseServerreaperInterval(long interval)voidreceive(Address sender, byte[] data, int offset, int length)Called by aConnectionimplementation when a message has been received.voidreceive(Address sender, java.io.DataInput in, int len)voidreceive(Address sender, java.nio.ByteBuffer buf)Called by aConnectionimplementation when a message has been receivedintreceiveBufferSize()BaseServerreceiveBufferSize(int recv_buf_size)Receiverreceiver()BaseServerreceiver(Receiver r)voidremoveConnectionIfPresent(Address address, Connection conn)Only removes the connection if conns.get(address) == connvoidremoveConnectionListener(ConnectionListener cml)voidreplaceConnection(Address address, Connection conn)voidretainAll(java.util.Collection<Address> current_mbrs)Removes all connections which are not in current_mbrsbooleanrunning()voidsend(Address dest, byte[] data, int offset, int length)voidsend(Address dest, java.nio.ByteBuffer data)intsendBufferSize()BaseServersendBufferSize(int send_buf_size)protected voidsendToAll(byte[] data, int offset, int length)protected voidsendToAll(java.nio.ByteBuffer data)BaseServersetMaxLength(int len)intsocketConnectionTimeout()BaseServersocketConnectionTimeout(int timeout)SocketFactorysocketFactory()BaseServersocketFactory(SocketFactory factory)voidstart()Starts accepting connections.voidstop()Stops listening for connections and handling traffic.booleantcpNodelay()BaseServertcpNodelay(boolean tcp_nodelay)TimeServicetimeService()BaseServertimeService(TimeService ts)java.lang.StringtoString()booleanusePeerConnections()BaseServerusePeerConnections(boolean flag)protected <T> booleanvalidateArgs(Address dest, T buffer)
-
-
-
Field Detail
-
local_addr
protected Address local_addr
-
conn_listeners
protected final java.util.List<ConnectionListener> conn_listeners
-
conns
protected final java.util.Map<Address,Connection> conns
-
sock_creation_lock
protected final java.util.concurrent.locks.Lock sock_creation_lock
-
factory
protected final ThreadFactory factory
-
socket_factory
protected SocketFactory socket_factory
-
reaperInterval
protected long reaperInterval
-
reaper
protected BaseServer.Reaper reaper
-
receiver
protected Receiver receiver
-
running
protected final java.util.concurrent.atomic.AtomicBoolean running
-
log
protected Log log
-
client_bind_addr
protected java.net.InetAddress client_bind_addr
-
client_bind_port
protected int client_bind_port
-
defer_client_binding
protected boolean defer_client_binding
-
conn_expire_time
protected long conn_expire_time
-
recv_buf_size
protected int recv_buf_size
-
send_buf_size
protected int send_buf_size
-
max_length
protected int max_length
-
use_peer_connections
protected boolean use_peer_connections
-
sock_conn_timeout
protected int sock_conn_timeout
-
tcp_nodelay
protected boolean tcp_nodelay
-
linger
protected int linger
-
time_service
protected TimeService time_service
-
-
Constructor Detail
-
BaseServer
protected BaseServer(ThreadFactory f, SocketFactory sf)
-
-
Method Detail
-
receiver
public Receiver receiver()
-
receiver
public BaseServer receiver(Receiver r)
-
reaperInterval
public long reaperInterval()
-
reaperInterval
public BaseServer reaperInterval(long interval)
-
log
public Log log()
-
log
public BaseServer log(Log the_log)
-
localAddress
public Address localAddress()
-
clientBindAddress
public java.net.InetAddress clientBindAddress()
-
clientBindAddress
public BaseServer clientBindAddress(java.net.InetAddress addr)
-
clientBindPort
public int clientBindPort()
-
clientBindPort
public BaseServer clientBindPort(int port)
-
deferClientBinding
public boolean deferClientBinding()
-
deferClientBinding
public BaseServer deferClientBinding(boolean defer)
-
socketFactory
public SocketFactory socketFactory()
-
socketFactory
public BaseServer socketFactory(SocketFactory factory)
-
usePeerConnections
public boolean usePeerConnections()
-
usePeerConnections
public BaseServer usePeerConnections(boolean flag)
-
socketConnectionTimeout
public int socketConnectionTimeout()
-
socketConnectionTimeout
public BaseServer socketConnectionTimeout(int timeout)
-
connExpireTime
public long connExpireTime()
-
connExpireTimeout
public BaseServer connExpireTimeout(long t)
-
timeService
public TimeService timeService()
-
timeService
public BaseServer timeService(TimeService ts)
-
receiveBufferSize
public int receiveBufferSize()
-
receiveBufferSize
public BaseServer receiveBufferSize(int recv_buf_size)
-
sendBufferSize
public int sendBufferSize()
-
sendBufferSize
public BaseServer sendBufferSize(int send_buf_size)
-
getMaxLength
public int getMaxLength()
-
setMaxLength
public BaseServer setMaxLength(int len)
-
linger
public int linger()
-
linger
public BaseServer linger(int linger)
-
tcpNodelay
public boolean tcpNodelay()
-
tcpNodelay
public BaseServer tcpNodelay(boolean tcp_nodelay)
-
running
public boolean running()
-
getNumConnections
public int getNumConnections()
-
getNumOpenConnections
public int getNumOpenConnections()
-
start
public void start() throws java.lang.ExceptionStarts accepting connections. Typically, socket handler or selectors thread are started here.- Throws:
java.lang.Exception
-
stop
public void stop()
Stops listening for connections and handling traffic. Typically, socket handler or selector threads are stopped, and server sockets or channels are closed.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
receive
public void receive(Address sender, byte[] data, int offset, int length)
Called by aConnectionimplementation when a message has been received. Note that data might be a reused buffer, so unless used to de-serialize an object from it, it should be copied (e.g. if we store a ref to it beyone the scope of this receive() method)
-
receive
public void receive(Address sender, java.nio.ByteBuffer buf)
Called by aConnectionimplementation when a message has been received
-
receive
public void receive(Address sender, java.io.DataInput in, int len) throws java.lang.Exception
- Throws:
java.lang.Exception
-
send
public void send(Address dest, byte[] data, int offset, int length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
send
public void send(Address dest, java.nio.ByteBuffer data) throws java.lang.Exception
- Throws:
java.lang.Exception
-
connectionClosed
public void connectionClosed(Connection conn)
- Specified by:
connectionClosedin interfaceConnectionListener
-
connectionEstablished
public void connectionEstablished(Connection conn)
- Specified by:
connectionEstablishedin interfaceConnectionListener
-
createConnection
protected abstract Connection createConnection(Address dest) throws java.lang.Exception
Creates a new connection object to target dest, but doesn't yet connect it- Throws:
java.lang.Exception
-
hasConnection
public boolean hasConnection(Address address)
-
connectionEstablishedTo
public boolean connectionEstablishedTo(Address address)
-
getConnection
public Connection getConnection(Address dest) throws java.lang.Exception
Creates a new connection to dest, or returns an existing one- Throws:
java.lang.Exception
-
replaceConnection
public void replaceConnection(Address address, Connection conn)
-
closeConnection
public void closeConnection(Connection conn)
-
addConnection
public void addConnection(Address peer_addr, Connection conn) throws java.lang.Exception
- Throws:
java.lang.Exception
-
addConnectionListener
public void addConnectionListener(ConnectionListener cml)
-
removeConnectionListener
public void removeConnectionListener(ConnectionListener cml)
-
printConnections
public java.lang.String printConnections()
-
removeConnectionIfPresent
public void removeConnectionIfPresent(Address address, Connection conn)
Only removes the connection if conns.get(address) == conn
-
clearConnections
public void clearConnections()
Used only for testing !
-
retainAll
public void retainAll(java.util.Collection<Address> current_mbrs)
Removes all connections which are not in current_mbrs
-
notifyConnectionClosed
public void notifyConnectionClosed(Connection conn)
-
notifyConnectionEstablished
public void notifyConnectionEstablished(Connection conn)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
sendToAll
protected void sendToAll(byte[] data, int offset, int length)
-
sendToAll
protected void sendToAll(java.nio.ByteBuffer data)
-
localAddress
protected static Address localAddress(java.net.InetAddress bind_addr, int local_port, java.net.InetAddress external_addr, int external_port)
-
validateArgs
protected <T> boolean validateArgs(Address dest, T buffer)
-
explanation
protected static java.lang.String explanation(boolean connection_existed, boolean replace)
-
-