Add a new extension library: connection (#39)

This commit is contained in:
wenyongh
2019-06-10 21:52:15 -05:00
committed by GitHub
parent 69d62f573a
commit 7f1e024fce
38 changed files with 1677 additions and 70 deletions

View File

@ -57,6 +57,8 @@ include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake)
include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake)
include (${WASM_DIR}/lib/native/libc/wasm_libc.cmake)
include (${WASM_DIR}/lib/native/extension/sensor/wasm_lib_sensor.cmake)
include (${WASM_DIR}/lib/native/extension/connection/wasm_lib_conn.cmake)
include (${WASM_DIR}/lib/native/extension/connection/${TARGET_PLATFORM}/connection_mgr.cmake)
include (${WASM_DIR}/lib/native-interface/native_interface.cmake)
include (${APP_MGR_DIR}/app-manager/app_mgr.cmake)
include (${APP_MGR_DIR}/app-mgr-shared/app_mgr_shared.cmake)
@ -83,6 +85,8 @@ add_library (vmlib
${WASM_LIB_BASE_SOURCE}
${WASM_LIB_EXT_SOURCE}
${WASM_LIB_SENSOR_SOURCE}
${WASM_LIB_CONN_SOURCE}
${WASM_LIB_CONN_MGR_SOURCE}
${PLATFORM_SHARED_SOURCE}
${UTILS_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}

View File

@ -16,18 +16,13 @@ simple/
│   ├── iwasm_main.c
│   └── main.c
└── wasm-apps
├── event_publisher
│   └── event_publisher.c
├── event_subscriber
│   └── event_subscriber.c
├── request_handler
│   └── request_handler.c
── request_sender
│   └── request_sender.c
├── sensor
│   └── sensor.c
└── timer
   └── timer.c
├── connection.c
── event_publisher.c
├── event_subscriber.c
├── request_handler.c
├── request_sender.c
├── sensor.c
── timer.c
```
- build.sh<br/>
@ -75,18 +70,19 @@ Execute the build.sh script then all binaries including wasm application files w
Out directory structure
------------------------------
```
```
out/
├── host_tool
├── simple
└── wasm-apps
├── connection.wasm
├── event_publisher.wasm
├── event_subscriber.wasm
├── request_handler.wasm
├── request_sender.wasm
├── sensor.wasm
└── timer.wasm
```
```
- host_tool:
A small testing tool to interact with WAMR. See the usage of this tool by executing "./host_tool -h".
@ -100,6 +96,8 @@ out/
- wasm-apps:
Sample wasm applications that demonstrate all APIs of the WAMR programming model. The source codes are in the wasm-apps directory under the root of this project.
+ connection.wasm<br/>
This application shows the connection programming model. It connects to a TCP server on 127.0.0.1:7777 and periodically sends message to it.
+ event_publisher.wasm<br/>
This application shows the sub/pub programming model. The pub application publishes the event "alert/overheat" by calling api_publish_event() API. The subscriber could be host_tool or other wasm application.
+ event_subscriber.wasm<br/>

View File

@ -8,7 +8,7 @@ BUILD_DIR=${PWD}/build
IWASM_ROOT=${PWD}/../../core/iwasm
APP_LIBS=${IWASM_ROOT}/lib/app-libs
NATIVE_LIBS=${IWASM_ROOT}/lib/native-interface
APP_LIB_SRC="${APP_LIBS}/base/*.c ${APP_LIBS}/extension/sensor/*.c ${NATIVE_LIBS}/*.c"
APP_LIB_SRC="${APP_LIBS}/base/*.c ${APP_LIBS}/extension/sensor/*.c ${APP_LIBS}/extension/connection/*.c ${NATIVE_LIBS}/*.c"
WASM_APPS=${PWD}/wasm-apps
rm -rf ${OUT_DIR}
@ -49,54 +49,23 @@ echo "#####################build host-tool success"
echo "#####################build wasm apps"
cd ${CURR_DIR}
cd ${WASM_APPS}
APP_SRC="${WASM_APPS}/timer/timer.c ${APP_LIB_SRC}"
for i in `ls *.c`
do
APP_SRC="$i ${APP_LIB_SRC}"
OUT_FILE=${i%.*}.wasm
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-I${APP_LIBS}/extension/connection \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/timer.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/request_handler/request_handler.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/request_handler.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/request_sender/request_sender.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/request_sender.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/event_publisher/event_publisher.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/event_publisher.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/event_subscriber/event_subscriber.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/event_subscriber.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/sensor/sensor.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/sensor.wasm ${APP_SRC}
echo "#####################build wasm apps success"
'_on_sensor_event', '_on_timer_callback', '_on_connection_data']" \
-o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
echo "build ${OUT_FILE} success"
else
echo "build ${OUT_FILE} fail"
fi
done
echo "#####################build wasm apps done"

View File

@ -1,8 +1,10 @@
#include "lib_export.h"
#include "sensor_api.h"
#include "connection_api.h"
static NativeSymbol extended_native_symbol_defs[] = {
#include "runtime_sensor.inl"
#include "connection.inl"
};
#include "ext_lib_export.h"

View File

@ -46,6 +46,7 @@ static int baudrate = B115200;
extern void * thread_timer_check(void *);
extern void init_sensor_framework();
extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
extern bool init_connection_framework();
#ifndef CONNECTION_UART
int listenfd = -1;
@ -213,7 +214,7 @@ void* func_server_mode(void* arg)
sockfd = -1;
pthread_mutex_unlock(&sock_lock);
sleep(2);
sleep(1);
break;
}
@ -442,6 +443,11 @@ int iwasm_main(int argc, char *argv[])
goto fail1;
}
if (!init_connection_framework()) {
vm_thread_sys_destroy();
goto fail1;
}
init_sensor_framework();
// timer manager

View File

@ -0,0 +1,93 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_app.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() */
}