ControlUnitServiceBuildTask.hpp

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 #ifndef _CONTROLUNITSERVICEBUILDTASK_HPP__
00023 #define _CONTROLUNITSERVICEBUILDTASK_HPP__
00024 
00025 #include <ServiceBuildTask.hpp>
00026 #include <ActiveObject.hpp>
00027 
00028 #include <Entity.hpp>
00029 #include <EntityRepository.hpp>
00030 #include <EntityDescription.hpp>
00031 #include <ServiceConfiguration.hpp>
00032 #include "UnitService.hpp"
00033 #include <ServiceInstanceDescription.hpp>
00034 #include <ServiceInterfaceDescription.hpp>
00035 #include <ServiceInterface.hpp>
00036 #include <ServiceTypeDescription.hpp>
00037 #include <DataFeedDescription.hpp>
00038 #include <DataFeed.hpp>
00039 #include <Pingable.hpp>
00040 
00041 #include <vector>
00042 #include <map>
00043 
00044 //
00045 // this class extends ServiceBuildTask functionality
00046 // I need to pass the services to be called somewhere
00047 //
00048 
00049 namespace mermaid
00050 {
00051   namespace support
00052   {
00053     namespace unitgenerator
00054     {
00055     
00056       using namespace std;
00057       using mermaid::support::service::ServiceBuildTask;
00058       
00059       using boost::shared_ptr;
00060       using mermaid::support::service::Service;
00061       using mermaid::support::service::Entity;
00062       using mermaid::support::service::EntityRepository;
00063       using mermaid::support::service::ServiceConfiguration;
00064       using mermaid::support::service::ServiceInstanceDescription;
00065       using mermaid::support::service::ServiceTypeDescription;
00066       using mermaid::support::service::ServiceInterfaceDescription;
00067       using mermaid::support::service::ServiceInterface;
00068       using mermaid::support::service::DataFeedDescription;
00069       using mermaid::support::service::DataFeed;
00070       using mermaid::support::service::Pingable;
00071       using mermaid::support::activeobject::ActiveObject;
00072       
00073       template<class SERVICE_TYPE>
00074       class ControlUnitServiceBuildTask : public ServiceBuildTask<SERVICE_TYPE>
00075       {
00076         public:
00077           ControlUnitServiceBuildTask (shared_ptr<ActiveObject> ao, shared_ptr<EntityRepository> entityRepository, std::string entityName, std::string serviceInstanceName, shared_ptr<ServiceConfiguration> serviceConfiguration, const map<string, shared_ptr<Service> >& map = shared_ptr<Service>()) :
00078               ServiceBuildTask<SERVICE_TYPE> (ao, entityRepository, entityName, serviceInstanceName, serviceConfiguration) {
00079             nameToServiceMap_ = map;
00080           }
00081           
00082           virtual void run() {
00083             shared_ptr<Entity> entity = ServiceBuildTask<SERVICE_TYPE>::entityRepository_->getOrCreateEntity (ServiceBuildTask<SERVICE_TYPE>::entityName_);
00084             shared_ptr<ServiceInstanceDescription> serviceInstanceDescription = ServiceBuildTask<SERVICE_TYPE>::EntityDescriptionRepository::getInstance()->getEntityDescription (ServiceBuildTask<SERVICE_TYPE>::entityName_)->getServiceInstanceDescription (ServiceBuildTask<SERVICE_TYPE>::serviceInstanceName_);
00085             
00086             // create service
00087             shared_ptr<UnitService> s = shared_ptr<UnitService> (new SERVICE_TYPE (ServiceBuildTask<SERVICE_TYPE>::ao_, ServiceBuildTask<SERVICE_TYPE>::entity, ServiceBuildTask<SERVICE_TYPE>::serviceInstanceDescription, ServiceBuildTask<SERVICE_TYPE>::serviceConfiguration_));
00088             
00089             // these 2 lines is an addition
00090             s->setNameToServiceMap (nameToServiceMap_);
00091             
00092             //go through all service types
00093             std::vector<shared_ptr<ServiceTypeDescription> > serviceTypes = ServiceBuildTask<SERVICE_TYPE>::serviceInstanceDescription->getServiceTypeDescriptions();
00094             std::vector<shared_ptr<ServiceTypeDescription> >::iterator typeIterator;
00095             
00096             for (typeIterator = serviceTypes.begin(); typeIterator != serviceTypes.end(); typeIterator++) {
00097               shared_ptr<ServiceTypeDescription> typeDescription = *typeIterator;
00098               
00099               // add service interfaces to service
00100               std::vector< shared_ptr<ServiceInterfaceDescription> > interfaces = typeDescription->getAllServiceInterfaceDescriptions();
00101               
00102               std::vector< shared_ptr<ServiceInterfaceDescription> >::iterator interfaceIt;
00103               
00104               for (interfaceIt = interfaces.begin(); interfaceIt != interfaces.end(); interfaceIt++) {
00105                 shared_ptr<ServiceInterfaceDescription> itDesc = *interfaceIt;
00106                 shared_ptr<ServiceInterface> interface (new ServiceInterface (itDesc));
00107                 s->addServiceInterface (interface);
00108               };
00109               
00110               // add data feeds to service
00111               std::vector< shared_ptr<DataFeedDescription> > datafeeds = typeDescription->getAllDataFeedDescriptions();
00112               
00113               std::vector< shared_ptr<DataFeedDescription> >::iterator datafeedIt;
00114               
00115               for (datafeedIt = datafeeds.begin(); datafeedIt != datafeeds.end(); datafeedIt++) {
00116                 shared_ptr<DataFeedDescription> dfDesc = *datafeedIt;
00117                 shared_ptr<DataFeed> datafeed (new DataFeed (dfDesc));
00118                 s->addDataFeed (datafeed);
00119               };
00120             }
00121             
00122             //add service to owning entity
00123             entity->addService (s);
00124             
00125             //register service in communication gateway
00126             ServiceBuildTask<SERVICE_TYPE>::ao_->getCommunicationGateway()->registerService (s);
00127             
00128             try {
00129               //attach default service type implementations
00130               //! @TODO use deployment configuration file to choose which default service type implementations should be attached to each service.
00131               Pingable * p = new Pingable();
00132               p->attachToService (s);
00133             }
00134             catch (...) {
00135               std::cout << "Failed to attach \"Pingable\" default service type implementation." << std::endl;
00136             }
00137             
00138             //service is ready for initialization
00139             s->initialize();
00140           }
00141           
00142         private:
00143           map<string, shared_ptr<Service> > nameToServiceMap_;
00144       };
00145     }
00146   }
00147 }
00148 
00149 #endif // _CONTROLUNITSERVICEBUILDTASK_HPP__
00150 
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3