UnitServiceBuildTask.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00047
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_ = ↦
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
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
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
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
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
00126 entity->addService (s);
00127
00128
00129 ServiceBuildTask<SERVICE_TYPE>::ao_->getCommunicationGateway()->registerService (s);
00130
00131 try {
00132
00133
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
00142 s->initialize();
00143 }
00144
00145 private:
00146 map<string, shared_ptr<Service> >* nameToServiceMap_;
00147 };
00148 }
00149 }
00150 }
00151
00152 #endif // _CONTROLUNITSERVICEBUILDTASK_HPP__
00153