server: add trace to display SSL ciphers
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / Server.java
index be847d02884ec3112affd0d730d0d364b158e458..f6dd7d86a13cbf94c4c08279458ae3724b8b2f5b 100644 (file)
@@ -3,6 +3,7 @@ package be.nikiroo.utils.serial.server;
 import java.io.IOException;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -62,6 +63,11 @@ abstract class Server implements Runnable {
         * 
         * @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, boolean ssl) throws IOException {
                this((String) null, port, ssl);
@@ -80,6 +86,11 @@ abstract class Server implements Runnable {
         * 
         * @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(String name, int port, boolean ssl) throws IOException {
                this.name = name;
@@ -180,7 +191,17 @@ abstract class Server implements Runnable {
                }
 
                try {
-                       tracer.trace(name + ": server starting on port " + port);
+                       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);
 
                        while (started && !exiting) {
                                count(1);
@@ -281,13 +302,13 @@ abstract class Server implements Runnable {
                                        }
                                }
                        }
+               }
 
-                       // only return when stopped
-                       while (started || exiting) {
-                               try {
-                                       Thread.sleep(10);
-                               } catch (InterruptedException e) {
-                               }
+               // return only when stopped
+               while (started || exiting) {
+                       try {
+                               Thread.sleep(10);
+                       } catch (InterruptedException e) {
                        }
                }
        }
@@ -334,13 +355,21 @@ abstract class Server implements Runnable {
         * 
         * @throws IOException
         *             in case of I/O error
+        * @throws UnknownHostException
+        *             if the host is not known
+        * @throws IllegalArgumentException
+        *             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);
-                       ((SSLSocket) s).setEnabledCipherSuites(ANON_CIPHERS);
+                       if (s instanceof SSLSocket) {
+                               // Should always be the case
+                               ((SSLSocket) s).setEnabledCipherSuites(ANON_CIPHERS);
+                       }
                } else {
                        s = new Socket(host, port);
                }
@@ -360,13 +389,21 @@ abstract class Server implements Runnable {
         * 
         * @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
         */
        static ServerSocket createSocketServer(int port, boolean ssl)
                        throws IOException {
                ServerSocket ss;
                if (ssl) {
                        ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
-                       ((SSLServerSocket) ss).setEnabledCipherSuites(ANON_CIPHERS);
+                       if (ss instanceof SSLServerSocket) {
+                               // Should always be the case
+                               ((SSLServerSocket) ss).setEnabledCipherSuites(ANON_CIPHERS);
+                       }
                } else {
                        ss = new ServerSocket(port);
                }
@@ -379,7 +416,7 @@ abstract class Server implements Runnable {
         * 
         * @return the list of such supported ciphers
         */
-       private static String[] getAnonCiphers() {
+       public static String[] getAnonCiphers() {
                List<String> anonCiphers = new ArrayList<String>();
                for (String cipher : ((SSLSocketFactory) SSLSocketFactory.getDefault())
                                .getSupportedCipherSuites()) {