Logo AND Algorithmique Numérique Distribuée

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