/** * implexusd - Implexus server daemon * * This daemon provides network access to an Implexus database, * supporting the client-server protocol for remote operations. * * Usage: * implexusd [OPTIONS] * * Options: * -p, --port PORT Port to listen on (default: 9090) * -d, --data-dir DIR Data directory path (required) * -h, --help Show this help message * -v, --version Show version information * * Example: * implexusd -p 9090 -d /var/lib/implexus * * @version 0.1 * @since 0.1 */ public int main(string[] args) { // Default values uint16 port = 9090; string? data_dir = null; bool show_help = false; bool show_version = false; // Parse command line arguments for (int i = 1; i < args.length; i++) { string arg = args[i]; if (arg == "-h" || arg == "--help") { show_help = true; } else if (arg == "-v" || arg == "--version") { show_version = true; } else if ((arg == "-p" || arg == "--port") && i + 1 < args.length) { i++; int64 parsed_port = int64.parse(args[i]); if (parsed_port <= 0 || parsed_port > 65535) { stderr.printf("Error: Invalid port number: %s\n", args[i]); return 1; } port = (uint16) parsed_port; } else if ((arg == "-d" || arg == "--data-dir") && i + 1 < args.length) { i++; data_dir = args[i]; } else if (arg.has_prefix("-")) { stderr.printf("Error: Unknown option: %s\n", arg); show_help = true; } } // Show help if (show_help) { print("Implexus Server Daemon - Remote database server\n"); print("\nUsage: implexusd [OPTIONS]\n"); print("\nOptions:\n"); print(" -p, --port PORT Port to listen on (default: 9090)\n"); print(" -d, --data-dir DIR Data directory path (required)\n"); print(" -h, --help Show this help message\n"); print(" -v, --version Show version information\n"); print("\nExample:\n"); print(" implexusd -p 9090 -d /var/lib/implexus\n"); return show_version ? 0 : 1; } // Show version if (show_version) { print("implexusd version 0.1.0\n"); print("Implexus - Path-based document database\n"); return 0; } // Validate required arguments if (data_dir == null) { stderr.printf("Error: Data directory is required. Use -d or --data-dir option.\n"); return 1; } // Check if data directory exists var data_path = File.new_for_path((!) data_dir); if (!data_path.query_exists()) { // Try to create the directory try { data_path.make_directory_with_parents(); print("Created data directory: %s\n", (!) data_dir); } catch (Error e) { stderr.printf("Error: Failed to create data directory: %s\n", e.message); return 1; } } print("Implexus Server Daemon\n"); print("=======================\n\n"); print("Configuration:\n"); print(" Port: %d\n", port); print(" Data directory: %s\n", (!) data_dir); print("\n"); try { // Create the embedded engine var engine = new Implexus.Engine.EmbeddedEngine.with_path((!) data_dir); // Create server configuration var config = new Implexus.Server.ServerConfiguration.with_port(port); // Create the server var server = new Implexus.Server.Server.with_configuration(engine, config); // Connect signals server.client_connected.connect(() => { print("[INFO] Client connected\n"); }); server.client_disconnected.connect(() => { print("[INFO] Client disconnected\n"); }); server.error_occurred.connect((message) => { print("[ERROR] %s\n", message); }); server.started.connect(() => { print("[INFO] Server started\n"); }); server.stopped.connect(() => { print("[INFO] Server stopped\n"); }); // Start the server server.start(); print("\nServer is running. Press Ctrl+C to stop.\n\n"); // Run main loop until stopped var loop = new MainLoop(); // Setup Unix signal handling for graceful shutdown // Signal.INT = 2, Signal.TERM = 15 Unix.signal_add(2, () => { print("\n[INFO] Shutting down...\n"); server.stop(); loop.quit(); return Source.REMOVE; }); Unix.signal_add(15, () => { print("\n[INFO] Shutting down...\n"); server.stop(); loop.quit(); return Source.REMOVE; }); loop.run(); return 0; } catch (Implexus.Server.ServerError e) { stderr.printf("Server error: %s\n", e.message); return 1; } catch (Error e) { stderr.printf("Error: %s\n", e.message); return 1; } }