Skip to content

Commit

Permalink
Refactor sockets to allow for port=0
Browse files Browse the repository at this point in the history
If port=0 the OS picks any available port. This is useful in
ServerConnectionTest where there before was an ugly hack to try
different ports.
  • Loading branch information
jacwah committed Sep 8, 2015
1 parent 35a1370 commit 6444e25
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
Expand Up @@ -69,8 +69,8 @@ public Server start() {
try {
logger.info("Starting Server...");

server.addConnections(new ServerSock(server, config.getPortSocket()));
server.addConnections(new ServerWeb(server, config.getPortWebsocket()));
server.addConnections(new ServerSock(server, config));
server.addConnections(new ServerWeb(server, config));

logger.info("Starting Console...");
CommandHandler commandHandler = server.getCommandHandler();
Expand Down
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

import com.cardshifter.server.main.ServerConfiguration;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

Expand All @@ -22,11 +23,22 @@ public class ServerSock implements ConnectionHandler {
private final Server server;
private final Thread thread;
private final ServerSocket serverSocket;

public ServerSock(Server server, int port) throws IOException {

/**
* Constructor.
* @param server Server instance
* @param config Uses the value of {@code config.getPortSocket} as port. If {@code port == 0} any available port is
* used and the real port number is set in {@code config} before returning.
* @throws IOException
*/
public ServerSock(Server server, ServerConfiguration config) throws IOException {
this.server = server;
this.executor = Executors.newCachedThreadPool(r -> new Thread(r, "Conn-" + threadCounter.getAndIncrement()));
this.serverSocket = new ServerSocket(port);

// If port = 0, use any open port. Set the config port to the real port used.
this.serverSocket = new ServerSocket(config.getPortSocket());
config.setPortSocket(serverSocket.getLocalPort());

this.thread = new Thread(this::run);
}

Expand Down
@@ -1,17 +1,12 @@
package com.cardshifter.server.model;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cardshifter.api.CardshifterSerializationException;
import com.cardshifter.api.messages.Message;
import com.cardshifter.api.serial.ByteTransformer;
import com.cardshifter.server.clients.Base64Utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.cardshifter.server.main.ServerConfiguration;
import net.zomis.cardshifter.ecs.usage.CardshifterIO;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
Expand All @@ -26,9 +21,16 @@ public class ServerWeb implements ConnectionHandler {
private static final Logger logger = LogManager.getLogger(ServerWeb.class);

private final InnerServer websocketServer;

public ServerWeb(Server server, int port) {
this.websocketServer = new InnerServer(server, port);

/**
* Constructor.
* @param server Server instance
* @param config Uses the value of {@code config.getPortWebsocket} as port. If {@code port == 0} any available port
* is used and the real port number is set in {@code config} before returning.
*/
public ServerWeb(Server server, ServerConfiguration config) {
this.websocketServer = new InnerServer(server, config.getPortWebsocket());
config.setPortWebsocket(websocketServer.getPort());
}

private static class InnerServer extends WebSocketServer {
Expand Down
Expand Up @@ -63,26 +63,15 @@ public void setup() throws IOException, InterruptedException {
PropertyConfigurator.configure(getClass().getResourceAsStream("log4j.properties"));
ServerConfiguration config = ServerConfiguration.defaults();

int basePortSocket = config.getPortSocket();
int basePortWebsocket = config.getPortWebsocket();

// The ports might be in use by another instance or application
// Could use port = 0, but would need access to the ServerSocket to get the real port number
for (int i = 0; i < MAX_SERVER_PORT_TRY; i++) {
config.setPortSocket(basePortSocket + i * 10);
config.setPortWebsocket(basePortWebsocket + i * 10);

main = new MainServer(config);
main.getMods().loadExternal(Paths.get("../extra-resources/groovy"));
server = main.start();

if (server.getClients().size() > 0) {
break;
}
}
// Use any available port
config.setPortSocket(0);
config.setPortWebsocket(0);

main = new MainServer(config);
main.getMods().loadExternal(Paths.get("../extra-resources/groovy"));
server = main.start();

assertTrue("Server did not start correctly after " + MAX_SERVER_PORT_TRY + " retries.",
server.getClients().size() > 0);
assertTrue("Server should start correctly.", server.getClients().size() > 0);

socketPort = config.getPortSocket();
client1 = createTestClient();
Expand Down

0 comments on commit 6444e25

Please sign in to comment.