-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
119 lines (107 loc) · 3.68 KB
/
Server.java
File metadata and controls
119 lines (107 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//imports
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public class Server{
final int port=8888;
private static ServerSocket ss;
public static PlayerServer p1;
public static PlayerServer p2;
private final int MAX_GAMES=10;
protected ExecutorService tpl =
Executors.newFixedThreadPool(2*MAX_GAMES);
public Server() throws IOException{
//create the server
Server.ss=new ServerSocket(port);
}
public void close() throws IOException{
Server.ss.close();
}
public Socket accept() throws IOException{
return Server.ss.accept();
}
public static class PlayerServer extends Thread{
public boolean isWhite;
public boolean ourTurn;
public boolean isGameOver=false;
public Socket s;
public PlayerServer opp;
private PrintWriter writer;
private BufferedReader reader;
public PlayerServer(Socket s,boolean ourTurn){
this.s=s;
this.ourTurn=this.isWhite=ourTurn;
System.out.println("player connected");
try {
this.writer=new PrintWriter(s.getOutputStream(),true);
this.reader=new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (Exception e) {
// TODO: handle exception
}
}
public void setOpp(PlayerServer opp){
this.opp=opp;
}
@Override
public void run() {
try {
this.writer.println(ourTurn ? "true" : "false");
} catch (Exception e) {
// TODO: handle exception
}
String msg="";
while(!isGameOver){
try {
if(ourTurn && this.opp!=null){
msg=this.reader.readLine();
if(!msg.equals("")){
this.opp.writer.println(msg);
if(msg.startsWith("GAME OVER"))
this.isGameOver=true;
}
}else if(this.opp!=null){
msg=this.opp.reader.readLine();
if(!msg.equals("")){
this.writer.println(msg);
if(msg.startsWith("GAME OVER"))
this.isGameOver=true;
}
}
} catch (Exception e) {
// TODO: handle exception
try {
this.writer.close();
this.reader.close();
this.s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws IOException {
Server server=new Server();
boolean isWhite=Math.random()>=0.5;
try {
while(true){
p1=new PlayerServer(server.accept(), isWhite);
server.tpl.execute(p1);
p2=new PlayerServer(server.accept(), !isWhite);
server.tpl.execute(p2);
p1.setOpp(p2);
p2.setOpp(p1);
isWhite=Math.random()>=0.5;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
server.close();
}
}
}