Switch to initialization by config and new event model

This commit is contained in:
2019-03-24 21:00:25 +01:00
parent d28f942cf7
commit 345450b35b
18 changed files with 505 additions and 735 deletions
+28 -37
View File
@@ -23,11 +23,16 @@
#define DEFAULT_HTTP_PORT 8080 /**< Default HTTP server port */
static bool
hello_world(aisl_stream_t s)
static void
hello_world(aisl_evt_t const evt, void * p_ctx)
{
if (evt->code != AISL_EVENT_STREAM_REQUEST)
return;
aisl_status_t status;
aisl_stream_t s = evt->source;
const char html[] =
"<html>"
"<head>"
@@ -51,58 +56,44 @@ hello_world(aisl_stream_t s)
aisl_reject(s);
}
return true; /**< do not propagate event anymore */
(void) p_ctx;
}
int
main(int argc, char ** argv)
{
aisl_t aisl; /**< AISL instance pointer */
aisl_status_t status; /**< AISL status code */
struct aisl_config config = AISL_CONFIG_DEFAULT;
uint16_t port = 0;
aisl_t aisl; /**< AISL instance pointer */
aisl_status_t status; /**< AISL status code */
struct aisl_cfg cfg = AISL_CFG_DEFAULT;
struct aisl_cfg_srv srv = {
.host = "0.0.0.0",
.port = DEFAULT_HTTP_PORT,
.secure = false
};
/* Try to use first argument as a port number */
if (argc > 1)
port = strtoll(argv[1], NULL, 10);
/* Fallback to default if not set or failed */
if (port == 0)
port = DEFAULT_HTTP_PORT;
cfg.srv = &srv;
cfg.srv_cnt = 1;
cfg.callback = hello_world;
/* Initialize instance */
if ( (aisl = aisl_new(&config)) != NULL )
if ( (aisl = aisl_new(&cfg)) != NULL )
{
if (aisl_listen( aisl, "0.0.0.0", port ) != NULL)
/* launch application loop */
fprintf(stdout, "Entering main loop" );
for(;;)
{
/* Set up request callback */
aisl_set_callback( aisl
, AISL_EVENT_STREAM_REQUEST
, AISL_CALLBACK(hello_world) );
status = aisl_run_cycle(aisl);
/* launch application loop */
fprintf(stdout, "Entering main loop" );
for(;;)
{
status = aisl_run_cycle(aisl);
if ( status != AISL_SUCCESS )
aisl_sleep(aisl, 500);
}
aisl_free(aisl);
if ( status != AISL_SUCCESS )
aisl_sleep(aisl, 500);
}
else
fprintf(stderr, "Failed to initialize HTTP server" );
aisl_free(aisl);
}
else
fprintf(stderr, "Failed to initialize AISL");
(void)argc;
(void)argv;
return 0;
}