24 #include "goby/moos/moos_string.h" 26 #include "pTranslator.h" 30 using goby::moos::operator<<;
33 CpTranslator* CpTranslator::inst_ = 0;
35 CpTranslator* CpTranslator::get_instance()
38 inst_ =
new CpTranslator();
42 void CpTranslator::delete_instance() {
delete inst_; }
44 CpTranslator::CpTranslator()
45 :
GobyMOOSApp(&cfg_), translator_(cfg_.translator_entry(), cfg_.common().lat_origin(),
46 cfg_.common().lon_origin(), cfg_.modem_id_lookup_path()),
47 work_(timer_io_service_)
49 goby::util::DynamicProtobufManager::enable_compilation();
52 for (
int i = 0, n = cfg_.load_shared_library_size(); i < n; ++i)
54 glog.is(VERBOSE) &&
glog <<
"Loading shared library: " << cfg_.load_shared_library(i)
57 void* handle = dlopen(cfg_.load_shared_library(i).c_str(), RTLD_LAZY);
60 glog << die <<
"Failed ... check path provided or add to /etc/ld.so.conf " 61 <<
"or LD_LIBRARY_PATH" << std::endl;
66 for (
int i = 0, n = cfg_.load_proto_file_size(); i < n; ++i)
68 glog.is(VERBOSE) &&
glog <<
"Loading protobuf file: " << cfg_.load_proto_file(i)
71 if (!goby::util::DynamicProtobufManager::find_descriptor(cfg_.load_proto_file(i)))
72 glog.is(DIE) &&
glog <<
"Failed to load file." << std::endl;
76 for (
int i = 0, n = cfg_.translator_entry_size(); i < n; ++i)
78 typedef boost::shared_ptr<google::protobuf::Message> GoogleProtobufMessagePointer;
79 glog.is(VERBOSE) &&
glog <<
"Checking translator entry: " 80 << cfg_.translator_entry(i).DebugString() << std::flush;
83 goby::util::DynamicProtobufManager::new_protobuf_message<GoogleProtobufMessagePointer>(
84 cfg_.translator_entry(i).protobuf_name());
86 if (cfg_.translator_entry(i).trigger().type() ==
87 goby::moos::protobuf::TranslatorEntry::Trigger::TRIGGER_PUBLISH)
90 GobyMOOSApp::subscribe(
91 cfg_.translator_entry(i).trigger().moos_var(),
92 boost::bind(&CpTranslator::create_on_publish,
this, _1, cfg_.translator_entry(i)));
94 else if (cfg_.translator_entry(i).trigger().type() ==
95 goby::moos::protobuf::TranslatorEntry::Trigger::TRIGGER_TIME)
97 timers_.push_back(boost::shared_ptr<Timer>(
new Timer(timer_io_service_)));
99 Timer& new_timer = *timers_.back();
101 new_timer.expires_from_now(
102 boost::posix_time::seconds(cfg_.translator_entry(i).trigger().period()));
104 new_timer.async_wait(boost::bind(&CpTranslator::create_on_timer,
this, _1,
105 cfg_.translator_entry(i), &new_timer));
108 for (
int j = 0, m = cfg_.translator_entry(i).create_size(); j < m; ++j)
111 subscribe(cfg_.translator_entry(i).create(j).moos_var());
115 for (
int i = 0, m = cfg_.multiplex_create_moos_var_size(); i < m; ++i)
117 GobyMOOSApp::subscribe(cfg_.multiplex_create_moos_var(i),
118 &CpTranslator::create_on_multiplex_publish,
this);
122 CpTranslator::~CpTranslator() {}
124 void CpTranslator::loop() { timer_io_service_.poll(); }
126 void CpTranslator::create_on_publish(
const CMOOSMsg& trigger_msg,
129 glog.is(VERBOSE) &&
glog <<
"Received trigger: " << trigger_msg << std::endl;
131 if (!entry.trigger().has_mandatory_content() ||
132 trigger_msg.GetString().find(entry.trigger().mandatory_content()) != std::string::npos)
133 do_translation(entry);
136 glog <<
"Message missing mandatory content for: " << entry.protobuf_name() << std::endl;
139 void CpTranslator::create_on_multiplex_publish(
const CMOOSMsg& moos_msg)
141 boost::shared_ptr<google::protobuf::Message> msg = dynamic_parse_for_moos(moos_msg.GetString());
145 glog.is(WARN) &&
glog <<
"Multiplex receive failed: Unknown Protobuf type for " 146 << moos_msg.GetString()
147 <<
"; be sure it is compiled in or directly loaded into the " 148 "goby::util::DynamicProtobufManager." 153 std::multimap<std::string, CMOOSMsg> out;
157 out = translator_.protobuf_to_inverse_moos(*msg);
159 for (std::multimap<std::string, CMOOSMsg>::iterator it = out.begin(), n = out.end();
162 glog.is(VERBOSE) &&
glog <<
"Inverse Publishing: " << it->second.GetKey() << std::endl;
166 catch (std::exception& e)
168 glog.is(WARN) &&
glog <<
"Failed to inverse publish: " << e.what() << std::endl;
172 void CpTranslator::create_on_timer(
const boost::system::error_code& error,
178 goby::util::as<double>(timer->expires_at()));
179 if (skew_seconds > ALLOWED_TIMER_SKEW_SECONDS)
181 glog.is(VERBOSE) &&
glog <<
"clock skew of " << skew_seconds
182 <<
" seconds detected, resetting timer." << std::endl;
185 boost::posix_time::seconds(boost::posix_time::seconds(entry.trigger().period())));
190 timer->expires_at(timer->expires_at() +
191 boost::posix_time::seconds(entry.trigger().period()));
194 timer->async_wait(boost::bind(&CpTranslator::create_on_timer,
this, _1, entry, timer));
196 glog.is(VERBOSE) &&
glog <<
"Received trigger for: " << entry.protobuf_name() << std::endl;
197 glog.is(VERBOSE) &&
glog <<
"Next expiry: " << timer->expires_at() << std::endl;
199 do_translation(entry);
205 boost::shared_ptr<google::protobuf::Message> created_message =
206 translator_.moos_to_protobuf<boost::shared_ptr<google::protobuf::Message> >(
207 dynamic_vars().all(), entry.protobuf_name());
209 glog.is(DEBUG1) &&
glog <<
"Created message: \n" << created_message->DebugString() << std::endl;
211 do_publish(created_message);
214 void CpTranslator::do_publish(boost::shared_ptr<google::protobuf::Message> created_message)
216 std::multimap<std::string, CMOOSMsg> out;
218 out = translator_.protobuf_to_moos(*created_message);
220 for (std::multimap<std::string, CMOOSMsg>::iterator it = out.begin(), n = out.end(); it != n;
223 glog.is(VERBOSE) &&
glog <<
"Publishing: " << it->second << std::endl;
double goby_time< double >()
Returns current UTC time as seconds and fractional seconds since 1970-01-01 00:00:00.
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
common::FlexOstream glog
Access the Goby logger through this object.