Logo AND Algorithmique Numérique Distribuée

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