aboutsummaryrefslogtreecommitdiff
path: root/data/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'data/server.c')
-rw-r--r--data/server.c46
1 files changed, 17 insertions, 29 deletions
diff --git a/data/server.c b/data/server.c
index ea94364..4574415 100644
--- a/data/server.c
+++ b/data/server.c
@@ -6,6 +6,7 @@
#include <threads.h>
#include "logger.c"
+#include "sql.c"
#define BACKLOG 10
@@ -14,11 +15,6 @@ struct Socket {
struct sockaddr_in addr;
};
-sqlite3 *g_sim_db;
-void (*g_info)(const char *fmt, ...) = log_info;
-void (*g_warn)(const char *fmt, ...) = log_warn;
-#include "sql.c"
-
// ----------------------------------------------------------------------------
// SQL functions
// ----------------------------------------------------------------------------
@@ -27,12 +23,14 @@ void (*g_warn)(const char *fmt, ...) = log_warn;
// Main functions
// ----------------------------------------------------------------------------
void sig_handler(int signo) {
- log_warn("Signal %d received, shutting down data server", signo);
+ (void)signo;
+ g_warn("Signal received, will stop SALIS data server");
+ sql_close();
exit(0);
}
void respond_name(int socket_fd) {
- log_info("Client requested simulation name");
+ g_info("Client requested simulation name");
struct json_object *sim_name = json_object_new_object();
json_object_object_add(sim_name, "name", json_object_new_string(NAME));
@@ -41,7 +39,7 @@ void respond_name(int socket_fd) {
}
void respond_opts(int socket_fd) {
- log_info("Client requested simulation options");
+ g_info("Client requested simulation options");
struct json_object *sim_opts = json_object_from_file(SIM_OPTS);
json_object_to_fd(socket_fd, sim_opts, 0);
@@ -49,7 +47,7 @@ void respond_opts(int socket_fd) {
}
void respond_hash(int socket_fd) {
- log_info("Client requested git hash");
+ g_info("Client requested git hash");
char buff[41] = { 0 };
FILE *pipe = popen("git rev-parse HEAD", "r");
@@ -66,12 +64,12 @@ void respond_data(int socket_fd, struct json_object *request) {
assert(request);
const char *request_str = json_object_to_json_string(request);
- log_info("Client requested simulation data with the following parameters: %s", request_str);
+ g_info("Client requested simulation data with the following parameters: %s", request_str);
struct json_object *response = json_object_new_object();
json_object_object_add(response, "response", json_object_new_string("hello!"));
const char *response_str = json_object_to_json_string(response);
- log_info("Sending response to client: %s", response_str);
+ g_info("Sending response to client: %s", response_str);
json_object_to_fd(socket_fd, response, 0);
shutdown(socket_fd, SHUT_WR);
@@ -83,7 +81,7 @@ int handle_client(struct Socket *socket) {
char socket_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &socket->addr.sin_addr, socket_ip, INET_ADDRSTRLEN);
- log_info("Client connected: %s:%d", socket_ip, ntohs(socket->addr.sin_port));
+ g_info("Client connected: %s:%d", socket_ip, ntohs(socket->addr.sin_port));
struct json_object *request_json = json_object_from_fd(socket->fd);
struct json_object *request_str = NULL;
@@ -107,7 +105,7 @@ int handle_client(struct Socket *socket) {
json_object_put(request_json);
- log_info("Client disconnected: %s:%d", socket_ip, ntohs(socket->addr.sin_port));
+ g_info("Client disconnected: %s:%d", socket_ip, ntohs(socket->addr.sin_port));
close(socket->fd);
free(socket);
@@ -115,23 +113,14 @@ int handle_client(struct Socket *socket) {
}
int main(void) {
+ g_info("Initializing salis data server");
+ g_info("Connecting to database in: %s", DATA_PUSH_PATH);
+ sql_open();
+
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
- log_info("Initializing salis data server");
- log_info("Connecting to database in: %s", DATA_PUSH_PATH);
- sqlite3_open(DATA_PUSH_PATH, &g_sim_db);
- assert(g_sim_db);
-
- // Install busy handler to retry transactions if DB is locked
- sqlite3_busy_timeout(g_sim_db, DATA_PUSH_BUSY_TIMEOUT);
-
- // Enable Write-Ahead Logging (WAL)
- // This seems to help prevent DB locks when displaying live data.
- // See: https://sqlite.org/wal.html
- salis_exec_sql(0, NULL, NULL, "pragma journal_mode=wal;");
-
- log_info("Binding to port: %d", PORT);
+ g_info("Binding to port: %d", PORT);
int opt = 1;
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
@@ -141,7 +130,7 @@ int main(void) {
socket_addr.sin_port = htons(PORT);
bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in));
- log_info("Listening...");
+ g_info("Listening...");
listen(socket_fd, BACKLOG);
while (true) {
@@ -154,6 +143,5 @@ int main(void) {
thrd_detach(thrd);
}
- close(socket_fd);
return 0;
}