
net.scattersphere.server.StreamServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scattersphere-core Show documentation
Show all versions of scattersphere-core Show documentation
Scattersphere Core library, used to run the main server.
The newest version!
package net.scattersphere.server;
import net.scattersphere.registry.StreamRegistry;
import net.scattersphere.server.handler.core.ConnectionCloseHandler;
import net.scattersphere.server.handler.stream.ConnectionReadHandler;
import net.scattersphere.util.PropertyUtil;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.Handler;
import org.vertx.java.core.VertxFactory;
import org.vertx.java.core.net.NetServer;
import org.vertx.java.core.net.NetSocket;
import java.util.Properties;
/**
* Created by kenji on 2/8/15.
*/
public class StreamServer {
private final NetServer controllerServer;
private final Properties properties = new Properties();
private final org.slf4j.Logger LOG = LoggerFactory.getLogger(StreamServer.class);
/**
* As a new connection is received by the server, read and close handlers are assigned to each connection. This way,
* new messages can be handled through their respective handlers by the server.
*/
public class ServerConnectionHandler implements Handler {
@Override
public void handle(NetSocket netSocket) {
ClientConnection client = new ClientConnection(netSocket);
ConnectionReadHandler readHandler = new ConnectionReadHandler(client);
ConnectionCloseHandler closeHandler = new ConnectionCloseHandler(client);
netSocket.dataHandler(readHandler);
// netSocket.closeHandler(closeHandler);
// This close handler needs to actually close the connection after the last portion of the
// stream is sent.
LOG.info("[Streaming] New connection: {}", netSocket);
}
}
/**
* Instantiates a new server.
*
* @param args An array of {@code String} objects provided at command line.
*/
public StreamServer(String args[]) {
if (args.length > 0) {
parseArguments(args);
}
// Initializes the Stream Registry.
StreamRegistry.instance();
controllerServer = VertxFactory.newVertx().createNetServer();
String serverBindAddress = PropertyUtil.get("server.bind.stream.address", Main.serverProperties, "localhost");
int serverBindPort = Integer.parseInt(PropertyUtil.get("server.bind.stream.port", Main.serverProperties, "10002"));
controllerServer.connectHandler(new ServerConnectionHandler())
.setTCPKeepAlive(true)
.listen(serverBindPort, serverBindAddress);
System.err.println("--- Streaming server listening for connections on " + serverBindAddress + ":" + serverBindPort);
}
private void parseArguments(String args[]) {
for(int i = 0; i < args.length; i++) {
String arg = args[i].toLowerCase();
switch(arg) {
default:
// Ignore the setting for now.
break;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy