This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/server/src/main/java/envoy/server/Startup.java

90 lines
2.9 KiB
Java
Executable File

package envoy.server;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.logging.Level;
import com.jenkov.nioserver.Server;
import envoy.data.Config;
import envoy.data.ConfigItem;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectMessageProcessor;
import envoy.server.net.ObjectMessageReader;
import envoy.server.processors.*;
import envoy.util.EnvoyLog;
/**
* Starts the server.<br>
* <br>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>Startup.java</strong><br>
* Created: <strong>24.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
public class Startup {
/**
* Initializes the logger with a new config instance.
*
* @since Envoy Server Standalone v0.1-beta
*/
private static void initLogging() {
final var items = new HashMap<String, ConfigItem<?>>();
items.put("homeDirectory",
new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy-server"), true));
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.WARNING, true));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true));
final var config = new Config();
config.load(items);
EnvoyLog.initialize(config);
EnvoyLog.attach("envoy");
}
/**
* 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 {
initLogging();
final Server 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 IDGeneratorRequestProcessor(),
new ContactSearchProcessor(),
new ContactOperationProcessor())));
// Initialize the current message ID
final PersistenceManager 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 (args.length == 0 || !args[0].equalsIgnoreCase("no-enter-to-stop")) {
System.out.println("Press the return key to stop the server...");
System.in.read();
System.out.println("Stopped");
System.exit(0);
}
}
}