package dismembery.server; import java.io.*; import java.net.*; import dismembery.common.*; /** * * The ServerStart class starts a per-client thread, * GameHandler, to handle communication with Clients/Views. *
* The GameHandler currently uses one game, created by * this class, ServerStart. It should be possible to create * new games when a game is full, for example, or when enough * time has elapsed since the first Client was added to the game. *
* All Client->Server communication is handled by the per-Client * GameHandler class which has the input-stream associated with * each Client. Outgoing communication is handled by the Game * object which eithe broadcasts information to all Clients or * sends per-Client information as appropriate. The Game class * maintains a map of Client->OutputStream pairs for this * Server->Client communication. *
* Note that there is then one thread per client on the * server side, though all * handler threads share some common resources such * as a game. * *@author Owen Astrachan * */ public class ServerStart extends Thread { private int myRequestCount; // # requests handled private ServerSocket myServerSocket; // client request source private Game myGame; // currently the only game public ServerStart(int port) { myRequestCount = 0; myGame = new Game(); try{ myServerSocket = new ServerSocket(port); Log.println("server started"); } catch(Exception e){ Log.println("Server error "+e); } } public void run() { while (true) { try { // accept blocks until request comes over socket Socket intoServer = myServerSocket.accept(); Thread t = new GameHandler(intoServer,myGame); t.start(); Log.println("Serviced client "+myRequestCount); myRequestCount++; } catch (Exception e){ Log.println("trouble with server socket "+e); } } } public static void main(String args[]) { ServerStart server = new ServerStart(Globals.PORT); server.start(); } }