package envoy.server;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import com.jenkov.nioserver.Server;
import envoy.server.data.ConfigItem;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectMessageProcessor;
import envoy.server.net.ObjectMessageReader;
import envoy.server.processors.*;
/**
* Starts the server.
*
* Project: envoy-server-standalone
* File: Startup.java
* Created: 24.12.2019
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
public class Startup {
/**
* Starts the server.
*
* @param args the run configuration. Currently unused.
* @throws IOException if the server crashes
* @since Envoy Server Standalone v0.1-alpha
*/
public static void main(String[] args) throws IOException {
Set> processors = new HashSet<>();
processors.add(new LoginCredentialProcessor());
processors.add(new MessageProcessor());
processors.add(new MessageStatusChangeProcessor());
processors.add(new UserStatusChangeProcessor());
processors.add(new IdGeneratorRequestProcessor());
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
initializeCurrentMessageId();
server.start();
server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance());
System.out.println("Press any key to stop the server...");
System.in.read();
System.exit(0);
}
private static void initializeCurrentMessageId() {
PersistenceManager persMan = PersistenceManager.getPersistenceManager();
if (persMan.getConfigItemById("currentMessageId") == null) persMan.addConfigItem(new ConfigItem("currentMessageId", "0"));
}
}