TOBI Interface A  0.1
/home/breidi/Dropbox/libtia/src/tia/config/control_message_encoder.cpp
Go to the documentation of this file.
00001 /*
00002     This file is part of the TOBI Interface A (TiA) library.
00003 
00004     Commercial Usage
00005     Licensees holding valid Graz University of Technology Commercial
00006     licenses may use this file in accordance with the Graz University
00007     of Technology Commercial License Agreement provided with the
00008     Software or, alternatively, in accordance with the terms contained in
00009     a written agreement between you and Graz University of Technology.
00010 
00011     --------------------------------------------------
00012 
00013     GNU Lesser General Public License Usage
00014     Alternatively, this file may be used under the terms of the GNU Lesser
00015     General Public License version 3.0 as published by the Free Software
00016     Foundation and appearing in the file lgpl.txt included in the
00017     packaging of this file.  Please review the following information to
00018     ensure the GNU General Public License version 3.0 requirements will be
00019     met: http://www.gnu.org/copyleft/lgpl.html.
00020 
00021     In case of GNU Lesser General Public License Usage ,the TiA library
00022     is distributed in the hope that it will be useful,
00023     but WITHOUT ANY WARRANTY; without even the implied warranty of
00024     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025     GNU General Public License for more details.
00026 
00027     You should have received a copy of the GNU Lesser General Public License
00028     along with the TiA library. If not, see <http://www.gnu.org/licenses/>.
00029 
00030     Copyright 2010 Graz University of Technology
00031     Contact: TiA@tobi-project.org
00032 */
00033 
00038 // Boost
00039 #include <boost/lexical_cast.hpp>
00040 
00041 // local
00042 #include "tia/constants.h"
00043 
00044 #include "tia-private/config/control_message_encoder.h"
00045 
00046 namespace tia
00047 {
00048 
00049 using boost::lexical_cast;
00050 using boost::bad_lexical_cast;
00051 
00052 using std::cout;
00053 using std::endl;
00054 using std::string;
00055 using std::stringstream;
00056 
00057 
00058 //-----------------------------------------------------------------------------
00059 
00060 void ControlMsgEncoderXML::encodeMsg(const KeepAliveMsg& msg, std::ostream& stream)
00061 {
00062   TiXmlDocument doc;
00063   TiXmlElement* xml_msg = 0;
00064   encodeBaseMsg(msg,"alive", doc, xml_msg);
00065   assert(xml_msg != 0);
00066 
00067   TiXmlElement* cmd = new TiXmlElement("alive");
00068   xml_msg->LinkEndChild(cmd);
00069 
00070   writeXMLMsg(doc, stream);
00071 }
00072 
00073 //-----------------------------------------------------------------------------
00074 
00075 void ControlMsgEncoderXML::encodeMsg(const GetConfigMsg& msg, std::ostream& stream)
00076 {
00077   TiXmlDocument doc;
00078   TiXmlElement* xml_msg = 0;
00079   encodeBaseMsg(msg, "getConfig", doc, xml_msg);
00080   assert(xml_msg != 0);
00081 
00082   TiXmlElement* cmd = new TiXmlElement("getConfig");
00083   xml_msg->LinkEndChild(cmd);
00084 
00085   writeXMLMsg(doc, stream);
00086 }
00087 
00088 //-----------------------------------------------------------------------------
00089 
00090 void ControlMsgEncoderXML::encodeMsg(const StopTransmissionMsg& msg, std::ostream& stream)
00091 {
00092   TiXmlDocument doc;
00093   TiXmlElement* xml_msg = 0;
00094   encodeBaseMsg(msg,"stopTransmission", doc, xml_msg);
00095   assert(xml_msg != 0);
00096 
00097   TiXmlElement* cmd = new TiXmlElement("stopTransmission");
00098   xml_msg->LinkEndChild(cmd);
00099   writeXMLMsg(doc, stream);
00100 }
00101 
00102 //-----------------------------------------------------------------------------
00103 
00104 void ControlMsgEncoderXML::encodeMsg(const GetDataConnectionMsg& msg, std::ostream& stream)
00105 {
00106   TiXmlDocument doc;
00107   TiXmlElement* xml_msg = 0;
00108   encodeBaseMsg(msg,"getDataConnection", doc, xml_msg);
00109   assert(xml_msg != 0);
00110 
00111   TiXmlElement* cmd_element = new TiXmlElement("getDataConnection");
00112   TiXmlElement* element = new TiXmlElement("connectionType");
00113 
00114   element->LinkEndChild(new TiXmlText(
00115       msg.connectionType() == GetDataConnectionMsg::Tcp ? "tcp" : "udp"));
00116   cmd_element->LinkEndChild(element);
00117   xml_msg->LinkEndChild(cmd_element);
00118 
00119   writeXMLMsg(doc, stream);
00120 }
00121 
00122 //-----------------------------------------------------------------------------
00123 
00124 void ControlMsgEncoderXML::encodeMsg(const DataConnectionMsg& msg, std::ostream& stream)
00125 {
00126   TiXmlDocument doc;
00127   TiXmlElement* xml_msg = 0;
00128   encodeBaseMsg(msg,"dataConnection", doc, xml_msg);
00129   assert(xml_msg != 0);
00130 
00131   TiXmlElement* reply_element = new TiXmlElement("dataConnection");
00132 
00133   TiXmlElement* element = 0;
00134 
00135   if (!msg.address().empty())
00136   {
00137     element = new TiXmlElement("address");
00138     element->LinkEndChild(new TiXmlText(msg.address()));
00139     reply_element->LinkEndChild(element);
00140   }
00141 
00142   element = new TiXmlElement("port");
00143   string value = lexical_cast<string>(msg.port());
00144   element->LinkEndChild(new TiXmlText(value));
00145   reply_element->LinkEndChild(element);
00146 
00147   xml_msg->LinkEndChild(reply_element);
00148 
00149   writeXMLMsg(doc, stream);
00150 }
00151 
00152 //-----------------------------------------------------------------------------
00153 
00154 void ControlMsgEncoderXML::encodeMsg(const StartTransmissionMsg& msg, std::ostream& stream)
00155 {
00156   TiXmlDocument doc;
00157   TiXmlElement* xml_msg = 0;
00158   encodeBaseMsg(msg,"startTransmission", doc, xml_msg);
00159   assert(xml_msg != 0);
00160 
00161   TiXmlElement* cmd_element = new TiXmlElement("startTransmission");
00162   xml_msg->LinkEndChild(cmd_element);
00163 
00164   writeXMLMsg(doc, stream);
00165 }
00166 
00167 //-----------------------------------------------------------------------------
00168 
00169 void ControlMsgEncoderXML::encodeMsg(const ConfigMsg& msg, std::ostream& stream)
00170 {
00171   TiXmlDocument doc;
00172   TiXmlElement* xml_msg = 0;
00173   encodeBaseMsg(msg,"config", doc, xml_msg);
00174   assert(xml_msg != 0);
00175 
00176   TiXmlElement* config = new TiXmlElement("config");
00177 
00178   xml_msg->LinkEndChild(config);
00179 
00180   const SubjectInfo& subject = msg.subject_info;
00181 
00182   TiXmlElement* subject_elem = new TiXmlElement("subject");
00183 
00184   TiXmlElement* element = 0;
00185 
00186   element = new TiXmlElement("id");
00187   element->LinkEndChild(new TiXmlText(subject.id()));
00188   subject_elem->LinkEndChild(element);
00189 
00190   element = new TiXmlElement("firstName");
00191   element->LinkEndChild(new TiXmlText(subject.firstName()));
00192   subject_elem->LinkEndChild(element);
00193 
00194   element = new TiXmlElement("surname");
00195   element->LinkEndChild(new TiXmlText(subject.surname()));
00196   subject_elem->LinkEndChild(element);
00197 
00198   element = new TiXmlElement("sex");
00199   element->LinkEndChild(new TiXmlText(subject.sex() == SubjectInfo::Male ? "m" : "f"));
00200   subject_elem->LinkEndChild(element);
00201 
00202   element = new TiXmlElement("birthday");
00203   element->LinkEndChild(new TiXmlText(subject.birthday()));
00204   subject_elem->LinkEndChild(element);
00205 
00206   element = new TiXmlElement("handedness");
00207   element->LinkEndChild(new TiXmlText(subject.handedness() == SubjectInfo::RightHanded ? "r" : "l"));
00208   subject_elem->LinkEndChild(element);
00209 
00210   element = new TiXmlElement("medication");
00211   element->LinkEndChild(new TiXmlText(subject.medication()));
00212   subject_elem->LinkEndChild(element);
00213 
00214   SubjectInfo::ShortInfoMap::const_iterator it_short_infos = subject.shortInfoMap().begin();
00215   SubjectInfo::ShortInfoMap::const_iterator end_short_infos = subject.shortInfoMap().end();
00216 
00217   for (; it_short_infos != end_short_infos; ++it_short_infos)
00218   {
00219     SubjectInfo::ShortInfoType type  = (*it_short_infos).first;
00220     SubjectInfo::ShortInfoValue value = (*it_short_infos).second;
00221 
00222     std::string value_as_str = value == SubjectInfo::Yes ? "yes" : "no";
00223     switch(type)
00224     {
00225       case SubjectInfo::Glasses:
00226         element = new TiXmlElement("glasses");
00227         element->LinkEndChild(new TiXmlText(value_as_str));
00228         subject_elem->LinkEndChild(element);
00229         break;
00230       case SubjectInfo::Smoking:
00231         element = new TiXmlElement("smoker");
00232         element->LinkEndChild(new TiXmlText(value_as_str));
00233         subject_elem->LinkEndChild(element);
00234         break;
00235     }
00236   }
00237 
00238   config->LinkEndChild(subject_elem);
00239 
00240   const SignalInfo& signal_info = msg.signal_info;
00241 
00242   TiXmlElement* signal_info_elem = new TiXmlElement("signalInfo");
00243 
00244   TiXmlElement* master_elem = new TiXmlElement("master");
00245 
00246   element = new TiXmlElement("blockSize");
00247   string value = lexical_cast<string>(signal_info.masterBlockSize());
00248   element->LinkEndChild(new TiXmlText(value));
00249   master_elem->LinkEndChild(element);
00250 
00251   element = new TiXmlElement("samplingRate");
00252   value = lexical_cast<string>(signal_info.masterSamplingRate());
00253   element->LinkEndChild(new TiXmlText(value));
00254   master_elem->LinkEndChild(element);
00255 
00256   signal_info_elem->LinkEndChild(master_elem);
00257 
00258   TiXmlElement* signals_elem = new TiXmlElement("signals");
00259 
00260   SignalInfo::SignalMap::const_iterator it_signals = signal_info.signals().begin();
00261   SignalInfo::SignalMap::const_iterator end_signals = signal_info.signals().end();
00262   for (; it_signals != end_signals; ++it_signals)
00263   {
00264     const std::string& type = (*it_signals).first;
00265     const Signal& signal = (*it_signals).second;
00266 
00267     TiXmlElement* signal_elem = new TiXmlElement("sig");
00268     signal_elem->SetAttribute("type", type);
00269 
00270     element = new TiXmlElement("blockSize");
00271     value = lexical_cast<string>(signal.blockSize());
00272     element->LinkEndChild(new TiXmlText(value));
00273     signal_elem->LinkEndChild(element);
00274 
00275     element = new TiXmlElement("samplingRate");
00276     value = lexical_cast<string>(signal.samplingRate());
00277     element->LinkEndChild(new TiXmlText(value));
00278     signal_elem->LinkEndChild(element);
00279 
00280     TiXmlElement* channels_elem = new TiXmlElement("channels");
00281     std::vector<Channel>::const_iterator it_channels = signal.channels().begin();
00282     std::vector<Channel>::const_iterator end_channels = signal.channels().end();
00283     for (; it_channels != end_channels; ++it_channels)
00284     {
00285       const Channel& channel = (*it_channels);
00286       element = new TiXmlElement("ch");
00287       element->SetAttribute("id", channel.id());
00288       channels_elem->LinkEndChild(element);
00289     }
00290 
00291     signal_elem->LinkEndChild(channels_elem);
00292 
00293     signals_elem->LinkEndChild(signal_elem);
00294   }
00295 
00296   signal_info_elem->LinkEndChild(signals_elem);
00297 
00298   config->LinkEndChild(signal_info_elem);
00299 
00300   writeXMLMsg(doc, stream);
00301 }
00302 
00303 //-----------------------------------------------------------------------------
00304 
00305 void ControlMsgEncoderXML::encodeMsg(const ReplyMsg& msg, std::ostream& stream)
00306 {
00307   TiXmlDocument doc;
00308   TiXmlElement* xml_msg = 0;
00309 
00310   switch (msg.msgType())
00311   {
00312     case ControlMsg::OkReply:
00313       encodeBaseMsg(msg, "okReply", doc, xml_msg);
00314       assert(xml_msg != 0);
00315       xml_msg->LinkEndChild(new TiXmlElement("okReply"));
00316       break;
00317     case ControlMsg::ErrorReply:
00318       encodeBaseMsg(msg, "errorReply", doc, xml_msg);
00319       assert(xml_msg != 0);
00320       xml_msg->LinkEndChild(new TiXmlElement("errorReply"));
00321       break;
00322     default:
00323     {
00324       std::cerr << "Unhandled reply message type '" << msg.msgType() << "'" << endl;
00325       assert(false);
00326     }
00327   }
00328 
00329   writeXMLMsg(doc, stream);
00330 }
00331 
00332 //-----------------------------------------------------------------------------
00333 
00334 void ControlMsgEncoderXML::encodeBaseMsg(const ControlMsg& msg, const std::string& xml_msg_type,
00335     TiXmlDocument& doc, TiXmlElement*& xml_msg)
00336 {
00337   TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
00338   doc.LinkEndChild( decl );
00339 
00340   xml_msg = new TiXmlElement("message");
00341   doc.LinkEndChild(xml_msg);
00342   xml_msg->SetAttribute("version", MESSAGE_VERSION);
00343 
00344   TiXmlElement* header = new TiXmlElement("header");
00345   xml_msg->LinkEndChild(header);
00346 
00347   TiXmlElement* type = new TiXmlElement("type");
00348   type->LinkEndChild(new TiXmlText(xml_msg_type));
00349   header->LinkEndChild(type);
00350 
00351   TiXmlElement* sender = new TiXmlElement("sender");
00352   sender->LinkEndChild(new TiXmlText(msg.sender()));
00353   header->LinkEndChild(sender);
00354 }
00355 
00356 //-----------------------------------------------------------------------------
00357 
00358 void ControlMsgEncoderXML::writeXMLMsg(TiXmlDocument& doc, std::ostream& stream)
00359 {
00360   stream << doc;
00361 }
00362 
00363 //-----------------------------------------------------------------------------
00364 
00365 } // namespace tobiss
00366 
00367 //-----------------------------------------------------------------------------
00368 
00369 // End Of File
 All Data Structures Files Functions Variables Typedefs Enumerations