Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
the is_not_found field is defined for WIN32 only
[simgrid.git] / src / cxx / ApplicationHandler.cxx
1 /*
2  * ApplicationHandler.cxx
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
5  * All right reserved. 
6  *
7  * This program is free software; you can redistribute 
8  * it and/or modify it under the terms of the license 
9  *(GNU LGPL) which comes with this package. 
10  *
11  */
12  
13  /* ApplicationHandler member functions implementation.
14   */  
15
16
17 #include <Object.hpp>
18 #include <ApplicationHandler.hpp>
19
20 #include <Process.hpp>
21 #include <Host.hpp>
22
23
24
25
26
27 #include <stdlib.h>
28
29 #include <surf/surfxml_parse.h>
30 #include <xbt/sysdep.h>
31
32
33 namespace SimGrid
34 {
35         namespace Msg
36         {
37
38                 ApplicationHandler::ProcessFactory* ApplicationHandler::processFactory = NULL;
39                         
40                 // Desable the default constructor, the copy constructor , the assignement operator
41                 // and the destructor of this class. Assume that this class is static.
42                 
43                 // Default constructor.
44                 ApplicationHandler::ApplicationHandler()
45                 {
46                         // NOTHING TODO
47                 }
48                 
49                 // Copy constructor.
50                 ApplicationHandler::ApplicationHandler(const ApplicationHandler& rApplicationHandler)
51                 {
52                         // NOTHING TODO
53                 }
54                 
55                 // Destructor
56                 ApplicationHandler::~ApplicationHandler()
57                 {
58                         // NOTHING TODO
59                 }
60                 
61                 // Assignement operator.
62                 const ApplicationHandler& ApplicationHandler::operator = (const ApplicationHandler& rApplicationHandler)
63                 {
64                         return *this;
65                 }
66                 
67                 void ApplicationHandler::onStartDocument(void)
68                 {
69                         // instanciate the factory at the begining of the parsing
70                         processFactory = new ProcessFactory();  
71                 }
72                 
73                 void ApplicationHandler::onEndDocument(void)
74                 {
75                         // release the handler at the end of the parsing.
76                         if(processFactory)
77                                 delete processFactory;
78                 }
79                         
80                 void ApplicationHandler::onBeginProcess(void)
81                 {
82                         // set the process identity at the begin of the xml process element.
83                         processFactory->setProcessIdentity(A_surfxml_process_host, A_surfxml_process_function);
84                 }               
85                 
86                 void ApplicationHandler::onProcessArg(void)
87                 {
88                         // register the argument of the current xml process element.
89                         processFactory->registerProcessArg(A_surfxml_argument_value);
90                 }
91                 
92                 void ApplicationHandler::OnProperty(void)
93                 {
94                         // set the property of the current xml process element.
95                          processFactory->setProperty(A_surfxml_prop_id, A_surfxml_prop_value);
96                 }
97                 
98                 void ApplicationHandler::onEndProcess(void)
99                 {
100                         // at the end of the xml process element create the wrapper process (of the native Msg process)
101                         processFactory->createProcess();
102                 }
103                 
104                 /////////////////////////////////////////////////////////////////////////////////////////////////
105                 // Process factory connected member functions
106                 
107                 ApplicationHandler::ProcessFactory::ProcessFactory() 
108                 {
109                         this->args = xbt_dynar_new(sizeof(char*),ApplicationHandler::ProcessFactory::freeCstr);
110                         this->properties = NULL; // TODO instanciate the dictionary
111                         this->hostName = NULL;
112                         this->function = NULL;
113                 } 
114
115                 ApplicationHandler::ProcessFactory::~ProcessFactory()
116                 {
117                         xbt_dynar_free(&(this->args));
118                 }
119                 
120                 // create the cxx process wrapper.
121                 void ApplicationHandler::ProcessFactory::createProcess() 
122                 throw (ClassNotFoundException, HostNotFoundException)
123                 {
124                         Host host;
125                         Class* c;
126                         Process* process;
127                         
128                         // try to dynamicaly create an instance of the process from its name (which is specified by the element function
129                         // in the xml application file.
130                         // if this static method fails, it throws an exception of the class ClassNotFoundException
131                         c = Class::fromName(this->function);
132                         process = reinterpret_cast<Process*>(c->createObject());
133                         
134                         // try to retrieve the host of the process from its name
135                         // if this method fails, it throws an exception of the class HostNotFoundException
136                         host = Host::getByName(this->hostName); 
137                         
138                         // build the list of the arguments of the newly created process.
139                         int argc = xbt_dynar_length(this->args);
140                         
141                         char** argv = (char**)calloc(argc, sizeof(char*));
142                         
143                         for(int i = argc -1; i >= 0; i--)
144                                 xbt_dynar_pop(this->args, &(argv[i]));
145                         
146                         // finaly create the process (for more detail on the process creation see Process::create()
147                         process->create(host, this->function , argc, argv);
148                         
149                         // TODO add the properties of the process
150                         /*process->properties = this->properties;
151                         this->properties = new Properties();*/
152                 }
153                         
154                 void ApplicationHandler::ProcessFactory::setProcessIdentity(const char* hostName, const char* function) 
155                 {
156                         this->hostName = hostName;
157                         this->function = function;
158                 
159                         /*if (!this->args->empty())
160                                 this->args->clear();
161                 
162                         if(!this->properties->empty())
163                                 this->properties->clear();*/
164                 }
165                 
166                 // callback function used by the dynamic array to cleanup all of its elements.
167                 void ApplicationHandler::ProcessFactory::freeCstr(void* cstr)
168                 {
169                         free(*(void**)cstr);
170                 }
171                 
172                 void ApplicationHandler::ProcessFactory::registerProcessArg(const char* arg) 
173                 {
174                         char* cstr = _strdup(arg);
175                         xbt_dynar_push(this->args, &cstr);
176                 }
177                 
178                 void ApplicationHandler::ProcessFactory::setProperty(const char* id, const char* value)
179                 {
180                         // TODO implement this function;        
181                 }
182                 
183                 const char* ApplicationHandler::ProcessFactory::getHostName(void)
184                 {
185                         return this->hostName;
186                 }
187
188         } // namespace Msg
189 } // namespace SimGrid
190