aboutsummaryrefslogtreecommitdiff
path: root/data/server.c
blob: d54d8533435371bb11f752b3070997632cd7181c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <arpa/inet.h>
#include <json-c/json.h>
#include <signal.h>
#include <sqlite3.h>
#include <string.h>
#include <threads.h>

#include "logger.c"
#include "sql.c"

#define BACKLOG 10

struct Socket {
    int fd;
    struct sockaddr_in addr;
};

struct json_object *g_response_header;

// ----------------------------------------------------------------------------
// SQL callbacks
// ----------------------------------------------------------------------------
void sql_callback_add_column_name(sqlite3_stmt *sql_stmt, void *data) {
    assert(sql_stmt);
    assert(data);
    assert(sqlite3_column_type(sql_stmt, 0) == SQLITE_TEXT);
    assert(!strcmp(sqlite3_column_name(sql_stmt, 0), "name"));

    const char *col_name = (const char *)sqlite3_column_text(sql_stmt, 0);
    struct json_object *response = (struct json_object *)data;

    if (!json_object_object_get_ex(response, col_name, NULL)) {
        json_object_object_add(response, col_name, json_object_new_array());
    }
}

void sql_callback_add_data(sqlite3_stmt *sql_stmt, void *data) {
    assert(sql_stmt);
    assert(data);

    struct json_object *response = (struct json_object *)data;

    for (int i = 0; i < sqlite3_column_count(sql_stmt); i++) {
        const char *col_name = sqlite3_column_name(sql_stmt, i);
        struct json_object *col_data = json_object_object_get(response, col_name);

        if (col_data) {
            if (sqlite3_column_type(sql_stmt, i) == SQLITE_BLOB) {
                // TODO: render blobs in parallel
            } else {
                json_object_array_add(col_data, json_object_new_int64(sqlite3_column_int64(sql_stmt, i)));
            }
        }
    }
}

// ----------------------------------------------------------------------------
// Main functions
// ----------------------------------------------------------------------------
void sig_handler(int signo) {
    (void)signo;
    log_warn("Signal received, will stop SALIS data server");
    json_object_put(g_response_header);
    sql_close();
    exit(0);
}

void respond_name(int socket_fd) {
    log_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));
    json_object_to_fd(socket_fd, sim_name, 0);
    json_object_put(sim_name);
}

void respond_opts(int socket_fd) {
    log_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);
    json_object_put(sim_opts);
}

void respond_hash(int socket_fd) {
    log_info("Client requested git hash");

    char buff[41] = { 0 };
    FILE *pipe = popen("git rev-parse HEAD", "r");
    fread(buff, sizeof(char), 40, pipe);
    pclose(pipe);

    struct json_object *git_hash = json_object_new_object();
    json_object_object_add(git_hash, "hash", json_object_new_string(buff));
    json_object_to_fd(socket_fd, git_hash, 0);
    json_object_put(git_hash);
}

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);
    const char *x_axis = json_object_get_string(json_object_object_get(request, "x-axis"));
    int64_t x_current = json_object_get_int64(json_object_object_get(request, "x-current"));
    int64_t x_high = json_object_get_int64(json_object_object_get(request, "x-high"));
    int64_t nth = json_object_get_int64(json_object_object_get(request, "nth"));
    int64_t entries = json_object_get_int64(json_object_object_get(request, "entries"));

    const char *x_axis_pref = (!strcmp(x_axis, "rowid") || !strcmp(x_axis, "step")) ? "core." : "";

    struct json_object *response = NULL;
    json_object_deep_copy(g_response_header, &response, NULL);

    sql_exec(
        0, NULL, NULL,
        sql_callback_add_data,
        response,
        "select * from ("
            "select core.rowid, core.step, * from core inner join arch "
            "where core.rowid = arch.rowid and %s%s > %ld and %s%s <= %ld and core.rowid %% %ld == 0 "
            "order by %s%s desc limit %ld"
        ") order by %s asc;",
        x_axis_pref,
        x_axis,
        x_current,
        x_axis_pref,
        x_axis,
        x_high,
        nth,
        x_axis_pref,
        x_axis,
        entries,
        x_axis
    );

    const char *response_str = json_object_to_json_string(response);
    log_info("Responding to client with: %s", response_str);
    json_object_to_fd(socket_fd, response, 0);
    json_object_put(response);

    shutdown(socket_fd, SHUT_WR);
}

int handle_client(struct Socket *socket) {
    assert(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));

    struct json_object *request_json = json_object_from_fd(socket->fd);
    struct json_object *request_str = NULL;

    if (!json_object_object_get_ex(request_json, "request", &request_str)) assert(false);

    const char *request = json_object_get_string(request_str);
    assert(request);

    if (!strcmp(request, "name")) {
        respond_name(socket->fd);
    } else if (!strcmp(request, "opts")) {
        respond_opts(socket->fd);
    } else if (!strcmp(request, "hash")) {
        respond_hash(socket->fd);
    } else if (!strcmp(request, "data")) {
        respond_data(socket->fd, request_json);
    } else {
        assert(false);
    }

    json_object_put(request_json);

    log_info("Client disconnected: %s:%d", socket_ip, ntohs(socket->addr.sin_port));
    close(socket->fd);

    free(socket);
    return 0;
}

int main(void) {
    log_info("Initializing salis data server");
    log_info("Connecting to database in: %s", DATA_PUSH_PATH);
    sql_open();

    signal(SIGINT, sig_handler);
    signal(SIGTERM, sig_handler);

    log_info("Creating response header");
    g_response_header = json_object_new_object();
    json_object_object_add(g_response_header, "rowid", json_object_new_array());
    sql_exec(
        0, NULL, NULL,
        sql_callback_add_column_name,
        g_response_header,
        "select name from pragma_table_info('core') union "
        "select name from pragma_table_info('arch');"
    );

    log_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));
    struct sockaddr_in socket_addr = { 0 };
    socket_addr.sin_family = AF_INET;
    socket_addr.sin_addr.s_addr = INADDR_ANY;
    socket_addr.sin_port = htons(PORT);
    bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in));

    listen(socket_fd, BACKLOG);
    log_info("Listening...");

    while (true) {
        struct Socket *socket = calloc(1, sizeof(struct Socket));
        socklen_t socket_len = sizeof(struct sockaddr_in);
        socket->fd = accept(socket_fd, (struct sockaddr *)&socket->addr, &socket_len);

        thrd_t thrd;
        thrd_create(&thrd, (thrd_start_t)handle_client, socket);
        thrd_detach(thrd);
    }

    return 0;
}