YarpServiceRequestConversionTask.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
00031 #include "config.h"
00032
00033 #include "YarpServiceRequestConversionTask.hpp"
00034
00035 #include <ActiveObject.hpp>
00036 #include "CommunicationGateway.hpp"
00037 #include <Exception.hpp>
00038 #include <ServiceRequestDeliveryTask.hpp>
00039 #include <Task.hpp>
00040
00041 #include <UnexpectedDataTypeException.hpp>
00042 #include <ServiceErrorReply.hpp>
00043 #include <UnrecognizedServiceInterfaceException.hpp>
00044
00045 #include <iostream>
00046
00047 using namespace mermaid::support::communication;
00048 using mermaid::support::activeobject::ActiveObject;
00049 using mermaid::support::activeobject::Task;
00050 using mermaid::support::errorhandling::Exception;
00051 using mermaid::support::service::ServiceRequestDeliveryTask;
00052 using mermaid::support::data::exceptions::UnexpectedDataTypeException;
00053 using mermaid::support::service::ServiceErrorReply;
00054 using mermaid::support::service::exceptions::UnrecognizedServiceInterfaceException;
00055
00056 YarpServiceRequestConversionTask::YarpServiceRequestConversionTask (shared_ptr<Service> targetService, YarpServiceAsynchRequestType &request) : Task()
00057 {
00058 targetService_ = targetService;
00059 request_ = shared_ptr<YarpServiceAsynchRequestType> (new YarpServiceAsynchRequestType (request));
00060 };
00061
00062 void YarpServiceRequestConversionTask::run()
00063 {
00064
00065
00066 std::string requesterEntityName = request_->getRequesterEntityName();
00067 std::string requesterServiceName = request_->getRequesterServiceName();
00068 std::string targetEntityName = request_->getTargetEntityName();
00069 std::string targetServiceName = request_->getTargetServiceName();
00070 std::string targetServiceInterfaceName = request_->getTargetServiceInterfaceName();
00071 int requestId = request_->getRequestId();
00072
00073
00074
00075
00076
00077
00078
00079 shared_ptr<ActiveObject> ao = targetService_->getActiveObject();
00080 shared_ptr<CommunicationGateway> commGateway = ao->getCommunicationGateway();
00081
00082 try {
00083 shared_ptr<ServiceRequest> serviceRequest = request_->convertToServiceRequest();
00084 shared_ptr<Task> dt ( (Task*) new ServiceRequestDeliveryTask (targetService_, serviceRequest));
00085 ao->addTask (dt);
00086 }
00087 catch (UnexpectedDataTypeException &e) {
00088 std::cerr << "YarpServiceRequestConversionTask::run : caught an UnexpectedDataTypeException while processing a Service Request : " << e.what() << std::endl;
00089 shared_ptr<ServiceErrorReply> errorReply (new ServiceErrorReply (requesterEntityName, requesterServiceName, targetEntityName, targetServiceName, targetServiceInterfaceName, requestId, "ERROR: " + targetServiceInterfaceName + ": unexpected data type."));
00090 commGateway->sendServiceReply (targetService_, requesterEntityName, requesterServiceName, errorReply);
00091 throw e;
00092 }
00093 catch (UnrecognizedServiceInterfaceException &e) {
00094 std::cerr << "YarpServiceRequestConversionTask::run : caught an UnrecognizedServiceInterfaceException while processing a Service Request : " << e.what() << std::endl;
00095 shared_ptr<ServiceErrorReply> errorReply (new ServiceErrorReply (requesterEntityName, requesterServiceName, targetEntityName, targetServiceName, targetServiceInterfaceName, requestId, "ERROR: Unrecognized service interface: " + targetServiceInterfaceName));
00096
00097 commGateway->sendServiceReply (targetService_, requesterEntityName, requesterServiceName, errorReply);
00098 throw e;
00099 }
00100
00101 };