diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index bc53244..37fb8ef 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -159,11 +159,11 @@ class Filter { public: Filter (ComparatorType comparator, DataCallback callback) - : comparator(comparator), callback(callback) {} + : comparator_(comparator), callback_(callback) {} virtual ~Filter () {} - ComparatorType comparator; - DataCallback callback; + ComparatorType comparator_; + DataCallback callback_; }; /*! @@ -206,14 +206,14 @@ typedef boost::shared_ptr BufferedFilterPtr; * \param e_what is a std::string that describes the cause of the error. */ class SerialListenerException : public std::exception { - const std::string e_what; + const std::string e_what_; public: - SerialListenerException(const std::string e_what) : e_what(e_what) {} + SerialListenerException(const std::string e_what) : e_what_(e_what) {} ~SerialListenerException() throw() {std::exception::~exception();} virtual const char* what() const throw() { std::stringstream ss; - ss << "SerialListenerException: " << this->e_what; + ss << "SerialListenerException: " << this->e_what_; return ss.str().c_str(); } }; @@ -250,7 +250,7 @@ public: return true; } - bool timed_wait_and_pop(Data& popped_value, size_t timeout) { + bool timed_wait_and_pop(Data& popped_value, long timeout) { using namespace boost::posix_time; bool result; boost::mutex::scoped_lock lock(the_mutex); @@ -334,7 +334,7 @@ public: */ void setChunkSize (size_t chunk_size) { - this->chunk_size = chunk_size; + this->chunk_size_ = chunk_size; } /***** Start and Stop Listening ******/ @@ -585,10 +585,11 @@ public: /*! * Sleeps for a given number of milliseconds. * - * \param ms number of milliseconds to sleep. + * \param milliseconds number of milliseconds to sleep. */ static void - sleep (size_t ms) { + sleep (long milliseconds) { + boost::int64_t ms(milliseconds); boost::this_thread::sleep(boost::posix_time::milliseconds(ms)); } @@ -787,10 +788,11 @@ private: // Persistent listening variables bool listening; - serial::Serial * serial_port; + char serial_port_padding[7]; + serial::Serial * serial_port_; boost::thread listen_thread; std::string data_buffer; - size_t chunk_size; + size_t chunk_size_; // Callback related variables // filter id, token @@ -821,13 +823,13 @@ class BlockingFilter { public: BlockingFilter (ComparatorType comparator, SerialListener &listener) { - this->listener = &listener; + this->listener_ = &listener; DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1); - this->filter_ptr = this->listener->createFilter(comparator, cb); + this->filter_ptr = this->listener_->createFilter(comparator, cb); } virtual ~BlockingFilter () { - this->listener->removeFilter(filter_ptr); + this->listener_->removeFilter(filter_ptr); this->result = ""; this->cond.notify_all(); } @@ -840,7 +842,7 @@ public: * * \return std::string token that was matched or "" if none were matched. */ - std::string wait(size_t ms) { + std::string wait(long ms) { this->result = ""; boost::unique_lock lock(this->mutex); this->cond.timed_wait(lock, boost::posix_time::milliseconds(ms)); @@ -855,7 +857,7 @@ public: } private: - SerialListener * listener; + SerialListener * listener_; boost::condition_variable cond; boost::mutex mutex; std::string result; @@ -883,15 +885,15 @@ class BufferedFilter public: BufferedFilter (ComparatorType comparator, size_t buffer_size, SerialListener &listener) - : buffer_size(buffer_size) + : buffer_size_(buffer_size) { - this->listener = &listener; + this->listener_ = &listener; DataCallback cb = boost::bind(&BufferedFilter::callback, this, _1); - this->filter_ptr = this->listener->createFilter(comparator, cb); + this->filter_ptr = this->listener_->createFilter(comparator, cb); } virtual ~BufferedFilter () { - this->listener->removeFilter(filter_ptr); + this->listener_->removeFilter(filter_ptr); this->queue.clear(); this->result = ""; } @@ -907,7 +909,7 @@ public: * * \return std::string token that was matched or "" if none were matched. */ - std::string wait(size_t ms) { + std::string wait(long ms) { if (ms == 0) if (!this->queue.try_pop(this->result)) this->result = ""; @@ -935,21 +937,21 @@ public: * Returns the capacity of the buffer. */ size_t capacity() { - return buffer_size; + return buffer_size_; } FilterPtr filter_ptr; void callback(const std::string &token) { std::string throw_away; - if (this->queue.size() == buffer_size) + if (this->queue.size() == this->buffer_size_) this->queue.wait_and_pop(throw_away); this->queue.push(token); } private: - size_t buffer_size; - SerialListener * listener; + size_t buffer_size_; + SerialListener * listener_; ConcurrentQueue queue; std::string result; diff --git a/src/serial_listener.cc b/src/serial_listener.cc index 6da8d76..85055db 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -17,11 +17,10 @@ inline void defaultInfoCallback(const std::string& msg) { inline void defaultExceptionCallback(const std::exception &error) { std::cerr << "SerialListener Unhandled Exception: " << error.what(); std::cerr << std::endl; - throw(error); } inline bool defaultComparator(const std::string &token) { - return true; + return token == token; } using namespace serial; @@ -34,7 +33,7 @@ SerialListener::default_handler(const std::string &token) { this->_default_handler(token); } -SerialListener::SerialListener() : listening(false), chunk_size(5) { +SerialListener::SerialListener() : listening(false), chunk_size_(5) { // Set default callbacks this->handle_exc = defaultExceptionCallback; this->info = defaultInfoCallback; @@ -68,7 +67,7 @@ SerialListener::callback() { std::cout << (*pair.second) << std::endl; if (this->listening) { try { - pair.first->callback((*pair.second)); + pair.first->callback_((*pair.second)); } catch (std::exception &e) { this->handle_exc(e); }// try callback @@ -88,8 +87,8 @@ SerialListener::startListening(Serial &serial_port) { } this->listening = true; - this->serial_port = &serial_port; - if (!this->serial_port->isOpen()) { + this->serial_port_ = &serial_port; + if (!this->serial_port_->isOpen()) { throw(SerialListenerException("Serial port not open.")); return; } @@ -110,7 +109,7 @@ SerialListener::stopListening() { callback_thread.join(); this->data_buffer = ""; - this->serial_port = NULL; + this->serial_port_ = NULL; // Delete all the filters this->removeAllFilters(); @@ -121,20 +120,20 @@ SerialListener::determineAmountToRead() { // TODO: Make a more intelligent method based on the length of the things // filters are looking for. e.g.: if the filter is looking for 'V=XX\r' // make the read amount at least 5. - return this->chunk_size; + 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) { + if (this->serial_port_ == NULL) { this->handle_exc(SerialListenerException("Invalid serial port.")); } // Make sure the serial port is open - if (!this->serial_port->isOpen()) { + if (!this->serial_port_->isOpen()) { this->handle_exc(SerialListenerException("Serial port not open.")); } - temp = this->serial_port->read(this_many); + temp = this->serial_port_->read(this_many); } void @@ -159,7 +158,7 @@ SerialListener::filter (FilterPtr filter, std::vector &tokens) if (it == tokens.end()-1) continue; TokenPtr token = (*it); - if (filter->comparator((*token))) + if (filter->comparator_((*token))) callback_queue.push(std::make_pair(filter,token)); } }