YarpDataFeedInputPort.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 YarpDataFeedInputPort.cpp
00024  * @Description YarpDataFeedInputPort implementation
00025  * @Status Work in Progress
00026  * @Version $Id: YarpDataFeedInputPort.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "YarpDataFeedInputPort.hpp"
00033 #include "YarpDataFeedReconnectTask.hpp"
00034 #include "YarpDataFeedReporter.hpp"
00035 
00036 #include <ActiveObject.hpp>
00037 
00038 #include <DataBox.hpp>
00039 #include <DataFactory.hpp>
00040 #include <Entity.hpp>
00041 #include <EntityDescription.hpp>
00042 #include <EntityDescriptionRepository.hpp>
00043 #include <Exception.hpp>
00044 #include <PortNamingConvention.hpp>
00045 #include <Service.hpp>
00046 #include <ServiceDataFeedDeliveryTask.hpp>
00047 #include <ServiceInstanceDescription.hpp>
00048 #include <ServiceTypeDescription.hpp>
00049 #include <Task.hpp>
00050 
00051 #include <iostream>
00052 
00053 using namespace mermaid::support::communication;
00054 
00055 using mermaid::support::activeobject::ActiveObject;
00056 using mermaid::support::activeobject::Task;
00057 using mermaid::support::data::DataBox;
00058 using mermaid::support::data::DataFactory;
00059 using mermaid::support::errorhandling::Exception;
00060 using boost::shared_ptr;
00061 using mermaid::support::service::Entity;
00062 using mermaid::support::service::EntityDescription;
00063 using mermaid::support::service::EntityDescriptionRepository;
00064 using mermaid::support::service::Service;
00065 using mermaid::support::service::ServiceDataFeedDeliveryTask;
00066 using mermaid::support::service::ServiceInstanceDescription;
00067 using mermaid::support::service::ServiceTypeDescription;
00068 
00069 using yarp::os::Network;
00070 
00071 YarpDataFeedInputPort::YarpDataFeedInputPort (std::string producerEntityName, std::string producerServiceName, std::string datafeedName, shared_ptr<Service> consumerService)
00072 {
00073   producerEntityName_ = producerEntityName;
00074   producerServiceName_ = producerServiceName;
00075   dataFeedName_ = datafeedName;
00076   consumerService_ = consumerService;
00077   
00078   std::string consumerServiceName = consumerService_->getServiceInstanceDescription()->getServiceInstanceName();
00079   std::string consumerEntityName = consumerService_->getEntity()->getEntityDescription()->getEntityName();
00080   
00081   std::string inputPortName = PortNamingConvention::getDataFeedInputPortName (consumerEntityName, consumerServiceName, producerEntityName_, producerServiceName_, dataFeedName_);
00082   
00083   //open port
00084   if (!open (inputPortName.c_str())) {
00085     throw Exception ("YarpDataFeedInputPort::YarpDataFeedInputPort: could not open YARP port with name: " + inputPortName);
00086   }
00087   this->useCallback();
00088   
00089   //set strict output
00090   this->setStrict (true);
00091   
00092   //add reporter to port
00093   setReporter (* (new YarpDataFeedReporter (this)));
00094   
00095   if (connect() == false) {
00096     std::string outputPortName = PortNamingConvention::getDataFeedOutputPortName (producerEntityName_, producerServiceName_, dataFeedName_);
00097     
00098     //schedule port reconnection
00099     shared_ptr<Task> reconnectTask (new YarpDataFeedReconnectTask (this));
00100     getConsumerService()->getActiveObject()->addTask (reconnectTask);
00101     //throw Exception("YarpDataFeedInputPort::YarpDataFeedInputPort : could not connect to port: " + outputPortName);
00102   }
00103   
00104 }; // YarpDataFeedInputPort()
00105 
00106 bool YarpDataFeedInputPort::connect()
00107 {
00108   //data feed port name
00109   std::string outputPortName = PortNamingConvention::getDataFeedOutputPortName (producerEntityName_, producerServiceName_, dataFeedName_);
00110   
00111   return Network::connect (outputPortName.c_str(), this->getName().c_str());
00112   
00113 }; // connect()
00114 
00115 YarpDataFeedInputPort::~YarpDataFeedInputPort()
00116 {
00117 
00118 }; // ~YarpDataFeedInputPort()
00119 
00120 std::string YarpDataFeedInputPort::getProducerEntityName()
00121 {
00122   return producerEntityName_;
00123 }; // getProducerEntityName()
00124 
00125 std::string YarpDataFeedInputPort::getProducerServiceName()
00126 {
00127   return producerServiceName_;
00128 }; // getProducerServiceName()
00129 
00130 std::string YarpDataFeedInputPort::getDataFeedName()
00131 {
00132   return dataFeedName_;
00133 }; // getDataFeedName()
00134 
00135 shared_ptr<Service> YarpDataFeedInputPort::getConsumerService()
00136 {
00137   return consumerService_;
00138 }; // getConsumerEntityName()
00139 
00140 void YarpDataFeedInputPort::onRead (Bottle& data)
00141 {
00142   std::string dataTypeName = EntityDescriptionRepository::getInstance()->getEntityDescription (producerEntityName_)->getServiceInstanceDescription (producerServiceName_)->getServiceTypeDescriptionWithDataFeedName (dataFeedName_)->getDataFeedDescription (dataFeedName_)->getDataFeedOutputDataName();
00143   
00144 //     std::cerr << "YarpDataFeedInputPort::onRead : dataTypeName=\"" << dataTypeName << "\"" << std::endl;
00145   shared_ptr<DataBox> db;
00146   
00147   try {
00148     db = DataFactory::buildDataBoxFromStructureAndBottle (dataTypeName, data);
00149   }
00150   catch (Exception e) {
00151     std::cerr << "YarpDataFeedInputPort::onRead : input data was not correctly formatted: discarding." << std::endl;
00152     return;
00153   }
00154 //     db->print();
00155 
00156   shared_ptr<Task> dt ( (Task*) new ServiceDataFeedDeliveryTask (consumerService_, producerEntityName_, producerServiceName_, dataFeedName_, db));
00157   shared_ptr<ActiveObject> ao = consumerService_->getActiveObject();
00158   ao->addTask (dt);
00159   
00160 }; // onRead()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3