fs: Fix formatting.

This commit is contained in:
Omar Rizwan 2019-02-25 19:29:04 -08:00
parent 784ec83696
commit a8ce61d9a1
3 changed files with 113 additions and 192 deletions

View file

@ -27,15 +27,15 @@ static cJSON *send_request_then_await_response(cJSON *req) {
return resp;
}
#define MAKE_REQ(op, req_body, resp_handler) \
#define MAKE_REQ(OP, REQ_BUILDER_BODY, RESP_HANDLER_BODY) \
do { \
int ret = -1; \
cJSON *req = NULL; \
cJSON *resp = NULL; \
\
req = cJSON_CreateObject(); \
cJSON_AddStringToObject(req, "op", op); \
req_body \
cJSON_AddStringToObject(req, "op", OP); \
REQ_BUILDER_BODY \
\
resp = send_request_then_await_response(req); \
\
@ -46,22 +46,20 @@ static cJSON *send_request_then_await_response(cJSON *req) {
} \
\
ret = -1; \
resp_handler \
RESP_HANDLER_BODY \
\
done: \
done: \
if (req != NULL) cJSON_Delete(req); \
if (resp != NULL) cJSON_Delete(resp); \
return ret; \
} while (0)
#define JSON_GET_PROP_INT(lvalue, key) \
#define JSON_GET_PROP_INT(LVALUE, KEY) \
do { \
lvalue = cJSON_GetObjectItemCaseSensitive(resp, key)->valueint; \
LVALUE = cJSON_GetObjectItemCaseSensitive(resp, KEY)->valueint; \
} while (0)
static int
tabfs_getattr(const char *path, struct stat *stbuf)
{
static int tabfs_getattr(const char *path, struct stat *stbuf) {
memset(stbuf, 0, sizeof(struct stat));
MAKE_REQ("getattr", {
@ -75,9 +73,7 @@ tabfs_getattr(const char *path, struct stat *stbuf)
});
}
static int
tabfs_readlink(const char *path, char *buf, size_t size)
{
static int tabfs_readlink(const char *path, char *buf, size_t size) {
MAKE_REQ("readlink", {
cJSON_AddStringToObject(req, "path", path);
}, {
@ -93,9 +89,7 @@ tabfs_readlink(const char *path, char *buf, size_t size)
});
}
static int
tabfs_open(const char *path, struct fuse_file_info *fi)
{
static int tabfs_open(const char *path, struct fuse_file_info *fi) {
MAKE_REQ("open", {
cJSON_AddStringToObject(req, "path", path);
cJSON_AddNumberToObject(req, "flags", fi->flags);
@ -109,8 +103,7 @@ tabfs_open(const char *path, struct fuse_file_info *fi)
static int
tabfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
struct fuse_file_info *fi) {
MAKE_REQ("read", {
cJSON_AddStringToObject(req, "path", path);
cJSON_AddNumberToObject(req, "size", size);
@ -143,9 +136,7 @@ static int tabfs_release(const char *path, struct fuse_file_info *fi) {
});
}
static int
tabfs_opendir(const char *path, struct fuse_file_info *fi)
{
static int tabfs_opendir(const char *path, struct fuse_file_info *fi) {
MAKE_REQ("opendir", {
cJSON_AddStringToObject(req, "path", path);
cJSON_AddNumberToObject(req, "flags", fi->flags);
@ -159,9 +150,7 @@ tabfs_opendir(const char *path, struct fuse_file_info *fi)
static int
tabfs_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
off_t offset, struct fuse_file_info *fi) {
MAKE_REQ("readdir", {
cJSON_AddStringToObject(req, "path", path);
}, {
@ -176,8 +165,7 @@ tabfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
}
static int
tabfs_releasedir(const char *path, struct fuse_file_info *fi)
{
tabfs_releasedir(const char *path, struct fuse_file_info *fi) {
MAKE_REQ("releasedir", {
cJSON_AddStringToObject(req, "path", path);
cJSON_AddNumberToObject(req, "fh", fi->fh);

89
fs/ws.c
View file

@ -49,37 +49,17 @@ static void receive_tabfs_request_then_send_to_browser() {
wby_write(con, request_data, strlen(request_data));
wby_frame_end(con);
/* 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); */
// Was allocated by sender (tabfs.c, send_request_then_await_response).
free(request_data);
}
static int
dispatch(struct wby_con *connection, void *userdata)
{
dispatch(struct wby_con *connection, void *userdata) {
return 1;
}
static int
websocket_connect(struct wby_con *connection, void *userdata)
{
websocket_connect(struct wby_con *connection, void *userdata) {
/* connection bound userdata */
connection->user_data = NULL;
if (0 == strcmp(connection->request.uri, "/"))
@ -88,8 +68,7 @@ websocket_connect(struct wby_con *connection, void *userdata)
}
static void
websocket_connected(struct wby_con *connection, void *userdata)
{
websocket_connected(struct wby_con *connection, void *userdata) {
printf("WebSocket connected\n");
con = connection;
}
@ -99,7 +78,8 @@ websocket_connected(struct wby_con *connection, void *userdata)
static int
websocket_frame(struct wby_con *connection, const struct wby_frame *frame, void *userdata)
{
unsigned char *data = calloc(1, MAX_DATA_LENGTH); // Will be freed at receiver (tabfs).
// Will be freed at receiver (tabfs.c, send_request_then_await_response).
unsigned char *data = calloc(1, MAX_DATA_LENGTH);
int i = 0;
DEBUG("WebSocket frame incoming\n");
@ -141,63 +121,20 @@ websocket_frame(struct wby_con *connection, const struct wby_frame *frame, void
common_send_ws_to_tabfs((char *) 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)
{
static void websocket_closed(struct wby_con *connection, void *userdata) {
printf("WebSocket closed\n");
if (con == connection) con = NULL;
}
static void
test_log(const char* text)
{
static void test_log(const char* text) {
DEBUG("[debug] %s\n", text);
}
void await_io_demand_or_timeout() {
/* pthread_mutex_lock(&queue_mutex); */
/* struct timeval now; */
/* gettimeofday(&now, NULL); */
/* struct timespec tsp; */
/* tsp.tv_sec = now.tv_sec; */
/* tsp.tv_nsec = now.tv_usec * 1000; */
/* tsp.tv_nsec += 200 * 1000000; // wait for 200ms max */
/* pthread_cond_timedwait(&queue_cv, &queue_mutex, &tsp); */
/* pthread_mutex_unlock(&queue_mutex); */
}
void *websocket_main(void *threadid)
{
void *websocket_main(void *threadid) {
void *memory = NULL;
wby_size needed_memory = 0;
@ -222,12 +159,8 @@ void *websocket_main(void *threadid)
printf("Awaiting WebSocket connection from Chrome extension.\n");
for (;;) {
// FIXME: makes reconnect impossible. :<
/* await_io_demand_or_timeout(); */
receive_tabfs_request_then_send_to_browser();
wby_update(&server); // We receive stuff during this phase.
wby_update(&server); // We receive stuff from the browser here.
}
wby_stop(&server);