X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2Fserver%2FServer.java;h=ed27557e4cd56e0c8335c1d0ef05cc5ca84b68f4;hb=088c1d7a37212c54251438841995aeb8e5b26fad;hp=f6dd7d86a13cbf94c4c08279458ae3724b8b2f5b;hpb=5795c978b9308afeb07e1cd41b0067c8278427c3;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index f6dd7d8..ed27557 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -4,13 +4,6 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; - -import javax.net.ssl.SSLServerSocket; -import javax.net.ssl.SSLServerSocketFactory; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.SSLSocketFactory; import be.nikiroo.utils.TraceHandler; @@ -24,10 +17,9 @@ import be.nikiroo.utils.TraceHandler; * @author niki */ abstract class Server implements Runnable { - static private final String[] ANON_CIPHERS = getAnonCiphers(); + protected final String key; private final String name; - private final boolean ssl; private final Object lock = new Object(); private final Object counterLock = new Object(); @@ -38,6 +30,9 @@ abstract class Server implements Runnable { private boolean exiting = false; private int counter; + private long bytesReceived; + private long bytesSent; + private TraceHandler tracer = new TraceHandler(); /** @@ -58,8 +53,32 @@ abstract class Server implements Runnable { * the port to listen on, or 0 to assign any unallocated port * found (which can later on be queried via * {@link Server#getPort()} - * @param ssl - * use a SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) + * + * @throws IOException + * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive + */ + public Server(int port, String key) throws IOException { + this((String) null, port, key); + } + + /** + * Create a new server that will start listening on the network when + * {@link Server#start()} is called. + *
+ * All the communications will happen in plain text. + * + * @param name + * the server name (only used for debug info and traces) + * @param port + * the port to listen on * * @throws IOException * in case of I/O error @@ -69,8 +88,8 @@ abstract class Server implements Runnable { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public Server(int port, boolean ssl) throws IOException { - this((String) null, port, ssl); + public Server(String name, int port) throws IOException { + this(name, port, null); } /** @@ -81,8 +100,9 @@ abstract class Server implements Runnable { * the server name (only used for debug info and traces) * @param port * the port to listen on - * @param ssl - * use a SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -92,11 +112,11 @@ abstract class Server implements Runnable { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public Server(String name, int port, boolean ssl) throws IOException { + public Server(String name, int port, String key) throws IOException { this.name = name; this.port = port; - this.ssl = ssl; - this.ss = createSocketServer(port, ssl); + this.key = key; + this.ss = new ServerSocket(port); if (this.port == 0) { this.port = this.ss.getLocalPort(); @@ -146,6 +166,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). *
@@ -192,16 +230,7 @@ abstract class Server implements Runnable {
try {
tracer.trace(name + ": server starting on port " + port + " ("
- + (ssl ? "SSL" : "plain text") + ")");
-
- String ciphers = "";
- for (String cipher : getAnonCiphers()) {
- if (!ciphers.isEmpty()) {
- ciphers += ", ";
- }
- ciphers += cipher;
- }
- tracer.trace("Available SSL ciphers: " + ciphers);
+ + (key != null ? "encrypted" : "plain text") + ")");
while (started && !exiting) {
count(1);
@@ -209,10 +238,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();
@@ -288,8 +323,8 @@ abstract class Server implements Runnable {
exiting = true;
try {
- new ConnectActionClientObject(createSocket(null, port, ssl))
- .connect();
+ new ConnectActionClientObject(new Socket((String) null,
+ port), key).connect();
long time = 0;
while (ss != null && timeout > 0 && timeout > time) {
Thread.sleep(10);
@@ -348,8 +383,6 @@ abstract class Server implements Runnable {
* the host to connect to
* @param port
* the port to connect to
- * @param ssl
- * TRUE for SSL mode (or FALSE for plain text mode)
*
* @return the {@link Socket}
*
@@ -361,20 +394,9 @@ abstract class Server implements Runnable {
* if the port parameter is outside the specified range of valid
* port values, which is between 0 and 65535, inclusive
*/
- static Socket createSocket(String host, int port, boolean ssl)
- throws IOException {
- Socket s;
- if (ssl) {
- s = SSLSocketFactory.getDefault().createSocket(host, port);
- if (s instanceof SSLSocket) {
- // Should always be the case
- ((SSLSocket) s).setEnabledCipherSuites(ANON_CIPHERS);
- }
- } else {
- s = new Socket(host, port);
- }
-
- return s;
+ @Deprecated
+ static Socket createSocket(String host, int port) throws IOException {
+ return new Socket(host, port);
}
/**
@@ -382,8 +404,6 @@ abstract class Server implements Runnable {
*
* @param port
* the port to accept connections on
- * @param ssl
- * TRUE for SSL mode (or FALSE for plain text mode)
*
* @return the {@link ServerSocket}
*
@@ -395,36 +415,8 @@ abstract class Server implements Runnable {
* if the port parameter is outside the specified range of valid
* port values, which is between 0 and 65535, inclusive
*/
- static ServerSocket createSocketServer(int port, boolean ssl)
- throws IOException {
- ServerSocket ss;
- if (ssl) {
- ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
- if (ss instanceof SSLServerSocket) {
- // Should always be the case
- ((SSLServerSocket) ss).setEnabledCipherSuites(ANON_CIPHERS);
- }
- } else {
- ss = new ServerSocket(port);
- }
-
- return ss;
- }
-
- /**
- * Return all the supported ciphers that do not use authentication.
- *
- * @return the list of such supported ciphers
- */
- public static String[] getAnonCiphers() {
- List