YarpServiceAsynchOutputPort.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 YarpServiceAsynchOutputPort.cpp
00024  * @Description YarpServiceAsynchOutputPort implementation
00025  * @Status Work in Progress
00026  * @Version $Id: YarpServiceAsynchOutputPort.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "YarpServiceAsynchOutputPort.hpp"
00033 #include "PortNamingConvention.hpp"
00034 
00035 #include <Entity.hpp>
00036 #include <EntityDescription.hpp>
00037 #include <Exception.hpp>
00038 #include <Service.hpp>
00039 #include <ServiceInterfaceDescription.hpp>
00040 
00041 #include <yarp/os/all.h>
00042 
00043 #include <iostream>
00044 
00045 using namespace mermaid::support::communication;
00046 
00047 using mermaid::support::errorhandling::Exception;
00048 using mermaid::support::service::Entity;
00049 using mermaid::support::service::EntityDescription;
00050 using mermaid::support::service::Service;
00051 using mermaid::support::service::ServiceInterfaceDescription;
00052 
00053 using yarp::os::Network;
00054 
00055 YarpServiceAsynchOutputPort::YarpServiceAsynchOutputPort (shared_ptr<Service> targetService, std::string requesterEntityName, std::string requesterServiceName) : BufferedPort<YarpServiceAsynchReplyType>()
00056 {
00057   //std::cerr << "YarpServiceAsynchOutputPort::YarpServiceAsynchOutputPort(clientName=" << clientName << " ,serviceName=" << serviceName << ")" << std::endl;
00058   
00059   targetService_ = targetService;
00060   requesterEntityName_ = requesterEntityName;
00061   requesterServiceName_ = requesterServiceName;
00062   
00063   std::string targetEntityName = targetService->getEntity()->getEntityDescription()->getEntityName();
00064   std::string targetServiceName = targetService->getServiceInstanceDescription()->getServiceInstanceName();
00065   
00066   std::string outputPortName = PortNamingConvention::getServiceRequestOutputPortName (targetEntityName, targetServiceName, requesterEntityName, requesterServiceName);
00067   
00068   //open port
00069   if (!open (outputPortName.c_str())) {
00070     throw Exception ("YarpServiceAsynchOutputPort::YarpServiceAsynchOutputPort: could not open YARP port with name: " + outputPortName);
00071   }
00072   
00073   //set strict output
00074   this->setStrict (true);
00075   
00076 }; // YarpServiceAsynchOutputPort
00077 
00078 
00079 YarpServiceAsynchReplyType& YarpServiceAsynchOutputPort::prepare()
00080 {
00081   //chack if we are connected
00082   if (getOutputCount() == 0) {
00083     std::string targetEntityName = targetService_->getEntity()->getEntityDescription()->getEntityName();
00084     std::string targetServiceName = targetService_->getServiceInstanceDescription()->getServiceInstanceName();
00085     
00086     std::string requesterServiceReplyPortName = PortNamingConvention::getServiceRequestReplyPortName (requesterEntityName_, requesterServiceName_, targetEntityName, targetServiceName);
00087     
00088     // connect to other port
00089     bool connectOk = Network::connect (this->getName().c_str(), requesterServiceReplyPortName.c_str());
00090     if (!connectOk) {
00091       throw Exception ("YarpServiceAsynchOutputPort::prepare() : could not connect to client with port name: \"" + requesterServiceReplyPortName + "\"");
00092     }
00093     else {
00094       //std::cerr << "YarpServiceAsynchOutputPort::prepare : CONNECT : connecting " << this->getName() << " to " << clientPortName << std::endl;
00095     }
00096   };
00097   
00098   return BufferedPort<YarpServiceAsynchReplyType>::prepare();
00099 }; // prepare()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3