Goby v2
interface.cpp
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 #include "interface.h"
24 #include "goby/common/exception.h"
25 #include "goby/common/logger.h"
26 
27 goby::util::LineBasedInterface::LineBasedInterface(const std::string& delimiter)
28  : work_(io_service_), active_(false)
29 {
30  goby::glog.set_lock_action(goby::common::logger_lock::lock);
31 
32  if (delimiter.empty())
33  throw Exception("Line based comms started with null string as delimiter!");
34 
35  delimiter_ = delimiter;
36  io_launcher_.reset(new IOLauncher(io_service_));
37 }
38 
39 void goby::util::LineBasedInterface::start()
40 {
41  if (active_)
42  return;
43 
44  // active_ = true;
45  io_service_.post(boost::bind(&LineBasedInterface::do_start, this));
46 }
47 
48 void goby::util::LineBasedInterface::clear()
49 {
50  boost::mutex::scoped_lock lock(in_mutex_);
51  in_.clear();
52 }
53 
54 bool goby::util::LineBasedInterface::readline(protobuf::Datagram* msg,
55  AccessOrder order /* = OLDEST_FIRST */)
56 {
57  if (in_.empty())
58  {
59  return false;
60  }
61  else
62  {
63  boost::mutex::scoped_lock lock(in_mutex_);
64  switch (order)
65  {
66  case NEWEST_FIRST:
67  msg->CopyFrom(in_.back());
68  in_.pop_back();
69  break;
70 
71  case OLDEST_FIRST:
72  msg->CopyFrom(in_.front());
73  in_.pop_front();
74  break;
75  }
76  return true;
77  }
78 }
79 
81  AccessOrder order /* = OLDEST_FIRST */)
82 {
83  if (in_.empty())
84  {
85  return false;
86  }
87  else
88  {
89  boost::mutex::scoped_lock lock(in_mutex_);
90  switch (order)
91  {
92  case NEWEST_FIRST:
93  (*s) = in_.back().data();
94  in_.pop_back();
95  break;
96 
97  case OLDEST_FIRST:
98  (*s) = in_.front().data();
99  in_.pop_front();
100  break;
101  }
102  return true;
103  }
104 }
105 
106 // pass the write data via the io service in the other thread
107 void goby::util::LineBasedInterface::write(const protobuf::Datagram& msg)
108 {
109  io_service_.post(boost::bind(&LineBasedInterface::do_write, this, msg));
110 }
111 
112 // call the do_close function via the io service in the other thread
113 void goby::util::LineBasedInterface::close()
114 {
115  io_service_.post(boost::bind(&LineBasedInterface::do_close, this, boost::system::error_code()));
116 }
117 
118 void goby::util::LineBasedInterface::sleep(int sec) { io_service_.post(boost::bind(::sleep, sec)); }
bool readline(std::string *s, AccessOrder order=OLDEST_FIRST)
returns string line (including delimiter)
Definition: interface.cpp:80
common::FlexOstream glog
Access the Goby logger through this object.