From: Niki Roo Date: Wed, 17 Apr 2019 15:57:43 +0000 (+0200) Subject: count the bytes we receive/send X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=4bb7e88e5165691ed1c14d93b4bb0a9fbe811090;p=fanfix-jexer.git count the bytes we receive/send --- diff --git a/src/be/nikiroo/utils/serial/server/ConnectAction.java b/src/be/nikiroo/utils/serial/server/ConnectAction.java index 39417a7..cef10ad 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -32,6 +32,9 @@ abstract class ConnectAction { private OutputStreamWriter 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. @@ -99,13 +102,31 @@ abstract class ConnectAction { return version; } + /** + * The total amount of bytes received. + * + * @return the amount of bytes received + */ + public long getBytesReceived() { + return bytesReceived; + } + + /** + * The total amount of bytes sent. + * + * @return the amount of bytes sent + */ + public long getBytesSent() { + return bytesSent; + } + /** * Actually start the process (this is synchronous). */ public void connect() { try { - in = new BufferedReader( - new InputStreamReader(s.getInputStream(), "UTF-8")); + in = new BufferedReader(new InputStreamReader(s.getInputStream(), + "UTF-8")); try { out = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); try { @@ -151,11 +172,10 @@ abstract class ConnectAction { ciphers += cipher; } - e = new SSLException( - "SSL error (available SSL ciphers: " + ciphers + ")", - e); + e = new SSLException("SSL error (available SSL ciphers: " + + ciphers + ")", e); } - + onError(e); } finally { try { @@ -188,12 +208,11 @@ abstract class ConnectAction { * @throws ClassNotFoundException * if a class described in the serialised data cannot be found */ - protected Object sendObject(Object data) - throws IOException, NoSuchFieldException, NoSuchMethodException, - ClassNotFoundException { + protected Object sendObject(Object data) throws IOException, + NoSuchFieldException, NoSuchMethodException, ClassNotFoundException { synchronized (lock) { - String rep = sendString( - new Exporter().append(data).toString(true, true)); + String rep = sendString(new Exporter().append(data).toString(true, + true)); if (rep != null) { return new Importer().read(rep).getValue(); } @@ -226,9 +245,9 @@ abstract class ConnectAction { * @throws java.lang.NullPointerException * if the counter part has no data to send */ - protected Object recObject() - throws IOException, NoSuchFieldException, NoSuchMethodException, - ClassNotFoundException, java.lang.NullPointerException { + protected Object recObject() throws IOException, NoSuchFieldException, + NoSuchMethodException, ClassNotFoundException, + java.lang.NullPointerException { String str = recString(); if (str == null) { throw new NullPointerException("No more data available"); @@ -254,6 +273,7 @@ abstract class ConnectAction { synchronized (lock) { out.write(line); out.write("\n"); + bytesSent += line.length() + 1; if (server) { out.flush(); @@ -287,7 +307,12 @@ abstract class ConnectAction { contentToSend = false; } - return in.readLine(); + String line = in.readLine(); + if (line != null) { + bytesReceived += line.length(); + } + + return line; } return null; diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java index 85b46a2..60f8db8 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java @@ -77,6 +77,24 @@ abstract class ConnectActionServer { }).start(); } + /** + * The total amount of bytes received. + * + * @return the amount of bytes received + */ + public long getBytesReceived() { + return action.getBytesReceived(); + } + + /** + * The total amount of bytes sent. + * + * @return the amount of bytes sent + */ + public long getBytesSent() { + return action.getBytesSent(); + } + /** * Method that will be called when an action is performed on the server. * diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index f6dd7d8..2dbc3bc 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -38,6 +38,9 @@ abstract class Server implements Runnable { private boolean exiting = false; private int counter; + private long bytesReceived; + private long bytesSent; + private TraceHandler tracer = new TraceHandler(); /** @@ -146,6 +149,24 @@ abstract class Server implements Runnable { return port; } + /** + * The total amount of bytes received. + * + * @return the amount of bytes received + */ + public long getBytesReceived() { + return bytesReceived; + } + + /** + * The total amount of bytes sent. + * + * @return the amount of bytes sent + */ + public long getBytesSent() { + return bytesSent; + } + /** * Start the server (listen on the network for new connections). *

@@ -209,10 +230,16 @@ abstract class Server implements Runnable { new Thread(new Runnable() { @Override public void run() { + ConnectActionServer action = null; try { - createConnectActionServer(s).connect(); + action = createConnectActionServer(s); + action.connect(); } finally { count(-1); + if (action != null) { + bytesReceived += action.getBytesReceived(); + bytesSent += action.getBytesSent(); + } } } }).start();