runTestService.cpp

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 // Autogenerated by MermaidLoaderGenerator
00024 
00025 //system includes
00026 #include <iostream>
00027 #include <vector>
00028 
00029 #include <signal.h>
00030 #include <argp.h>
00031 
00032 //MeRMaID includes
00033 #include <ActiveObject.hpp>
00034 #include <ActiveObjectAce.hpp>
00035 #include <ActiveObjectManager.hpp>
00036 #include <ActiveObjectManagerAce.hpp>
00037 #include <CommunicationGateway.hpp>
00038 
00039 
00040 #include <DataFactory.hpp>
00041 #include <EntityDescriptionFactory.hpp>
00042 #include <EntityDescriptionRepository.hpp>
00043 #include <EntityRepository.hpp>
00044 #include <FilePathSearch.hpp>
00045 #include <Service.hpp>
00046 #include <ServiceBuildTask.hpp>
00047 #include <ServiceConfiguration.hpp>
00048 #include <ServiceTypeDescriptionFactory.hpp>
00049 #include <ServiceTypeDescriptionRepository.hpp>
00050 #include <Time.hpp>
00051 #include <XmlDocument.hpp>
00052 #include <XmlElement.hpp>
00053 
00054 // Service Includes
00055 #include "TestService.hpp"
00056 
00057 //system using directives
00058 using std::cout;
00059 using std::endl;
00060 
00061 //mermaid using directives
00062 using mermaid::support::activeobject::ActiveObject;
00063 using mermaid::support::activeobject::ActiveObjectAce;
00064 using mermaid::support::activeobject::ActiveObjectManager;
00065 using mermaid::support::activeobject::ActiveObjectManagerAce;
00066 using mermaid::support::activeobject::Task;
00067 using mermaid::support::communication::CommunicationGateway;
00068 using mermaid::support::data::DataFactory;
00069 using boost::shared_ptr;
00070 
00071 using mermaid::support::service::EntityDescriptionFactory;
00072 using mermaid::support::service::EntityDescriptionRepository;
00073 using mermaid::support::service::EntityRepository;
00074 using mermaid::support::service::Service;
00075 using mermaid::support::service::ServiceBuildTask;
00076 using mermaid::support::service::ServiceConfiguration;
00077 using mermaid::support::service::ServiceTypeDescriptionFactory;
00078 using mermaid::support::service::ServiceTypeDescriptionRepository;
00079 using mermaid::support::service::ServiceRequest;
00080 using mermaid::support::system::Time;
00081 using mermaid::support::system::FilePathSearch;
00082 using mermaid::support::xml::XmlDocument;
00083 using mermaid::support::xml::XmlElement;
00084 
00085 // Service uses
00086 
00087 
00088 std::vector<std::string> dataDescriptionVector;
00089 std::vector<std::string> entityDescriptionVector;
00090 std::vector<std::string> pathVector;
00091 std::vector<std::string> serviceDescriptionVector;
00092 
00093 shared_ptr<ActiveObject> ioActiveObject;
00094 
00095 void exit_program (int sig)
00096 {
00097   //disable signal
00098   //signal(SIGINT, SIG_IGN);
00099   
00100   std::cerr << "Catched SIGINT..." << std::endl;
00101   std::cerr << "\tnumber of running Active Objects: " << ActiveObjectManagerAce::getInstance()->getNumberOfActiveObjects() << std::endl;
00102   std::cerr << "\tshutting down ActiveObjects..." << std::endl;
00103   ActiveObjectManagerAce::getInstance()->stopAllActiveObjects();
00104   std::cerr << "\tnumber of running Active Objects: " << ActiveObjectManagerAce::getInstance()->getNumberOfActiveObjects() << std::endl;
00105   std::cerr << "\tShutdown procedure finished." << std::endl;
00106 }
00107 
00108 static error_t parse_opt (int key, char *arg, struct argp_state *state)
00109 {
00110   //struct arguments *arguments = state->input;
00111   
00112   switch (key) {
00113     case 'd': {
00114       /*std::cout << "Loading data type description file specified in command-line: " << arg << std::endl;
00115       shared_ptr<XmlDocument>  dataDoc = shared_ptr<XmlDocument>(new XmlDocument());
00116       dataDoc->parseFile(std::string(arg));
00117       DataFactory::loadDataDescription(dataDoc);*/
00118       dataDescriptionVector.push_back (std::string (arg));
00119       break;
00120     }
00121     case 'e': {
00122       /*std::cout << "Loading entity description file specified in command-line: " << arg << std::endl;
00123       shared_ptr<XmlDocument> entDoc = shared_ptr<XmlDocument>(new XmlDocument());
00124       entDoc->parseFile(std::string(arg));
00125       shared_ptr<XmlElement> entityDescriptionList = entDoc->getRootElement();
00126       shared_ptr<ServiceTypeDescriptionRepository> serviceTypeDescriptionRepository = ServiceTypeDescriptionRepository::getInstance();
00127       shared_ptr<EntityDescriptionRepository> entityDescriptionRepository = EntityDescriptionRepository::getInstance();
00128       EntityDescriptionFactory::populateEntityDescriptionRepository(entityDescriptionRepository, entityDescriptionList, serviceTypeDescriptionRepository);*/
00129       entityDescriptionVector.push_back (std::string (arg));
00130       break;
00131     }
00132     case 'p': {
00133       /*std::cout << "Adding search path: " << arg << std::endl;
00134       FilePathSearch::addSearchPath(std::string(arg));*/
00135       pathVector.push_back (std::string (arg));
00136       break;
00137     }
00138     case 's': {
00139       /*std::cout << "Loading service type description file specified in command-line: " << arg << std::endl;
00140       shared_ptr<XmlDocument> docServiceType = shared_ptr<XmlDocument>(new XmlDocument());
00141         docServiceType->parseFile(std::string(arg));
00142         shared_ptr<XmlElement> serviceTypeDescriptionList = docServiceType->getRootElement();
00143         shared_ptr<ServiceTypeDescriptionRepository> serviceTypeDescriptionRepository = ServiceTypeDescriptionRepository::getInstance();
00144         ServiceTypeDescriptionFactory::populateServiceTypeDescriptionRepository(serviceTypeDescriptionRepository, serviceTypeDescriptionList);*/
00145       serviceDescriptionVector.push_back (std::string (arg));
00146       break;
00147     }
00148     default:
00149       //std::cout << "parse_opt : default" << std::endl;
00150       return ARGP_ERR_UNKNOWN;
00151   }
00152   return 0;
00153 }; // parse_opt()
00154 
00155 const char *argp_program_version = "Built with mlgen (MeRMaID Loader Generator)";
00156 const char *argp_program_bug_address = "<mafb@isr.ist.utl.pt>";
00157 
00158 int main (int argc, char *argv[])
00159 {
00160   signal (SIGINT, &exit_program);
00161   
00162   static struct argp_option options[] = {
00163     {"entity-description", 'e', "file", 0, "Define Entity Description file to load"},
00164     {"service-type-description", 's', "file", 0, "Define Service Type Description file to load"},
00165     {"data-type-description", 'd', "file", 0, "Define Data Type Description file to load"},
00166     {"search-path", 'p', "path", 0, "Define search path where to look for files"},
00167     {0}
00168   };
00169   
00170   //static char args_doc[] = 0;
00171   
00172   static char doc[] = "Deploys MeRMaID services";
00173   
00174   static struct argp argp = {options, parse_opt, 0, doc};
00175   
00176   
00177   // parse command line arguments/options
00178   argp_parse (&argp, argc, argv, 0, 0, 0);
00179   
00180   //Setting file search paths
00181   pathVector.push_back ("/opt/mermaid/config");
00182   pathVector.push_back ("./");
00183   pathVector.push_back ("./"); //Default file search path
00184   
00185   // Create ActiveObjects
00186   ioActiveObject = ActiveObjectManagerAce::getInstance()->createActiveObject();
00187   
00188   // ActiveObjects start
00189   ioActiveObject->start();
00190   
00191   
00192   // Wait for ActiveObjects to stop
00193   ActiveObjectManagerAce::getInstance()->waitForActiveObjectsToStop();
00194   
00195   return 0;
00196 }; // main
00197 
00198 void setupServiceA (char dataDescFile[], char serviceTypeDescFile[], char entityDescFile[], char configFile[], char serviceName[])
00199 {
00200   //Load data description files
00201   //Loading default data description files
00202   dataDescriptionVector.push_back ("ping-tool-data-description-file.xml");
00203   //loading io-test-read-data-description-file.xml
00204   dataDescriptionVector.push_back (dataDescFile);
00205   
00206   //Load service type description files
00207   
00208   //Loading default service type description files
00209   serviceDescriptionVector.push_back ("ping-tool-service-type-description-file.xml");
00210   //loading io-test-read-service-type-description-file.xml
00211   serviceDescriptionVector.push_back (serviceTypeDescFile);
00212   
00213   //Load entity description files
00214   //loading io-test-read-entity-description-file.xml
00215   entityDescriptionVector.push_back (entityDescFile);   //setup search paths
00216   std::vector<std::string>::iterator pathIterator;
00217   for (pathIterator = pathVector.begin(); pathIterator != pathVector.end(); ++pathIterator) {
00218     std::string path = *pathIterator;
00219     std::cerr << "Loading path: " << path << std::endl;
00220     FilePathSearch::addSearchPath (path);
00221   }
00222   //load data descriptions
00223   std::vector<std::string>::iterator dataDescriptionIterator;
00224   for (dataDescriptionIterator = dataDescriptionVector.begin(); dataDescriptionIterator != dataDescriptionVector.end(); ++dataDescriptionIterator) {
00225     std::string dataDescriptionFile = *dataDescriptionIterator;
00226     std::cerr << "Loading data type description file: " << dataDescriptionFile << std::endl;
00227     shared_ptr<XmlDocument>  dataDoc = shared_ptr<XmlDocument> (new XmlDocument());
00228     dataDoc->parseFile (dataDescriptionFile);
00229     DataFactory::loadDataDescription (dataDoc);
00230   }
00231   //load service type descriptions
00232   std::vector<std::string>::iterator serviceDescriptionIterator;
00233   for (serviceDescriptionIterator = serviceDescriptionVector.begin(); serviceDescriptionIterator != serviceDescriptionVector.end(); ++serviceDescriptionIterator) {
00234     std::string serviceDescriptionFile = *serviceDescriptionIterator;
00235     std::cerr << "Loading service type description file: " << serviceDescriptionFile << std::endl;
00236     shared_ptr<XmlDocument> docServiceType = shared_ptr<XmlDocument> (new XmlDocument());
00237     docServiceType->parseFile (serviceDescriptionFile);
00238     shared_ptr<XmlElement> serviceTypeDescriptionList = docServiceType->getRootElement();
00239     shared_ptr<ServiceTypeDescriptionRepository> serviceTypeDescriptionRepository = ServiceTypeDescriptionRepository::getInstance();
00240     ServiceTypeDescriptionFactory::populateServiceTypeDescriptionRepository (serviceTypeDescriptionRepository, serviceTypeDescriptionList);
00241   }
00242   //load entity descriptions
00243   std::vector<std::string>::iterator entityDescriptionIterator;
00244   for (entityDescriptionIterator = entityDescriptionVector.begin(); entityDescriptionIterator != entityDescriptionVector.end(); ++entityDescriptionIterator) {
00245     std::string entityDescriptionFile = *entityDescriptionIterator;
00246     std::cerr << "Loading entity type description file: " << entityDescriptionFile << std::endl;
00247     shared_ptr<XmlDocument> docEntityDescription = shared_ptr<XmlDocument> (new XmlDocument());
00248     docEntityDescription->parseFile (entityDescriptionFile);
00249     shared_ptr<XmlElement> entityDescriptionList = docEntityDescription->getRootElement();
00250     shared_ptr<EntityDescriptionRepository> entityDescriptionRepository = EntityDescriptionRepository::getInstance();
00251     shared_ptr<ServiceTypeDescriptionRepository> serviceTypeDescriptionRepository = ServiceTypeDescriptionRepository::getInstance();
00252     EntityDescriptionFactory::populateEntityDescriptionRepository (entityDescriptionRepository, entityDescriptionList, serviceTypeDescriptionRepository);
00253   }
00254   
00255   // Create entity repository
00256   shared_ptr<EntityRepository> aoEntityRepository (new EntityRepository());
00257   
00258   // Create Services
00259   {
00260     shared_ptr<XmlDocument> serviceConfigurationDocument (new XmlDocument());
00261     serviceConfigurationDocument->parseFile (configFile);
00262     shared_ptr<ServiceConfiguration> serviceConfiguration (new ServiceConfiguration (serviceConfigurationDocument));
00263     shared_ptr<Task> buildTestService ( (Task*) new ServiceBuildTask<TestService> (ioActiveObject, aoEntityRepository, "MyComputer", serviceName, serviceConfiguration));
00264     ioActiveObject->addTask (buildTestService);
00265   }
00266 }
00267 // EOF
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3