GyrosMoosCommsInterface.cpp
Go to the documentation of this file.
00001 /* 
00002  * File:   MoosCommsInterface.cpp
00003  * Author: tomi
00004  * 
00005  * Created on February 14, 2011, 3:35 PM
00006  */
00007 
00008 #include <queue>
00009 #include <fstream>
00010 #include <moos/GyrosMoosCommsInterface.h>
00011 #include <boost/system/system_error.hpp>
00012 #include <labust/plugins/PlugableDefs.hpp>
00013 
00014 namespace LABUST
00015 {
00016     namespace COMMUNICATION
00017     {
00018 
00019         GyrosMoosCommsInterface::GyrosMoosCommsInterface(const labust::xml::Reader &reader, std::string configToUse)
00020         {
00021             std::cout << "Configuring MOOS Comms interface" << std::endl;
00022 
00023             std::string configQuery;
00024             if (configToUse.empty())
00025             {
00026                 configQuery = "commsConfig[@type='moos']";
00027             }
00028             else
00029             {
00030                 configQuery = "commsConfig[@type='moos' and @name='" + configToUse + "']";
00031             }
00032 
00033             //setDefaults
00034             callbackObject = NULL;
00035 
00036             _xmlNode* configNode = NULL;
00037             try
00038             {
00039                 if (reader.try_value(configQuery, &configNode))
00040                 {
00041                    _xmlNode* origin = reader.currentNode();
00042                     const_cast<labust::xml::Reader&>(reader).useNode(configNode);
00043                     std::ofstream configFile;
00044                     config = moos_configure(reader, configFile);
00045                     //start MOOS
00046                                         commsThread = boost::thread(static_cast<bool (GyrosMoosCommsInterface::*)(const char*, const char*)> (&GyrosMoosCommsInterface::Run), this, config.processName.c_str(), "config.moos");
00047                                                         
00048                                         const_cast<labust::xml::Reader&>(reader).useNode(origin);
00049                 }
00050             }
00051             catch (boost::system::system_error exc)
00052             {
00053                 std::cerr << "Error starting MOOS Comms interface: " << exc.what() << std::endl;
00054                 throw;
00055             }
00056             std::cout << "MOOS Comms interface started" << std::endl;
00057         }
00058 
00059         GyrosMoosCommsInterface::GyrosMoosCommsInterface(const std::string &configPath, std::string configToUse)
00060         {
00061             std::ifstream configFile;
00062             configFile.open(configPath.c_str());
00063             std::stringstream configXML;
00064             if (configFile.is_open())
00065             {
00066                 std::string line;
00067                 while (configFile.good())
00068                 {
00069                     std::getline(configFile, line);
00070                     configXML << line << std::endl;
00071                 }
00072                 configFile.close();
00073             }
00074 
00075             labust::xml::Reader reader(configXML.str());
00076 
00077             std::cout << "Configuring MOOS Comms interface" << std::endl;
00078 
00079             std::string configQuery;
00080             if (configToUse.empty())
00081             {
00082                 configQuery = "//commsConfig[@type='moos']";
00083             }
00084             else
00085             {
00086                 configQuery = "//commsConfig[@type='moos' and @name='" + configToUse + "']";
00087             }
00088 
00089             //setDefaults
00090             callbackObject = NULL;
00091 
00092             reader.useRootNode();
00093             _xmlNode* configNode = NULL;
00094             try
00095             {
00096                 if (reader.try_value(configQuery, &configNode))
00097                 {
00098                     reader.useNode(configNode);
00099                     std::ofstream configFile;
00100                     config = moos_configure(reader, configFile);
00101                     //start MOOS
00102                                         commsThread = boost::thread(static_cast<bool (GyrosMoosCommsInterface::*)(const char*, const char*)> (&GyrosMoosCommsInterface::Run), this, config.processName.c_str(), "config.moos");
00103                                 }
00104             }
00105             catch (boost::system::system_error exc)
00106             {
00107                 std::cerr << "Error starting MOOS Comms interface: " << exc.what() << std::endl;
00108                 throw;
00109             }
00110             std::cout << "MOOS Comms interface started" << std::endl;
00111         }
00112 
00113         GyrosMoosCommsInterface::GyrosMoosCommsInterface(const GyrosMoosCommsInterface& orig)
00114         {
00115         }
00116 
00117         GyrosMoosCommsInterface::~GyrosMoosCommsInterface()
00118         {
00119             std::cout << "Stopping Moos Comms Interface" << std::endl;
00120             this->RequestQuit();
00121             commsThread.join();
00122             std::cout << "Moos Comms Interface stopped" << std::endl;
00123         }
00124 
00125                 COMMERRORS::CommError GyrosMoosCommsInterface::Send(const labust::xml::GyrosWriter &data, bool wait)
00126         {
00127             boost::mutex::scoped_lock lock(locker);
00128             messagesToSend.push_back(data);
00129             if (wait)
00130             { //wait for comms thread to confirm data is sent
00131                 dataSentSignal.wait(lock);
00132             }
00133             return COMMERRORS::noError;
00134         }
00135 
00136         COMMERRORS::CommError GyrosMoosCommsInterface::Send(const std::vector<labust::xml::GyrosWriter> &data, bool wait)
00137         {
00138             boost::mutex::scoped_lock lock(locker);
00139             for (std::vector<labust::xml::GyrosWriter>::const_iterator gyros = data.begin(); gyros != data.end(); gyros++)
00140             {
00141                 messagesToSend.push_back(*gyros);
00142             }
00143             if (wait)
00144             { //wait for comms thread to confirm data is sent
00145                 dataSentSignal.wait(lock);
00146             }
00147             return COMMERRORS::noError;
00148         }
00149 
00150         COMMERRORS::CommError GyrosMoosCommsInterface::Receive(std::vector<labust::xml::GyrosReader>& gyrosObjects, bool wait)
00151         {
00152             COMMERRORS::CommError retVal = COMMERRORS::noError;
00153             if (callbackObject == NULL)
00154             {
00155                 boost::mutex::scoped_lock lock(locker);
00156                 while (wait && messagesReceived.empty())
00157                 { //if no data received, wait for comms thread to signal some data is received
00158                     dataReceivedSignal.wait(lock);
00159                 }
00160 
00161                 if (!messagesReceived.empty())
00162                 {
00163                     gyrosObjects.insert(gyrosObjects.end(), messagesReceived.begin(), messagesReceived.end());
00164                     messagesReceived.clear();
00165                 }
00166                 else if (!wait)
00167                 {
00168                     retVal = COMMERRORS::noData;
00169                 }
00170             }
00171             else
00172             {
00173                 std::cout << "Callback object is registered, please unregister it before using Receive function" << std::endl;
00174                 retVal = COMMERRORS::callbackRegistered;
00175             }
00176             return retVal;
00177         }
00178 
00179         const void* GyrosMoosCommsInterface::GetCommObject()
00180         {
00181             return &m_Comms;
00182         }
00183 
00184         void GyrosMoosCommsInterface::RegisterCallbackObject(CommEntity* entity)
00185         {
00186             this->callbackObject = entity;
00187         }
00188 
00189         void GyrosMoosCommsInterface::UnRegisterCallbackObject()
00190         {
00191             //mutex to prevent unregistration of callbackObject while it is being used
00192             boost::mutex::scoped_lock lock(locker);
00193             this->callbackObject = NULL;
00194         }
00195 
00196         bool GyrosMoosCommsInterface::OnNewMail(MOOSMSG_LIST& NewMail)
00197         {
00198           using namespace labust::xml;
00199             std::vector<GyrosReader> incomingMessages;
00200             boost::mutex::scoped_lock lock(locker);
00201             for (MOOSMSG_LIST::iterator mailIterator = NewMail.begin(); mailIterator != NewMail.end(); mailIterator++)
00202             {
00203                 CMOOSMsg &message = *mailIterator;
00204                 std::string messageString = message.GetString();
00205                 try
00206                 {
00207                     GyrosReader reader(message.GetString());
00208                     incomingMessages.push_back(reader);
00209                 }
00210                 catch (GyrosError error)
00211                 {
00212                     incomingMessages.push_back(GyrosReader(error.what()));
00213                 }
00214                 catch (XMLException e)
00215                 {
00216                     std::cerr << e.what() << std::endl;
00217                 }
00218 
00219             }
00220             if (callbackObject != NULL)
00221             {
00222                 callbackObject->AcceptData(incomingMessages);
00223             }
00224             else
00225             {
00226                 messagesReceived.insert(messagesReceived.end(), incomingMessages.begin(), incomingMessages.end());
00227             }
00228             if (!messagesReceived.empty())
00229             { //signal data is ready
00230                 dataReceivedSignal.notify_all();
00231             }
00232             return true;
00233         }
00234 
00235         bool GyrosMoosCommsInterface::Iterate()
00236         {
00237             boost::mutex::scoped_lock lock(locker);
00238             std::string outputMessageName;
00239             for (std::vector<labust::xml::GyrosWriter>::iterator gyros = messagesToSend.begin(); gyros != messagesToSend.end(); gyros++)
00240             {
00241                 outputMessageName = (*gyros).GetLabel();
00242                 if (outputMessageName.empty())
00243                 {
00244                     m_Comms.Notify(config.processName + "_OUT", (*gyros).GyrosXML(), MOOSTime());
00245                 }
00246                 else
00247                 {
00248                     m_Comms.Notify(outputMessageName, (*gyros).GyrosXML(), MOOSTime());
00249                 }
00250             }
00251             messagesToSend.clear();
00252 
00253             //signal data is sent
00254             dataSentSignal.notify_all();
00255             return true;
00256         }
00257 
00258         bool GyrosMoosCommsInterface::OnConnectToServer()
00259         {
00260                         connected = true;
00261             if (!config.subscriptionVars.empty())
00262             {
00263                 for (unsigned int i = 0; i < config.subscriptionVars.size(); i++)
00264                 {
00265                     std::cout << "\nRegistering for '" << config.subscriptionVars[i] << "'";
00266                     m_Comms.Register(config.subscriptionVars[i], 0);
00267                 }
00268             }
00269             else
00270             {
00271                 std::cerr << "No subscriptions specified" << std::endl;
00272             }
00273             return true;
00274         }
00275 
00276         bool GyrosMoosCommsInterface::OnStartUp()
00277         {
00278                         remove("config.moos");
00279                         if (!config.subscriptionVars.empty())
00280             {
00281                 for (unsigned int i = 0; i < config.subscriptionVars.size(); i++)
00282                 {
00283                     std::cout << "\nRegistering for '" << config.subscriptionVars[i] << "'";
00284                     m_Comms.Register(config.subscriptionVars[i], 0);
00285                 }
00286             }
00287             else
00288             {
00289                 std::cerr << "Warning: no subscriptions specified" << std::endl;
00290             }
00291             return true;
00292         }
00293 
00294                 bool GyrosMoosCommsInterface::OnDisconnectFromServer()
00295                 {
00296                         connected = false;
00297                         return true;
00298                 }
00299 
00300                 LABUST_EXTERN
00301                 {
00302                         LABUST_EXPORT LABUST::COMMUNICATION::CommsFactoryPtr createCommsFactory()
00303                         {
00304                                 return LABUST::COMMUNICATION::CommsFactoryPtr(new LABUST::COMMUNICATION::CommsFactory::Impl<GyrosMoosCommsInterface>());
00305                         }
00306                 }
00307     }
00308 }
00309 
00310 


acoustic_vr
Author(s):
autogenerated on Fri Feb 7 2014 11:37:09