Goby v2
moos_node.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 "moos_node.h"
24 
25 #include "goby/common/logger.h"
26 
27 #include "goby/util/binary.h"
28 
29 using goby::glog;
31 using namespace goby::common::logger_lock;
32 using namespace goby::common::logger;
33 
34 goby::moos::MOOSNode::MOOSNode(ZeroMQService* service)
35  : goby::common::NodeInterface<CMOOSMsg>(service)
36 {
37  glog.add_group("in_hex", common::Colors::green, "Goby MOOS (hex) - Incoming");
38  glog.add_group("out_hex", common::Colors::magenta, "Goby MOOS (hex) - Outgoing");
39 }
40 
41 void goby::moos::MOOSNode::inbox(common::MarshallingScheme marshalling_scheme,
42  const std::string& identifier, const std::string& bytes,
43  int socket_id)
44 {
45  glog.is(DEBUG2) && glog << group("in_hex")
46  << "Received marshalling scheme: " << marshalling_scheme << std::endl;
47 
48  if (marshalling_scheme == goby::common::MARSHALLING_MOOS)
49  {
50  boost::shared_ptr<CMOOSMsg> msg(new CMOOSMsg);
51 
52  glog.is(DEBUG2) && glog << group("in_hex") << goby::util::hex_encode(bytes) << std::endl;
53  MOOSSerializer::parse(msg.get(), bytes);
54 
55  const std::string& key = msg->GetKey();
56  newest_vars[key] = msg;
57  moos_inbox(*msg);
58  }
59 }
60 
61 void goby::moos::MOOSNode::send(const CMOOSMsg& msg, int socket_id, const std::string& group_unused)
62 {
63  std::string bytes;
64  MOOSSerializer::serialize(msg, &bytes);
65 
66  glog.is(DEBUG1) && glog << "Sent: "
67  << "CMOOSMsg/" << msg.GetKey() << "/" << std::endl;
68 
69  glog.is(DEBUG2) && glog << group("out_hex") << goby::util::hex_encode(bytes) << std::endl;
70 
71  zeromq_service()->send(goby::common::MARSHALLING_MOOS, "CMOOSMsg/" + msg.GetKey() + "/", bytes,
72  socket_id);
73 }
74 
75 void goby::moos::MOOSNode::subscribe(const std::string& full_or_partial_moos_name, int socket_id)
76 {
77  std::string trimmed_name = boost::trim_copy(full_or_partial_moos_name);
78  unsigned size = trimmed_name.size();
79  if (!size)
80  {
81  zeromq_service()->subscribe(goby::common::MARSHALLING_MOOS, "CMOOSMsg/", socket_id);
82  }
83  else if (trimmed_name[size - 1] == '*')
84  {
85  zeromq_service()->subscribe(goby::common::MARSHALLING_MOOS,
86  "CMOOSMsg/" + trimmed_name.substr(0, size - 1), socket_id);
87  }
88  else
89  {
90  zeromq_service()->subscribe(goby::common::MARSHALLING_MOOS,
91  "CMOOSMsg/" + trimmed_name + "/", socket_id);
92  }
93 }
94 
95 void goby::moos::MOOSNode::unsubscribe(const std::string& full_or_partial_moos_name, int socket_id)
96 {
97  std::string trimmed_name = boost::trim_copy(full_or_partial_moos_name);
98  unsigned size = trimmed_name.size();
99  if (!size)
100  {
101  glog.is(WARN) && glog << warn << "Not unsubscribing for empty string!" << std::endl;
102  }
103  else if (trimmed_name[size - 1] == '*')
104  {
105  zeromq_service()->unsubscribe(goby::common::MARSHALLING_MOOS,
106  "CMOOSMsg/" + trimmed_name.substr(0, size - 1), socket_id);
107  }
108  else
109  {
110  zeromq_service()->unsubscribe(goby::common::MARSHALLING_MOOS,
111  "CMOOSMsg/" + trimmed_name + "/", socket_id);
112  }
113 }
114 
115 CMOOSMsg& goby::moos::MOOSNode::newest(const std::string& key)
116 {
117  if (!newest_vars.count(key))
118  newest_vars[key] = boost::shared_ptr<CMOOSMsg>(new CMOOSMsg);
119 
120  return *newest_vars[key];
121 }
122 
123 std::vector<CMOOSMsg> goby::moos::MOOSNode::newest_substr(const std::string& substring)
124 {
125  std::vector<CMOOSMsg> out;
126 
127  std::string trimmed_substring = boost::trim_copy(substring);
128  unsigned size = trimmed_substring.size();
129 
130  if (size)
131  {
132  if (trimmed_substring[size - 1] == '*')
133  {
134  trimmed_substring = trimmed_substring.substr(0, size - 1);
135  }
136  else
137  {
138  if (newest_vars.count(trimmed_substring))
139  out.push_back(*newest_vars[trimmed_substring]);
140  return out;
141  }
142  }
143 
144  for (std::map<std::string, boost::shared_ptr<CMOOSMsg> >::const_iterator
145  it = newest_vars.begin(),
146  end = newest_vars.end();
147  it != end; ++it)
148  {
149  if (!size || it->first.compare(0, trimmed_substring.size(), trimmed_substring) == 0)
150  out.push_back(*(it->second));
151  }
152  return out;
153 }
void add_group(const std::string &name, Colors::Color color=Colors::nocolor, const std::string &description="")
Add another group to the logger. A group provides related manipulator for categorizing log messages...
common::FlexOstream glog
Access the Goby logger through this object.
The global namespace for the Goby project.