143 lines
2.7 KiB
C
143 lines
2.7 KiB
C
|
/******************************************************************************
|
||
|
*
|
||
|
* Copyright (c) 2017-2019 by Löwenware Ltd
|
||
|
* Please, refer LICENSE file for legal information
|
||
|
*
|
||
|
******************************************************************************/
|
||
|
|
||
|
/**
|
||
|
* @file main.c
|
||
|
* @author Ilja Kartašov <ik@lowenware.com>
|
||
|
* @brief Lowenware.com backend main source code file
|
||
|
*
|
||
|
* @see https://lowenware.com/
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
#include <errno.h>
|
||
|
#include <locale.h>
|
||
|
|
||
|
/* Include library meta header */
|
||
|
#include <aisl/aisl.h>
|
||
|
#include "log.h"
|
||
|
#include "config.h"
|
||
|
#include "observer.h"
|
||
|
#include "agent.h"
|
||
|
|
||
|
|
||
|
static const struct aisl_cfg_srv m_srv[] = {{
|
||
|
.host = "0.0.0.0",
|
||
|
.port = CONFIG_HTTP_PORT,
|
||
|
.secure = false
|
||
|
}};
|
||
|
|
||
|
|
||
|
static const struct aisl_cfg m_cfg = {
|
||
|
AISL_CFG_DEFAULTS
|
||
|
, .srv = m_srv
|
||
|
, .srv_cnt = sizeof (m_srv) / sizeof (m_srv[0])
|
||
|
, .ssl = NULL
|
||
|
, .ssl_cnt = 0
|
||
|
, .callback = observer_on_aisl_event
|
||
|
};
|
||
|
|
||
|
|
||
|
static int
|
||
|
process_args(int argc, char **argv)
|
||
|
{
|
||
|
int i;
|
||
|
char c = 0;
|
||
|
|
||
|
for (i = 1; i < argc; i++) {
|
||
|
switch (c) {
|
||
|
case 'd':
|
||
|
if (chdir(argv[i]) != 0) {
|
||
|
LOG_ERROR("Failed to set working directory `%s`: %s", argv[i],
|
||
|
strerror(errno));
|
||
|
return -1;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
if (strcmp(argv[i], "-d") == 0) {
|
||
|
c = 'd';
|
||
|
} else if (strcmp(argv[i], "-h") == 0) {
|
||
|
printf("arcd %d.%d.%d\n\n", VERSION_MAJOR, VERSION_MINOR, VERSION_TWEAK);
|
||
|
printf("USAGE: arcd [ARGUMENTS]\n");
|
||
|
printf("ARGUMENTS:\n");
|
||
|
printf("\t-h - show this information\n");
|
||
|
printf("\t-d :dir - set custom working directory\n");
|
||
|
printf("\n");
|
||
|
exit(0);
|
||
|
}
|
||
|
continue;
|
||
|
}
|
||
|
c = 0;
|
||
|
}
|
||
|
|
||
|
if (c) {
|
||
|
LOG_ERROR("No value for -%c argument", c);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
int
|
||
|
main(int argc, char **argv)
|
||
|
{
|
||
|
AislInstance aisl; /**< AISL instance pointer */
|
||
|
char cwd[64];
|
||
|
|
||
|
AX_LOG_INIT(CSTUFF_LOG_ALL);
|
||
|
|
||
|
if (!setlocale(LC_ALL, "C")) {
|
||
|
LOG_ERROR("Failed to set C locale");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (process_args(argc, argv)) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
LOG_STATE("working directory: `%s`", getcwd(cwd, sizeof(cwd)));
|
||
|
|
||
|
/* Initialize instance */
|
||
|
if ((aisl = aisl_new(&m_cfg)) != NULL) {
|
||
|
if (observer_init() == 0) {
|
||
|
if (agent_init() == 0) {
|
||
|
/* launch application loop */
|
||
|
fprintf(stdout, "Entering main loop");
|
||
|
for(;;) {
|
||
|
int rc, do_sleep = 1;
|
||
|
|
||
|
if ((rc = aisl_run_cycle(aisl)) == AISL_SUCCESS)
|
||
|
do_sleep = 0;
|
||
|
|
||
|
if ((rc = agent_run_cycle()) == 0)
|
||
|
do_sleep = 0;
|
||
|
|
||
|
if (!do_sleep)
|
||
|
continue;
|
||
|
|
||
|
aisl_sleep(aisl, 500);
|
||
|
}
|
||
|
agent_release();
|
||
|
}
|
||
|
observer_release();
|
||
|
}
|
||
|
aisl_free(aisl);
|
||
|
} else {
|
||
|
fprintf(stderr, "Failed to initialize AISL");
|
||
|
}
|
||
|
|
||
|
AX_LOG_RELEASE();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|