25 #ifndef GOBY_MOOS_MOOS_TRANSLATOR_H
26 #define GOBY_MOOS_MOOS_TRANSLATOR_H
38 #include <MOOS/libMOOS/Comms/MOOSMsg.h>
39 #include <MOOS/libMOOS/Utils/MOOSUtilityFunctions.h>
40 #include <boost/lexical_cast.hpp>
41 #include <boost/lexical_cast/bad_lexical_cast.hpp>
42 #include <google/protobuf/descriptor.h>
45 #include "dccl/dynamic_protobuf_manager.h"
56 namespace transitional
66 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
70 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
74 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
96 double lat_origin = std::numeric_limits<double>::quiet_NaN(),
97 double lon_origin = std::numeric_limits<double>::quiet_NaN(),
98 const std::string& modem_id_lookup_path =
"")
100 initialize(lat_origin, lon_origin, modem_id_lookup_path);
101 if (entry.IsInitialized())
107 double lat_origin = std::numeric_limits<double>::quiet_NaN(),
108 double lon_origin = std::numeric_limits<double>::quiet_NaN(),
109 const std::string& modem_id_lookup_path =
"")
111 initialize(lat_origin, lon_origin, modem_id_lookup_path);
115 void clear_entry(
const std::string& protobuf_name) { dictionary_.erase(protobuf_name); }
120 throw(std::runtime_error(
"Duplicate translator entry for " + entry.
protobuf_name()));
124 void add_entry(
const std::set<goby::moos::protobuf::TranslatorEntry>& entries)
126 for (
const auto& entry : entries) {
add_entry(entry); }
132 for (
const auto& entry : entries) {
add_entry(entry); }
136 template <
typename GoogleProtobufMessagePo
inter,
class StringCMOOSMsgMap>
137 GoogleProtobufMessagePointer
moos_to_protobuf(
const StringCMOOSMsgMap& moos_variables,
138 const std::string& protobuf_name);
140 std::multimap<std::string, CMOOSMsg>
144 std::multimap<std::string, CMOOSMsg>
147 const std::map<std::string, goby::moos::protobuf::TranslatorEntry>&
dictionary()
const
155 const std::string& pb_name)
160 moos_msg.SetSourceAux(
169 auto return_double = boost::lexical_cast<double>(
str);
170 return CMOOSMsg(MOOS_NOTIFY, var, return_double);
172 catch (boost::bad_lexical_cast&)
175 moos_msg.SetSourceAux(
187 void initialize(
double lat_origin = std::numeric_limits<double>::quiet_NaN(),
188 double lon_origin = std::numeric_limits<double>::quiet_NaN(),
189 const std::string& modem_id_lookup_path =
"");
192 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
195 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
198 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
201 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
208 std::map<std::string, goby::moos::protobuf::TranslatorEntry> dictionary_;
215 os <<
"= Begin MOOSTranslator =\n";
220 os <<
"== Begin Entry " << i <<
" ==\n"
221 << it.second.DebugString() <<
"== End Entry " << i <<
" ==\n";
226 os <<
"= End MOOSTranslator =";
241 inline std::multimap<std::string, CMOOSMsg>
244 std::map<std::string, goby::moos::protobuf::TranslatorEntry>::const_iterator it =
247 const std::string& pb_name = protobuf_msg.
GetDescriptor()->full_name();
249 if (it == dictionary_.end())
250 throw(std::runtime_error(
"No TranslatorEntry for Protobuf type: " + pb_name));
254 std::multimap<std::string, CMOOSMsg> moos_msgs;
256 bool is_binary =
false;
260 std::string return_string;
268 serialize(&return_string, protobuf_msg);
274 serialize(&return_string, protobuf_msg);
280 serialize(&return_string, protobuf_msg);
286 serialize(&return_string, protobuf_msg);
292 serialize(&return_string, protobuf_msg);
299 serialize(&return_string, protobuf_msg);
325 std::make_pair(moos_var,
make_moos_msg(moos_var, return_string, is_binary,
332 inline std::multimap<std::string, CMOOSMsg>
335 std::map<std::string, goby::moos::protobuf::TranslatorEntry>::const_iterator it =
338 const std::string& pb_name = protobuf_msg.
GetDescriptor()->full_name();
340 if (it == dictionary_.end())
341 throw(std::runtime_error(
"No TranslatorEntry for Protobuf type: " + pb_name));
345 std::multimap<std::string, CMOOSMsg> moos_msgs;
347 bool is_binary =
false;
349 for (
int i = 0, n = entry.
create_size(); i < n; ++i)
351 std::string return_string;
359 serialize(&return_string, protobuf_msg);
364 goby::moos::protobuf::TranslatorEntry::
365 TECHNIQUE_PREFIXED_PROTOBUF_TEXT_FORMAT>::serialize(&return_string,
372 serialize(&return_string, protobuf_msg);
378 serialize(&return_string, protobuf_msg);
384 serialize(&return_string, protobuf_msg);
390 goby::moos::protobuf::TranslatorEntry::
391 TECHNIQUE_PREFIXED_PROTOBUF_NATIVE_ENCODED>::serialize(&return_string,
405 serialize(&return_string, protobuf_msg, empty_algorithms,
417 &return_string, protobuf_msg, empty_algorithms, entry.
create(i).
format(),
424 std::make_pair(moos_var, make_moos_msg(moos_var, return_string, is_binary,
433 typedef std::multimap<std::string, CMOOSMsg>::iterator It;
434 std::pair<It, It> p = moos_msgs.equal_range(entry.
trigger().
moos_var());
435 for (
auto it = p.first; it != p.second; ++it) it->second.m_dfTime = MOOSTime();
448 template <
typename GoogleProtobufMessagePo
inter,
class StringCMOOSMsgMap>
449 GoogleProtobufMessagePointer
451 const std::string& protobuf_name)
453 std::map<std::string, goby::moos::protobuf::TranslatorEntry>::const_iterator it =
454 dictionary_.find(protobuf_name);
456 if (it == dictionary_.end())
457 throw(std::runtime_error(
"No TranslatorEntry for Protobuf type: " + protobuf_name));
461 GoogleProtobufMessagePointer
msg;
466 msg = dccl::DynamicProtobufManager::new_protobuf_message<GoogleProtobufMessagePointer>(
471 throw(std::runtime_error(
"Unknown Protobuf type: " + protobuf_name +
472 "; be sure it is compiled in or directly loaded into the "
473 "dccl::DynamicProtobufManager."));
475 for (
int i = 0, n = entry.
create_size(); i < n; ++i)
478 std::string source_string =
479 (it == moos_variables.end())
481 : (it->second.IsString() ? it->second.GetString()
482 : goby::util::as<std::string>(it->second.GetDouble()));
495 parse(source_string, &*
msg);
507 parse(source_string, &*
msg);
513 parse(source_string, &*
msg);
519 parse(source_string, &*
msg);