main.vala 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * implexusd - Implexus server daemon
  3. *
  4. * This daemon provides network access to an Implexus database,
  5. * supporting the client-server protocol for remote operations.
  6. *
  7. * Usage:
  8. * implexusd [OPTIONS]
  9. *
  10. * Options:
  11. * -p, --port PORT Port to listen on (default: 9090)
  12. * -d, --data-dir DIR Data directory path (required)
  13. * -h, --help Show this help message
  14. * -v, --version Show version information
  15. *
  16. * Example:
  17. * implexusd -p 9090 -d /var/lib/implexus
  18. *
  19. * @version 0.1
  20. * @since 0.1
  21. */
  22. public int main(string[] args) {
  23. // Default values
  24. uint16 port = 9090;
  25. string? data_dir = null;
  26. bool show_help = false;
  27. bool show_version = false;
  28. // Parse command line arguments
  29. for (int i = 1; i < args.length; i++) {
  30. string arg = args[i];
  31. if (arg == "-h" || arg == "--help") {
  32. show_help = true;
  33. } else if (arg == "-v" || arg == "--version") {
  34. show_version = true;
  35. } else if ((arg == "-p" || arg == "--port") && i + 1 < args.length) {
  36. i++;
  37. int64 parsed_port = int64.parse(args[i]);
  38. if (parsed_port <= 0 || parsed_port > 65535) {
  39. stderr.printf("Error: Invalid port number: %s\n", args[i]);
  40. return 1;
  41. }
  42. port = (uint16) parsed_port;
  43. } else if ((arg == "-d" || arg == "--data-dir") && i + 1 < args.length) {
  44. i++;
  45. data_dir = args[i];
  46. } else if (arg.has_prefix("-")) {
  47. stderr.printf("Error: Unknown option: %s\n", arg);
  48. show_help = true;
  49. }
  50. }
  51. // Show help
  52. if (show_help) {
  53. print("Implexus Server Daemon - Remote database server\n");
  54. print("\nUsage: implexusd [OPTIONS]\n");
  55. print("\nOptions:\n");
  56. print(" -p, --port PORT Port to listen on (default: 9090)\n");
  57. print(" -d, --data-dir DIR Data directory path (required)\n");
  58. print(" -h, --help Show this help message\n");
  59. print(" -v, --version Show version information\n");
  60. print("\nExample:\n");
  61. print(" implexusd -p 9090 -d /var/lib/implexus\n");
  62. return show_version ? 0 : 1;
  63. }
  64. // Show version
  65. if (show_version) {
  66. print("implexusd version 0.1.0\n");
  67. print("Implexus - Path-based document database\n");
  68. return 0;
  69. }
  70. // Validate required arguments
  71. if (data_dir == null) {
  72. stderr.printf("Error: Data directory is required. Use -d or --data-dir option.\n");
  73. return 1;
  74. }
  75. // Check if data directory exists
  76. var data_path = File.new_for_path((!) data_dir);
  77. if (!data_path.query_exists()) {
  78. // Try to create the directory
  79. try {
  80. data_path.make_directory_with_parents();
  81. print("Created data directory: %s\n", (!) data_dir);
  82. } catch (Error e) {
  83. stderr.printf("Error: Failed to create data directory: %s\n", e.message);
  84. return 1;
  85. }
  86. }
  87. print("Implexus Server Daemon\n");
  88. print("=======================\n\n");
  89. print("Configuration:\n");
  90. print(" Port: %d\n", port);
  91. print(" Data directory: %s\n", (!) data_dir);
  92. print("\n");
  93. try {
  94. // Create the embedded engine
  95. var engine = new Implexus.Engine.EmbeddedEngine.with_path((!) data_dir);
  96. // Create server configuration
  97. var config = new Implexus.Server.ServerConfiguration.with_port(port);
  98. // Create the server
  99. var server = new Implexus.Server.Server.with_configuration(engine, config);
  100. // Connect signals
  101. server.client_connected.connect(() => {
  102. print("[INFO] Client connected\n");
  103. });
  104. server.client_disconnected.connect(() => {
  105. print("[INFO] Client disconnected\n");
  106. });
  107. server.error_occurred.connect((message) => {
  108. print("[ERROR] %s\n", message);
  109. });
  110. server.started.connect(() => {
  111. print("[INFO] Server started\n");
  112. });
  113. server.stopped.connect(() => {
  114. print("[INFO] Server stopped\n");
  115. });
  116. // Start the server
  117. server.start();
  118. print("\nServer is running. Press Ctrl+C to stop.\n\n");
  119. // Run main loop until stopped
  120. var loop = new MainLoop();
  121. // Setup Unix signal handling for graceful shutdown
  122. // Signal.INT = 2, Signal.TERM = 15
  123. Unix.signal_add(2, () => {
  124. print("\n[INFO] Shutting down...\n");
  125. server.stop();
  126. loop.quit();
  127. return Source.REMOVE;
  128. });
  129. Unix.signal_add(15, () => {
  130. print("\n[INFO] Shutting down...\n");
  131. server.stop();
  132. loop.quit();
  133. return Source.REMOVE;
  134. });
  135. loop.run();
  136. return 0;
  137. } catch (Implexus.Server.ServerError e) {
  138. stderr.printf("Server error: %s\n", e.message);
  139. return 1;
  140. } catch (Error e) {
  141. stderr.printf("Error: %s\n", e.message);
  142. return 1;
  143. }
  144. }