ControlUnitServiceBuildTask.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 <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
00046
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
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
00090 s->setNameToServiceMap (nameToServiceMap_);
00091
00092
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
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
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
00123 entity->addService (s);
00124
00125
00126 ServiceBuildTask<SERVICE_TYPE>::ao_->getCommunicationGateway()->registerService (s);
00127
00128 try {
00129
00130
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
00139 s->initialize();
00140 }
00141
00142 private:
00143 map<string, shared_ptr<Service> > nameToServiceMap_;
00144 };
00145 }
00146 }
00147 }
00148
00149 #endif // _CONTROLUNITSERVICEBUILDTASK_HPP__
00150