ServiceBuildTask.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00074
00075
00076
00077
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
00096 shared_ptr<Service> s = shared_ptr<Service> (new SERVICE_TYPE (ao_, entity, serviceInstanceDescription, serviceConfiguration_));
00097
00098
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
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
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
00129 entity->addService (s);
00130
00131
00132 ao_->getCommunicationGateway()->registerService (s);
00133
00134 try {
00135
00136
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
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 };
00158 }
00159 }
00160 }
00161
00162
00163
00164 #endif // __SERVICE_SERVICEBUILDTASK_H