package com square WeakLinkServer import java io import java net Socke

  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
120
121
122
123
124
125
package com.square.WeakLinkServer;
import java.io.*;
import java.net.Socket;
import java.util.LinkedList;
public class Client extends Thread {
private Socket socket = null;
private ClientManager manager = null;
private int ID = -1;
private DataInputStream streamIn = null;
private DataOutputStream streamOut = null;
private boolean threadDone = false;
private LinkedList<String> receives = new LinkedList<String>();
private String name = Integer.toString(ID);
public Client(ClientManager _manager, Socket _socket) {
super();
manager = _manager;
socket = _socket;
ID = socket.getPort();
}
public void send(String msg) {
try {
Printer.printLine("Sending '" + msg + "' to " + socket);
streamOut.writeUTF(msg);
streamOut.flush();
} catch (IOException ioe) {
Printer.exception(ID + " Error sending '" + msg + "' to " + socket, ioe);
manager.remove(ID);
terminate(true);
}
}
public int getID() {
return ID;
}
public String getPlayerName() {
return name;
}
public void run() {
Printer.printLine("Thread " + ID + " running.");
while (true) {
if (threadDone) {
return;
}
try {
String msg = streamIn.readUTF();
receives.add(msg);
manager.handle(ID, msg);
} catch (IOException ioe) {
Printer.exception(ID + " Error reading: ", ioe);
manager.remove(ID);
terminate(true);
}
}
}
public void open() throws IOException {
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
public void close() throws IOException {
if (socket != null) {
socket.close();
}
if (streamIn != null) {
streamIn.close();
}
if (streamOut != null) {
streamOut.close();
}
}
public void terminate(boolean done) {
threadDone = done;
}
public String getReceive() {
if (receives.isEmpty()) {
return null;
}
return receives.poll();
}
public void flush() {
receives.clear();
}
//=========================================================================//
private boolean enabled = true;
public boolean isEnabled() {
return enabled;
}
public void enable() {
enabled = true;
}
public void disable() {
enabled = false;
}
private int correct = 0;
public int getCorrect() {
return correct;
}
public void correctUp() {
correct++;
}
public void correctDown() {
correct--;
}
public void correctFlush() {
correct = 0;
}
public void botOn() {
}
}