X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2Fserver%2FConnectAction.java;h=aafad62facfa5a3a3433df3a7aa54de8f23806b4;hb=f743f2ca02a518a7e91ee6376bff28c68977f8da;hp=dfdb53ed017da95df65d80f5b8111fc5e0fdc153;hpb=217a3310efed6da8b1765738e6386825b3002570;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 dfdb53e..aafad62 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -1,14 +1,17 @@ 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.OutputStream; import java.net.Socket; -import be.nikiroo.utils.Version; +import javax.net.ssl.SSLException; + +import be.nikiroo.utils.CryptUtils; +import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.serial.Exporter; import be.nikiroo.utils.serial.Importer; +import be.nikiroo.utils.streams.NextableInputStream; +import be.nikiroo.utils.streams.NextableInputStreamStep; /** * Base class used for the client/server basic handling. @@ -22,45 +25,32 @@ import be.nikiroo.utils.serial.Importer; abstract class ConnectAction { private Socket s; private boolean server; - private Version version; - private Version clientVersion; + + private CryptUtils crypt; private Object lock = new Object(); - private BufferedReader in; - private OutputStreamWriter out; + private NextableInputStream in; + private OutputStream 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 - * * @throws Exception * in case of I/O error */ - abstract protected void action(Version version) throws Exception; - - /** - * Method called when we negotiate the version with the client. - *

- * Thus, it is only called on the server. - *

- * Will return the actual server version by default. - * - * @param clientVersion - * the client version - * - * @return the version to send to the client - */ - abstract protected Version negotiateVersion(Version clientVersion); + abstract protected void action() throws Exception; /** * 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); @@ -72,29 +62,34 @@ abstract class ConnectAction { * @param server * TRUE for a server action, FALSE for a client action (will * impact the process) - * @param version - * the version of this client-or-server + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) */ - protected ConnectAction(Socket s, boolean server, Version version) { + protected ConnectAction(Socket s, boolean server, String key) { this.s = s; this.server = server; - - if (version == null) { - this.version = new Version(); - } else { - this.version = version; + if (key != null) { + crypt = new CryptUtils(key); } + } - clientVersion = new Version(); + /** + * The total amount of bytes received. + * + * @return the amount of bytes received + */ + public long getBytesReceived() { + return bytesReceived; } /** - * The version of this client-or-server. + * The total amount of bytes sent. * - * @return the version + * @return the amount of bytes sent */ - public Version getVersion() { - return version; + public long getBytesSent() { + return bytesSent; } /** @@ -102,40 +97,20 @@ abstract class ConnectAction { */ public void connect() { try { - in = new BufferedReader(new InputStreamReader(s.getInputStream(), - "UTF-8")); + in = new NextableInputStream(s.getInputStream(), + new NextableInputStreamStep('\b')); + try { - out = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); + out = s.getOutputStream(); try { - if (server) { - String line = in.readLine(); - 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()); - } - - action(clientVersion); - } else { - String v = sendString("VERSION " + version.toString()); - if (v != null && v.startsWith("VERSION ")) { - v = v.substring("VERSION ".length()); - } - - action(new Version(v)); - } + action(); } finally { out.close(); + out = null; } } finally { in.close(); + in = null; } } catch (Exception e) { onError(e); @@ -156,8 +131,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 @@ -173,9 +149,20 @@ abstract class ConnectAction { protected Object sendObject(Object data) throws IOException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException { synchronized (lock) { - String rep = sendString(new Exporter().append(data).toString(true)); - if (rep != null) { - return new Importer().read(rep).getValue(); + + new Exporter(out).append(data); + out.write('\b'); + + if (server) { + out.flush(); + return null; + } + + contentToSend = true; + try { + return recObject(); + } catch (NullPointerException e) { + // We accept no data here } return null; @@ -209,12 +196,22 @@ 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"); - } + synchronized (lock) { + if (server || contentToSend) { + if (contentToSend) { + out.flush(); + contentToSend = false; + } + + if (in.next()) { + return new Importer().read(in).getValue(); + } - return new Importer().read(str).getValue(); + throw new NullPointerException(); + } + + return null; + } } /** @@ -229,11 +226,12 @@ 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 { synchronized (lock) { - out.write(line); - out.write("\n"); + writeLine(out, line); if (server) { out.flush(); @@ -258,6 +256,8 @@ abstract class ConnectAction { * * @throws IOException * in case of I/O error + * @throws SSLException + * in case of crypt error */ protected String recString() throws IOException { synchronized (lock) { @@ -267,10 +267,65 @@ abstract class ConnectAction { contentToSend = false; } - return in.readLine(); + return readLine(in); } return null; } } + + /** + * Read a possibly encrypted line. + * + * @param in + * the stream to read from + * @return the unencrypted line + * + * + * @throws IOException + * in case of I/O error + * @throws SSLException + * in case of crypt error + */ + private String readLine(NextableInputStream in) throws IOException { + String line = null; + if (in.next()) { + line = IOUtils.readSmallStream(in); + } + + if (line != null) { + bytesReceived += line.length(); + if (crypt != null) { + line = crypt.decrypt64s(line, false); + } + } + + return line; + } + + /** + * Write a line, possible encrypted. + * + * @param out + * the stream to write to + * @param line + * the line to write + * @throws IOException + * in case of I/O error + * @throws SSLException + * in case of crypt error + */ + private void writeLine(OutputStream out, String line) throws IOException { + if (crypt == null) { + out.write(line.getBytes("UTF-8")); + bytesSent += line.length(); + } else { + // TODO: how NOT to create so many big Strings? + String b64 = crypt.encrypt64(line, false); + out.write(b64.getBytes("UTF-8")); + bytesSent += b64.length(); + } + out.write('\b'); + bytesSent++; + } } \ No newline at end of file