1 what would be the consequence of omitting game2pserver java line 58 2 what would b 5346931
1. What would be the consequence of omitting Game2PServer .java Line: 58?
2.What would be the consequence of changing Game2PServer.java Line 58 so that it was unconditional
3.Why can't Game2PServer.java: Line 54-55 be moved to just after Game2PServer,java Line 47?
1 import java.io.*;
2 import java.net.*;
3 import java.util.*;
4
5 abstract public class Game2PServer extends Thread {
6 static boolean playerAvailable = false;
7 static HashMap interconnect = null;
8
9 private boolean amFirst;
10 private Game2PGame myGame = null;
11 private HashMap myInterconnect = null;
12 private Socket sock = null;
13 private PrintStream toPlayer = null;
14 private PrintStream toOpponent = null;
15 private Scanner fromPlayer = null;
16
17 private void diag(String s) {
18 System.err.println(myThread()+”: Player:”+
19 (myGame==null?”none so far:”:amFirst?”first:”:”second:”)+s);
20 }
21 private static String myThread() {return Thread.currentThread().getName();}
22 private void waitFor2ndPlayer() {
23 while (myInterconnect.size()
24 try {Thread.sleep(5);} catch (InterruptedException ie) {}
25 }
26 private void closeAll() {
27 try {if (toPlayer!=null) toPlayer.close();} catch(Exception e) {}
28 try {if (fromPlayer!=null) fromPlayer.close();}catch(Exception e){}
29 try {if (sock!=null) sock.close();} catch(Exception e) {}
30 }
31 protected Game2PServer(Socket sock) {
32 this.sock = sock;
33 if (playerAvailable) {
34 amFirst=false;
35 diag(“SECOND CLIENT ACCEPTED”);
36 myInterconnect = interconnect;
37 interconnect = null;
38 myGame=getNewGame(amFirst);
39 playerAvailable=false;
40 } else {
41 amFirst=true;
42 diag(“FIRST CLIENT ACCEPTED”);
43 myInterconnect = new HashMap();
44 interconnect = myInterconnect;
45 myGame=getNewGame(amFirst);
46 playerAvailable=true;
47 }
48 }
49 abstract protected Game2PGame getNewGame(boolean goesFirst);
50
51 public void run() {
52 try {
53 diag(“CLIENT HANDLER STARTED”);
54 toPlayer = new PrintStream(sock.getOutputStream());
55 fromPlayer = new Scanner(sock.getInputStream());
56 if (!fromPlayer.next().equals(“0”)) return; // “authenticate”
57 myInterconnect.put(amFirst?1:2,toPlayer);
58 if (amFirst) waitFor2ndPlayer();
59 toOpponent = myInterconnect.get(amFirst?2:1);
60 toPlayer.println(amFirst);
61 String move = fromPlayer.next();
62 while (!move.equals(Game2PGame.GAMEOVER)) {
63 myGame.move(move);
64 toOpponent.println(move);
65 move = fromPlayer.next();
66 }
67 } catch (Exception e){diag(e.toString());
68 } finally {closeAll(); diag(“completes”);}
69 }
70
71 public static ServerSocket getServerSocket(String[] arg) throws Exception {
72 if (arg.length
73 int port = Integer.parseInt(arg[0]);
74 return new ServerSocket(port);
75 }
76 }