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