aboutsummaryrefslogtreecommitdiffstats
path: root/fs/hello.c
blob: f86ae59074ef41af48ba6fcd63e6b6f3465119fa (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <fuse.h>

#define WBY_STATIC
#define WBY_IMPLEMENTATION
#define WBY_USE_FIXED_TYPES
#define WBY_USE_ASSERT
#include "mmx/web.h"

#include "cJSON/cJSON.h"
#include "cJSON/cJSON.c"

struct wby_server server;
struct wby_con *con = NULL;

pthread_cond_t queue_cv = PTHREAD_COND_INITIALIZER;
pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;

enum request_response_state {
  EMPTY = 0,
  SEND_REQUEST,
  RECEIVE_RESPONSE,
  HANDLE_RESPONSE
};

struct request_response {
  enum request_response_state state;

  char *request;
  cJSON *response;

  clock_t start;
};

#define REQUEST_RESPONSE_QUEUE_SIZE 128
typedef int request_id;
struct request_response queue[REQUEST_RESPONSE_QUEUE_SIZE];

static request_id enqueue_request(cJSON *req) {
  pthread_mutex_lock(&queue_mutex);

  // Look for the first free slot.
  request_id id;
  for (id = 0; id < REQUEST_RESPONSE_QUEUE_SIZE; id++) {
    if (queue[id].state == EMPTY) break;
  }
  if (id >= REQUEST_RESPONSE_QUEUE_SIZE) {
    printf("Request-response queue is full!\n");
    exit(1);
  }
  cJSON_AddNumberToObject(req, "id", id);

  queue[id].state = SEND_REQUEST;
  queue[id].request = cJSON_Print(req);
  queue[id].response = NULL;
  queue[id].start = clock();

  /* printf("%s\n", queue[id].request); */

  pthread_mutex_unlock(&queue_mutex);

  return id;
}

void send_any_enqueued_requests() {
  pthread_mutex_lock(&queue_mutex);

  if (con == NULL) goto done;

  for (request_id id = 0; id < REQUEST_RESPONSE_QUEUE_SIZE; id++) {
    if (queue[id].state == SEND_REQUEST) {
      char *request = queue[id].request;

      wby_frame_begin(con, WBY_WSOP_TEXT_FRAME);
      wby_write(con, request, strlen(request));
      wby_frame_end(con);

      queue[id].state = RECEIVE_RESPONSE;
      free(request);
      queue[id].request = NULL;
    }
  }

 done:
  pthread_mutex_unlock(&queue_mutex);
}

static cJSON *await_response(request_id id) {
  pthread_mutex_lock(&queue_mutex);

  while (queue[id].state != HANDLE_RESPONSE) {
    pthread_cond_wait(&queue_cv, &queue_mutex);
  }

  cJSON *resp = queue[id].response;
  queue[id].state = EMPTY;
  queue[id].response = NULL;

  /* printf("Elapsed: %f seconds\n", (double)(clock() - queue[id].start) / CLOCKS_PER_SEC); */

  pthread_mutex_unlock(&queue_mutex);

  return resp;
}

#define MAKE_REQ(op, req_body, resp_handler) \
  do { \
    int ret = -1;                                     \
    cJSON *req = NULL;                              \
    cJSON *resp = NULL;                           \
                                                  \
    pthread_mutex_lock(&queue_mutex); \
    int disconnected = (con == NULL); \
    pthread_mutex_unlock(&queue_mutex); \
    if (disconnected) { ret = -EIO; goto done; }        \
    \
    req = cJSON_CreateObject(); \
    cJSON_AddStringToObject(req, "op", op);        \
    req_body \
    \
    request_id id = enqueue_request(req); \
    resp = await_response(id);        \
    \
    cJSON *error_item = cJSON_GetObjectItemCaseSensitive(resp, "error"); \
    if (error_item) { \
      ret = -error_item->valueint; \
      if (ret != 0) goto done; \
    } \
    \
    ret = -1; \
    resp_handler \
    \
done: \
    if (req != NULL) cJSON_Delete(req); \
    if (resp != NULL) cJSON_Delete(resp); \
    return ret;                               \
  } while (0)

#define JSON_GET_PROP_INT(lvalue, key) \
  do { \
    lvalue = cJSON_GetObjectItemCaseSensitive(resp, key)->valueint;     \
  } while (0)

static int
hello_getattr(const char *path, struct stat *stbuf)
{
    memset(stbuf, 0, sizeof(struct stat));

    MAKE_REQ("getattr", {
        cJSON_AddStringToObject(req, "path", path);
    }, {
        JSON_GET_PROP_INT(stbuf->st_mode, "st_mode");
        JSON_GET_PROP_INT(stbuf->st_nlink, "st_nlink");
        JSON_GET_PROP_INT(stbuf->st_size, "st_size");

        ret = 0;
    });
}

static int
hello_open(const char *path, struct fuse_file_info *fi)
{
    MAKE_REQ("open", {
        cJSON_AddStringToObject(req, "path", path);
        cJSON_AddNumberToObject(req, "flags", fi->flags);
    }, {
        cJSON *fh_item = cJSON_GetObjectItemCaseSensitive(resp, "fh");
        if (fh_item) fi->fh = fh_item->valueint;

        ret = 0;
    });
}

static int
hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
              off_t offset, struct fuse_file_info *fi)
{
    // send {op: "readdir", path} to the websocket handler
    MAKE_REQ("readdir", {
        cJSON_AddStringToObject(req, "path", path);
    }, {
        cJSON *entries = cJSON_GetObjectItemCaseSensitive(resp, "entries");
        cJSON *entry;
        cJSON_ArrayForEach(entry, entries) {
            filler(buf, cJSON_GetStringValue(entry), NULL, 0);
        }

        ret = 0;
    });
}

