Class MyServer

java.lang.Object
mazealgo.model.server.MyServer

public class MyServer extends Object
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

    Constructors
    Constructor
    Description
    MyServer(int port, int listeningIntervalMS, IServerStrategy strategy)
     
    MyServer(int port, int listeningIntervalMS, IServerStrategy strategy, ExecutorService executor)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the port the server is bound to (useful when port 0 was passed).
    void
    Binds the listening socket synchronously and starts the accept loop on a background daemon thread.
    void
    Asks the accept loop to exit and waits up to a few seconds for in-flight client work to finish.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • 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.