Goby v2
message_publish.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 <boost/foreach.hpp>
24 
25 #include "goby/acomms/dccl.h"
26 #include "message.h"
27 #include "message_publish.h"
28 
29 void goby::transitional::DCCLPublish::initialize(const DCCLMessage& msg)
30 {
31  repeat_ = msg.repeat();
32 
33  // check and add all publish names grabbed by the xml parser
34  BOOST_FOREACH (const std::string& name, names_)
35  add_message_var(msg.name2message_var(name));
36 
37  BOOST_FOREACH (const std::vector<std::string>& algs, algorithms_)
38  {
39  BOOST_FOREACH (const std::string& alg, algs)
40  ap_->check_algorithm(alg, msg);
41  }
42 
43  // add names for any <all/> publishes and empty std::vector for algorithms
44  if (use_all_names_)
45  {
46  BOOST_FOREACH (const boost::shared_ptr<DCCLMessageVar> mv, msg.header_const())
47  {
48  // ignore header pieces not explicitly overloaded by the <name> tag
49  if (!mv->name().empty() && !(mv->name()[0] == '_') &&
50  !std::count(names_.begin(), names_.end(), mv->name()))
51  {
52  add_message_var(mv);
53  // add an empty std::vector for algorithms (no algorithms allowed for <all/> tag)
54  add_algorithms(std::vector<std::string>());
55  }
56  }
57 
58  BOOST_FOREACH (const boost::shared_ptr<DCCLMessageVar> mv, msg.layout_const())
59  {
60  if (!std::count(names_.begin(), names_.end(), mv->name()))
61  {
62  add_message_var(mv);
63  // add an empty std::vector for algorithms (no algorithms allowed for <all/> tag)
64  add_algorithms(std::vector<std::string>());
65  }
66  }
67  }
68 
69  int format_count = 0;
70  // add format if publish doesn't have one
71  if (!format_set_)
72  {
73  std::string format_str;
74  for (std::vector<boost::shared_ptr<DCCLMessageVar> >::size_type j = 0,
75  m = message_vars_.size();
76  j < m; ++j)
77  {
78  if (m > 1)
79  {
80  if (j)
81  format_str += ",";
82 
83  // allows you to use the same message var twice but gives a unique name based on the algorithms used
84  unsigned size = algorithms_[j].size();
85  if (count(message_vars_.begin(), message_vars_.end(), message_vars_[j]) > 1 && size)
86  {
87  for (unsigned i = 0; i < size; ++i) format_str += algorithms_[j][i];
88 
89  format_str += "(" + message_vars_[j]->name() + ")=";
90  }
91  else
92  format_str += message_vars_[j]->name() + "=";
93  }
94 
95  for (unsigned i = 0, n = (repeat_ > 1) ? 1 : message_vars_[j]->array_length(); i < n;
96  ++i)
97  {
98  ++format_count;
99  std::stringstream ss;
100 
101  if (m > 1 && n > 1 && i == 0)
102  ss << "{";
103  if (i)
104  ss << ",";
105 
106  ss << "%" << format_count << "%";
107 
108  if (m > 1 && n > 1 && i + 1 == n)
109  ss << "}";
110  format_str += ss.str();
111  }
112  }
113  format_ = format_str;
114  }
115 }
STL namespace.