package envoy.server; import java.io.IOException; import java.util.Set; import java.util.logging.Level; import com.jenkov.nioserver.Server; import envoy.server.data.*; import envoy.server.net.*; import envoy.server.processors.*; import envoy.util.EnvoyLog; /** * Starts the server. * * @author Kai S. K. Engelbart * @since Envoy Server Standalone v0.1-alpha */ public final class Startup { /** * Stores the configuration used for the whole server */ public static final ServerConfig config = ServerConfig.getInstance(); /** * Starts the server. * * @param args the run configuration. If it is "no-enter-to-stop" at position 0, * no command to read in an enter press will be generated * @throws IOException if the server crashes * @since Envoy Server Standalone v0.1-alpha */ public static void main(String[] args) throws IOException { try { config.loadAll(Startup.class, "server.properties", args); EnvoyLog.initialize(config); } catch (final IllegalStateException e) { EnvoyLog.getLogger(Startup.class).log(Level.SEVERE, "Error loading configuration values: ", e); System.exit(1); } final var server = new Server(8080, ObjectMessageReader::new, new ObjectMessageProcessor(Set.of(new LoginCredentialProcessor(), new MessageProcessor(), new GroupMessageProcessor(), new GroupCreationProcessor(), new MessageStatusChangeProcessor(), new GroupMessageStatusChangeProcessor(), new UserStatusChangeProcessor(), new GroupResizeProcessor(), new IDGeneratorRequestProcessor(), new UserSearchProcessor(), new UserOperationProcessor(), new IsTypingProcessor(), new NameChangeProcessor(), new ProfilePicChangeProcessor(), new PasswordChangeRequestProcessor(), new IssueProposalProcessor()))); // Initialize the current message ID final var persistenceManager = PersistenceManager.getInstance(); if (persistenceManager.getConfigItemByID("currentMessageId") == null) persistenceManager.addConfigItem(new envoy.server.data.ConfigItem("currentMessageId", "0")); server.start(); server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance()); if (config.isEnterToStop()) { System.out.println("Press Enter to stop the server..."); System.in.read(); System.out.println("Stopped"); System.exit(0); } } }