YarpServiceRequestConversionTask.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 YarpServiceRequestConversionTask.cpp
00024  * @Description YarpServiceRequestConversionTask implementation
00025  * @Status Work in Progress
00026  * @Version $Id: YarpServiceRequestConversionTask.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer
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)); // copying because this object is under yarp's control
00060 }; // YarpServiceRequestConversionTask()
00061 
00062 void YarpServiceRequestConversionTask::run()
00063 {
00064   //std::cerr << "YarpServiceRequestConversionTask::run()" << std::endl;
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 //     std::cerr << "YarpServiceRequestConversionTask::run() : requesterEntityName=\"" << requesterEntityName << "\", requesterServiceName=\"" << requesterServiceName << "\"" << std::endl;
00074 
00075 //     std::cerr << "YarpServiceRequestConversionTask::run() : targetEntityName=\"" << targetEntityName << "\", targetServiceName=\"" << targetServiceName << "\", targetServiceInterfaceName=\"" << targetServiceInterfaceName << "\", requestId=" << requestId << std::endl;
00076 
00077   //throw Exception("YarpServiceRequestConversionTask::run() : got here");
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 }; // run()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3