96{
97 int port = 9090;
98 bool verbose = false;
100
101 for (int i = 1; i < argc; ++i) {
102 std::string arg = argv[i];
103
104 if (arg == "-h" || arg == "--help") {
106 return 0;
107 }
108
109 if (arg == "-p" || arg == "--port") {
110 if (i + 1 < argc) {
111 port = std::atoi(argv[++i]);
112 if (port <= 0 || port > 65535) {
113 std::cerr << "Error: Invalid port number\n";
114 return 1;
115 }
116 } else {
117 std::cerr << "Error: --port requires an argument\n";
118 return 1;
119 }
120 } else if (arg == "-v" || arg == "--verbose") {
121 verbose = true;
122 } else if (arg == "-l" || arg == "--level") {
123 if (i + 1 < argc) {
125 } else {
126 std::cerr << "Error: --level requires an argument\n";
127 return 1;
128 }
129 } else {
130 std::cerr << "Error: Unknown option '" << arg << "'\n";
132 return 1;
133 }
134 }
135
137 logger.set_level(log_level);
138 logger.set_verbose(verbose);
139
142
143 LILA_INFO(Lila::Emitter::SYSTEM,
"Starting Lila live coding server");
144 LILA_INFO(Lila::Emitter::SYSTEM, std::string(
"Port: ") + std::to_string(port));
145
146 std::string level_str;
147 switch (log_level) {
148 case Lila::LogLevel::TRACE:
149 level_str = "TRACE";
150 break;
151 case Lila::LogLevel::DEBUG:
152 level_str = "DEBUG";
153 break;
154 case Lila::LogLevel::INFO:
155 level_str = "INFO";
156 break;
157 case Lila::LogLevel::WARN:
158 level_str = "WARN";
159 break;
160 case Lila::LogLevel::ERROR:
161 level_str = "ERROR";
162 break;
163 case Lila::LogLevel::FATAL:
164 level_str = "FATAL";
165 break;
166 default:
167 level_str = "UNKNOWN";
168 break;
169 }
170 LILA_INFO(Lila::Emitter::SYSTEM, std::string(
"Log level: ") + level_str);
171
172 if (verbose) {
173 LILA_INFO(Lila::Emitter::SYSTEM,
"Verbose mode enabled");
174 }
175
177
178 if (!playground.
initialize(Lila::OperationMode::Server, port)) {
180 std::string(
"Failed to initialize: ") + playground.
get_last_error());
181 return 1;
182 }
183
185 std::cout << "LILA_SERVER_READY\n"
186 << std::flush;
187 LILA_INFO(Lila::Emitter::SYSTEM,
"Server is ready to accept connections");
188 });
189
191 LILA_INFO(Lila::Emitter::GENERAL,
"Code evaluation succeeded");
192 });
193
194
195
196
197
198
199
200
203 std::string(
"New client connection (fd: ") + std::to_string(client.
fd) +
", session: " + (client.
session_id.empty() ?
"none" : client.session_id) +
")");
204 });
205
208 std::string(
"Client disconnected (fd: ") + std::to_string(client.
fd) +
", session: " + (client.
session_id.empty() ?
"none" : client.session_id) +
")");
209 });
210
211 LILA_INFO(Lila::Emitter::SYSTEM,
"Server running. Press Ctrl+C to stop.");
212
215 LILA_ERROR(Lila::Emitter::SYSTEM,
"Server stopped unexpectedly");
216 break;
217 }
218 std::this_thread::sleep_for(std::chrono::milliseconds(100));
219 }
220
221 LILA_INFO(Lila::Emitter::SYSTEM,
"Shutting down...");
223 LILA_INFO(Lila::Emitter::SYSTEM,
"Goodbye!");
224
225 return 0;
226}
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.
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.