From 732ae5a4ed7b716b2eb7ac5a5acad658303418da Mon Sep 17 00:00:00 2001 From: vms Date: Tue, 20 Aug 2019 16:34:16 +0200 Subject: [PATCH] some improvements --- backend-c/Makefile | 2 +- backend-c/run.sh | 73 ++++++++++++++++++++++++++++++++++ backend-c/sdk/syscalls_stubs.c | 14 +++++++ backend-c/src/main.c | 27 +++++-------- backend-c/src/model.c | 2 +- 5 files changed, 99 insertions(+), 19 deletions(-) create mode 100755 backend-c/run.sh create mode 100644 backend-c/sdk/syscalls_stubs.c diff --git a/backend-c/Makefile b/backend-c/Makefile index 0905740..0560122 100644 --- a/backend-c/Makefile +++ b/backend-c/Makefile @@ -1,4 +1,4 @@ -TARGET = hello_world +TARGET = fluid CC = /opt/wasi-sdk/bin/clang SYSROOT = /opt/wasi-sdk/share/wasi-sysroot TARGET_TRIPLE = wasm32-unknown-wasi diff --git a/backend-c/run.sh b/backend-c/run.sh new file mode 100755 index 0000000..20e5279 --- /dev/null +++ b/backend-c/run.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +set -e +set -o pipefail + +mkdir -p wasm + +# Download SQLite +SQLITE="sqlite3_0.2.0.wasm" +if [ ! -f "wasm/$SQLITE" ]; then + echo "Downloading $SQLITE..." + wget -q https://github.com/fluencelabs/sqlite/releases/download/v0.2.0_w/$SQLITE -O ./wasm/$SQLITE +fi + +# Build fluid WASM module +echo "Building to WASM..." +docker-compose up --build +cp fluid.wasm wasm/ +echo + +# Run it all on 30000 port with default Fluence API +echo "Running..." +docker rm -f frun &>/dev/null || true +echo 'docker run -d --name frun --rm -v "$(pwd)/wasm:/code" -p 30000:30000 fluencelabs/frun:latest' +docker run -d --name frun --rm -v "$(pwd)/wasm:/code" -p 30000:30000 fluencelabs/frun:latest >/dev/null +echo + +# Wait for app to be initialized +sleep 1 && (docker logs -f frun 2>&1 &) | grep -q initialized && sleep 1 + + + +############################################ +### *** --- === Sending post === --- *** ### +############################################ + +# Assign json to a variable using heredoc technique +JSON=$(cat </dev/null || echo "$RESPONSE") +# Parse json or print response as is +echo "$RESPONSE" | jq . 2>/dev/null || echo "$RESPONSE" + + +############################################## +### *** --- === Fetching posts === --- *** ### +############################################## + +# Assign json to a variable using heredoc technique +JSON=$(cat </dev/null || echo "$RESPONSE") + +# Parse json or print response as is +echo "$RESPONSE" | jq . 2>/dev/null || echo "$RESPONSE" + + +# Remove frun container +echo -e "Stopping..." +docker rm -f frun >/dev/null diff --git a/backend-c/sdk/syscalls_stubs.c b/backend-c/sdk/syscalls_stubs.c new file mode 100644 index 0000000..5587850 --- /dev/null +++ b/backend-c/sdk/syscalls_stubs.c @@ -0,0 +1,14 @@ +#include + +size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) { + return 1; +} + +int __stdio_close(FILE *f) { + return 1; +} + +off_t __stdio_seek(FILE *_f, off_t _offset, int _value) { + return 1; +} + diff --git a/backend-c/src/main.c b/backend-c/src/main.c index 9e097f0..4e539b0 100644 --- a/backend-c/src/main.c +++ b/backend-c/src/main.c @@ -1,23 +1,11 @@ +#include "model.h" #include "../sdk/allocator.h" #include "../sdk/logger.h" -#include "model.h" +#include "../sdk/syscalls_stubs.c" #include "../libs/tiny-json/tiny-json.h" #include #include -#include - -size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) { - return 1; -} - -int __stdio_close(FILE *f) { - return 1; -} - -off_t __stdio_seek(FILE *_f, off_t _offset, int _value) { - return 0; -} char *prepare_response(const char *response, int response_length) { const int RESPONSE_SIZE_BYTES = 4; @@ -38,17 +26,16 @@ const char *fetch_posts_request(const json_t *json); bool isInited = 0; const char *invoke(char *str, int length) { + // initialize SQLite by creating schema if(0 == isInited) { create_scheme(); isInited = 1; } - wasm_log(str, length); - wasm_log("\n", 1); - json_t pool[10]; const unsigned int pool_size = sizeof pool / sizeof *pool; + // try to parse json and extract action field const json_t *json = json_create(str, pool, pool_size); if(!json) { const char error[] = "Mailformed json given"; @@ -68,12 +55,14 @@ const char *invoke(char *str, int length) { const char *action = json_getValue(action_json); + // use action to determine the desired activity const char *result = ""; if(0 == strcmp(action, "Post")) { result = add_post_request(json); } else if(0 == strcmp(action, "Fetch")) { result = fetch_posts_request(json); } else { + // no suitable action given char *error = (char *)malloc(1024); const int error_size = snprintf(error, 1024, "%s given as the action field, but only `Post` and `Fetch` are supported", action); result = prepare_response(error, error_size); @@ -83,6 +72,7 @@ const char *invoke(char *str, int length) { } const char *add_post_request(const json_t *json) { + // try to extract username and message properties const json_t *username_json = json_getProperty(json, "username"); const json_t *message_json = json_getProperty(json, "message"); if(0 == message_json || 0 == username_json) { @@ -105,6 +95,7 @@ const char *add_post_request(const json_t *json) { return prepare_response(error, sizeof error); } + // returns the updated post count to the client const char *count = get_posts_count(); if(0 == count) { const char error[] = "get_posts_count failed"; @@ -118,6 +109,7 @@ const char *add_post_request(const json_t *json) { } const char *fetch_posts_request(const json_t *json) { + // try to extract username, offset and count fields const json_t *username_json = json_getProperty(json, "username"); const json_t *offset_json = json_getProperty(json, "offset"); const json_t *count_json = json_getProperty(json, "count"); @@ -142,6 +134,7 @@ const char *fetch_posts_request(const json_t *json) { char *result = ""; if(0 == username_json) { + // if no username specified, jsut return all posts result = get_all_posts(offset, count); if(0 == result) { const char error[] = "get_all_posts failed"; diff --git a/backend-c/src/model.c b/backend-c/src/model.c index ba5f169..cfcea26 100644 --- a/backend-c/src/model.c +++ b/backend-c/src/model.c @@ -18,7 +18,7 @@ char *add_post(const char *username, int username_length, const char *message, i const int request_size = username_length + message_length + 50; char *add_sql = (char *)malloc(request_size); - const int add_sql_length = snprintf(add_sql, request_size, "INSERT INTO messages VALUES(%s, %s)", message, username); + const int add_sql_length = snprintf(add_sql, request_size, "INSERT INTO messages VALUES(\"%s\", \"%s\")", message, username); if(add_sql_length < 0) { return 0; }