X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2Fserver%2FConnectAction.java;h=6a19368bbf97824deb624ffaf9eac238b44dfad7;hb=5a90a6ca7261f4c17407131fef58d5bcc793e2ce;hp=7d724248384d77a5ba459f4215a1aeebf80e7a13;hpb=8468bb79f0fc9c88fa21355509731625732eb10e;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/server/ConnectAction.java b/src/be/nikiroo/utils/serial/server/ConnectAction.java index 7d72424..6a19368 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -1,15 +1,23 @@ package be.nikiroo.utils.serial.server; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; +import java.io.InputStream; +import java.io.OutputStream; import java.net.Socket; +import javax.net.ssl.SSLException; + import be.nikiroo.utils.CryptUtils; +import be.nikiroo.utils.IOUtils; +import be.nikiroo.utils.StringUtils; import be.nikiroo.utils.Version; import be.nikiroo.utils.serial.Exporter; import be.nikiroo.utils.serial.Importer; +import be.nikiroo.utils.streams.BufferedOutputStream; +import be.nikiroo.utils.streams.NextableInputStream; +import be.nikiroo.utils.streams.NextableInputStreamStep; +import be.nikiroo.utils.streams.ReplaceInputStream; +import be.nikiroo.utils.streams.ReplaceOutputStream; /** * Base class used for the client/server basic handling. @@ -21,27 +29,32 @@ import be.nikiroo.utils.serial.Importer; * @author niki */ abstract class ConnectAction { + // We separate each "packet" we send with this character and make sure it + // does not occurs in the message itself. + static private char STREAM_SEP = '\b'; + static private String[] STREAM_RAW = new String[] { "\\", "\b" }; + static private String[] STREAM_CODED = new String[] { "\\\\", "\\b" }; + private Socket s; private boolean server; - private Version version; + private Version clientVersion; + private Version serverVersion; private CryptUtils crypt; private Object lock = new Object(); - private BufferedReader in; - private OutputStreamWriter out; + private NextableInputStream in; + private BufferedOutputStream out; private boolean contentToSend; - private long bytesReceived; - private long bytesSent; - /** * Method that will be called when an action is performed on either the * client or server this {@link ConnectAction} represent. * * @param version - * the counter part version + * the version on the other side of the communication (client or + * server) * * @throws Exception * in case of I/O error @@ -66,7 +79,8 @@ abstract class ConnectAction { * Handler called when an unexpected error occurs in the code. * * @param e - * the exception that occurred + * the exception that occurred, SSLException usually denotes a + * crypt error */ abstract protected void onError(Exception e); @@ -82,7 +96,8 @@ abstract class ConnectAction { * an optional key to encrypt all the communications (if NULL, * everything will be sent in clear text) * @param version - * the version of this client-or-server + * the client-or-server version (depending upon the boolean + * parameter server) */ protected ConnectAction(Socket s, boolean server, String key, Version version) { @@ -93,12 +108,14 @@ abstract class ConnectAction { } if (version == null) { - this.version = new Version(); - } else { - this.version = version; + version = new Version(); } - clientVersion = new Version(); + if (server) { + serverVersion = version; + } else { + clientVersion = version; + } } /** @@ -107,7 +124,11 @@ abstract class ConnectAction { * @return the version */ public Version getVersion() { - return version; + if (server) { + return serverVersion; + } + + return clientVersion; } /** @@ -116,7 +137,7 @@ abstract class ConnectAction { * @return the amount of bytes received */ public long getBytesReceived() { - return bytesReceived; + return in.getBytesRead(); } /** @@ -124,8 +145,8 @@ abstract class ConnectAction { * * @return the amount of bytes sent */ - public long getBytesSent() { - return bytesSent; + public long getBytesWritten() { + return out.getBytesWritten(); } /** @@ -133,42 +154,39 @@ abstract class ConnectAction { */ public void connect() { try { - in = new BufferedReader(new InputStreamReader(s.getInputStream(), - "UTF-8")); + in = new NextableInputStream(s.getInputStream(), + new NextableInputStreamStep(STREAM_SEP)); try { - out = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); + out = new BufferedOutputStream(s.getOutputStream()); try { + // Negotiate version + Version version; if (server) { - String line = readLine(in); - if (line != null && line.startsWith("VERSION ")) { - // "VERSION client-version" (VERSION 1.0.0) - Version clientVersion = new Version( - line.substring("VERSION ".length())); - this.clientVersion = clientVersion; - Version v = negotiateVersion(clientVersion); - if (v == null) { - v = new Version(); - } - - sendString("VERSION " + v.toString()); + String HELLO = recString(); + if (HELLO == null || !HELLO.startsWith("VERSION ")) { + throw new SSLException( + "Client used bad encryption key"); } - - action(clientVersion); + version = negotiateVersion(new Version( + HELLO.substring("VERSION ".length()))); + sendString("VERSION " + version); } else { - String v = sendString("VERSION " + version.toString()); - if (v != null && v.startsWith("VERSION ")) { - v = v.substring("VERSION ".length()); + String HELLO = sendString("VERSION " + clientVersion); + if (HELLO == null || !HELLO.startsWith("VERSION ")) { + throw new SSLException( + "Server did not accept the encryption key"); } - - action(new Version(v)); + version = new Version(HELLO.substring("VERSION " + .length())); } + + // Actual code + action(version); } finally { out.close(); - out = null; } } finally { in.close(); - in = null; } } catch (Exception e) { onError(e); @@ -189,8 +207,9 @@ abstract class ConnectAction { * @param data * the data to send * - * @return the answer (which can be NULL) if this action is a client, always - * NULL if it is a server + * @return the answer (which can be NULL if no answer, or NULL for an answer + * which is NULL) if this action is a client, always NULL if it is a + * server * * @throws IOException * in case of I/O error @@ -205,15 +224,7 @@ abstract class ConnectAction { */ protected Object sendObject(Object data) throws IOException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException { - synchronized (lock) { - String rep = sendString(new Exporter().append(data).toString(true, - true)); - if (rep != null) { - return new Importer().read(rep).getValue(); - } - - return null; - } + return send(out, data, false); } /** @@ -243,12 +254,7 @@ abstract class ConnectAction { protected Object recObject() throws IOException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, java.lang.NullPointerException { - String str = recString(); - if (str == null) { - throw new NullPointerException("No more data available"); - } - - return new Importer().read(str).getValue(); + return rec(false); } /** @@ -263,10 +269,117 @@ abstract class ConnectAction { * * @throws IOException * in case of I/O error + * @throws SSLException + * in case of crypt error */ protected String sendString(String line) throws IOException { + try { + return (String) send(out, line, true); + } catch (NoSuchFieldException e) { + // Cannot happen + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // Cannot happen + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // Cannot happen + e.printStackTrace(); + } + + return null; + } + + /** + * Reserved for the server (externally): flush the data to the client and + * retrieve its answer. + *
+ * Also used internally for the client (only do something if there is + * contentToSend). + *
+ * Will only flush the data if there is contentToSend. + * + * @return the answer (which can be NULL if no more content) + * + * @throws IOException + * in case of I/O error + * @throws SSLException + * in case of crypt error + */ + protected String recString() throws IOException { + try { + return (String) rec(true); + } catch (NoSuchFieldException e) { + // Cannot happen + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // Cannot happen + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // Cannot happen + e.printStackTrace(); + } catch (NullPointerException e) { + // Should happen + e.printStackTrace(); + } + + return null; + } + + /** + * Serialise and send the given object to the counter part (and, only for + * client, return the deserialised answer -- the server will always receive + * NULL). + * + * @param out + * the stream to write to + * @param data + * the data to write + * @param asString + * TRUE to write it as a String, FALSE to write it as an Object + * + * @return the answer (which can be NULL if no answer, or NULL for an answer + * which is NULL) if this action is a client, always NULL if it is a + * server + * + * @throws IOException + * in case of I/O error + * @throws SSLException + * in case of crypt error + * @throws IOException + * in case of I/O error + * @throws NoSuchFieldException + * if the serialised data contains information about a field + * which does actually not exist in the class we know of + * @throws NoSuchMethodException + * if a class described in the serialised data cannot be created + * because it is not compatible with this code + * @throws ClassNotFoundException + * if a class described in the serialised data cannot be found + */ + private Object send(BufferedOutputStream out, Object data, boolean asString) + throws IOException, NoSuchFieldException, NoSuchMethodException, + ClassNotFoundException, java.lang.NullPointerException { + synchronized (lock) { - writeLine(out, line); + OutputStream sub; + if (crypt != null) { + sub = crypt.encrypt64(out.open()); + } else { + sub = out.open(); + } + + sub = new ReplaceOutputStream(sub, STREAM_RAW, STREAM_CODED); + try { + if (asString) { + sub.write(StringUtils.getBytes(data.toString())); + } else { + new Exporter(sub).append(data); + } + } finally { + sub.close(); + } + + out.write(STREAM_SEP); if (server) { out.flush(); @@ -274,25 +387,57 @@ abstract class ConnectAction { } contentToSend = true; - return recString(); + try { + return rec(asString); + } catch (NullPointerException e) { + // We accept no data here for Objects + } + + return null; } } /** - * Reserved for the server (externally): flush the data to the client and - * retrieve its answer. + * Reserved for the server: flush the data to the client and retrieve its + * answer. *
* Also used internally for the client (only do something if there is * contentToSend). *
* Will only flush the data if there is contentToSend. + *
+ * Note that the behaviour is slightly different for String and Object + * reading regarding exceptions: + *