Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding non-blocking function to ndn_app API #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 84 additions & 57 deletions app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down