YarpServiceAsynchRequestType.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 YarpServiceAsynchRequestType.cpp
00024  * @Description YarpServiceAsynchRequestType implementation
00025  * @Status Work in Progress
00026  * @Version $Id: YarpServiceAsynchRequestType.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "YarpServiceAsynchRequestType.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 YarpServiceAsynchRequestType::YarpServiceAsynchRequestType()
00067 {
00068   //std::cerr << "YarpServiceAsynchRequestType::YarpServiceAsynchRequestType()" << std::endl;
00069   //validData_ = false;
00070 }; // YarpYarpServiceAsynchRequestType()
00071 
00072 YarpServiceAsynchRequestType::YarpServiceAsynchRequestType (std::string requesterEntityName, std::string requesterServiceName, std::string targetEntityName, std::string targetServiceName, std::string targetServiceInterfaceName, int requestId)
00073 {
00074   requesterEntityName_ = requesterEntityName;
00075   requesterServiceName_ = requesterServiceName;
00076   targetEntityName_ = targetEntityName;
00077   targetServiceName_ = targetServiceName;
00078   targetServiceInterfaceName_ = targetServiceInterfaceName;
00079   requestId_ = requestId;
00080   //validData_ = true;
00081 };
00082 
00083 YarpServiceAsynchRequestType::YarpServiceAsynchRequestType (const YarpServiceAsynchRequestType &ysart)
00084 {
00085   //throw Exception("YarpServiceAsynchRequestType::YarpServiceAsynchRequestType(const YarpServiceAsynchRequestType &ysart)");
00086   
00087   requesterEntityName_ = ysart.requesterEntityName_;
00088   requesterServiceName_ = ysart.requesterServiceName_;
00089   targetEntityName_ = ysart.targetEntityName_;
00090   targetServiceName_ = ysart.targetServiceName_;
00091   targetServiceInterfaceName_ = ysart.targetServiceInterfaceName_;
00092   requestId_ = ysart.requestId_;
00093   requestData_ = Bottle (ysart.requestData_);
00094   
00095 }; // copy constructor
00096 
00097 
00098 YarpServiceAsynchRequestType& YarpServiceAsynchRequestType::operator= (const YarpServiceAsynchRequestType & ysart)
00099 {
00100   throw Exception ("YarpServiceAsynchRequestType::operator=");
00101 }; // assignment operator
00102 
00103 bool YarpServiceAsynchRequestType::write (ConnectionWriter& connection)
00104 {
00105   //std::cerr << "YarpServiceAsynchRequestType::write()" << std::endl;
00106   
00107   /*if(validData_ == false)
00108   {
00109       return false;
00110   }*/
00111   
00112   //sleep(5);
00113   Bottle b = Bottle();
00114   /*connection.appendInt(BOTTLE_TAG_LIST);
00115   connection.appendInt(4); // 4 elements
00116   connection.appendInt(BOTTLE_TAG_STRING);
00117   connection.appendString(clientName_.c_str());
00118   connection.appendInt(BOTTLE_TAG_STRING);
00119   connection.appendString(serviceInterfaceName_.c_str());
00120   connection.appendInt(BOTTLE_TAG_INT);
00121   connection.appendInt(requestId_);*/
00122   //std::cerr << "YarpServiceAsynchRequestType::write() : writing data bottle" << std::endl;
00123   /*connection.appendInt(BOTTLE_TAG_LIST);
00124   bool writeOk = requestData_.write(connection);*/
00125   
00126   b.addString (requesterEntityName_.c_str());
00127   b.addString (requesterServiceName_.c_str());
00128   b.addString (targetEntityName_.c_str());
00129   b.addString (targetServiceName_.c_str());
00130   b.addString (targetServiceInterfaceName_.c_str());
00131   b.addInt (requestId_);
00132   Bottle& d = b.addList();
00133   //std::cerr << "Service Request Data:" << requestData_.toString().c_str() << std::endl;
00134   for (int i = 0; i < (int) requestData_.size(); i++) {
00135     d.add (requestData_.get (i));
00136   }
00137   //std::cerr << "YarpServiceAsynchRequestType::write : writing bottle:" << b.toString().c_str() << std::endl;
00138   bool writeOk = b.write (connection);
00139   
00140   
00141   connection.convertTextMode(); // if connection is text-mode, convert!
00142   
00143   /*if(writeOk)
00144   {
00145       std::cerr << "YarpServiceAsynchRequestType::write : write OK" << std::endl;
00146   }
00147   else
00148   {
00149       std::cerr << "YarpServiceAsynchRequestType::write : write FAILED" << std::endl;
00150   }*/
00151   return writeOk;
00152 }; // write()
00153 
00154 
00155 bool YarpServiceAsynchRequestType::read (ConnectionReader& connection)
00156 {
00157   try {
00158     connection.convertTextMode(); // if connection is text-mode, convert!
00159     
00160     Bottle b = Bottle();
00161     bool readOk = b.read (connection);
00162     
00163     int bottleSize = b.size();
00164     if (bottleSize != 7) {
00165       std::cerr << "YarpServiceAsynchRequestType::read : size of the bottle should be 7, but it is: " << bottleSize << std::endl;
00166       return false;
00167     }
00168     
00169     
00170     Value requesterEntityNameValue = b.get (0);
00171     if (requesterEntityNameValue.isString() == false) {
00172       std::cerr << "YarpServiceAsynchRequestType::read : expected a string at position 0 of the bottle" << std::endl;
00173       return false;
00174     }
00175     requesterEntityName_ = std::string (requesterEntityNameValue.asString().c_str());
00176     
00177     Value requesterServiceNameValue = b.get (1);
00178     if (requesterServiceNameValue.isString() == false) {
00179       std::cerr << "YarpServiceAsynchRequestType::read : expected a string at position 1 of the bottle" << std::endl;
00180       return false;
00181     }
00182     requesterServiceName_ = std::string (requesterServiceNameValue.asString().c_str());
00183     
00184     Value targetEntityNameValue = b.get (2);
00185     if (targetEntityNameValue.isString() == false) {
00186       std::cerr << "YarpServiceAsynchRequestType::read : expected a string at position 2 of the bottle" << std::endl;
00187       return false;
00188     }
00189     targetEntityName_ = std::string (targetEntityNameValue.asString().c_str());
00190     
00191     Value targetServiceNameValue = b.get (3);
00192     if (targetServiceNameValue.isString() == false) {
00193       std::cerr << "YarpServiceAsynchRequestType::read : expected a string at position 3 of the bottle" << std::endl;
00194       return false;
00195     }
00196     targetServiceName_ = std::string (targetServiceNameValue.asString().c_str());
00197     
00198     Value targetServiceInterfaceValue = b.get (4);
00199     if (targetServiceInterfaceValue.isString() == false) {
00200       std::cerr << "YarpServiceAsynchRequestType::read : expected a string at position 4 of the bottle" << std::endl;
00201       return false;
00202     }
00203     targetServiceInterfaceName_ = std::string (targetServiceInterfaceValue.asString().c_str());
00204     
00205     Value requestIdValue = b.get (5);
00206     if (requestIdValue.isInt() == false) {
00207       std::cerr << "YarpServiceAsynchRequestType::read : expected an int at position 5 of the bottle" << std::endl;
00208     }
00209     requestId_ = requestIdValue.asInt();
00210     
00211     Value dataValue = b.get (6);
00212     if (dataValue.isList() == false) {
00213       std::cerr << "YarpServiceAsynchRequestType::read : expected a list at position 6 of the bottle" << std::endl;
00214     }
00215     
00216     requestData_ = Bottle (*dataValue.asList()); //! @TODO check if we can skip using the copy-constructor
00217     
00218     return readOk;
00219   }
00220   catch (...) {
00221     std::cerr << "YarpServiceAsynchRequestType: got a very strange exception" << std::endl;
00222     return false;
00223   }
00224 }
00225 
00226 
00227 void YarpServiceAsynchRequestType::setTargetEntityName (std::string targetEntityName)
00228 {
00229   targetEntityName_ = targetEntityName;
00230 }
00231 void YarpServiceAsynchRequestType::setTargetServiceName (std::string targetServiceName)
00232 {
00233   targetServiceName_ = targetServiceName;
00234 };
00235 
00236 std::string YarpServiceAsynchRequestType::getRequesterEntityName()
00237 {
00238   return requesterEntityName_;
00239 }; // getRequesterEntityName()
00240 
00241 std::string YarpServiceAsynchRequestType::getRequesterServiceName()
00242 {
00243   return requesterServiceName_;
00244 }; // getRequesterServiceName()
00245 
00246 std::string YarpServiceAsynchRequestType::getTargetEntityName()
00247 {
00248   return targetEntityName_;
00249 }; // getTargetEntityName()
00250 
00251 std::string YarpServiceAsynchRequestType::getTargetServiceName()
00252 {
00253   return targetServiceName_;
00254 }; // getTargetServiceName()
00255 
00256 std::string YarpServiceAsynchRequestType::getTargetServiceInterfaceName()
00257 {
00258   return targetServiceInterfaceName_;
00259 }; // getServiceInterfaceName()
00260 
00261 int YarpServiceAsynchRequestType::getRequestId()
00262 {
00263   return requestId_;
00264 }; // getRequestId()
00265 
00266 Bottle& YarpServiceAsynchRequestType::getRequestData()
00267 {
00268   return requestData_;
00269 }; // getRequestData()
00270 
00271 void YarpServiceAsynchRequestType::convertFromServiceRequest (ServiceRequest &sar)
00272 {
00273   requesterEntityName_ = sar.getRequesterEntityName();
00274   requesterServiceName_ = sar.getRequesterServiceName();
00275   targetEntityName_ = sar.getTargetEntityName();
00276   targetServiceName_ = sar.getTargetServiceName();
00277   targetServiceInterfaceName_ = sar.getTargetServiceInterfaceName();
00278   requestId_ = sar.getRequestId();
00279   requestData_.clear();
00280   DataFactory::writeDataBoxToBottle (sar.getRequestDataBox(), requestData_);
00281   
00282 }; // convertFromServiceRequest()
00283 
00284 shared_ptr<ServiceRequest> YarpServiceAsynchRequestType::convertToServiceRequest()
00285 {
00286 
00287   shared_ptr<ServiceTypeDescription> serviceTypeDescription = EntityDescriptionRepository::getInstance()->getEntityDescription (targetEntityName_)->getServiceInstanceDescription (targetServiceName_)->getServiceTypeDescriptionWithInterfaceName (targetServiceInterfaceName_);
00288   
00289   std::string structureName = serviceTypeDescription->getServiceInterfaceDescription (targetServiceInterfaceName_)->getServiceInterfaceInputDataName();
00290   shared_ptr<DataBox> box = DataFactory::buildDataBoxFromStructureAndBottle (structureName, requestData_);
00291   
00292   shared_ptr<ServiceRequest> request (new ServiceRequest (requesterEntityName_, requesterServiceName_, targetEntityName_, targetServiceName_, targetServiceInterfaceName_, requestId_));
00293   request->setRequestDataBox (box);
00294   
00295   return request;
00296 }; // convertToServiceRequest
00297 
00298 
00299 /*YarpServiceAsynchRequestType& YarpServiceAsynchRequestType::operator=(ServiceRequest &sar)
00300 {
00301     requesterEntityName_ = sar.getRequesterEntityName();
00302     requesterServiceName_= sar.getRequesterServiceName();
00303     serviceInterfaceName_ = sar.getServiceInterfaceName();
00304     requestId_ = sar.getRequestId();
00305     requestData_ = *sar.getRequestData()->getDataValueVector();
00306     validData_ = true;
00307     return *this;
00308 }; // operator=()
00309 
00310 YarpServiceAsynchRequestType::operator ServiceRequest ()
00311 {
00312     CowPtr<DataBox> requestData = DataFactory::buildDataBoxFromStructureAndBottle(requestData_);
00313     return ServiceRequest(requesterEntityName_, requesterServiceName_, serviceInterfaceName_, requestId_, requestData);
00314 }; // operator ServiceRequest &()*/
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3