YarpServiceAsynchReplyType.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename YarpServiceAsynchReplyType.cpp
00024  * @Description YarpServiceAsynchReplyType implementation
00025  * @Status Work in Progress
00026  * @Version $Id: YarpServiceAsynchReplyType.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "YarpServiceAsynchReplyType.hpp"
00033 
00034 #include <DataBox.hpp>
00035 #include <DataFactory.hpp>
00036 #include <EntityDescription.hpp>
00037 #include <EntityDescriptionRepository.hpp>
00038 #include <Service.hpp>
00039 #include <ServiceInstanceDescription.hpp>
00040 #include <ServiceInterfaceDescription.hpp>
00041 #include <ServiceTypeDescription.hpp>
00042 #include <ServiceTypeDescriptionRepository.hpp>
00043 #include <Exception.hpp>
00044 
00045 #include <iostream>
00046 
00047 using namespace mermaid::support::communication;
00048 
00049 using mermaid::support::data::DataBox;
00050 using mermaid::support::data::DataValueVector;
00051 using mermaid::support::data::DataFactory;
00052 using mermaid::support::errorhandling::Exception;
00053 using mermaid::support::service::EntityDescription;
00054 using mermaid::support::service::EntityDescriptionRepository;
00055 using mermaid::support::service::Service;
00056 using mermaid::support::service::ServiceInstanceDescription;
00057 using mermaid::support::service::ServiceInterfaceDescription;
00058 using mermaid::support::service::ServiceTypeDescription;
00059 using mermaid::support::service::ServiceTypeDescriptionRepository;
00060 
00061 
00062 using yarp::os::ConstString;
00063 using yarp::os::Value;
00064 
00065 
00066 YarpServiceAsynchReplyType::YarpServiceAsynchReplyType()
00067 {
00068   //std::cerr << "YarpServiceAsynchReplyType::YarpServiceAsynchReplyType()" << std::endl;
00069   //validData_ = false;
00070 }; // YarpServiceAsynchReplyType()
00071 
00072 YarpServiceAsynchReplyType::YarpServiceAsynchReplyType (const YarpServiceAsynchReplyType &ysart)
00073 {
00074   //throw Exception("YarpServiceAsynchReplyType::YarpServiceAsynchReplyType(const YarpServiceAsynchReplyType &ysart)");
00075   
00076   requesterEntityName_ = ysart.requesterEntityName_;
00077   requesterServiceName_ = ysart.requesterServiceName_;
00078   targetEntityName_ = ysart.targetEntityName_;
00079   targetServiceName_ = ysart.targetServiceName_;
00080   targetServiceInterfaceName_ = ysart.targetServiceInterfaceName_;
00081   requestId_ = ysart.requestId_;
00082   executionStatus_ = ysart.executionStatus_;
00083   replyData_ = Bottle (ysart.replyData_);
00084 }; // copy constructor
00085 
00086 YarpServiceAsynchReplyType& YarpServiceAsynchReplyType::operator= (const YarpServiceAsynchReplyType & ysart)
00087 {
00088   throw Exception ("YarpServiceAsynchReplyType::operator=(const YarpServiceAsynchReplyType &ysart)");
00089 }; // assignment operator
00090 
00091 
00092 bool YarpServiceAsynchReplyType::write (ConnectionWriter& connection)
00093 {
00094   //std::cerr << "YarpServiceAsynchReplyType::write()" << std::endl;
00095   
00096   /*if(validData_ == false)
00097   {
00098       return false;
00099   }*/
00100   
00101   Bottle b = Bottle();
00102   b.addString (requesterEntityName_.c_str());
00103   b.addString (requesterServiceName_.c_str());
00104   b.addString (targetEntityName_.c_str());
00105   b.addString (targetServiceName_.c_str());
00106   b.addString (targetServiceInterfaceName_.c_str());
00107   b.addInt (requestId_);
00108   Bottle& d = b.addList();
00109   b.addString (executionStatus_.c_str());
00110   for (int i = 0; i < (int) replyData_.size(); i++) {
00111     d.add (replyData_.get (i));
00112   }
00113   //std::cerr << "YarpServiceAsynchReplyType::write : writing bottle:" << b.toString().c_str() << std::endl;
00114   bool writeOk = b.write (connection);
00115   connection.convertTextMode(); // if connection is text-mode, convert!
00116   /*
00117       if(writeOk)
00118       {
00119           //std::cerr << "YarpServiceAsynchReplyType::write : write OK" << std::endl;
00120       }
00121       else
00122       {
00123   //         throw Exception("YarpServiceAsynchReplyType::write : write FAILED");
00124       }*/
00125   return writeOk;
00126 }; // write()
00127 
00128 bool YarpServiceAsynchReplyType::read (ConnectionReader& connection)
00129 {
00130   //std::cerr << "YarpServiceAsynchReplyType::read()" << std::endl;
00131   
00132   //throw Exception("YarpServiceAsynchReplyType::read() : not implemented");
00133   
00134   connection.convertTextMode(); // if connection is text-mode, convert!
00135   
00136   Bottle b = Bottle();
00137   bool readOk = b.read (connection);
00138 //     std::cerr << "YarpServiceAsynchReplyType::read : received bottle=\"" << b.toString().c_str() << "\"" << std::endl;
00139 
00140   //std::cerr << "\t" << b.toString() << std::endl;
00141   int bottleSize = b.size();
00142   if (bottleSize != 8) {
00143     std::cerr << "YarpServiceAsynchReplyType::read : size of the bottle should be 8, but it is: " << bottleSize << std::endl;
00144     return false;
00145   }
00146   
00147   Value requesterEntityNameValue = b.get (0);
00148   if (requesterEntityNameValue.isString() == false) {
00149     std::cerr << "YarpServiceAsynchReplyType::read : expected a string at position 0 of the bottle" << std::endl;
00150     return false;
00151   }
00152   requesterEntityName_ = std::string (requesterEntityNameValue.asString().c_str());
00153   
00154   Value requesterServiceNameValue = b.get (1);
00155   if (requesterServiceNameValue.isString() == false) {
00156     std::cerr << "YarpServiceAsynchReplyType::read : expected a string at position 1 of the bottle" << std::endl;
00157     return false;
00158   }
00159   requesterServiceName_ = std::string (requesterServiceNameValue.asString().c_str());
00160 //     std::cerr << "YarpServiceAsynchReplyType:read() : serviceInterfaceNameValue_=" << executionStatus_ << std::endl;
00161 
00162   Value targetEntityNameValue = b.get (2);
00163   if (targetEntityNameValue.isString() == false) {
00164     std::cerr << "YarpServiceAsynchReplyType::read : expected a string at position 2 of the bottle" << std::endl;
00165     return false;
00166   }
00167   targetEntityName_ = std::string (targetEntityNameValue.asString().c_str());
00168   
00169   Value targetServiceNameValue = b.get (3);
00170   if (targetServiceNameValue.isString() == false) {
00171     std::cerr << "YarpServiceAsynchReplyType::read : expected a string at position 3 of the bottle" << std::endl;
00172     return false;
00173   }
00174   targetServiceName_ = std::string (targetServiceNameValue.asString().c_str());
00175   
00176   Value targetServiceInterfaceNameValue = b.get (4);
00177   if (targetServiceInterfaceNameValue.isString() == false) {
00178     std::cerr << "YarpServiceAsynchReplyType::read : expected a string at position 4 of the bottle" << std::endl;
00179     return false;
00180   }
00181   targetServiceInterfaceName_ = std::string (targetServiceInterfaceNameValue.asString().c_str());
00182   
00183   Value requestIdValue = b.get (5);
00184   if (requestIdValue.isInt() == false) {
00185     std::cerr << "YarpServiceAsynchReplyType::read : expected an int at position 5 of the bottle" << std::endl;
00186     return false;
00187   }
00188   requestId_ = requestIdValue.asInt();
00189 //     std::cerr << "YarpServiceAsynchReplyType::read() : requestId_=" << requestId_ << std::endl;
00190 
00191   Value dataValue = b.get (6);
00192   if (dataValue.isList() == false) {
00193     std::cerr << "YarpServiceAsynchReplyType::read : expected a list at position 6 of the bottle" << std::endl;
00194     return false;
00195   }
00196 //     std::cerr << "YarpServiceAsynchReplyType: reply data received=" << dataValue.toString().c_str() << std::endl;
00197   replyData_ = Bottle (*dataValue.asList()); //! @TODO check if we can skip using the copy-constructor
00198   //std::cerr << "YarpServiceAsynchReplyType::read : replyData_=" << replyData_.toString().c_str() << std::endl;
00199   
00200   
00201   Value executionStatusValue = b.get (7);
00202   if (executionStatusValue.isString() == false) {
00203     std::cerr << "YarpServiceAsynchReplyType::read : expected a string at position 7 of the bottle" << std::endl;
00204     return false;
00205   }
00206   executionStatus_ = std::string (executionStatusValue.asString().c_str());
00207 //     std::cerr << "YarpServiceAsynchReplyType:read() : executionStatus_=" << executionStatus_ << std::endl;
00208 
00209   return readOk;
00210 }
00211 
00212 std::string YarpServiceAsynchReplyType::getRequesterEntityName()
00213 {
00214   return requesterEntityName_;
00215 }; // getRequesterEntityName()
00216 
00217 std::string YarpServiceAsynchReplyType::getRequesterServiceName()
00218 {
00219   return requesterServiceName_;
00220 }; // getRequesterServiceName()
00221 
00222 std::string YarpServiceAsynchReplyType::getTargetEntityName()
00223 {
00224   return targetEntityName_;
00225 }; // getTargetEntityName()
00226 
00227 std::string YarpServiceAsynchReplyType::getTargetServiceName()
00228 {
00229   return targetServiceName_;
00230 }; // getTargetServiceName()
00231 
00232 std::string YarpServiceAsynchReplyType::getTargetServiceInterfaceName()
00233 {
00234   return targetServiceInterfaceName_;
00235 }; // getTargetServiceInterfaceName()
00236 
00237 int YarpServiceAsynchReplyType::getRequestId()
00238 {
00239   return requestId_;
00240 }; // getRequestId()
00241 
00242 void YarpServiceAsynchReplyType::convertFromServiceReply (ServiceReply &sr)
00243 {
00244   requesterEntityName_ = sr.getRequesterEntityName();
00245   requesterServiceName_ = sr.getRequesterServiceName();
00246   targetEntityName_ = sr.getTargetEntityName();
00247   targetServiceName_ = sr.getTargetServiceName();
00248   targetServiceInterfaceName_ = sr.getTargetServiceInterfaceName();
00249   requestId_ = sr.getRequestId();
00250   executionStatus_ = sr.getExecutionStatus();
00251   replyData_.clear();
00252   
00253   if (executionStatus_.compare ("OK") == 0) {
00254     DataFactory::writeDataBoxToBottle (sr.getReplyDataBox(), replyData_);
00255   }
00256 }; // convertFromServiceReply()
00257 
00258 shared_ptr<ServiceReply> YarpServiceAsynchReplyType::convertToServiceReply (shared_ptr<ServiceRequest> originalRequest)
00259 {
00260 
00261   std::string targetEntityName = originalRequest->getTargetEntityName();
00262   std::string targetServiceName = originalRequest->getTargetServiceName();
00263   std::string targetServiceInterfaceName = originalRequest->getTargetServiceInterfaceName();
00264   
00265   shared_ptr<ServiceTypeDescription> serviceTypeDescription = EntityDescriptionRepository::getInstance()->getEntityDescription (targetEntityName)->getServiceInstanceDescription (targetServiceName)->getServiceTypeDescriptionWithInterfaceName (targetServiceInterfaceName);
00266   
00267   std::string structureName = serviceTypeDescription->getServiceInterfaceDescription (targetServiceInterfaceName)->getServiceInterfaceOutputDataName();
00268   shared_ptr<DataBox> box = DataFactory::buildDataBoxFromStructureAndBottle (structureName, replyData_);
00269   
00270   shared_ptr<ServiceReply> reply (new ServiceReply (originalRequest, executionStatus_));
00271   reply->setReplyDataBox (box);
00272   
00273   return reply;
00274 }; // convertToServiceReply
00275 
00276 Bottle& YarpServiceAsynchReplyType::getReplyData()
00277 {
00278   return replyData_;
00279 }; // getReplyData()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3