- java.lang.Object
-
- dev.bitbite.networking.Client
-
- Direct Known Subclasses:
SSLClient
public abstract class Client extends java.lang.ObjectRepresents an abstract implementation of the client-side connection.
The connection process can be initiated using the client objectsconnect()method, which will return true, if the connection process has been successful.
Closing the connection can be done using the client objectsclose()method, which will return true, if the disconnection process has been completed successfully.
Incoming data from the server will be preprocessed bypreprocessReceivedData(byte[])and then forwarded toprocessReceivedData(byte[]).
Data to the server can be sent using the clientsIOHandler.
Some events trigger the notification of registeredClientListeners.- See Also:
IOHandler,ClientListener
-
-
Field Summary
Fields Modifier and Type Field Description protected DisconnectedServerDetectordisconnectedServerDetectorjava.lang.StringHOSTintPORTprotected java.net.Socketsocket
-
Constructor Summary
Constructors Constructor Description Client(java.lang.String host, int port)Creates a Client object and sets the endpoint on which on startup the client will try to connect toClient(java.net.URL url)Creates a Client object and sets the endpoint to which the client will connect on startup.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanclose()Closes the connection.
Listeners will be called before the disconnection process is started and when it succeeded or failed.
It will also call the close method of the client objects IOHandlerbooleanconnect()Initiates the connection process.
The IOHandler will be created.booleanisConnected()Indicates whether the current client object has an active connection to the serverprotected voidopenSocket()Opens the Socket connection.protected voidpreprocessReceivedData(byte[] data)Preprocesses incomming data by sending it to theDataPreProcessor.protected abstract voidprocessReceivedData(byte[] data)This function will be called once the client receives data from the server.voidregisterListener(ClientListener listener)Registers a ClientListenervoidregisterListener(IOHandlerListener listener)Registers a IOHandlerListenervoidremoveListener(ClientListener listener)Removes ClientListener from the listenersvoidremoveListener(IOHandlerListener listener)Removes IOHandlerListener from the listenersvoidsend(byte[] data)Sends data to the Server
-
-
-
Field Detail
-
HOST
public final java.lang.String HOST
-
PORT
public final int PORT
-
socket
protected java.net.Socket socket
-
disconnectedServerDetector
protected DisconnectedServerDetector disconnectedServerDetector
-
-
Constructor Detail
-
Client
public Client(java.lang.String host, int port)Creates a Client object and sets the endpoint on which on startup the client will try to connect to- Parameters:
host- the domain or IP-adress without protocoll information or portport- to connect to
-
Client
public Client(java.net.URL url)
Creates a Client object and sets the endpoint to which the client will connect on startup.- Parameters:
url- the URL object that specifies the endpoint adress and port.
-
-
Method Detail
-
connect
public boolean connect()
Initiates the connection process.
The IOHandler will be created. Listeners will be called before the client tries to connect to the server, when the connection was successful, and when it wasn't.- Returns:
- true if the connection process as been completed successfully
-
openSocket
protected void openSocket() throws java.net.UnknownHostException, java.io.IOExceptionOpens the Socket connection. Moved to a different function to make it easier to replace the Socket implementation.- Throws:
java.net.UnknownHostException- if the specified Host is unknownjava.io.IOException- when the process of opening the Socket fails.
-
close
public boolean close()
Closes the connection.
Listeners will be called before the disconnection process is started and when it succeeded or failed.
It will also call the close method of the client objects IOHandler- Returns:
- true if the connection has been closed successfully
- See Also:
IOHandler.close()
-
send
public void send(byte[] data)
Sends data to the Server- Parameters:
data- to send
-
preprocessReceivedData
protected void preprocessReceivedData(byte[] data)
Preprocesses incomming data by sending it to theDataPreProcessor. The processed data is then forwarded toprocessReceivedData(byte[]);- Parameters:
data- to process
-
processReceivedData
protected abstract void processReceivedData(byte[] data)
This function will be called once the client receives data from the server.- Parameters:
data- sent by the server
-
registerListener
public void registerListener(ClientListener listener)
Registers a ClientListener- Parameters:
listener- to add
-
registerListener
public void registerListener(IOHandlerListener listener)
Registers a IOHandlerListener- Parameters:
listener- to add
-
removeListener
public void removeListener(ClientListener 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
-
isConnected
public boolean isConnected()
Indicates whether the current client object has an active connection to the server- Returns:
- true if there is an active connection to the server
-
-