-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountExec.java~
78 lines (54 loc) · 1.66 KB
/
AccountExec.java~
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
// class/thread that manages server's interacions with a particular client
import java.net.*;
import java.util.*;
import java.io.*;
public class AccountExec implements Runnable {
private Socket socket;
private int id;
private PrintWriter out;
private BufferedReader in;
private Player player; //in-game PC corresponding to this account
public AccountExec(Socket s,int id) {
socket=s;
this.id=id;
player=new Player(id);
try {
out=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch(IOException e) {
System.err.println("WARNING: error while constructing in/out for account "+this);
}
} //constructor
public void run() {
// all clients do is submit messages, which this sends to the server
try {
String s=in.readLine();
while(s!=null) {
GameActionType type=GameActionType.parse(s);
if(type==null) sendMessage("BadAction");
else Server.submitAction(new GameAction(player,type));
s=in.readLine();
}
} catch(IOException e) {
System.err.println("WARNING: error while reading input from account"+this);
}
//done reading, close account:
try {
socket.close();
Server.removeAccount(this);
} catch(IOException e) {
System.err.println("WARNING: could not close socket for account "+this);
}
} //run
public void sendMessage(String s) {
out.println(s);
out.flush();
} //sendMessage
/////////////////// ACCESSORS /////////////////////
public String toString() {
return "Account "+id;
} //toString
public Player getPlayer() {
return player;
} //getPlayer
} //class