cef10adf1c80736a4603c8d99c1fe4c82710d480
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
1 package be.nikiroo.utils.serial.server;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.OutputStreamWriter;
7 import java.net.Socket;
8
9 import javax.net.ssl.SSLException;
10
11 import be.nikiroo.utils.Version;
12 import be.nikiroo.utils.serial.Exporter;
13 import be.nikiroo.utils.serial.Importer;
14
15 /**
16 * Base class used for the client/server basic handling.
17 * <p>
18 * It represents a single action: a client is expected to only execute one
19 * action, while a server is expected to execute one action for each client
20 * action.
21 *
22 * @author niki
23 */
24 abstract class ConnectAction {
25 private Socket s;
26 private boolean server;
27 private Version version;
28 private Version clientVersion;
29
30 private Object lock = new Object();
31 private BufferedReader in;
32 private OutputStreamWriter out;
33 private boolean contentToSend;
34
35 private long bytesReceived;
36 private long bytesSent;
37
38 /**
39 * Method that will be called when an action is performed on either the
40 * client or server this {@link ConnectAction} represent.
41 *
42 * @param version
43 * the counter part version
44 *
45 * @throws Exception
46 * in case of I/O error
47 */
48 abstract protected void action(Version version) throws Exception;
49
50 /**
51 * Method called when we negotiate the version with the client.
52 * <p>
53 * Thus, it is only called on the server.
54 * <p>
55 * Will return the actual server version by default.
56 *
57 * @param clientVersion
58 * the client version
59 *
60 * @return the version to send to the client
61 */
62 abstract protected Version negotiateVersion(Version clientVersion);
63
64 /**
65 * Handler called when an unexpected error occurs in the code.
66 *
67 * @param e
68 * the exception that occurred
69 */
70 abstract protected void onError(Exception e);
71
72 /**
73 * Create a new {@link ConnectAction}.
74 *
75 * @param s
76 * the socket to bind to
77 * @param server
78 * TRUE for a server action, FALSE for a client action (will
79 * impact the process)
80 * @param version
81 * the version of this client-or-server
82 */
83 protected ConnectAction(Socket s, boolean server, Version version) {
84 this.s = s;
85 this.server = server;
86
87 if (version == null) {
88 this.version = new Version();
89 } else {
90 this.version = version;
91 }
92
93 clientVersion = new Version();
94 }
95
96 /**
97 * The version of this client-or-server.
98 *
99 * @return the version
100 */
101 public Version getVersion() {
102 return version;
103 }
104
105 /**
106 * The total amount of bytes received.
107 *
108 * @return the amount of bytes received
109 */
110 public long getBytesReceived() {
111 return bytesReceived;
112 }
113
114 /**
115 * The total amount of bytes sent.
116 *
117 * @return the amount of bytes sent
118 */
119 public long getBytesSent() {
120 return bytesSent;
121 }
122
123 /**
124 * Actually start the process (this is synchronous).
125 */
126 public void connect() {
127 try {
128 in = new BufferedReader(new InputStreamReader(s.getInputStream(),
129 "UTF-8"));
130 try {
131 out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
132 try {
133 if (server) {
134 String line = in.readLine();
135 if (line != null && line.startsWith("VERSION ")) {
136 // "VERSION client-version" (VERSION 1.0.0)
137 Version clientVersion = new Version(
138 line.substring("VERSION ".length()));
139 this.clientVersion = clientVersion;
140 Version v = negotiateVersion(clientVersion);
141 if (v == null) {
142 v = new Version();
143 }
144
145 sendString("VERSION " + v.toString());
146 }
147
148 action(clientVersion);
149 } else {
150 String v = sendString("VERSION " + version.toString());
151 if (v != null && v.startsWith("VERSION ")) {
152 v = v.substring("VERSION ".length());
153 }
154
155 action(new Version(v));
156 }
157 } finally {
158 out.close();
159 out = null;
160 }
161 } finally {
162 in.close();
163 in = null;
164 }
165 } catch (Exception e) {
166 if (e instanceof SSLException) {
167 String ciphers = "";
168 for (String cipher : Server.getAnonCiphers()) {
169 if (!ciphers.isEmpty()) {
170 ciphers += ", ";
171 }
172 ciphers += cipher;
173 }
174
175 e = new SSLException("SSL error (available SSL ciphers: "
176 + ciphers + ")", e);
177 }
178
179 onError(e);
180 } finally {
181 try {
182 s.close();
183 } catch (Exception e) {
184 onError(e);
185 }
186 }
187 }
188
189 /**
190 * Serialise and send the given object to the counter part (and, only for
191 * client, return the deserialised answer -- the server will always receive
192 * NULL).
193 *
194 * @param data
195 * the data to send
196 *
197 * @return the answer (which can be NULL) if this action is a client, always
198 * NULL if it is a server
199 *
200 * @throws IOException
201 * in case of I/O error
202 * @throws NoSuchFieldException
203 * if the serialised data contains information about a field
204 * which does actually not exist in the class we know of
205 * @throws NoSuchMethodException
206 * if a class described in the serialised data cannot be created
207 * because it is not compatible with this code
208 * @throws ClassNotFoundException
209 * if a class described in the serialised data cannot be found
210 */
211 protected Object sendObject(Object data) throws IOException,
212 NoSuchFieldException, NoSuchMethodException, ClassNotFoundException {
213 synchronized (lock) {
214 String rep = sendString(new Exporter().append(data).toString(true,
215 true));
216 if (rep != null) {
217 return new Importer().read(rep).getValue();
218 }
219
220 return null;
221 }
222 }
223
224 /**
225 * Reserved for the server: flush the data to the client and retrieve its
226 * answer.
227 * <p>
228 * Also used internally for the client (only do something if there is
229 * contentToSend).
230 * <p>
231 * Will only flush the data if there is contentToSend.
232 *
233 * @return the deserialised answer (which can actually be NULL)
234 *
235 * @throws IOException
236 * in case of I/O error
237 * @throws NoSuchFieldException
238 * if the serialised data contains information about a field
239 * which does actually not exist in the class we know of
240 * @throws NoSuchMethodException
241 * if a class described in the serialised data cannot be created
242 * because it is not compatible with this code
243 * @throws ClassNotFoundException
244 * if a class described in the serialised data cannot be found
245 * @throws java.lang.NullPointerException
246 * if the counter part has no data to send
247 */
248 protected Object recObject() throws IOException, NoSuchFieldException,
249 NoSuchMethodException, ClassNotFoundException,
250 java.lang.NullPointerException {
251 String str = recString();
252 if (str == null) {
253 throw new NullPointerException("No more data available");
254 }
255
256 return new Importer().read(str).getValue();
257 }
258
259 /**
260 * Send the given string to the counter part (and, only for client, return
261 * the answer -- the server will always receive NULL).
262 *
263 * @param line
264 * the data to send (we will add a line feed)
265 *
266 * @return the answer if this action is a client (without the added line
267 * feed), NULL if it is a server
268 *
269 * @throws IOException
270 * in case of I/O error
271 */
272 protected String sendString(String line) throws IOException {
273 synchronized (lock) {
274 out.write(line);
275 out.write("\n");
276 bytesSent += line.length() + 1;
277
278 if (server) {
279 out.flush();
280 return null;
281 }
282
283 contentToSend = true;
284 return recString();
285 }
286 }
287
288 /**
289 * Reserved for the server (externally): flush the data to the client and
290 * retrieve its answer.
291 * <p>
292 * Also used internally for the client (only do something if there is
293 * contentToSend).
294 * <p>
295 * Will only flush the data if there is contentToSend.
296 *
297 * @return the answer (which can be NULL)
298 *
299 * @throws IOException
300 * in case of I/O error
301 */
302 protected String recString() throws IOException {
303 synchronized (lock) {
304 if (server || contentToSend) {
305 if (contentToSend) {
306 out.flush();
307 contentToSend = false;
308 }
309
310 String line = in.readLine();
311 if (line != null) {
312 bytesReceived += line.length();
313 }
314
315 return line;
316 }
317
318 return null;
319 }
320 }
321 }