Package mazealgo.model.server
Class MyServer
java.lang.Object
mazealgo.model.server.MyServer
A small accept-loop server that dispatches each accepted client to an
ExecutorService thread pool. The pool size caps concurrent
client handling; additional clients queue until a worker is free.
Lifecycle:
MyServer server = new MyServer(5400, 1000, new SolveMazeStrategy()); server.start(); // ... clients connect, are handled in parallel ... server.stop();
stop() signals the accept loop via a flag and shuts the
pool down; ServerSocket.setSoTimeout(int) keeps the loop
responsive (the listening interval) so the flag is checked even when
no clients are connecting.
-
Constructor Summary
ConstructorsConstructorDescriptionMyServer(int port, int listeningIntervalMS, IServerStrategy strategy) MyServer(int port, int listeningIntervalMS, IServerStrategy strategy, ExecutorService executor) -
Method Summary
Modifier and TypeMethodDescriptionintgetPort()Returns the port the server is bound to (useful when port 0 was passed).voidstart()Binds the listening socket synchronously and starts the accept loop on a background daemon thread.voidstop()Asks the accept loop to exit and waits up to a few seconds for in-flight client work to finish.
-
Constructor Details
-
MyServer
-
MyServer
public MyServer(int port, int listeningIntervalMS, IServerStrategy strategy, ExecutorService executor)
-
-
Method Details
-
start
public void start()Binds the listening socket synchronously and starts the accept loop on a background daemon thread. Throws if the port is taken; after this returns,getPort()reports the bound port. -
getPort
public int getPort()Returns the port the server is bound to (useful when port 0 was passed). -
stop
public void stop()Asks the accept loop to exit and waits up to a few seconds for in-flight client work to finish. Safe to call from any thread.
-