import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
private boolean exiting = false;
private int counter;
+ private long bytesReceived;
+ private long bytesSent;
+
private TraceHandler tracer = new TraceHandler();
/**
*
* @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);
*
* @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;
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).
* <p>
}
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);
- Socket s = ss.accept();
- createConnectActionServer(s).connectAsync();
+ final Socket s = ss.accept();
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ ConnectActionServer action = null;
+ try {
+ action = createConnectActionServer(s);
+ action.connect();
+ } finally {
+ count(-1);
+ if (action != null) {
+ bytesReceived += action.getBytesReceived();
+ bytesSent += action.getBytesSent();
+ }
+ }
+ }
+ }).start();
}
// Will be covered by @link{Server#stop(long)} for timeouts
}
}
}
+ }
- // 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) {
}
}
}
- /**
- * This method will be called on errors.
- * <p>
- * By default, it will only call the trace handler (so you may want to call
- * super {@link Server#onError} if you override it).
- *
- * @param e
- * the error
- */
- protected void onError(Exception e) {
- tracer.error(e);
- }
-
/**
* Change the number of currently serviced actions.
*
*
* @return the current number after this operation
*/
- int count(int change) {
+ private int count(int change) {
synchronized (counterLock) {
counter += change;
return counter;
}
}
+ /**
+ * This method will be called on errors.
+ * <p>
+ * By default, it will only call the trace handler (so you may want to call
+ * super {@link Server#onError} if you override it).
+ *
+ * @param e
+ * the error
+ */
+ protected void onError(Exception e) {
+ tracer.error(e);
+ }
+
/**
* Create a {@link Socket}.
*
*
* @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);
}
*
* @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);
}
*
* @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()) {