Logo AND Algorithmique Numérique Distribuée

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