From ffbdf5f8b3c39680107349fdac87331e2de0541f Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Fri, 20 Jun 2014 13:35:16 +0200 Subject: [PATCH] class WQueue: method 'T remove(const long delay)' added. --- lib/wqueue.h | 33 +++++++++++++++++++++++- src/baseloop.cpp | 65 ++++++++++++++++++++++++++++++------------------ src/ebusloop.h | 2 +- src/network.cpp | 2 +- 4 files changed, 75 insertions(+), 27 deletions(-) diff --git a/lib/wqueue.h b/lib/wqueue.h index 7a5ac932..bdff4bf2 100644 --- a/lib/wqueue.h +++ b/lib/wqueue.h @@ -20,8 +20,11 @@ #ifndef WQUEUE_H_ #define WQUEUE_H_ -#include #include +#include +#include +#include +#include template class WQueue { @@ -64,6 +67,34 @@ public: return item; } + T remove(const long delay) + { + struct timeval tv; + struct timezone tz; + struct timespec timeout; + + int ret = 0; + + pthread_mutex_lock(&m_mutex); + + gettimeofday(&tv, &tz); + timeout.tv_sec = tv.tv_sec + delay; + timeout.tv_nsec = tv.tv_usec * 1000; + + while (m_queue.size() == 0 && ret != ETIMEDOUT) + ret = pthread_cond_timedwait(&m_condv, &m_mutex, &timeout); + + if (ret == ETIMEDOUT) + return NULL; + + T item = m_queue.front(); + m_queue.pop_front(); + + pthread_mutex_unlock(&m_mutex); + + return item; + } + int size() { pthread_mutex_lock(&m_mutex); diff --git a/src/baseloop.cpp b/src/baseloop.cpp index 9fcafb31..3da3ab4a 100644 --- a/src/baseloop.cpp +++ b/src/baseloop.cpp @@ -96,22 +96,27 @@ std::string BaseLoop::decodeMessage(const std::string& data) m_ebusloop->addBusCommand(new BusCommand(type, ebusCommand)); BusCommand* busCommand = m_ebusloop->getBusCommand(); - if (busCommand->getResult().c_str()[0] != '-') { - // decode data - Command* command = new Command(index, (*m_commands)[index], busCommand->getResult()); + if (busCommand != NULL) { + if (busCommand->getResult().c_str()[0] != '-') { + // decode data + Command* command = new Command(index, (*m_commands)[index], busCommand->getResult()); - // return result - result << command->calcResult(cmd); + // return result + result << command->calcResult(cmd); - delete command; + delete command; + } else { + L.log(bas, error, " %s", busCommand->getResult().c_str()); + result << busCommand->getResult(); + } + + + delete busCommand; } else { - L.log(bas, error, " %s", busCommand->getResult().c_str()); - result << busCommand->getResult(); + L.log(bas, error, " busCommand timeout reached"); + result << "busCommand timeout reached"; } - - delete busCommand; - } else { result << "ebus command not found"; } @@ -150,19 +155,25 @@ std::string BaseLoop::decodeMessage(const std::string& data) m_ebusloop->addBusCommand(new BusCommand(type, ebusCommand)); BusCommand* busCommand = m_ebusloop->getBusCommand(); - if (busCommand->getResult().c_str()[0] != '-') { - // decode result - if (busCommand->getResult().substr(busCommand->getResult().length()-8) == "00000000") - result << "done"; - else - result << "error"; + if (busCommand != NULL) { + if (busCommand->getResult().c_str()[0] != '-') { + // decode result + if (busCommand->getResult().substr(busCommand->getResult().length()-8) == "00000000") + result << "done"; + else + result << "error"; + } else { + L.log(bas, error, " %s", busCommand->getResult().c_str()); + result << busCommand->getResult(); + } + + delete busCommand; } else { - L.log(bas, error, " %s", busCommand->getResult().c_str()); - result << busCommand->getResult(); + L.log(bas, error, " busCommand timeout reached"); + result << "busCommand timeout reached"; } - delete busCommand; delete command; } else { @@ -220,12 +231,18 @@ std::string BaseLoop::decodeMessage(const std::string& data) m_ebusloop->addBusCommand(new BusCommand(type, ebusCommand)); BusCommand* busCommand = m_ebusloop->getBusCommand(); - if (busCommand->getResult().c_str()[0] == '-') - L.log(bas, error, " %s", busCommand->getResult().c_str()); + if (busCommand != NULL) { + if (busCommand->getResult().c_str()[0] == '-') + L.log(bas, error, " %s", busCommand->getResult().c_str()); - result << busCommand->getResult(); + result << busCommand->getResult(); + + delete busCommand; + } else { + L.log(bas, error, " busCommand timeout reached"); + result << "busCommand timeout reached"; + } - delete busCommand; } else { result << "specified message type is incorrect"; } diff --git a/src/ebusloop.h b/src/ebusloop.h index 44efc05c..7d2d6a1a 100644 --- a/src/ebusloop.h +++ b/src/ebusloop.h @@ -40,7 +40,7 @@ public: std::string getData() { return m_cycBuffer.remove(); } void addBusCommand(BusCommand* busCommand) { m_sendBuffer.add(busCommand); } - BusCommand* getBusCommand() { return m_recvBuffer.remove(); } + BusCommand* getBusCommand() { return m_recvBuffer.remove(10); } void dump(const bool dumpState) { m_bus->setDumpState(dumpState); } diff --git a/src/network.cpp b/src/network.cpp index 544936aa..425211df 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -122,7 +122,7 @@ Network::Network(const bool localhost) : m_listening(false), m_running(false) else m_Server = new TCPServer(A.getParam("p_port"), "0.0.0.0"); - if (m_Server && m_Server->start() == 0) + if (m_Server != NULL && m_Server->start() == 0) m_listening = true; }