39 if (signal == SIGINT || signal == SIGTERM) {
40 LILA_INFO(Lila::Emitter::SYSTEM,
"Received shutdown signal");
51 std::cout <<
"Usage: " << program_name <<
" [OPTIONS]\n"
53 <<
" -p, --port <port> Server port (default: 9090)\n"
54 <<
" -v, --verbose Enable verbose logging\n"
55 <<
" -l, --level <level> Set log level (TRACE, DEBUG, INFO, WARN, ERROR, FATAL)\n"
56 <<
" -h, --help Show this help message\n"
58 <<
" " << program_name <<
" # Start on default port 9090\n"
59 <<
" " << program_name <<
" -p 8080 # Start on port 8080\n"
60 <<
" " << program_name <<
" -v -l DEBUG # Verbose mode with DEBUG level\n"
71 if (level_str ==
"TRACE")
72 return Lila::LogLevel::TRACE;
73 if (level_str ==
"DEBUG")
74 return Lila::LogLevel::DEBUG;
75 if (level_str ==
"INFO")
76 return Lila::LogLevel::INFO;
77 if (level_str ==
"WARN")
78 return Lila::LogLevel::WARN;
79 if (level_str ==
"ERROR")
80 return Lila::LogLevel::ERROR;
81 if (level_str ==
"FATAL")
82 return Lila::LogLevel::FATAL;
85 std::string(
"Unknown log level '") + level_str +
"', using INFO");
86 return Lila::LogLevel::INFO;
95int main(
int argc,
char** argv)
99 Lila::LogLevel log_level = Lila::LogLevel::INFO;
101 for (
int i = 1; i < argc; ++i) {
102 std::string arg = argv[i];
104 if (arg ==
"-h" || arg ==
"--help") {
109 if (arg ==
"-p" || arg ==
"--port") {
111 port = std::atoi(argv[++i]);
112 if (port <= 0 || port > 65535) {
113 std::cerr <<
"Error: Invalid port number\n";
117 std::cerr <<
"Error: --port requires an argument\n";
120 }
else if (arg ==
"-v" || arg ==
"--verbose") {
122 }
else if (arg ==
"-l" || arg ==
"--level") {
126 std::cerr <<
"Error: --level requires an argument\n";
130 std::cerr <<
"Error: Unknown option '" << arg <<
"'\n";
137 logger.set_level(log_level);
138 logger.set_verbose(verbose);
143 LILA_INFO(Lila::Emitter::SYSTEM,
"Starting Lila live coding server");
144 LILA_INFO(Lila::Emitter::SYSTEM, std::string(
"Port: ") + std::to_string(port));
146 std::string level_str;
148 case Lila::LogLevel::TRACE:
151 case Lila::LogLevel::DEBUG:
154 case Lila::LogLevel::INFO:
157 case Lila::LogLevel::WARN:
160 case Lila::LogLevel::ERROR:
163 case Lila::LogLevel::FATAL:
167 level_str =
"UNKNOWN";
170 LILA_INFO(Lila::Emitter::SYSTEM, std::string(
"Log level: ") + level_str);
173 LILA_INFO(Lila::Emitter::SYSTEM,
"Verbose mode enabled");
178 if (!playground.
initialize(Lila::OperationMode::Server, port)) {
180 std::string(
"Failed to initialize: ") + playground.
get_last_error());
185 std::cout <<
"LILA_SERVER_READY\n"
187 LILA_INFO(Lila::Emitter::SYSTEM,
"Server is ready to accept connections");
191 LILA_INFO(Lila::Emitter::GENERAL,
"Code evaluation succeeded");
203 std::string(
"New client connection (fd: ") + std::to_string(client.
fd) +
", session: " + (client.
session_id.empty() ?
"none" : client.
session_id) +
")");
208 std::string(
"Client disconnected (fd: ") + std::to_string(client.
fd) +
", session: " + (client.
session_id.empty() ?
"none" : client.
session_id) +
")");
211 LILA_INFO(Lila::Emitter::SYSTEM,
"Server running. Press Ctrl+C to stop.");
215 LILA_ERROR(Lila::Emitter::SYSTEM,
"Server stopped unexpectedly");
218 std::this_thread::sleep_for(std::chrono::milliseconds(100));
221 LILA_INFO(Lila::Emitter::SYSTEM,
"Shutting down...");
223 LILA_INFO(Lila::Emitter::SYSTEM,
"Goodbye!");
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)
bool initialize(OperationMode mode=OperationMode::Direct, int server_port=9090) noexcept
Initializes the live coding environment.
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.
bool is_server_running() const
Checks if the server is currently running.
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.
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.