Class 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
    • Field Detail

      • local_addr

        protected Address local_addr
      • sock_creation_lock

        protected final java.util.concurrent.locks.Lock sock_creation_lock
      • reaperInterval

        protected long reaperInterval
      • 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
    • Method Detail

      • reaperInterval

        public long reaperInterval()
      • reaperInterval

        public BaseServer reaperInterval​(long interval)
      • log

        public Log 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)
      • 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)
      • 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 interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • receive

        public void receive​(Address sender,
                            byte[] data,
                            int offset,
                            int length)
        Called by a Connection 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 a Connection 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
      • 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
      • 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:
        toString in class java.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)