Logo AND Algorithmique Numérique Distribuée

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