Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some new files of the cxx version of the MSG API
[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                         // dynamic creation of a instance fo the process from its name (which is specified by the element function\r
89                         // in the xml application file.\r
90                         Process* process = (Process*)Class::fromName(this->function);\r
91                         \r
92                         // retrieve the host of the process from its name\r
93                         Host host = Host::getByName(this->hostName);\r
94                         \r
95                         // build the list of the arguments of the newly created process.\r
96                         int argc = xbt_dynar_length(this->args);\r
97                         \r
98                         char** argv = (char**)calloc(argc, sizeof(char*));\r
99                         \r
100                         for(int i = 0; i < argc; i++)\r
101                                 xbt_dynar_pop(this->args, &(argv[i]));\r
102                         \r
103                         // finaly create the process (for more detail on the process creation see Process::create()\r
104                         process->create(host, this->function , argc, argv);\r
105                         \r
106                         // TODO add the properties of the process\r
107                         /*process->properties = this->properties;\r
108                         this->properties = new Properties();*/\r
109                 }\r
110                         \r
111                 void ApplicationHandler::ProcessFactory::setProcessIdentity(const string& hostName, const string& function) \r
112                 {\r
113                         this->hostName = hostName;\r
114                         this->function = function;\r
115                 \r
116                         /*if (!this->args->empty())\r
117                                 this->args->clear();\r
118                 \r
119                         if(!this->properties->empty())\r
120                                 this->properties->clear();*/\r
121                 }\r
122                 \r
123                 // callback function used by the dynamic array to cleanup all of its elements.\r
124                 void ApplicationHandler::ProcessFactory::freeCstr(void* cstr)\r
125                 {\r
126                         free(*(void**)str);\r
127                 }\r
128                 \r
129                 void ApplicationHandler::ProcessFactory::registerProcessArg(const char* arg) \r
130                 {\r
131                         char* cstr = strdup(arg);\r
132                         xbt_dynar_push(this->args, &cstr);\r
133                 }\r
134                 \r
135                 void ApplicationHandler::ProcessFactory::setProperty(const char* id, const char* value)\r
136                 {\r
137                         // TODO implement this function;        \r
138                 }\r
139                 \r
140                 const const char* ApplicationHandler::ProcessFactory::getHostName(void)\r
141                 {\r
142                         return this->hostName;\r
143                 }\r
144 \r
145         } // namespace Msg\r
146 } // namespace SimGrid