YarpServiceAsynchRequestPort.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 YarpServiceAsynchRequestPort.cpp
00024  * @Description YarpServiceAsynchRequestPort implementation
00025  * @Status Work in Progress
00026  * @Version $Id: YarpServiceAsynchRequestPort.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "YarpServiceAsynchRequestPort.hpp"
00033 #include "YarpServiceRequestPortReporter.hpp"
00034 #include "PortNamingConvention.hpp"
00035 
00036 #include <Exception.hpp>
00037 
00038 #include <yarp/os/all.h>
00039 
00040 #include <iostream>
00041 
00042 using namespace mermaid::support::communication;
00043 
00044 using mermaid::support::errorhandling::Exception;
00045 using yarp::os::Network;
00046 
00047 YarpServiceAsynchRequestPort::YarpServiceAsynchRequestPort (shared_ptr<Service> requesterService, shared_ptr<ServiceRequest> request) : BufferedPort<YarpServiceAsynchRequestType>()
00048 {
00049   //std::cerr << "YarpServiceAsynchRequestPort::YarpServiceAsynchRequestPort(clientName=" << clientName << " ,serviceName=" << serviceName << ")" << std::endl;
00050   
00051   requesterService_ = requesterService;
00052   request_ = request;
00053   
00054   
00055   std::string requesterEntityName = request_->getRequesterEntityName();
00056   std::string requesterServiceName = request_->getRequesterServiceName();
00057   std::string targetEntityName = request_->getTargetEntityName();
00058   std::string targetServiceName = request_->getTargetServiceName();
00059   
00060   
00061   std::string portName = PortNamingConvention::getServiceRequestRequestPortName (requesterEntityName, requesterServiceName, targetEntityName, targetServiceName);
00062   
00063   //open port
00064   if (!open (portName.c_str())) {
00065     throw Exception ("YarpServiceAsynchRequestPort::YarpServiceAsynchRequestPort : could not open YARP port with name: " + portName);
00066   }
00067   
00068   //set strict output
00069   this->setStrict (true);
00070   
00071   //add reporter to port
00072   setReporter (* (new YarpServiceRequestPortReporter (this)));
00073   
00074 }; // YarpServiceAsynchRequestPort()
00075 
00076 
00077 YarpServiceAsynchRequestType& YarpServiceAsynchRequestPort::prepare()
00078 {
00079 
00080   //std::cerr << "YarpServiceAsynchRequestPort::prepare : output count=" << getOutputCount() << std::endl;
00081   
00082   //check if we are connected
00083   if (getOutputCount() == 0) {
00084     std::string targetServiceInputPortName;
00085 #ifdef USE_COMM
00086     std::string requesterEntityName = request_->getRequesterEntityName();
00087     std::string targetEntityName = request_->getTargetEntityName();
00088     if (requesterEntityName == targetEntityName)
00089       targetServiceInputPortName = PortNamingConvention::getServiceRequestInputPortName (targetEntityName, request_->getTargetServiceName());
00090     else
00091       targetServiceInputPortName = PortNamingConvention::getServiceRequestInputPortName (requesterEntityName, "COMM");
00092 #else
00093     targetServiceInputPortName = PortNamingConvention::getServiceRequestInputPortName (request_->getTargetEntityName(), request_->getTargetServiceName());
00094 #endif
00095       
00096     // connect to other port
00097     bool connectOk = Network::connect (this->getName().c_str(), targetServiceInputPortName.c_str());
00098     if (!connectOk) {
00099       throw Exception ("YarpServiceAsynchRequestPort::prepare() : could not connect to service with port name: " + targetServiceInputPortName);
00100     }
00101     else {
00102       //std::cerr << "YarpServiceAsynchRequestPort::prepare : CONNECT : connecting " << this->getName() << " to " << servicePortName << std::endl;
00103     }
00104     
00105   };
00106   
00107   return BufferedPort<YarpServiceAsynchRequestType>::prepare();
00108 }; // prepare()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3