Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use pointers instead references.
[simgrid.git] / src / cxx / Host.cxx
1 /*\r
2  * Host.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  /* Host class member functions implementation.\r
14   */ \r
15 \r
16 #include <Task.hpp>\r
17 #include <Process.hpp>\r
18 \r
19 #include <Host.hpp>\r
20 \r
21 #include <stdlib.h>\r
22 #include <stdio.h>\r
23 \r
24 #include <msg/msg.h>\r
25 #include <msg/private.h>\r
26 \r
27 #include <xbt/fifo.h>\r
28 \r
29 namespace SimGrid\r
30 {\r
31         namespace Msg\r
32         {\r
33                 Host::Host()\r
34                 {\r
35                         nativeHost = NULL;\r
36                         data = NULL;\r
37                 }\r
38                 \r
39                 Host::Host(const Host& rHost)\r
40                 {\r
41                         this->nativeHost = rHost.nativeHost;\r
42                         this->data = rHost.getData();   \r
43                 }\r
44                 \r
45                 Host::~Host()\r
46                 {\r
47                         // NOTHING TODO\r
48                 }\r
49                 \r
50                 \r
51                 Host& Host::getByName(const char* hostName)\r
52                 throw(HostNotFoundException, NullPointerException, BadAllocException)\r
53                 {\r
54                         // check the parameters\r
55                         if(!hostName)\r
56                                 throw NullPointerException("hostName");\r
57                                 \r
58                         m_host_t nativeHost = NULL;     // native host.\r
59                         Host* host = NULL;                      // wrapper host.\r
60                         \r
61                         if(!(nativeHost = MSG_get_host_by_name(hostName))) \r
62                                 throw HostNotFoundException(hostName);\r
63                         \r
64                         if(!nativeHost->data) \r
65                         { // native host not associated yet with  its wrapper\r
66                         \r
67                                 // instanciate a new wrapper \r
68                                 if(!(host = new Host()))\r
69                                         throw BadAllocException(hostName);\r
70                                 \r
71                                 host->nativeHost = nativeHost; \r
72                         \r
73                                 // the native host data field is set with its wrapper returned \r
74                                 nativeHost->data = (void*)host;\r
75                         }\r
76                         \r
77                         // return the reference to cxx wrapper\r
78                         return *((Host*)nativeHost->data);                              \r
79                 }\r
80                 \r
81                 int Host::getNumber(void)\r
82                 {\r
83                         return MSG_get_host_number();\r
84                 }\r
85                 \r
86                 Host& Host::currentHost(void)\r
87                 {\r
88                         Host* host = NULL;\r
89                         m_host_t nativeHost = MSG_host_self();\r
90                         \r
91                         if(!nativeHost->data) \r
92                         {\r
93                                 // the native host not yet associated with its wrapper\r
94                         \r
95                                 // instanciate a new wrapper\r
96                                 host = new Host();\r
97                         \r
98                                 host->nativeHost = nativeHost;\r
99                         \r
100                                 nativeHost->data = (void*)host;\r
101                         } \r
102                         else \r
103                         {\r
104                                 host = (Host*)nativeHost->data;\r
105                         }\r
106                         \r
107                         return *host;\r
108                 }\r
109                 \r
110                 void Host::all(Host*** hosts, int* len) \r
111                 throw(InvalidArgumentException, BadAllocException) \r
112                 {\r
113                         // check the parameters\r
114                         if(!hosts)\r
115                                 throw InvalidArgumentException("hosts");\r
116                                 \r
117                         if(len < 0)\r
118                                 throw InvalidArgumentException("len parameter must be positive");\r
119                         \r
120                         int count = xbt_fifo_size(msg_global->host);\r
121                         \r
122                         if(*len < count)\r
123                                 throw InvalidArgumentException("len parameter must be more than the number of installed host\n (use Host::getNumber() to get the number of hosts)");\r
124                         \r
125                         int index;\r
126                         m_host_t nativeHost;\r
127                         Host* host;\r
128                 \r
129                         m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);\r
130                         \r
131                         for(index = 0; index < count; index++) \r
132                         {\r
133                                 nativeHost = table[index];\r
134                                 host = (Host*)(nativeHost->data);\r
135                         \r
136                                 if(!host) \r
137                                 {\r
138                                         if(!(host = new Host()))\r
139                                         {\r
140                                                 // release all allocated memory.\r
141                                                 for(int i = 0; i < index; i++)\r
142                                                         delete (*(hosts)[i]);\r
143                                                 \r
144                                                 throw BadAllocException("to fill the table of the hosts installed on your platform");\r
145                                         }\r
146                                         \r
147                                         host->nativeHost = nativeHost;\r
148                                         nativeHost->data = (void*)host;\r
149                                 }\r
150                                 \r
151                                 (*hosts)[index] = host;\r
152                   }\r
153                 \r
154                   *len = count;  \r
155                 }\r
156                 \r
157                 const char* Host::getName(void) const\r
158                 {\r
159                         return nativeHost->name;\r
160                 }\r
161                 \r
162                 void Host::setData(void* data)\r
163                 {\r
164                         this->data = data;\r
165                 }\r
166                 \r
167                 void* Host::getData(void) const\r
168                 {\r
169                         return this->data;\r
170                 }\r
171                 \r
172                 int Host::getRunningTaskNumber(void) const\r
173                 {\r
174                         return MSG_get_host_msgload(nativeHost); \r
175                 }\r
176                 \r
177                 double Host::getSpeed(void) const\r
178                 {\r
179                         return MSG_get_host_speed(nativeHost);\r
180                 }\r
181                 \r
182                 bool Host::hasData(void) const\r
183                 {\r
184                         return (NULL != this->data);\r
185                 }\r
186                 \r
187                 int Host::isAvailable(void) const\r
188                 {\r
189                         return SIMIX_host_get_state(nativeHost->simdata->smx_host);\r
190                 }\r
191                 \r
192                 void Host::put(int channel, Task* task) \r
193                 throw(MsgException, InvalidArgumentException)\r
194                 {\r
195                         // checks the parameters\r
196                         if(channel < 0)\r
197                                 throw InvalidArgumentException("channel (must be more or equal to zero)");\r
198                                 \r
199                         if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, nativeHost, channel , -1.0))\r
200                                 throw MsgException("MSG_task_put_with_timeout() failed");\r
201                 } \r
202 \r
203                 void Host::put(int channel, Task* task, double timeout) \r
204                 throw(MsgException, InvalidArgumentException) \r
205                 {\r
206                         // checks the parameters\r
207                         if(channel < 0)\r
208                                 throw InvalidArgumentException("channel (must be more or equal to zero)");\r
209                                 \r
210                         if(timeout < 0.0 && timeout != -1.0)\r
211                                 throw InvalidArgumentException("timeout (must be more or equal to zero or equal to -1.0)");     \r
212                                 \r
213                                 \r
214                     if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, nativeHost, channel , timeout))\r
215                                 throw MsgException("MSG_task_put_with_timeout() failed");\r
216                 }\r
217                 \r
218                 void Host::putBounded(int channel, Task* task, double maxRate) \r
219                 throw(MsgException, InvalidArgumentException)\r
220                 {\r
221                     // checks the parameters\r
222                         if(channel < 0)\r
223                                 throw InvalidArgumentException("channel (must be more or equal to zero)");\r
224                                 \r
225                         if(maxRate < 0.0 && maxRate != -1.0)\r
226                                 throw InvalidArgumentException("maxRate (must be more or equal to zero or equal to -1.0)");     \r
227                     \r
228                         if(MSG_OK != MSG_task_put_bounded(task->nativeTask, nativeHost, channel, maxRate))\r
229                                 throw MsgException("MSG_task_put_bounded() failed");\r
230                 }\r
231                 \r
232                 void Host::send(Task* task) \r
233                 throw(MsgException, BadAllocException)  \r
234                 {       \r
235                         MSG_error_t rv;\r
236                         \r
237                         char* alias = (char*)calloc(strlen(this->getName())+ strlen(Process::currentProcess().getName()) + 2, sizeof(char));\r
238                                 \r
239                         if(!alias)\r
240                                 throw BadAllocException("alias");\r
241                                 \r
242                         sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());\r
243                                 \r
244                         rv = MSG_task_send_with_timeout(task->nativeTask, alias, -1.0);\r
245                         \r
246                         free(alias);\r
247                         \r
248                         if(MSG_OK != rv)\r
249                                 throw MsgException("MSG_task_send_with_timeout() failed");\r
250                 } \r
251                 \r
252                 void Host::send(const char* alias, Task* task) \r
253                 throw(InvalidArgumentException, MsgException) \r
254                 {\r
255                         // check the parameters\r
256                         if(!alias)\r
257                                 throw InvalidArgumentException("alias (must not be NULL)");\r
258                         \r
259                         if(MSG_OK != MSG_task_send_with_timeout(task->nativeTask, alias, -1.0))\r
260                                 throw MsgException("MSG_task_send_with_timeout() failed");\r
261                 }\r
262                 \r
263                 void Host::send(Task* task, double timeout) \r
264                 throw(InvalidArgumentException, BadAllocException, MsgException) \r
265                 {\r
266                         // check the parameters\r
267                         if(timeout < 0 && timeout != -1.0)\r
268                                 throw InvalidArgumentException("timeout (must be positive or equal to zero or equal to -1.0)");\r
269                         \r
270                         MSG_error_t rv;\r
271                         \r
272                         char* alias = (char*)calloc(strlen(this->getName()) + strlen(Process::currentProcess().getName()) + 2, sizeof(char));\r
273                                 \r
274                         if(!alias)\r
275                                 throw BadAllocException("alias");\r
276                                 \r
277                         sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());\r
278                                 \r
279                                 \r
280                         rv = MSG_task_send_with_timeout(task->nativeTask, alias, timeout);\r
281                         \r
282                         free(alias);\r
283                         \r
284                         if(MSG_OK != rv)\r
285                                 throw MsgException("MSG_task_send_with_timeout() failed");\r
286                 }\r
287                 \r
288                 void Host::send(const char* alias, Task* task, double timeout) \r
289                 throw(InvalidArgumentException, MsgException) \r
290                 {\r
291                         // check the parameter\r
292                         \r
293                         if(!alias)\r
294                                 throw InvalidArgumentException("alias (must not be NULL)");\r
295                                 \r
296                         if(timeout < 0 && timeout != -1.0)\r
297                                 throw InvalidArgumentException("timeout (must be positive or equal to zero or equal to -1.0)");\r
298                                         \r
299                         if(MSG_OK != MSG_task_send_with_timeout(task->nativeTask, alias, timeout))\r
300                                 throw MsgException("MSG_task_send_with_timeout() failed");\r
301                 }\r
302                 \r
303                 \r
304                 void Host::sendBounded(Task* task, double maxRate) \r
305                 throw(InvalidArgumentException, BadAllocException, MsgException) \r
306                 {\r
307                         if(maxRate < 0 && maxRate != -1.0)\r
308                                 throw InvalidArgumentException("maxRate (must be positive or equal to zero or equal to -1.0)");\r
309                         \r
310                         MSG_error_t rv;\r
311                         \r
312                         char* alias = (char*)calloc(strlen(this->getName()) + strlen(Process::currentProcess().getName()) + 2, sizeof(char));\r
313                         \r
314                         if(!alias)\r
315                                 throw BadAllocException("alias");\r
316                                 \r
317                         sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());\r
318                                 \r
319                         rv = MSG_task_send_bounded(task->nativeTask, alias, maxRate);\r
320                         \r
321                         free(alias);\r
322                         \r
323                         if(MSG_OK != rv)\r
324                                 throw MsgException("MSG_task_send_bounded() failed");\r
325                 }  \r
326                 \r
327                 void Host::sendBounded(const char* alias, Task* task, double maxRate) \r
328                 throw(InvalidArgumentException, MsgException) \r
329                 {\r
330                         // check the parameters\r
331                         if(!alias)\r
332                                 throw InvalidArgumentException("alias (must not be NULL)");\r
333                         \r
334                         if(maxRate < 0 && maxRate != -1)\r
335                                 throw InvalidArgumentException("maxRate (must be positive or equal to zero or equal to -1.0)");\r
336                         \r
337                         if(MSG_OK != MSG_task_send_bounded(task->nativeTask, alias, maxRate))\r
338                                 throw MsgException("MSG_task_send_bounded() failed");\r
339                         \r
340                 }\r
341         } // namspace Msg\r
342 } // namespace SimGrid\r
343 \r