static int
hello_read(const char *path, char *buf, size_t size, off_t offset,
           struct fuse_file_info *fi)
{
    MAKE_REQ("read", {
        cJSON_AddStringToObject(req, "path", path);
        cJSON_AddNumberToObject(req, "size", size);
        cJSON_AddNumberToObject(req, "offset", offset);

        cJSON_AddNumberToObject(req, "fh", fi->fh);
        cJSON_AddNumberToObject(req, "flags", fi->flags);
    }, {
        size_t resp_size;
        JSON_GET_PROP_INT(resp_size, "size");
        size = resp_size < size ? resp_size : size;

        cJSON *resp_buf_item = cJSON_GetObjectItemCaseSensitive(resp, "buf");
        char *resp_buf = cJSON_GetStringValue(resp_buf_item);
        size_t resp_buf_len = strlen(resp_buf);
        size = resp_buf_len < size ? resp_buf_len : size;

        memcpy(buf, resp_buf, size);

        ret = size;
    });
}

static int hello_release(const char *path, struct fuse_file_info *fi) {
    MAKE_REQ("release", {
        cJSON_AddStringToObject(req, "path", path);
        cJSON_AddNumberToObject(req, "fh", fi->fh);
    }, {
        ret = 0;
    });
}

static struct fuse_operations hello_filesystem_operations = {
    .getattr = hello_getattr, /* To provide size, permissions, etc. */
    .open    = hello_open,    /* To enforce read-only access.       */
    .read    = hello_read,    /* To provide file content.           */
    .release = hello_release,
    .readdir = hello_readdir, /* To provide directory listing.      */
};

static int
dispatch(struct wby_con *connection, void *userdata)
{
    return 1;
}

static int
websocket_connect(struct wby_con *connection, void *userdata)
{
    /* connection bound userdata */
    connection->user_data = NULL;
    if (0 == strcmp(connection->request.uri, "/"))
        return 0;
    return 1;
}

static void
websocket_connected(struct wby_con *connection, void *userdata)
{
    printf("WebSocket connected\n");
    con = connection;
}

