101{
102 int port = 9090;
103 bool verbose = false;
105
106 for (int i = 1; i < argc; ++i) {
107 std::string arg = argv[i];
108
109 if (arg == "-h" || arg == "--help") {
111 return 0;
112 }
113
114 if (arg == "-p" || arg == "--port") {
115 if (i + 1 < argc) {
116 port = std::atoi(argv[++i]);
117 if (port <= 0 || port > 65535) {
118 std::cerr << "Error: Invalid port number\n";
119 return 1;
120 }
121 } else {
122 std::cerr << "Error: --port requires an argument\n";
123 return 1;
124 }
125 } else if (arg == "-v" || arg == "--verbose") {
126 verbose = true;
127 } else if (arg == "-l" || arg == "--level") {
128 if (i + 1 < argc) {
130 } else {
131 std::cerr << "Error: --level requires an argument\n";
132 return 1;
133 }
134 } else {
135 std::cerr << "Error: Unknown option '" << arg << "'\n";
137 return 1;
138 }
139 }
140
142 logger.set_level(log_level);
143 logger.set_verbose(verbose);
144
147
148 LILA_INFO(Lila::Emitter::SYSTEM,
"Starting Lila live coding server");
149 LILA_INFO(Lila::Emitter::SYSTEM, std::string(
"Port: ") + std::to_string(port));
150
151 std::string level_str;
152 switch (log_level) {
153 case Lila::LogLevel::TRACE:
154 level_str = "TRACE";
155 break;
156 case Lila::LogLevel::DEBUG:
157 level_str = "DEBUG";
158 break;
159 case Lila::LogLevel::INFO:
160 level_str = "INFO";
161 break;
162 case Lila::LogLevel::WARN:
163 level_str = "WARN";
164 break;
165 case Lila::LogLevel::ERROR:
166 level_str = "ERROR";
167 break;
168 case Lila::LogLevel::FATAL:
169 level_str = "FATAL";
170 break;
171 default:
172 level_str = "UNKNOWN";
173 break;
174 }
175 LILA_INFO(Lila::Emitter::SYSTEM, std::string(
"Log level: ") + level_str);
176
177 if (verbose) {
178 LILA_INFO(Lila::Emitter::SYSTEM,
"Verbose mode enabled");
179 }
180
182
183 if (!playground.
initialize(Lila::OperationMode::Server, port)) {
185 std::string(
"Failed to initialize: ") + playground.
get_last_error());
186 return 1;
187 }
188
190 std::cout << "LILA_SERVER_READY\n"
191 << std::flush;
192 LILA_INFO(Lila::Emitter::SYSTEM,
"Server is ready to accept connections");
193 });
194
196 LILA_INFO(Lila::Emitter::GENERAL,
"Code evaluation succeeded");
197 });
198
199
200
201
202
203
204
205
208 std::string(
"New client connection (fd: ") + std::to_string(client.
fd) +
", session: " + (client.
session_id.empty() ?
"none" : client.session_id) +
")");
209 });
210
213 std::string(
"Client disconnected (fd: ") + std::to_string(client.
fd) +
", session: " + (client.
session_id.empty() ?
"none" : client.session_id) +
")");
214 });
215
216 LILA_INFO(Lila::Emitter::SYSTEM,
"Server running. Press Ctrl+C to stop.");
217
219
220 LILA_INFO(Lila::Emitter::SYSTEM,
"Shutting down...");
222 LILA_INFO(Lila::Emitter::SYSTEM,
"Goodbye!");
223
224 return 0;
225}
bool initialize(OperationMode mode=OperationMode::Direct, int server_port=9090, bool skip_host_library_load=false) noexcept
Initializes the live coding environment.
void stop_server()
Stops the TCP server and disconnects all clients.
void on_server_client_disconnected(std::function< void(const ClientInfo &)> callback)
Registers a callback for client disconnections (server mode)
void on_server_client_connected(std::function< void(const ClientInfo &)> callback)
Registers a callback for new client connections (server mode)
std::string get_last_error() const
Gets the last error message.
void await_shutdown(const std::atomic< bool > *external_flag)
Blocks until server shutdown (main thread event loop)
void on_success(std::function< void()> callback)
Registers a callback for successful code evaluation.
void on_server_started(std::function< void()> callback)
Registers a callback for server start events.
LogLevel
Log severity levels for Commentator.
void signal_handler(int signal)
Handles SIGINT and SIGTERM for graceful shutdown.
Lila::LogLevel parse_log_level(const std::string &level_str)
Parses a string to a Lila::LogLevel value.
std::atomic< bool > g_running
Global flag to control server running state.
void print_usage(const char *program_name)
Prints usage information for the server binary.
std::string session_id
Session identifier (if set)
int fd
Client socket file descriptor.
Information about a connected client.