-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.java
More file actions
119 lines (97 loc) · 2.81 KB
/
client.java
File metadata and controls
119 lines (97 loc) · 2.81 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
package sample;
import javafx.application.Platform;
import javafx.scene.text.Text;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.function.Consumer;
//CLIENT CODE
public abstract class NetworkConnection {
private ConnThread connthread = new ConnThread();
private Consumer<Serializable> callback;
String turn;
public NetworkConnection(Consumer<Serializable> callback) {
this.callback = callback;
connthread.setDaemon(true);
}
public void startConn() throws Exception{
connthread.start();
}
public void send(Serializable data) throws Exception{
connthread.out.writeObject(data);
}
public void closeConn() throws Exception{
try {
connthread.socket.close();
}
catch (Exception e){
System.out.println("Connection did not close properly.");
}
}
abstract protected String getIP();
abstract protected int getPort();
class ConnThread extends Thread{
private Socket socket;
private ObjectOutputStream out;
public void run() {
try(
Socket socket = new Socket(getIP(), getPort());
ObjectOutputStream out = new ObjectOutputStream( socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream())){
this.socket = socket;
this.out = out;
socket.setTcpNoDelay(true);
while(true) {
Serializable data = (Serializable) in.readObject();
//callback.accept(data);
if(data.toString().equals("ArrayList")){
FXNet.clientListID = (ArrayList<Integer>) in.readObject();
FXNet.clientList.getItems().clear();
for(int i : FXNet.clientListID){
FXNet.clientList.getItems().addAll("Client "+i);
}
}
else if(data.toString().equals("-)")){
Serializable temp = (Serializable) in.readObject();
//callback.accept("~backend~ "+temp.toString());
// Platform.runLater(() -> {
// FXNet.getBTN(temp.toString());
// });
}
else if(data.toString().equals("-*")){
Serializable temp1 = (Serializable) in.readObject();
Serializable temp2 = (Serializable) in.readObject();
Platform.runLater(() -> {
FXNet.setPoints((Integer) temp1,(Integer) temp2);
});
}
else if(data.toString().equals("-!")){
Platform.runLater(() -> {
FXNet.disableBTNs();
});
}
else if(data.toString().equals("-&")){
Platform.runLater(() -> {
FXNet.enableBTNs();
});
}
else if(data.toString().equals("-$")){
Platform.runLater(() -> {
FXNet.disableBTNs();
FXNet.endGame();
});
}
else{
callback.accept(data);
}
}
}
catch(Exception e) {
callback.accept("Connection Closed");
}
}
}
}