Logo AND Algorithmique Numérique Distribuée

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