ServiceBuildTask.hpp

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