YarpServiceAsynchRequestType.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00069
00070 };
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
00081 };
00082
00083 YarpServiceAsynchRequestType::YarpServiceAsynchRequestType (const YarpServiceAsynchRequestType &ysart)
00084 {
00085
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 };
00096
00097
00098 YarpServiceAsynchRequestType& YarpServiceAsynchRequestType::operator= (const YarpServiceAsynchRequestType & ysart)
00099 {
00100 throw Exception ("YarpServiceAsynchRequestType::operator=");
00101 };
00102
00103 bool YarpServiceAsynchRequestType::write (ConnectionWriter& connection)
00104 {
00105
00106
00107
00108
00109
00110
00111
00112
00113 Bottle b = Bottle();
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
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
00134 for (int i = 0; i < (int) requestData_.size(); i++) {
00135 d.add (requestData_.get (i));
00136 }
00137
00138 bool writeOk = b.write (connection);
00139
00140
00141 connection.convertTextMode();
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 return writeOk;
00152 };
00153
00154
00155 bool YarpServiceAsynchRequestType::read (ConnectionReader& connection)
00156 {
00157 try {
00158 connection.convertTextMode();
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());
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 };
00240
00241 std::string YarpServiceAsynchRequestType::getRequesterServiceName()
00242 {
00243 return requesterServiceName_;
00244 };
00245
00246 std::string YarpServiceAsynchRequestType::getTargetEntityName()
00247 {
00248 return targetEntityName_;
00249 };
00250
00251 std::string YarpServiceAsynchRequestType::getTargetServiceName()
00252 {
00253 return targetServiceName_;
00254 };
00255
00256 std::string YarpServiceAsynchRequestType::getTargetServiceInterfaceName()
00257 {
00258 return targetServiceInterfaceName_;
00259 };
00260
00261 int YarpServiceAsynchRequestType::getRequestId()
00262 {
00263 return requestId_;
00264 };
00265
00266 Bottle& YarpServiceAsynchRequestType::getRequestData()
00267 {
00268 return requestData_;
00269 };
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 };
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 };
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314