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 class
BaseServer.Reaper
-
Field Summary
Fields Modifier and Type Field Description protected java.net.InetAddress
client_bind_addr
protected int
client_bind_port
protected long
conn_expire_time
protected java.util.List<ConnectionListener>
conn_listeners
protected java.util.Map<Address,Connection>
conns
protected boolean
defer_client_binding
protected ThreadFactory
factory
static byte[]
FAIL
protected int
linger
protected Address
local_addr
protected Log
log
protected boolean
log_details
protected int
max_length
static byte[]
OK
protected BaseServer.Reaper
reaper
protected long
reaperInterval
protected Receiver
receiver
protected int
recv_buf_size
protected java.util.concurrent.atomic.AtomicBoolean
running
protected int
send_buf_size
protected int
sock_conn_timeout
protected SocketFactory
socket_factory
protected boolean
tcp_nodelay
protected TimeService
time_service
protected boolean
use_acks
protected boolean
use_peer_connections
-
Constructor Summary
Constructors Modifier Constructor Description protected
BaseServer(ThreadFactory f, SocketFactory sf, int recv_buf_size)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addConnection(Address peer_addr, Connection conn)
BaseServer
addConnectionListener(ConnectionListener cl)
void
clearConnections()
Used only for testing !java.net.InetAddress
clientBindAddress()
BaseServer
clientBindAddress(java.net.InetAddress addr)
int
clientBindPort()
BaseServer
clientBindPort(int port)
void
close()
boolean
closeConnection(Address addr)
boolean
closeConnection(Address addr, boolean notify)
void
closeConnection(Connection conn)
void
closeConnection(Connection conn, boolean notify)
protected static boolean
connected(Connection c)
void
connectionClosed(Connection conn)
void
connectionEstablished(Connection conn)
boolean
connectionEstablishedTo(Address address)
long
connExpireTime()
BaseServer
connExpireTimeout(long t)
protected abstract Connection
createConnection(Address dest)
Creates a new connection object to target dest, but doesn't yet connect itboolean
deferClientBinding()
BaseServer
deferClientBinding(boolean defer)
protected static java.lang.String
explanation(boolean connection_existed, boolean replace)
void
flush(Address dest)
void
flushAll()
void
forAllConnections(java.util.function.BiConsumer<Address,Connection> c)
Connection
getConnection(Address dest)
Creates a new connection to dest, or returns an existing oneConnection
getConnection(Address dest, boolean retry)
int
getMaxLength()
int
getNumConnections()
int
getNumOpenConnections()
boolean
hasConnection(Address address)
int
linger()
BaseServer
linger(int linger)
Address
localAddress()
protected static Address
localAddress(java.net.InetAddress bind_addr, int local_port, java.net.InetAddress external_addr, int external_port)
Log
log()
BaseServer
log(Log the_log)
boolean
logDetails()
BaseServer
logDetails(boolean l)
void
notifyConnectionClosed(Connection conn)
void
notifyConnectionEstablished(Connection conn)
java.lang.String
printConnections()
long
reaperInterval()
BaseServer
reaperInterval(long interval)
void
receive(Address sender, byte[] data, int offset, int length)
Called by aConnection
implementation when a message has been received.void
receive(Address sender, java.io.DataInput in, int len)
void
receive(Address sender, java.nio.ByteBuffer buf)
Called by aConnection
implementation when a message has been receivedint
receiveBufferSize()
BaseServer
receiveBufferSize(int recv_buf_size)
Receiver
receiver()
BaseServer
receiver(Receiver r)
void
removeConnectionIfPresent(Address address, Connection conn)
Only removes the connection if conns.get(address) == connBaseServer
removeConnectionListener(ConnectionListener cl)
void
replaceConnection(Address address, Connection conn)
void
retainAll(java.util.Collection<Address> current_mbrs)
Removes all connections which are not in current_mbrsboolean
running()
void
send(Address dest, byte[] data, int offset, int length)
void
send(Address dest, java.nio.ByteBuffer data)
int
sendBufferSize()
BaseServer
sendBufferSize(int send_buf_size)
void
sendToAll(byte[] data, int offset, int length)
void
sendToAll(java.nio.ByteBuffer data)
BaseServer
setMaxLength(int len)
int
socketConnectionTimeout()
BaseServer
socketConnectionTimeout(int timeout)
SocketFactory
socketFactory()
BaseServer
socketFactory(SocketFactory factory)
void
start()
Starts accepting connections.void
stop()
Stops listening for connections and handling traffic.boolean
tcpNodelay()
BaseServer
tcpNodelay(boolean tcp_nodelay)
TimeService
timeService()
BaseServer
timeService(TimeService ts)
java.lang.String
toString()
java.lang.String
toString(boolean details)
boolean
useAcks()
BaseServer
useAcks(boolean f)
boolean
usePeerConnections()
BaseServer
usePeerConnections(boolean flag)
protected <T> boolean
validateArgs(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
-
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
-
use_acks
protected boolean use_acks
-
log_details
protected boolean log_details
-
sock_conn_timeout
protected int sock_conn_timeout
-
tcp_nodelay
protected boolean tcp_nodelay
-
linger
protected int linger
-
time_service
protected TimeService time_service
-
OK
public static final byte[] OK
-
FAIL
public static final byte[] FAIL
-
-
Constructor Detail
-
BaseServer
protected BaseServer(ThreadFactory f, SocketFactory sf, int recv_buf_size)
-
-
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)
-
useAcks
public boolean useAcks()
-
useAcks
public BaseServer useAcks(boolean f)
-
logDetails
public boolean logDetails()
-
logDetails
public BaseServer logDetails(boolean l)
-
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.Exception
Starts 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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
flush
public void flush(Address dest)
-
flushAll
public void flushAll()
-
receive
public void receive(Address sender, byte[] data, int offset, int length)
Called by aConnection
implementation 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 aConnection
implementation 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:
connectionClosed
in interfaceConnectionListener
-
connectionEstablished
public void connectionEstablished(Connection conn)
- Specified by:
connectionEstablished
in 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, boolean retry) throws java.lang.Exception
- Throws:
java.lang.Exception
-
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)
-
closeConnection
public void closeConnection(Connection conn, boolean notify)
-
closeConnection
public boolean closeConnection(Address addr)
-
closeConnection
public boolean closeConnection(Address addr, boolean notify)
-
addConnection
public void addConnection(Address peer_addr, Connection conn) throws java.lang.Exception
- Throws:
java.lang.Exception
-
addConnectionListener
public BaseServer addConnectionListener(ConnectionListener cl)
-
removeConnectionListener
public BaseServer removeConnectionListener(ConnectionListener cl)
-
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 !
-
forAllConnections
public void forAllConnections(java.util.function.BiConsumer<Address,Connection> c)
-
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:
toString
in classjava.lang.Object
-
toString
public java.lang.String toString(boolean details)
-
sendToAll
public void sendToAll(byte[] data, int offset, int length)
-
sendToAll
public void sendToAll(java.nio.ByteBuffer data)
-
connected
protected static boolean connected(Connection c)
-
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)
-
-