diff options
Diffstat (limited to 'fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps')
7 files changed, 400 insertions, 0 deletions
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/connection.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/connection.c new file mode 100644 index 00000000..d8efefdc --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/connection.c @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" +#include "wa-inc/connection.h" +#include "wa-inc/timer_wasm_app.h" +#include "wa-inc/request.h" + +/* User global variable */ +static int num = 0; +static user_timer_t g_timer; +static connection_t *g_conn = NULL; + +void +on_data1(connection_t *conn, conn_event_type_t type, const char *data, + uint32 len, void *user_data) +{ + if (type == CONN_EVENT_TYPE_DATA) { + char message[64] = { 0 }; + memcpy(message, data, len); + printf("Client got a message from server -> %s\n", message); + } + else if (type == CONN_EVENT_TYPE_DISCONNECT) { + printf("connection is close by server!\n"); + } + else { + printf("error: got unknown event type!!!\n"); + } +} + +/* Timer callback */ +void +timer1_update(user_timer_t timer) +{ + char message[64] = { 0 }; + /* Reply to server */ + snprintf(message, sizeof(message), "Hello %d", num++); + api_send_on_connection(g_conn, message, strlen(message)); +} + +void +my_close_handler(request_t *request) +{ + response_t response[1]; + + if (g_conn != NULL) { + api_timer_cancel(g_timer); + api_close_connection(g_conn); + } + + make_response_for_request(request, response); + set_response(response, DELETED_2_02, 0, NULL, 0); + api_response_send(response); +} + +void +on_init() +{ + user_timer_t timer; + attr_container_t *args; + char *str = "this is client!"; + + api_register_resource_handler("/close", my_close_handler); + + args = attr_container_create(""); + attr_container_set_string(&args, "address", "127.0.0.1"); + attr_container_set_uint16(&args, "port", 7777); + + g_conn = api_open_connection("TCP", args, on_data1, NULL); + if (g_conn == NULL) { + printf("connect to server fail!\n"); + return; + } + + printf("connect to server success! handle: %p\n", g_conn); + + /* set up a timer */ + timer = api_timer_create(1000, true, false, timer1_update); + api_timer_restart(timer, 1000); +} + +void +on_destroy() +{ + /* real destroy work including killing timer and closing sensor is + accomplished in wasm app library version of on_destroy() */ +} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/event_publisher.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/event_publisher.c new file mode 100644 index 00000000..2fa4418c --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/event_publisher.c @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" +#include "wa-inc/request.h" +#include "wa-inc/timer_wasm_app.h" + +int num = 0; + +void +publish_overheat_event() +{ + attr_container_t *event; + + event = attr_container_create("event"); + attr_container_set_string(&event, "warning", "temperature is over high"); + + api_publish_event("alert/overheat", FMT_ATTR_CONTAINER, event, + attr_container_get_serialize_length(event)); + + attr_container_destroy(event); +} + +/* Timer callback */ +void +timer1_update(user_timer_t timer) +{ + publish_overheat_event(); +} + +void +start_timer() +{ + user_timer_t timer; + + /* set up a timer */ + timer = api_timer_create(1000, true, false, timer1_update); + api_timer_restart(timer, 1000); +} + +void +on_init() +{ + start_timer(); +} + +void +on_destroy() +{ + /* real destroy work including killing timer and closing sensor is + accomplished in wasm app library version of on_destroy() */ +} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/event_subscriber.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/event_subscriber.c new file mode 100644 index 00000000..7ebd309e --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/event_subscriber.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" +#include "wa-inc/request.h" + +void +over_heat_event_handler(request_t *request) +{ + printf("### user over heat event handler called\n"); + + if (request->payload != NULL && request->fmt == FMT_ATTR_CONTAINER) + attr_container_dump((attr_container_t *)request->payload); +} + +void +on_init() +{ + api_subscribe_event("alert/overheat", over_heat_event_handler); +} + +void +on_destroy() +{ + /* real destroy work including killing timer and closing sensor is + accomplished in wasm app library version of on_destroy() */ +} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/request_handler.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/request_handler.c new file mode 100644 index 00000000..be6c5603 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/request_handler.c @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" +#include "wa-inc/request.h" + +static void +url1_request_handler(request_t *request) +{ + response_t response[1]; + attr_container_t *payload; + + printf("[resp] ### user resource 1 handler called\n"); + + if (request->payload != NULL && request->fmt == FMT_ATTR_CONTAINER) + attr_container_dump((attr_container_t *)request->payload); + + payload = attr_container_create("wasm app response payload"); + if (payload == NULL) + return; + + attr_container_set_string(&payload, "key1", "value1"); + attr_container_set_string(&payload, "key2", "value2"); + + make_response_for_request(request, response); + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, (void *)payload, + attr_container_get_serialize_length(payload)); + api_response_send(response); + + attr_container_destroy(payload); +} + +static void +url2_request_handler(request_t *request) +{ + response_t response[1]; + make_response_for_request(request, response); + set_response(response, DELETED_2_02, 0, NULL, 0); + api_response_send(response); + + printf("### user resource 2 handler called\n"); +} + +void +on_init() +{ + /* register resource uri */ + api_register_resource_handler("/url1", url1_request_handler); + api_register_resource_handler("/url2", url2_request_handler); +} + +void +on_destroy() +{ + /* real destroy work including killing timer and closing sensor is + accomplished in wasm app library version of on_destroy() */ +} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/request_sender.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/request_sender.c new file mode 100644 index 00000000..823f7f62 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/request_sender.c @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" +#include "wa-inc/request.h" + +static void +my_response_handler(response_t *response, void *user_data) +{ + char *tag = (char *)user_data; + + if (response == NULL) { + printf("[req] request timeout!\n"); + return; + } + + printf("[req] response handler called mid:%d, status:%d, fmt:%d, " + "payload:%p, len:%d, tag:%s\n", + response->mid, response->status, response->fmt, response->payload, + response->payload_len, tag); + + if (response->payload != NULL && response->payload_len > 0 + && response->fmt == FMT_ATTR_CONTAINER) { + printf("[req] dump the response payload:\n"); + attr_container_dump((attr_container_t *)response->payload); + } +} + +static void +test_send_request(char *url, char *tag) +{ + request_t request[1]; + + init_request(request, url, COAP_PUT, 0, NULL, 0); + api_send_request(request, my_response_handler, tag); +} + +void +on_init() +{ + test_send_request("/app/request_handler/url1", "a request to target app"); + test_send_request("url1", "a general request"); +} + +void +on_destroy() +{ + /* real destroy work including killing timer and closing sensor is + accomplished in wasm app library version of on_destroy() */ +} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/sensor.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/sensor.c new file mode 100644 index 00000000..c45ff67d --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/sensor.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" +#include "wa-inc/sensor.h" + +static sensor_t sensor1 = NULL; +static sensor_t sensor2 = NULL; +static char *user_data = NULL; + +/* Sensor event callback*/ +void +sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data) +{ + if (sensor == sensor1) { + printf("### app get sensor event from sensor1\n"); + attr_container_dump(event); + } + else { + printf("### app get sensor event from sensor2\n"); + attr_container_dump(event); + } +} + +void +on_init() +{ + attr_container_t *config; + + printf("### app on_init 1\n"); + /* open a sensor */ + user_data = malloc(100); + if (!user_data) { + printf("allocate memory failed\n"); + return; + } + + printf("### app on_init 2\n"); + sensor1 = sensor_open("sensor_test1", 0, sensor_event_handler, user_data); + if (!sensor1) { + printf("open sensor1 failed\n"); + return; + } + /* config the sensor */ + sensor_config(sensor1, 1000, 0, 0); + + printf("### app on_init 3\n"); + sensor2 = sensor_open("sensor_test2", 0, sensor_event_handler, user_data); + if (!sensor2) { + printf("open sensor2 failed\n"); + return; + } + /* config the sensor */ + sensor_config(sensor2, 5000, 0, 0); + + printf("### app on_init 4\n"); + /* + config = attr_container_create("sensor config"); + sensor_config(sensor, config); + attr_container_destroy(config); + */ +} + +void +on_destroy() +{ + if (NULL != sensor1) { + sensor_config(sensor1, 0, 0, 0); + } + + if (NULL != sensor2) { + sensor_config(sensor2, 0, 0, 0); + } + + if (NULL != user_data) { + free(user_data); + } + + /* real destroy work including killing timer and closing sensor is + accomplished in wasm app library version of on_destroy() */ +} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/timer.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/timer.c new file mode 100644 index 00000000..5bf0822c --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/timer.c @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" +#include "wa-inc/timer_wasm_app.h" + +/* User global variable */ +static int num = 0; + +/* Timer callback */ +void +timer1_update(user_timer_t timer) +{ + printf("Timer update %d\n", num++); +} + +void +on_init() +{ + user_timer_t timer; + + /* set up a timer */ + timer = api_timer_create(1000, true, false, timer1_update); + api_timer_restart(timer, 1000); +} + +void +on_destroy() +{ + /* real destroy work including killing timer and closing sensor is + accomplished in wasm app library version of on_destroy() */ +} |