- java.lang.Object
-
- dev.bitbite.networking.Server
-
- Direct Known Subclasses:
SSLServer
public abstract class Server extends java.lang.ObjectRepresents an abstract implementation of the server-side connection.
The server must be started usingstart()for clients to be able to connect.
Shutting down the server can be done usingclose()
Incoming data from any client will be processed by the DataProcessingLayers and then propagated toprocessReceivedData(String, byte[])containing the clients address of the client the data came from. In order to send data to the client you must request the properCommunicationHandlerusing the serversClientManager(ClientManager.getCommunicationHandlerByIP(String))
-
-
Field Summary
Fields Modifier and Type Field Description protected ClientManagerclientManagerprotected DataPreProcessordataPreProcessorprotected DisconnectedClientDetectordisconnectedClientDetectorprotected java.util.ArrayList<IOHandlerListener>ioListenersprotected java.util.ArrayList<ServerListener>listenersintPORTprotected java.net.ServerSocketserverSocket
-
Constructor Summary
Constructors Constructor Description Server(int port)Creates a server object and sets the port the server will try to listen on
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidbroadcast(byte[] data)Sends the data to all connected clients.voidclose()Initiates the closing process of the Server with closing theClientManagerand disabling theDataProcessingLayers.ClientManagergetClientManager()Returns the current ClientManagerprotected DataPreProcessorgetDataPreProcessor()Returns the DataPreProcessor of this server object.java.util.ArrayList<IOHandlerListener>getIOHandlerListeners()Returns a list of all IOHandlers registered at the serverjava.util.ArrayList<ServerListener>getServerListeners()Returns a list of all ServerListeners registered at the serverprotected java.net.ServerSocketgetServerSocket()Returns the current ServerSocketprotected voidnotifyListeners(dev.bitbite.networking.Server.EventType type, java.lang.Object... args)Calls the respective function of each listener depending on the event type.
Optionally Propagates additional info such as exceptions.protected voidopenServerSocket()Opens theServerSocket.protected abstract voidprocessReceivedData(java.lang.String clientAddress, byte[] data)This function will be called once the server receives data from the client.voidregisterListener(IOHandlerListener listener)Registers an IOHandlerListenervoidregisterListener(ServerListener listener)Registers a ClientListenervoidremoveListener(IOHandlerListener listener)Removes IOHandlerListener from the listenersvoidremoveListener(ServerListener listener)Removes ClientListener from the listenersbooleansend(java.lang.String clientAddress, byte[] data)voidsetSoTimeout(int timeout)Sets SO_TIMEOUT of the serversocketvoidstart()Opens aServerSocket, initializes theDataProcessingLayers and starts listening on the specified port
-
-
-
Field Detail
-
PORT
public final int PORT
-
serverSocket
protected java.net.ServerSocket serverSocket
-
clientManager
protected ClientManager clientManager
-
dataPreProcessor
protected DataPreProcessor dataPreProcessor
-
disconnectedClientDetector
protected DisconnectedClientDetector disconnectedClientDetector
-
listeners
protected java.util.ArrayList<ServerListener> listeners
-
ioListeners
protected java.util.ArrayList<IOHandlerListener> ioListeners
-
-
Method Detail
-
start
public void start()
Opens aServerSocket, initializes theDataProcessingLayers and starts listening on the specified port
-
openServerSocket
protected void openServerSocket() throws java.io.IOExceptionOpens theServerSocket. Moved to a different function to make it easier to replace the ServerSocket implementation.- Throws:
java.io.IOException- when the process of opening the ServerSocket fails.
-
close
public void close()
Initiates the closing process of the Server with closing theClientManagerand disabling theDataProcessingLayers. Finally it closes the serverSocket
-
processReceivedData
protected abstract void processReceivedData(java.lang.String clientAddress, byte[] data)This function will be called once the server receives data from the client.- Parameters:
clientAddress- of the client the data came fromdata- sent by the server
-
send
public boolean send(java.lang.String clientAddress, byte[] data)
-
broadcast
public void broadcast(byte[] data)
Sends the data to all connected clients.- Parameters:
data- to broadcast
-
registerListener
public void registerListener(ServerListener listener)
Registers a ClientListener- Parameters:
listener- to add
-
registerListener
public void registerListener(IOHandlerListener listener)
Registers an IOHandlerListener- Parameters:
listener- to add
-
removeListener
public void removeListener(ServerListener listener)
Removes ClientListener from the listeners- Parameters:
listener- to remove
-
removeListener
public void removeListener(IOHandlerListener listener)
Removes IOHandlerListener from the listeners- Parameters:
listener- to remove
-
notifyListeners
protected void notifyListeners(dev.bitbite.networking.Server.EventType type, java.lang.Object... args)Calls the respective function of each listener depending on the event type.
Optionally Propagates additional info such as exceptions.- Parameters:
type- of event that occuredargs- optional additional data- Throws:
java.lang.IllegalArgumentException- if additional arguments are supplied whose types do not match the expected types of the listeners eventfunction- See Also:
ServerListener
-
getServerSocket
protected java.net.ServerSocket getServerSocket()
Returns the current ServerSocket- Returns:
- the current ServerSocket
-
getClientManager
public ClientManager getClientManager()
Returns the current ClientManager- Returns:
- the current ClientManager
-
getServerListeners
public java.util.ArrayList<ServerListener> getServerListeners()
Returns a list of all ServerListeners registered at the server- Returns:
- the list of ServerListeners
-
getIOHandlerListeners
public java.util.ArrayList<IOHandlerListener> getIOHandlerListeners()
Returns a list of all IOHandlers registered at the server- Returns:
- the list of IOHandlers
-
getDataPreProcessor
protected DataPreProcessor getDataPreProcessor()
Returns the DataPreProcessor of this server object.- Returns:
- the DataPreProcessor of this server object
-
setSoTimeout
public void setSoTimeout(int timeout) throws java.net.SocketExceptionSets SO_TIMEOUT of the serversocket- Parameters:
timeout- in milliseconds after which blocking operations should stop blocking- Throws:
java.net.SocketException- when setting SO_TIMEOUT fails
-
-