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 00018 #ifndef ASIOLineBasedInterface20100715H 00019 #define ASIOLineBasedInterface20100715H 00020 00021 #include <iostream> 00022 #include <deque> 00023 #include <fstream> 00024 #include <boost/thread.hpp> 00025 #include <boost/bind.hpp> 00026 #include <boost/array.hpp> 00027 #include <boost/asio.hpp> 00028 00029 #include "goby/util/time.h" 00030 #include "goby/protobuf/linebasedcomms.pb.h" 00031 00032 #include <string> 00033 00034 namespace goby 00035 { 00036 namespace util 00037 { 00039 class LineBasedInterface 00040 { 00041 public: 00042 // start the connection 00043 void start(); 00044 // close the connection cleanly 00045 void close(); 00046 // is the connection alive and well? 00047 bool active() { return active_; } 00048 00049 00050 enum AccessOrder { NEWEST_FIRST, OLDEST_FIRST }; 00051 00055 bool readline(std::string* s, AccessOrder order = OLDEST_FIRST) 00056 { 00057 static protobuf::Datagram datagram; 00058 datagram.Clear(); 00059 bool is_data = readline(&datagram, order); 00060 *s = datagram.data(); 00061 return is_data; 00062 } 00063 00064 00065 bool readline(protobuf::Datagram* msg, AccessOrder order = OLDEST_FIRST); 00066 00067 // write a line to the buffer 00068 void write(const std::string& s) 00069 { 00070 static protobuf::Datagram datagram; 00071 datagram.Clear(); 00072 datagram.set_data(s); 00073 write(datagram); 00074 } 00075 00076 00077 void write(const protobuf::Datagram& msg); 00078 00079 // empties the read buffer 00080 void clear(); 00081 00082 void set_delimiter(const std::string& s) { delimiter_ = s; } 00083 std::string delimiter() const { return delimiter_; } 00084 00085 00086 00087 protected: 00088 LineBasedInterface(const std::string& delimiter); 00089 00090 // all implementors of this line based interface must provide do_start, do_write, do_close, and put all read data into "in_" 00091 virtual void do_start() = 0; 00092 virtual void do_write(const protobuf::Datagram& line) = 0; 00093 virtual void do_close(const boost::system::error_code& error) = 0; 00094 00095 void set_active(bool active) { active_ = active; } 00096 00097 static std::string delimiter_; 00098 static boost::asio::io_service io_service_; // the main IO service that runs this connection 00099 static std::deque<protobuf::Datagram> in_; // buffered read data 00100 static boost::mutex in_mutex_; 00101 00102 00103 private: 00104 00105 boost::asio::io_service::work work_; 00106 bool active_; // remains true while this object is still operating 00107 00108 }; 00109 00110 } 00111 } 00112 00113 #endif 00114