Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add comment and exception mechanism to the Host class.
[simgrid.git] / src / cxx / Task.cxx
1 #include <Task.hpp>\r
2 \r
3 namespace msg\r
4 {\r
5         \r
6 Task::Task()\r
7 {\r
8         nativeTask = NULL;\r
9 }\r
10 \r
11 \r
12 Task::Task(const Task& rTask)\r
13 {\r
14 }\r
15 \r
16 \r
17 Task::~Task()\r
18 throw(NativeException)\r
19 {\r
20         if(NULL != nativeTask)\r
21         {\r
22                 if(MSG_OK != MSG_task_destroy(nativeTask))\r
23                 {\r
24                         // TODO throw NativeException\r
25                 }       \r
26         }\r
27 }\r
28 \r
29 \r
30 Task::Task(const char* name, double computeDuration, double messageSize)\r
31 {\r
32          \r
33         if(computeDuration < 0) \r
34         {\r
35                 // TODO throw exception\r
36                 return;\r
37         }\r
38         \r
39         if(messageSize < 0) \r
40         {\r
41                 // TODO throw exception\r
42                 return;\r
43         }\r
44         \r
45         if(!name) \r
46         {\r
47                 // TODO throw exception\r
48                 return;\r
49         }\r
50         \r
51         // create the task\r
52         nativeTask = MSG_task_create(name, computeDuration, messageSize, NULL);\r
53         \r
54         nativeTask->data = (void*)this;\r
55 }\r
56 \r
57 Task::Task(const char* name, Host* hosts, double* computeDurations, double* messageSizes)\r
58 {\r
59         // TODO parallel task create    \r
60 }\r
61 \r
62 \r
63 const char* Task::getName(void) const\r
64 {\r
65         return nativeTask->name;\r
66 }\r
67 \r
68 Process& Task::getSender(void) const\r
69 {\r
70         m_proccess_t nativeProcess = MSG_task_get_sender(nativeTask);\r
71         \r
72         return (*((Process*)(nativeProcess->data)));\r
73 }\r
74 \r
75 Host& Task::getSource(void) const \r
76 throw(NativeException)\r
77 {\r
78         m_host_t nativeHost = MSG_task_get_source(nativeTask);\r
79         \r
80         if(!nativeHost->data) \r
81         {\r
82         // TODO throw the exception\r
83         return NULL;\r
84         }\r
85         \r
86         return (*((Host*)(nativeHost->data)));  \r
87 }\r
88 \r
89 double Task::getComputeDuration(void) const\r
90 {\r
91         return MSG_task_get_compute_duration(nativeTask);\r
92 }\r
93 \r
94 double Task::getRemainingDuration(void) const\r
95 {\r
96         return MSG_task_get_remaining_computation(nativeTask);\r
97 }\r
98 \r
99 void Task::setPriority(double priority)\r
100 {\r
101         MSG_task_set_priority(nativeTask, priority);\r
102 }\r
103 \r
104 Task& Task::get(int channel) \r
105 throw(NativeException)\r
106 {\r
107         m_task_t nativeTask = NULL;\r
108         \r
109         \r
110         if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , -1.0, NULL)) \r
111         {\r
112                 // TODO throw the NativeException\r
113                 return NULL;\r
114         }\r
115         \r
116         return (*((Task*)(nativeTask->data)));\r
117 }\r
118 \r
119 Task& Task::get(int channel, const Host& rHost) \r
120 throw(NativeException)\r
121 {\r
122         m_task_t nativeTask = NULL;\r
123         \r
124         \r
125         if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , -1.0, rHost.nativeHost)) \r
126         {\r
127                 // TODO throw the NativeException\r
128                 return NULL;\r
129         }\r
130         \r
131         return (*((Task*)(nativeTask->data)));\r
132 }\r
133 \r
134 Task& Task::get(int channel, double timeout, const Host& rHost) \r
135 throw(NativeException)\r
136 {\r
137         m_task_t nativeTask = NULL;\r
138         \r
139         \r
140         if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , timeout, rHost.nativeHost)) \r
141         {\r
142                 // TODO throw the NativeException\r
143                 return NULL;\r
144         }\r
145         \r
146         return (*((Task*)(nativeTask->data)));\r
147 }\r
148 \r
149 bool static Task::probe(int channel)\r
150 {\r
151         return (bool)MSG_task_Iprobe(channel);\r
152 }\r
153 \r
154 int Task::probe(int channel, const Host& rHost)\r
155 {\r
156         return MSG_task_probe_from_host(chan_id,rHost.nativeHost);\r
157 }\r
158 \r
159 void Task::execute(void) \r
160 throw(NativeException)\r
161 {\r
162         if(MSG_OK != MSG_task_execute(nativeTask))\r
163         {\r
164                 // TODO throw NativeException\r
165         }       \r
166 }\r
167 \r
168 void Task::cancel(void) \r
169 throw(NativeException)\r
170 {\r
171         if(MSG_OK != MSG_task_cancel(nativeTask))\r
172         {\r
173                 // TODO throw NativeException\r
174         }       \r
175 }\r
176 \r
177 void Task::send(void) \r
178 throw(NativeException)\r
179 {\r
180         MSG_error_t rv;\r
181         \r
182         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
183         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
184                 \r
185                 \r
186 \r
187         rv = MSG_task_send_with_timeout(nativeTask, alias, -1.0);\r
188 \r
189         free(alias)\r
190 \r
191         if(MSG_OK != rv)\r
192         {\r
193                 // TODO throw the NativeException\r
194         }\r
195 }\r
196 \r
197 \r
198 void Task::send(const char* alias) \r
199 throw(NativeException)\r
200 {\r
201 \r
202         if(MSG_OK != MSG_task_send_with_timeout(nativeTask, alias, -1.0))\r
203         {\r
204                 // TODO throw the NativeException\r
205         }\r
206 }\r
207 \r
208 void Task::send(double timeout) \r
209 throw(NativeException)\r
210 {\r
211         MSG_error_t rv;\r
212         \r
213         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
214         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
215                 \r
216                 \r
217 \r
218         rv = MSG_task_send_with_timeout(nativeTask, alias, timeout);\r
219 \r
220         free(alias)\r
221 \r
222         if(MSG_OK != rv)\r
223         {\r
224                 // TODO throw the NativeException\r
225         }\r
226 }       \r
227 \r
228 void Task::send(const char* alias, double timeout) \r
229 throw(NativeException)\r
230 {\r
231         if(MSG_OK != MSG_task_send_with_timeout(nativeTask, alias, timeout))\r
232         {\r
233                 // TODO throw the NativeException\r
234         }\r
235 }\r
236 \r
237 void Task::sendBounded(double maxRate) \r
238 throw(NativeException)\r
239 {\r
240         MSG_error_t rv;\r
241         \r
242         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
243         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
244         \r
245         rv = MSG_task_send_bounded(nativeTask, alias, maxRate);\r
246         \r
247         free(alias);\r
248         \r
249         \r
250         \r
251         if(MSG_OK != rv)\r
252         {\r
253                 // TODO throw the NativeException\r
254         }\r
255 }\r
256 \r
257 void Task::sendBounded(const char* alias, double maxRate) \r
258 throw(NativeException)\r
259 {\r
260         if(MSG_OK != MSG_task_send_bounded(nativeTask, alias, maxRate))\r
261         {\r
262                 // TODO throw the NativeException\r
263         }\r
264 }\r
265 \r
266 Task& Task::receive(void) \r
267 throw(NativeException)\r
268 {\r
269         MSG_error_t rv;\r
270         \r
271         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
272         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
273                 \r
274         m_task_t nativeTask = NULL;\r
275         \r
276         rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL);      \r
277 \r
278         free(alias);\r
279         \r
280         if(MSG_OK != rv) \r
281         {\r
282                 // TODO thow NativeException\r
283                 return NULL;\r
284         }\r
285 \r
286         return (*((Task*)nativeTask->data));\r
287 }\r
288 \r
289 Task& Task::receive(const char* alias) \r
290 throw(NativeException)\r
291 {\r
292         m_task_t nativeTask = NULL;\r
293         \r
294         if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL)) \r
295         {\r
296                 // TODO thow NativeException\r
297                 return NULL;\r
298         }\r
299 \r
300         return (*((Task*)nativeTask->data));\r
301 }\r
302 \r
303 Task& Task::receive(const char* alias, double timeout) \r
304 throw(NativeException)\r
305 {\r
306         m_task_t nativeTask = NULL;\r
307         \r
308         if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, NULL)) \r
309         {\r
310                 // TODO thow NativeException\r
311                 return NULL;\r
312         }\r
313 \r
314         return (*((Task*)nativeTask->data));\r
315 }\r
316 \r
317 Task& Task::receive(const char* alias, const Host& rHost) \r
318 throw(NativeException)\r
319 {\r
320         m_task_t nativeTask = NULL;\r
321         \r
322         \r
323         if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost)) \r
324         {\r
325                 // TODO thow NativeException\r
326                 return NULL;\r
327         }\r
328 \r
329         return (*((Task*)nativeTask->data));\r
330 }       \r
331 \r
332 Task& Task::receive(const char* alias, double timeout, const Host& rHost) \r
333 throw(NativeException)\r
334 {\r
335         m_task_t nativeTask = NULL;\r
336         \r
337         \r
338         if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost)) \r
339         {\r
340                 // TODO thow NativeException\r
341                 return NULL;\r
342         }\r
343 \r
344         return (*((Task*)nativeTask->data));\r
345 }\r
346 \r
347 bool Task::listen(void) \r
348 throw(NativeException)\r
349 {\r
350         int rv;\r
351         \r
352         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
353         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
354                 \r
355         rv = MSG_task_listen(alias);\r
356         \r
357         free(alias);\r
358         \r
359         return (bool)rv;\r
360 }       \r
361 \r
362 bool Task::listen(const char* alias) \r
363 throw(NativeException)\r
364 {\r
365         return (bool)MSG_task_listen(alias);\r
366 }\r
367 \r
368 bool Task::listenFrom(void) \r
369 throw(NativeException)\r
370 {\r
371         int rv;\r
372         \r
373         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
374         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
375         \r
376         rv = MSG_task_listen_from(alias);\r
377         \r
378         free(alias);\r
379         \r
380         return  (int)rv;\r
381 }       \r
382 \r
383 bool Task::listenFrom(const char* alias) \r
384 throw(NativeException)\r
385 {\r
386         return (bool)MSG_task_listen_from(alias);\r
387         \r
388 }\r
389 \r
390 bool Task::listenFromHost(const Host& rHost) \r
391 throw(NativeException)\r
392 {\r
393         int rv;\r
394         \r
395         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
396         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
397         \r
398         rv = MSG_task_listen_from_host(alias, rHost.nativeHost);\r
399         \r
400         free(alias);\r
401         \r
402         return (bool)rv;\r
403 }\r
404         \r
405 bool Task::listenFromHost(const char* alias, const Host& rHost) \r
406 throw(NativeException)\r
407 {\r
408         return (bool)MSG_task_listen_from_host(alias, rHost.nativeHost);\r
409 }                                                                                                                                                                                                       \r
410 \r
411 }\r
412 \r