Goby v2
interface.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef ASIOLineBasedInterface20100715H
24 #define ASIOLineBasedInterface20100715H
25 
26 #include <boost/array.hpp>
27 #include <boost/asio.hpp>
28 #include <boost/bind.hpp>
29 #include <boost/thread.hpp>
30 #include <deque>
31 #include <fstream>
32 #include <iostream>
33 
34 #include "goby/common/time.h"
35 #include "goby/util/protobuf/linebasedcomms.pb.h"
36 
37 #include <string>
38 
39 namespace goby
40 {
41 namespace util
42 {
45 {
46  public:
47  LineBasedInterface(const std::string& delimiter);
48  virtual ~LineBasedInterface() {}
49 
50  // start the connection
51  void start();
52  // close the connection cleanly
53  void close();
54  // is the connection alive and well?
55  bool active() { return active_; }
56 
57  void sleep(int sec);
58 
59  enum AccessOrder
60  {
61  NEWEST_FIRST,
62  OLDEST_FIRST
63  };
64 
68  bool readline(std::string* s, AccessOrder order = OLDEST_FIRST);
69  bool readline(protobuf::Datagram* msg, AccessOrder order = OLDEST_FIRST);
70 
71  // write a line to the buffer
72  void write(const std::string& s)
73  {
74  protobuf::Datagram datagram;
75  datagram.set_data(s);
76  write(datagram);
77  }
78 
79  void write(const protobuf::Datagram& msg);
80 
81  // empties the read buffer
82  void clear();
83 
84  void set_delimiter(const std::string& s) { delimiter_ = s; }
85  std::string delimiter() const { return delimiter_; }
86 
87  boost::asio::io_service& io_service() { return io_service_; }
88 
89  protected:
90  // all implementors of this line based interface must provide do_start, do_write, do_close, and put all read data into "in_"
91  virtual void do_start() = 0;
92  virtual void do_write(const protobuf::Datagram& line) = 0;
93  virtual void do_close(const boost::system::error_code& error) = 0;
94 
95  void set_active(bool active) { active_ = active; }
96 
97  std::string delimiter_;
98  boost::asio::io_service io_service_; // the main IO service that runs this connection
99  std::deque<protobuf::Datagram> in_; // buffered read data
100  boost::mutex in_mutex_;
101 
102  template <typename ASIOAsyncReadStream> friend class LineBasedConnection;
103 
104  std::string& delimiter() { return delimiter_; }
105  std::deque<goby::util::protobuf::Datagram>& in() { return in_; }
106  boost::mutex& in_mutex() { return in_mutex_; }
107 
108  private:
109  class IOLauncher
110  {
111  public:
112  IOLauncher(boost::asio::io_service& io_service)
113  : io_service_(io_service), t_(boost::bind(&boost::asio::io_service::run, &io_service))
114  {
115  }
116 
117  ~IOLauncher()
118  {
119  io_service_.stop();
120  t_.join();
121  }
122 
123  private:
124  boost::asio::io_service& io_service_;
125  boost::thread t_;
126  };
127 
128  boost::shared_ptr<IOLauncher> io_launcher_;
129 
130  boost::asio::io_service::work work_;
131  bool active_; // remains true while this object is still operating
132 };
133 
134 } // namespace util
135 } // namespace goby
136 
137 #endif
bool readline(std::string *s, AccessOrder order=OLDEST_FIRST)
returns string line (including delimiter)
Definition: interface.cpp:80
The global namespace for the Goby project.
basic interface class for all the derived serial (and networking mimics) line-based nodes (serial...
Definition: interface.h:44