From d446d3739b75ec211df2fb7a52763679a03cb098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dauphin?= Date: Thu, 15 Jun 2017 19:18:25 +0200 Subject: [PATCH] added ndn_app_run_once --- app.c | 141 ++++++++++++++++++++++++++++++++++------------------------ app.h | 13 ++++++ 2 files changed, 97 insertions(+), 57 deletions(-) diff --git a/app.c b/app.c index 8d37bd9..ac862ea 100644 --- a/app.c +++ b/app.c @@ -205,91 +205,118 @@ static int _sched_call_cb(ndn_app_t* handle, msg_t* msg) return r; } -int ndn_app_run(ndn_app_t* handle) +static int _process_msg(ndn_app_t* handle, msg_t* msg) { - if (handle == NULL) return NDN_APP_ERROR; - int ret = NDN_APP_CONTINUE; ndn_shared_block_t* ptr; - msg_t msg, reply; + msg_t reply; reply.type = GNRC_NETAPI_MSG_TYPE_ACK; reply.content.value = (uint32_t)(-ENOTSUP); - while (1) { - msg_receive(&msg); + switch (msg->type) { + case NDN_APP_MSG_TYPE_TERMINATE: + DEBUG("ndn_app: TERMINATE msg received from thread %" + PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", + msg->sender_pid, handle->id); + return NDN_APP_STOP; + + case MSG_XTIMER: + DEBUG("ndn_app: XTIMER msg received from thread %" + PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", + msg->sender_pid, handle->id); + + ret = _sched_call_cb(handle, (msg_t*)msg->content.ptr); + + break; + + case NDN_APP_MSG_TYPE_TIMEOUT: + DEBUG("ndn_app: TIMEOUT msg received from thread %" + PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", + msg->sender_pid, handle->id); + ptr = (ndn_shared_block_t*)msg->content.ptr; - switch (msg.type) { - case NDN_APP_MSG_TYPE_TERMINATE: - DEBUG("ndn_app: TERMINATE msg received from thread %" - PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", - msg.sender_pid, handle->id); - return NDN_APP_STOP; + ret = _notify_consumer_timeout(handle, &ptr->block); - case MSG_XTIMER: - DEBUG("ndn_app: XTIMER msg received from thread %" - PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", - msg.sender_pid, handle->id); + ndn_shared_block_release(ptr); - ret = _sched_call_cb(handle, (msg_t*)msg.content.ptr); + break; - break; + case NDN_APP_MSG_TYPE_INTEREST: + DEBUG("ndn_app: INTEREST msg received from thread %" + PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", + msg->sender_pid, handle->id); + ptr = (ndn_shared_block_t*)msg->content.ptr; - case NDN_APP_MSG_TYPE_TIMEOUT: - DEBUG("ndn_app: TIMEOUT msg received from thread %" - PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", - msg.sender_pid, handle->id); - ptr = (ndn_shared_block_t*)msg.content.ptr; + ret = _notify_producer_interest(handle, &ptr->block); - ret = _notify_consumer_timeout(handle, &ptr->block); + ndn_shared_block_release(ptr); - ndn_shared_block_release(ptr); + break; - break; + case NDN_APP_MSG_TYPE_DATA: + DEBUG("ndn_app: DATA msg received from thread %" + PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", + msg->sender_pid, handle->id); + ptr = (ndn_shared_block_t*)msg->content.ptr; - case NDN_APP_MSG_TYPE_INTEREST: - DEBUG("ndn_app: INTEREST msg received from thread %" - PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", - msg.sender_pid, handle->id); - ptr = (ndn_shared_block_t*)msg.content.ptr; + ret = _notify_consumer_data(handle, &ptr->block); - ret = _notify_producer_interest(handle, &ptr->block); + ndn_shared_block_release(ptr); - ndn_shared_block_release(ptr); + break; - break; + case GNRC_NETAPI_MSG_TYPE_GET: + case GNRC_NETAPI_MSG_TYPE_SET: + msg_reply(msg, &reply); + break; + default: + DEBUG("ndn_app: unknown msg type %u (pid=%" PRIkernel_pid ")\n", + msg->type, handle->id); + break; + } - case NDN_APP_MSG_TYPE_DATA: - DEBUG("ndn_app: DATA msg received from thread %" - PRIkernel_pid " (pid=%" PRIkernel_pid ")\n", - msg.sender_pid, handle->id); - ptr = (ndn_shared_block_t*)msg.content.ptr; + if (ret != NDN_APP_CONTINUE) { + DEBUG("ndn_app: stop app because callback returned" + " %s (pid=%" PRIkernel_pid ")\n", + ret == NDN_APP_STOP ? "STOP" : "ERROR", + handle->id); + return ret; + } - ret = _notify_consumer_data(handle, &ptr->block); + return ret; +} - ndn_shared_block_release(ptr); +int ndn_app_run(ndn_app_t* handle) +{ + if (handle == NULL) return NDN_APP_ERROR; - break; + msg_t msg; - case GNRC_NETAPI_MSG_TYPE_GET: - case GNRC_NETAPI_MSG_TYPE_SET: - msg_reply(&msg, &reply); - break; - default: - DEBUG("ndn_app: unknown msg type %u (pid=%" PRIkernel_pid ")\n", - msg.type, handle->id); - break; + while (1) { + msg_receive(&msg); + int rc = _process_msg(handle, &msg); + if (rc != NDN_APP_CONTINUE) { + return rc; } + } + + return NDN_APP_STOP; +} + +int ndn_app_run_once(ndn_app_t* handle) +{ + if (handle == NULL) return NDN_APP_ERROR; - if (ret != NDN_APP_CONTINUE) { - DEBUG("ndn_app: stop app because callback returned" - " %s (pid=%" PRIkernel_pid ")\n", - ret == NDN_APP_STOP ? "STOP" : "ERROR", - handle->id); - return ret; + msg_t msg; + + while (msg_try_receive(&msg) == 1) { + int rc = _process_msg(handle, &msg); + if (rc != NDN_APP_CONTINUE) { + return rc; } } - return ret; + return NDN_APP_STOP; } static inline void _release_sched_cb_table(ndn_app_t* handle) diff --git a/app.h b/app.h index 6428921..8b37acf 100644 --- a/app.h +++ b/app.h @@ -139,6 +139,19 @@ ndn_app_t* ndn_app_create(void); */ int ndn_app_run(ndn_app_t* handle); +/** + * @brief Runs the event loop (non blocking) with the app handle. + * + * @details This function is reentrant and can be called from multiple threads. + * However, the same handle cannot be used twice by this function at + * the same time. + * + * @param[in] handle Handle of the app to run. + * + * @return One of the return codes for the callbacks. + */ +int ndn_app_run_once(ndn_app_t* handle); + /** * @brief Releases the app handle and all associated memory. */