package GPServer; /** *
Title:
*Description:
*Copyright: Copyright (c) 2000
*Company:
* @author not attributable * @version 1.0 */ import java.util.*; import java.io.*; import java.net.*; public class ServerThread extends Thread { static GameServer server; private Socket s; private DataInputStream din; private DataOutputStream dout; private boolean THIS_THREAD_ACTIVE; public ServerThread(Socket s) { this.s = s; try{ din = new DataInputStream(s.getInputStream()); dout = new DataOutputStream(s.getOutputStream()); } catch(IOException e){ e.printStackTrace(); } THIS_THREAD_ACTIVE = true; start(); } public void run(){ try{ while (THIS_THREAD_ACTIVE) { String message = din.readUTF(); if (message == "new") { startGame(); } else if(message.regionMatches(0,"join",0,4)){ joinGame( message.substring(5) ); } else if(message == "get-opengames"){ System.out.println(message); getOpenGamesList(); } } } catch(IOException e){ e.printStackTrace(); } } private void startGame(){ GameThread game = new GameThread(s); server.addOpenGame(game,s.toString()); THIS_THREAD_ACTIVE = false; } private void joinGame(String key){ GameThread game = server.getOpenGame(key); game.joinGame(s); THIS_THREAD_ACTIVE = false; } private void getOpenGamesList() throws IOException{ Enumeration list; list = server.getOpenGamesList(); dout.writeUTF("Opengames-LIST"); System.out.println("open saadetud"); while(list.hasMoreElements()){ dout.writeUTF( (String)list.nextElement() ); } dout.writeUTF("END"); } }