Goby Underwater Autonomy Project
Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
|
00001 // copyright 2010 t. schneider tes@mit.edu 00002 // 00003 // 00004 // This program is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This software is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this software. If not, see <http://www.gnu.org/licenses/>. 00016 00017 #include "interface.h" 00018 #include "goby/core/libcore/exception.h" 00019 00020 std::string goby::util::LineBasedInterface::delimiter_; 00021 boost::asio::io_service goby::util::LineBasedInterface::io_service_; 00022 std::deque<goby::util::protobuf::Datagram> goby::util::LineBasedInterface::in_; 00023 boost::mutex goby::util::LineBasedInterface::in_mutex_; 00024 00025 00026 00027 00028 goby::util::LineBasedInterface::LineBasedInterface(const std::string& delimiter) 00029 : work_(io_service_), 00030 active_(false) 00031 { 00032 if(delimiter.empty()) 00033 throw Exception("Line based comms started with null string as delimiter!"); 00034 00035 delimiter_ = delimiter; 00036 boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service_)); 00037 } 00038 00039 00040 void goby::util::LineBasedInterface::start() 00041 { 00042 if(active_) return; 00043 00044 active_ = true; 00045 io_service_.post(boost::bind(&LineBasedInterface::do_start, this)); 00046 } 00047 00048 void goby::util::LineBasedInterface::clear() 00049 { 00050 boost::mutex::scoped_lock lock(in_mutex_); 00051 in_.clear(); 00052 } 00053 00054 bool goby::util::LineBasedInterface::readline(protobuf::Datagram* msg, AccessOrder order /* = OLDEST_FIRST */) 00055 { 00056 if(in_.empty()) 00057 { 00058 return false; 00059 } 00060 else 00061 { 00062 boost::mutex::scoped_lock lock(in_mutex_); 00063 switch(order) 00064 { 00065 case NEWEST_FIRST: 00066 msg->CopyFrom(in_.back()); 00067 in_.pop_back(); 00068 break; 00069 00070 case OLDEST_FIRST: 00071 msg->CopyFrom(in_.front()); 00072 in_.pop_front(); 00073 break; 00074 } 00075 return true; 00076 } 00077 } 00078 00079 00080 // pass the write data via the io service in the other thread 00081 void goby::util::LineBasedInterface::write(const protobuf::Datagram& msg) 00082 { io_service_.post(boost::bind(&LineBasedInterface::do_write, this, msg)); } 00083 00084 // call the do_close function via the io service in the other thread 00085 void goby::util::LineBasedInterface::close() 00086 { io_service_.post(boost::bind(&LineBasedInterface::do_close, this, boost::system::error_code())); }