static int
websocket_frame(struct wby_con *connection, const struct wby_frame *frame, void *userdata)
{
    unsigned char data[131072] = {0};

    int i = 0;
    /* printf("WebSocket frame incoming\n"); */
    /* printf("  Frame OpCode: %d\n", frame->opcode); */
    /* printf("  Final frame?: %s\n", (frame->flags & WBY_WSF_FIN) ? "yes" : "no"); */
    /* printf("  Masked?     : %s\n", (frame->flags & WBY_WSF_MASKED) ? "yes" : "no"); */
    /* printf("  Data Length : %d\n", (int) frame->payload_length); */

    if ((unsigned long) frame->payload_length > sizeof(data)) {
        printf("Data too long!\n");
        exit(1);
    }
    
    while (i < frame->payload_length) {
        unsigned char buffer[16];
        int remain = frame->payload_length - i;
        size_t read_size = remain > (int) sizeof buffer ? sizeof buffer : (size_t) remain;
        size_t k;

        /* printf("%08x ", (int) i); */
        if (0 != wby_read(connection, buffer, read_size))
            break;
        /* for (k = 0; k < read_size; ++k) */
        /*     printf("%02x ", buffer[k]); */
        /* for (k = read_size; k < 16; ++k) */
        /*     printf("   "); */
        /* printf(" | "); */
        /* for (k = 0; k < read_size; ++k) */
        /*     printf("%c", isprint(buffer[k]) ? buffer[k] : '?'); */
        /* printf("\n"); */
        for (k = 0; k < read_size; ++k)
          data[i + k] = buffer[k];
        i += (int)read_size;
    }

    if ((int) strlen((const char *) data) != frame->payload_length) {
      printf("Null in data! [%s]\n", data);
    }

    // Will be freed at the receiver end.
    cJSON *resp = cJSON_Parse((const char *) data);

    cJSON *id_item = cJSON_GetObjectItemCaseSensitive(resp, "id");
    if (id_item == NULL) {
      printf("No id in response!\n");
      exit(1);
    }
    request_id id = id_item->valueint;

    pthread_mutex_lock(&queue_mutex);

    if (queue[id].state != RECEIVE_RESPONSE) {
      printf("Got response to request in wrong state!\n");
      exit(1);
    }
    queue[id].state = HANDLE_RESPONSE;
    queue[id].response = resp;

    pthread_cond_signal(&queue_cv);
    pthread_mutex_unlock(&queue_mutex);

    return 0;
}

static void
websocket_closed(struct wby_con *connection, void *userdata)
{
    printf("WebSocket closed\n");
    con = NULL;
}

static void
test_log(const char* text)
{
    /* printf("[debug] %s\n", text); */
}

void *websocket_main(void *threadid)
{
    void *memory = NULL;
    wby_size needed_memory = 0;

    struct wby_config config;
    memset(&config, 0, sizeof config);
    config.userdata = NULL;
    config.address = "127.0.0.1";
    config.port = 8888;
    config.connection_max = 4;
    config.request_buffer_size = 2048;
    config.io_buffer_size = 8192;
    config.log = test_log;
    config.dispatch = dispatch;
    config.ws_connect = websocket_connect;
    config.ws_connected = websocket_connected;
    config.ws_frame = websocket_frame;
    config.ws_closed = websocket_closed;
    
    wby_init(&server, &config, &needed_memory);
    memory = calloc(needed_memory, 1);
    wby_start(&server, memory);

    printf("Awaiting WebSocket connection from Chrome extension.\n");
    for (;;) {
        send_any_enqueued_requests();

        wby_update(&server);
    }

    wby_stop(&server);
    free(memory);
#if defined(_WIN32)
    WSACleanup();
#endif
    return 0;
}

int
main(int argc, char **argv)
{
    pthread_t websocket_thread;
    pthread_create(&websocket_thread, NULL, websocket_main, NULL);
    return fuse_main(argc, argv, &hello_filesystem_operations, NULL);

}