YarpDataFeedInputPort.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 #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
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
00090 this->setStrict (true);
00091
00092
00093 setReporter (* (new YarpDataFeedReporter (this)));
00094
00095 if (connect() == false) {
00096 std::string outputPortName = PortNamingConvention::getDataFeedOutputPortName (producerEntityName_, producerServiceName_, dataFeedName_);
00097
00098
00099 shared_ptr<Task> reconnectTask (new YarpDataFeedReconnectTask (this));
00100 getConsumerService()->getActiveObject()->addTask (reconnectTask);
00101
00102 }
00103
00104 };
00105
00106 bool YarpDataFeedInputPort::connect()
00107 {
00108
00109 std::string outputPortName = PortNamingConvention::getDataFeedOutputPortName (producerEntityName_, producerServiceName_, dataFeedName_);
00110
00111 return Network::connect (outputPortName.c_str(), this->getName().c_str());
00112
00113 };
00114
00115 YarpDataFeedInputPort::~YarpDataFeedInputPort()
00116 {
00117
00118 };
00119
00120 std::string YarpDataFeedInputPort::getProducerEntityName()
00121 {
00122 return producerEntityName_;
00123 };
00124
00125 std::string YarpDataFeedInputPort::getProducerServiceName()
00126 {
00127 return producerServiceName_;
00128 };
00129
00130 std::string YarpDataFeedInputPort::getDataFeedName()
00131 {
00132 return dataFeedName_;
00133 };
00134
00135 shared_ptr<Service> YarpDataFeedInputPort::getConsumerService()
00136 {
00137 return consumerService_;
00138 };
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
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
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 };