YarpServiceAsynchReplyType.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 "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
00069
00070 };
00071
00072 YarpServiceAsynchReplyType::YarpServiceAsynchReplyType (const YarpServiceAsynchReplyType &ysart)
00073 {
00074
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 };
00085
00086 YarpServiceAsynchReplyType& YarpServiceAsynchReplyType::operator= (const YarpServiceAsynchReplyType & ysart)
00087 {
00088 throw Exception ("YarpServiceAsynchReplyType::operator=(const YarpServiceAsynchReplyType &ysart)");
00089 };
00090
00091
00092 bool YarpServiceAsynchReplyType::write (ConnectionWriter& connection)
00093 {
00094
00095
00096
00097
00098
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
00114 bool writeOk = b.write (connection);
00115 connection.convertTextMode();
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 return writeOk;
00126 };
00127
00128 bool YarpServiceAsynchReplyType::read (ConnectionReader& connection)
00129 {
00130
00131
00132
00133
00134 connection.convertTextMode();
00135
00136 Bottle b = Bottle();
00137 bool readOk = b.read (connection);
00138
00139
00140
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
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
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
00197 replyData_ = Bottle (*dataValue.asList());
00198
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
00208
00209 return readOk;
00210 }
00211
00212 std::string YarpServiceAsynchReplyType::getRequesterEntityName()
00213 {
00214 return requesterEntityName_;
00215 };
00216
00217 std::string YarpServiceAsynchReplyType::getRequesterServiceName()
00218 {
00219 return requesterServiceName_;
00220 };
00221
00222 std::string YarpServiceAsynchReplyType::getTargetEntityName()
00223 {
00224 return targetEntityName_;
00225 };
00226
00227 std::string YarpServiceAsynchReplyType::getTargetServiceName()
00228 {
00229 return targetServiceName_;
00230 };
00231
00232 std::string YarpServiceAsynchReplyType::getTargetServiceInterfaceName()
00233 {
00234 return targetServiceInterfaceName_;
00235 };
00236
00237 int YarpServiceAsynchReplyType::getRequestId()
00238 {
00239 return requestId_;
00240 };
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 };
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 };
00275
00276 Bottle& YarpServiceAsynchReplyType::getReplyData()
00277 {
00278 return replyData_;
00279 };