- 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.protected 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)voidstart()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
-
-