MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ main()

int main ( int  argc,
char **  argv 
)

Main entry point for the Lila server binary.

Parameters
argcArgument count
argvArgument vector
Returns
Exit code

Definition at line 95 of file lila_server.cpp.

96{
97 int port = 9090;
98 bool verbose = false;
99 Lila::LogLevel log_level = Lila::LogLevel::INFO;
100
101 for (int i = 1; i < argc; ++i) {
102 std::string arg = argv[i];
103
104 if (arg == "-h" || arg == "--help") {
105 print_usage(argv[0]);
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) {
124 log_level = parse_log_level(argv[++i]);
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";
131 print_usage(argv[0]);
132 return 1;
133 }
134 }
135
136 auto& logger = Lila::Commentator::instance();
137 logger.set_level(log_level);
138 logger.set_verbose(verbose);
139
140 std::signal(SIGINT, signal_handler);
141 std::signal(SIGTERM, signal_handler);
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
176 Lila::Lila playground;
177
178 if (!playground.initialize(Lila::OperationMode::Server, port)) {
179 LILA_FATAL(Lila::Emitter::SYSTEM,
180 std::string("Failed to initialize: ") + playground.get_last_error());
181 return 1;
182 }
183
184 playground.on_server_started([]() {
185 std::cout << "LILA_SERVER_READY\n"
186 << std::flush;
187 LILA_INFO(Lila::Emitter::SYSTEM, "Server is ready to accept connections");
188 });
189
190 playground.on_success([]() {
191 LILA_INFO(Lila::Emitter::GENERAL, "Code evaluation succeeded");
192 });
193
194 /*
195 playground.on_error([](const std::string& error) {
196 LILA_ERROR(Lila::Emitter::GENERAL,
197 std::string("Evaluation error: ") + error);
198 });
199 */
200
201 playground.on_server_client_connected([](const Lila::ClientInfo& client) {
202 LILA_INFO(Lila::Emitter::SERVER,
203 std::string("New client connection (fd: ") + std::to_string(client.fd) + ", session: " + (client.session_id.empty() ? "none" : client.session_id) + ")");
204 });
205
206 playground.on_server_client_disconnected([](const Lila::ClientInfo& client) {
207 LILA_INFO(Lila::Emitter::SERVER,
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
213 while (g_running) {
214 if (!playground.is_server_running()) {
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...");
222 playground.stop_server();
223 LILA_INFO(Lila::Emitter::SYSTEM, "Goodbye!");
224
225 return 0;
226}
#define LILA_ERROR(emitter, msg)
#define LILA_INFO(emitter, msg)
#define LILA_FATAL(emitter, msg)
static Commentator & instance()
void stop_server()
Stops the TCP server and disconnects all clients.
Definition Lila.cpp:128
void on_server_client_disconnected(std::function< void(const ClientInfo &)> callback)
Registers a callback for client disconnections (server mode)
Definition Lila.cpp:194
bool initialize(OperationMode mode=OperationMode::Direct, int server_port=9090) noexcept
Initializes the live coding environment.
Definition Lila.cpp:23
void on_server_client_connected(std::function< void(const ClientInfo &)> callback)
Registers a callback for new client connections (server mode)
Definition Lila.cpp:186
std::string get_last_error() const
Gets the last error message.
Definition Lila.cpp:202
bool is_server_running() const
Checks if the server is currently running.
Definition Lila.cpp:137
void on_success(std::function< void()> callback)
Registers a callback for successful code evaluation.
Definition Lila.cpp:166
void on_server_started(std::function< void()> callback)
Registers a callback for server start events.
Definition Lila.cpp:178
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)
Definition EventBus.hpp:60
int fd
Client socket file descriptor.
Definition EventBus.hpp:59
Information about a connected client.
Definition EventBus.hpp:58

References Lila::ClientInfo::fd, g_running, Lila::Lila::get_last_error(), Lila::Lila::initialize(), Lila::Commentator::instance(), Lila::Lila::is_server_running(), LILA_ERROR, LILA_FATAL, LILA_INFO, Lila::Lila::on_server_client_connected(), Lila::Lila::on_server_client_disconnected(), Lila::Lila::on_server_started(), Lila::Lila::on_success(), parse_log_level(), print_usage(), Lila::ClientInfo::session_id, signal_handler(), and Lila::Lila::stop_server().

+ Here is the call graph for this function: