From f610fb79ff07e7d9c88530e294ccfccbc199354f Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 24 Jan 2012 23:05:28 -0600 Subject: [PATCH] fixing empty token bug, and adding some debugging stuff. --- include/serial/serial_listener.h | 59 +++++++++++++++++++++++++++++++- src/serial_listener.cc | 17 +++------ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index c62910f..022623b 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -36,9 +36,14 @@ #ifndef SERIAL_LISTENER_H #define SERIAL_LISTENER_H +#ifndef SERIAL_LISTENER_DEBUG +#define SERIAL_LISTENER_DEBUG 0 +#endif + // STL #include #include +#include // Serial #include @@ -49,6 +54,10 @@ #include #include +#if SERIAL_LISTENER_DEBUG +# warning SerialListener in debug mode +#endif + namespace serial { /*! @@ -643,16 +652,46 @@ private: // exact comparator function static bool _exactly (const std::string& token, std::string exact_str) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In exactly callback(" << token.length() << "): "; + std::cerr << token << " == " << exact_str << ": "; + if (token == exact_str) + std::cerr << "True"; + else + std::cerr << "False"; + std::cerr << std::endl; +#endif return token == exact_str; } // startswith comparator function static bool _startsWith (const std::string& token, std::string prefix) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In startsWith callback(" << token.length() << "): "; + std::cerr << token << " starts with " << prefix; + std::cerr << "?: "; + if (token.substr(0,prefix.length()) == prefix) + std::cerr << "True"; + else + std::cerr << "False"; + std::cerr << std::endl; +#endif return token.substr(0,prefix.length()) == prefix; } // endswith comparator function static bool _endsWith (const std::string& token, std::string postfix) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In endsWith callback("; + std::cerr << token.length(); + std::cerr << "): " << token; + std::cerr << " ends with " << postfix << "?: "; + if (token.substr(token.length()-postfix.length()) == postfix) + std::cerr << "True"; + else + std::cerr << "False"; + std::cerr << std::endl; +#endif return token.substr(token.length()-postfix.length()) == postfix; } // contains comparator function @@ -662,7 +701,17 @@ private: } // Gets some data from the serial port - void readSomeData (std::string&, size_t); + void readSomeData (std::string &temp, size_t this_many) { + // Make sure there is a serial port + if (this->serial_port_ == NULL) { + this->handle_exc(SerialListenerException("Invalid serial port.")); + } + // Make sure the serial port is open + if (!this->serial_port_->isOpen()) { + this->handle_exc(SerialListenerException("Serial port not open.")); + } + temp = this->serial_port_->read(this_many); + } // Runs the new_tokens through all the filters void filter (std::vector &tokens); // Function that loops while listening is true @@ -750,6 +799,10 @@ public: FilterPtr filter_ptr; void callback(const std::string& token) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In BlockingFilter callback(" << token.length() << "): "; + std::cerr << token << std::endl; +#endif this->cond.notify_all(); this->result = token; } @@ -844,6 +897,10 @@ public: FilterPtr filter_ptr; void callback(const std::string &token) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In BufferedFilter callback(" << token.length() << "): "; + std::cerr << token << std::endl; +#endif std::string throw_away; if (this->queue.size() == this->buffer_size_) { this->queue.wait_and_pop(throw_away); diff --git a/src/serial_listener.cc b/src/serial_listener.cc index fc6c298..bc1258d 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -105,19 +105,6 @@ SerialListener::determineAmountToRead() { return this->chunk_size_; } -void -SerialListener::readSomeData(std::string &temp, size_t this_many) { - // Make sure there is a serial port - if (this->serial_port_ == NULL) { - this->handle_exc(SerialListenerException("Invalid serial port.")); - } - // Make sure the serial port is open - if (!this->serial_port_->isOpen()) { - this->handle_exc(SerialListenerException("Serial port not open.")); - } - temp = this->serial_port_->read(this_many); -} - void SerialListener::filter(std::vector &tokens) { // Lock the filters while filtering @@ -126,6 +113,10 @@ SerialListener::filter(std::vector &tokens) { std::vector::iterator it; for (it=tokens.begin(); it!=tokens.end(); it++) { TokenPtr token = (*it); + // If it is empty then pass it + if (token->empty()) { + continue; + } bool matched = false; // Iterate through each filter std::vector::iterator itt;