1 | package com.framsticks.net.client3D; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.InputStreamReader; |
---|
6 | import java.io.PrintWriter; |
---|
7 | import java.net.Socket; |
---|
8 | import java.util.ArrayList; |
---|
9 | |
---|
10 | /** |
---|
11 | * Communication class. |
---|
12 | * |
---|
13 | * @author MoMaT (modified by vorg @ 2007/01/25) |
---|
14 | */ |
---|
15 | public class Communication { |
---|
16 | private Socket socket = null; |
---|
17 | private PrintWriter out = null; |
---|
18 | private BufferedReader in = null; |
---|
19 | protected boolean connected = false; |
---|
20 | |
---|
21 | private ArrayList<String> log = new ArrayList<String>(); |
---|
22 | |
---|
23 | /** |
---|
24 | * Connect to a server. |
---|
25 | * |
---|
26 | * @param ip |
---|
27 | * @param port |
---|
28 | * @throws IOException |
---|
29 | * Thrown on I/O error. |
---|
30 | */ |
---|
31 | public void connect(String ip, int port) throws IOException { |
---|
32 | try { |
---|
33 | socket = new Socket(ip, port); |
---|
34 | out = new PrintWriter(socket.getOutputStream(), true); |
---|
35 | in = new BufferedReader(new InputStreamReader( |
---|
36 | socket.getInputStream())); |
---|
37 | |
---|
38 | connected = true; |
---|
39 | sendMessage("version 3"); |
---|
40 | String message = in.readLine(); |
---|
41 | log.add("> " + message); |
---|
42 | |
---|
43 | } catch (Exception e) { |
---|
44 | Log.getInstance().log("err", e.toString()); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Close connection. |
---|
50 | * |
---|
51 | * @throws IOException |
---|
52 | * Thrown on socket close error. |
---|
53 | */ |
---|
54 | public void disconnect() throws IOException { |
---|
55 | connected = false; |
---|
56 | out.close(); |
---|
57 | in.close(); |
---|
58 | socket.close(); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Read a server response. |
---|
63 | * |
---|
64 | * @return server answer |
---|
65 | * @throws IOException |
---|
66 | * Thrown on read error. |
---|
67 | * @throws InterruptedException |
---|
68 | * Thrown on thread sleep interruption. |
---|
69 | * @throws CommunicationErrorException |
---|
70 | * Thrown on protocol error. |
---|
71 | */ |
---|
72 | public ArrayList<String> readMessage() throws IOException, |
---|
73 | InterruptedException, CommunicationErrorException { |
---|
74 | if (!connected) { |
---|
75 | Log.getInstance().log("wrn", |
---|
76 | "Reading message failed. You are not connected!"); |
---|
77 | return new ArrayList<String>(); |
---|
78 | } |
---|
79 | String message = null; |
---|
80 | ArrayList<String> buffer = new ArrayList<String>(); |
---|
81 | |
---|
82 | // gentle wait for input on socket |
---|
83 | // elapsedTime added by vorg to avoid infinite loop |
---|
84 | int elapsedTime = 0; |
---|
85 | while (false == in.ready()) { |
---|
86 | Thread.sleep(100); |
---|
87 | elapsedTime++; |
---|
88 | if (elapsedTime > 100) { |
---|
89 | throw new InterruptedException("Request timeout"); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | // read server answer |
---|
94 | while ((message = in.readLine()) != null) { |
---|
95 | log.add("> " + message); |
---|
96 | // stop if reading complete |
---|
97 | if (message.startsWith("ok")) { |
---|
98 | // add ok line if it contains additional info |
---|
99 | if (3 < message.length()) { |
---|
100 | buffer.add(message); |
---|
101 | } |
---|
102 | break; |
---|
103 | } else if (message.startsWith("error")) { |
---|
104 | String error = message.substring(message.indexOf(' ')); |
---|
105 | throw new CommunicationErrorException(error); |
---|
106 | } |
---|
107 | buffer.add(message); |
---|
108 | } |
---|
109 | return buffer; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Send request to a server. |
---|
114 | * |
---|
115 | * @param message |
---|
116 | */ |
---|
117 | public void sendMessage(String message) { |
---|
118 | Log.getInstance().log(">>>", message); |
---|
119 | log.add(message); |
---|
120 | if (!connected) { |
---|
121 | Log.getInstance().log("wrn", |
---|
122 | "Sending message failed. You are not connected!"); |
---|
123 | return; |
---|
124 | } |
---|
125 | out.println(message); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Log getter. |
---|
130 | * |
---|
131 | * @return log array |
---|
132 | */ |
---|
133 | public ArrayList<String> getLog() { |
---|
134 | return log; |
---|
135 | } |
---|
136 | |
---|
137 | public Boolean isConnected() { |
---|
138 | return connected; |
---|
139 | } |
---|
140 | } |
---|