1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.InputStreamReader
;
7 import java
.io
.OutputStream
;
8 import java
.net
.Socket
;
10 import javax
.net
.ssl
.SSLException
;
12 import be
.nikiroo
.utils
.CryptUtils
;
13 import be
.nikiroo
.utils
.Version
;
14 import be
.nikiroo
.utils
.serial
.Exporter
;
15 import be
.nikiroo
.utils
.serial
.Importer
;
18 * Base class used for the client/server basic handling.
20 * It represents a single action: a client is expected to only execute one
21 * action, while a server is expected to execute one action for each client
26 abstract class ConnectAction
{
28 private boolean server
;
29 private Version version
;
30 private Version clientVersion
;
32 private CryptUtils crypt
;
34 private Object lock
= new Object();
35 private InputStream in
;
36 private OutputStream out
;
37 private boolean contentToSend
;
39 private long bytesReceived
;
40 private long bytesSent
;
43 * Method that will be called when an action is performed on either the
44 * client or server this {@link ConnectAction} represent.
47 * the counter part version
50 * in case of I/O error
52 abstract protected void action(Version version
) throws Exception
;
55 * Method called when we negotiate the version with the client.
57 * Thus, it is only called on the server.
59 * Will return the actual server version by default.
61 * @param clientVersion
64 * @return the version to send to the client
66 abstract protected Version
negotiateVersion(Version clientVersion
);
69 * Handler called when an unexpected error occurs in the code.
72 * the exception that occurred, SSLException usually denotes a
75 abstract protected void onError(Exception e
);
78 * Create a new {@link ConnectAction}.
81 * the socket to bind to
83 * TRUE for a server action, FALSE for a client action (will
86 * an optional key to encrypt all the communications (if NULL,
87 * everything will be sent in clear text)
89 * the version of this client-or-server
91 protected ConnectAction(Socket s
, boolean server
, String key
,
96 crypt
= new CryptUtils(key
);
99 if (version
== null) {
100 this.version
= new Version();
102 this.version
= version
;
105 clientVersion
= new Version();
109 * The version of this client-or-server.
111 * @return the version
113 public Version
getVersion() {
118 * The total amount of bytes received.
120 * @return the amount of bytes received
122 public long getBytesReceived() {
123 return bytesReceived
;
127 * The total amount of bytes sent.
129 * @return the amount of bytes sent
131 public long getBytesSent() {
136 * Actually start the process (this is synchronous).
138 public void connect() {
140 in
= s
.getInputStream();
142 out
= s
.getOutputStream();
148 } catch (SSLException e
) {
149 out
.write("Unauthorized\n".getBytes());
153 if (line
!= null && line
.startsWith("VERSION ")) {
154 // "VERSION client-version" (VERSION 1.0.0)
155 Version clientVersion
= new Version(
156 line
.substring("VERSION ".length()));
157 this.clientVersion
= clientVersion
;
158 Version v
= negotiateVersion(clientVersion
);
163 sendString("VERSION " + v
.toString());
166 action(clientVersion
);
168 String v
= sendString("VERSION " + version
.toString());
169 if (v
!= null && v
.startsWith("VERSION ")) {
170 v
= v
.substring("VERSION ".length());
173 action(new Version(v
));
183 } catch (Exception e
) {
188 } catch (Exception e
) {
195 * Serialise and send the given object to the counter part (and, only for
196 * client, return the deserialised answer -- the server will always receive
202 * @return the answer (which can be NULL) if this action is a client, always
203 * NULL if it is a server
205 * @throws IOException
206 * in case of I/O error
207 * @throws NoSuchFieldException
208 * if the serialised data contains information about a field
209 * which does actually not exist in the class we know of
210 * @throws NoSuchMethodException
211 * if a class described in the serialised data cannot be created
212 * because it is not compatible with this code
213 * @throws ClassNotFoundException
214 * if a class described in the serialised data cannot be found
216 protected Object
sendObject(Object data
) throws IOException
,
217 NoSuchFieldException
, NoSuchMethodException
, ClassNotFoundException
{
218 synchronized (lock
) {
219 String rep
= sendString(new Exporter().append(data
).toString(true,
222 return new Importer().read(rep
).getValue();
230 * Reserved for the server: flush the data to the client and retrieve its
233 * Also used internally for the client (only do something if there is
236 * Will only flush the data if there is contentToSend.
238 * @return the deserialised answer (which can actually be NULL)
240 * @throws IOException
241 * in case of I/O error
242 * @throws NoSuchFieldException
243 * if the serialised data contains information about a field
244 * which does actually not exist in the class we know of
245 * @throws NoSuchMethodException
246 * if a class described in the serialised data cannot be created
247 * because it is not compatible with this code
248 * @throws ClassNotFoundException
249 * if a class described in the serialised data cannot be found
250 * @throws java.lang.NullPointerException
251 * if the counter part has no data to send
253 protected Object
recObject() throws IOException
, NoSuchFieldException
,
254 NoSuchMethodException
, ClassNotFoundException
,
255 java
.lang
.NullPointerException
{
256 String str
= recString();
258 throw new NullPointerException("No more data available");
261 return new Importer().read(str
).getValue();
265 * Send the given string to the counter part (and, only for client, return
266 * the answer -- the server will always receive NULL).
269 * the data to send (we will add a line feed)
271 * @return the answer if this action is a client (without the added line
272 * feed), NULL if it is a server
274 * @throws IOException
275 * in case of I/O error
276 * @throws SSLException
277 * in case of crypt error
279 protected String
sendString(String line
) throws IOException
{
280 synchronized (lock
) {
281 writeLine(out
, line
);
288 contentToSend
= true;
294 * Reserved for the server (externally): flush the data to the client and
295 * retrieve its answer.
297 * Also used internally for the client (only do something if there is
300 * Will only flush the data if there is contentToSend.
302 * @return the answer (which can be NULL)
304 * @throws IOException
305 * in case of I/O error
306 * @throws SSLException
307 * in case of crypt error
309 protected String
recString() throws IOException
{
310 synchronized (lock
) {
311 if (server
|| contentToSend
) {
314 contentToSend
= false;
325 * Read a possibly encrypted line.
328 * the stream to read from
329 * @return the unencrypted line
332 * @throws IOException
333 * in case of I/O error
334 * @throws SSLException
335 * in case of crypt error
337 private String
readLine(InputStream in
) throws IOException
{
338 if (inReader
== null) {
339 inReader
= new BufferedReader(new InputStreamReader(in
));
341 String line
= inReader
.readLine();
343 bytesReceived
+= line
.length();
345 line
= crypt
.decrypt64s(line
, false);
352 private BufferedReader inReader
;
355 * Write a line, possible encrypted.
358 * the stream to write to
361 * @throws IOException
362 * in case of I/O error
363 * @throws SSLException
364 * in case of crypt error
366 private void writeLine(OutputStream out
, String line
) throws IOException
{
368 out
.write(line
.getBytes());
369 bytesSent
+= line
.length();
371 // TODO: how NOT to create so many big Strings?
372 String b64
= crypt
.encrypt64(line
, false);
373 out
.write(b64
.getBytes());
374 bytesSent
+= b64
.length();
376 out
.write("\n".getBytes());