Class Server
java.lang.Object
java.lang.Thread
fr.r1r0r0.deltaengine.model.engines.network.server.Server
- All Implemented Interfaces:
java.lang.Runnable
public final class Server
extends java.lang.Thread
The server implementation for DeltaEngine.
Creates a server using java Socket and handle them.
It is possible to send or receive collections of objects with connected clients,
or send data to specific client
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<ClientHandler>
clients
private java.lang.Thread
connectionHandler
private boolean
interrupted
private java.util.concurrent.locks.ReadWriteLock
lock
private java.net.ServerSocket
server
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
Constructors Constructor Description Server(int port)
Default constructor. -
Method Summary
Modifier and Type Method Description void
close()
Close server socket, stop accepting new clients, and close all client connectionsvoid
closeClient(int clientIndex)
Close specific client from all connected clients and remove it from handled clientsboolean
isInterrupted()
int
nbConnectedClients()
Returns number of connected clients currentlyjava.util.List<java.util.Collection<java.lang.Object>>
receive()
Retrieve all data received by clients until call of this method.void
run()
void
send(java.lang.Object... objects)
Send collection of objects to all clients (broadcast)void
sendToClient(int clientIndex, java.lang.Object... objects)
Send collection of objects to a specific clientMethods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Field Details
-
server
private final java.net.ServerSocket server -
clients
-
connectionHandler
private final java.lang.Thread connectionHandler -
lock
private final java.util.concurrent.locks.ReadWriteLock lock -
interrupted
private volatile boolean interrupted
-
-
Constructor Details
-
Server
public Server(int port) throws java.io.IOExceptionDefault constructor. Declare a new server with specified port, and prepare to receive new clients when server will be launched.- Parameters:
port
- server port- Throws:
java.io.IOException
- exception from ServerSocket when it occurs
-
-
Method Details
-
run
public void run()- Specified by:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
send
public void send(java.lang.Object... objects)Send collection of objects to all clients (broadcast)- Parameters:
objects
- objects to send
-
sendToClient
public void sendToClient(int clientIndex, java.lang.Object... objects) throws java.io.IOExceptionSend collection of objects to a specific client- Parameters:
clientIndex
- index of specific clientobjects
- objects to send- Throws:
java.io.IOException
- when socket exception occurs
-
receive
public java.util.List<java.util.Collection<java.lang.Object>> receive()Retrieve all data received by clients until call of this method.- Returns:
- List of collection of objects for each client
-
close
public void close()Close server socket, stop accepting new clients, and close all client connections -
closeClient
public void closeClient(int clientIndex)Close specific client from all connected clients and remove it from handled clients- Parameters:
clientIndex
- index of specific client
-
isInterrupted
public boolean isInterrupted()- Overrides:
isInterrupted
in classjava.lang.Thread
-
nbConnectedClients
public int nbConnectedClients()Returns number of connected clients currently- Returns:
- integer number of connected clients
-