Logo AND Algorithmique Numérique Distribuée

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