Commit 7bc1bb29 authored by Mall, Anon's avatar Mall, Anon
Browse files

coap frontend busy waiting :P

parent 7f1b02de
......@@ -33,16 +33,20 @@ using namespace desk;
map<coap_resource_t*, CoapFrontend*> CoapFrontend::s_callbackMap;
CoapFrontend::CoapFrontend(Controller &controller, uint16_t port) : Frontend(controller),
m_constructFinished(false),
m_controller(controller),
AvahiService("desk", "_coap._udp", port),
thread(&CoapFrontend::run, this),
m_port(port),
m_async(NULL)
{
unique_lock<mutex> lck(m_ctxMutex);
m_ccontext = get_context("::", port);
if (!m_ccontext)
throw CoapException("Context could not be initialized");
init_resources(m_ccontext);
m_constructFinished = true;
m_ctxWait.notify_all();
}
void CoapFrontend::s_get_index(coap_context_t *ctx UNUSED_PARAM,
......@@ -431,18 +435,22 @@ void CoapFrontend::check_async(coap_context_t *ctx,
void CoapFrontend::run()
{
unique_lock<mutex> lck(m_ctxMutex);
while(!m_constructFinished){
m_ctxWait.wait(lck);
}
cout << "Starting CoapFrontend thread" << endl;
coap_queue_t *nextpdu;
coap_tick_t now;
struct timeval tv, *timeout;
int result;
fd_set readfds;
struct coap_resource_t *time_resource = NULL;
sleep(1);
while (true)
{
FD_ZERO(&m_readfds);
FD_SET( m_ccontext->sockfd, &m_readfds );
FD_ZERO(&readfds);
FD_SET( m_ccontext->sockfd, &readfds );
nextpdu = coap_peek_next( m_ccontext );
coap_ticks(&now);
......@@ -461,13 +469,13 @@ void CoapFrontend::run()
tv.tv_sec = COAP_RESOURCE_CHECK_TIME;
timeout = &tv;
}
result = select( FD_SETSIZE, &m_readfds, 0, 0, timeout );
result = select( FD_SETSIZE, &readfds, 0, 0, timeout );
if ( result < 0 ) { /* error */
if (errno != EINTR)
perror("select");
} else if ( result > 0 ) { /* read from socket */
if ( FD_ISSET( m_ccontext->sockfd, &m_readfds ) ) {
if ( FD_ISSET( m_ccontext->sockfd, &readfds ) ) {
coap_read( m_ccontext ); /* read received data */
/* coap_dispatch( ctx ); /\* and dispatch PDUs from receivequeue *\/ */
}
......
......@@ -18,6 +18,8 @@
#include <exception>
#include <map>
#include <utility>
#include <condition_variable>
#include <mutex>
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
......@@ -146,8 +148,9 @@ protected:
coap_async_state_t* m_async;
coap_resource_t* m_res_height;
coap_resource_t* m_res_offset;
fd_set m_readfds;
std::condition_variable m_ctxWait;
std::mutex m_ctxMutex;
bool m_constructFinished;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment