4 * Copyright 2006,2007 Martin Quinson, Malek Cherier
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.
13 /* Host class member functions implementation.
16 #include <MsgTask.hpp>
17 #include <MsgProcess.hpp>
19 #include <MsgHost.hpp>
25 #include <msg/private.h>
39 Host::Host(const Host& rHost)
41 this->nativeHost = rHost.nativeHost;
42 this->data = rHost.getData();
51 Host& Host::getByName(const char* hostName)
52 throw(HostNotFoundException, NullPointerException, BadAllocException)
54 // check the parameters
56 throw NullPointerException("hostName");
58 m_host_t nativeHost = NULL; // native host.
59 Host* host = NULL; // wrapper host.
61 if(!(nativeHost = MSG_get_host_by_name(hostName)))
62 throw HostNotFoundException(hostName);
65 { // native host not associated yet with its wrapper
67 // instanciate a new wrapper
68 if(!(host = new Host()))
69 throw BadAllocException(hostName);
71 host->nativeHost = nativeHost;
73 // the native host data field is set with its wrapper returned
74 nativeHost->data = (void*)host;
77 // return the reference to cxx wrapper
78 return *((Host*)nativeHost->data);
81 int Host::getNumber(void)
83 return MSG_get_host_number();
86 Host& Host::currentHost(void)
89 m_host_t nativeHost = MSG_host_self();
93 // the native host not yet associated with its wrapper
95 // instanciate a new wrapper
98 host->nativeHost = nativeHost;
100 nativeHost->data = (void*)host;
104 host = (Host*)nativeHost->data;
110 void Host::all(Host*** hosts, int* len)
111 throw(InvalidArgumentException, BadAllocException)
113 // check the parameters
115 throw InvalidArgumentException("hosts");
118 throw InvalidArgumentException("len parameter must be positive");
120 int count = xbt_fifo_size(msg_global->host);
123 throw InvalidArgumentException("len parameter must be more than the number of installed host\n (use Host::getNumber() to get the number of hosts)");
129 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
131 for(index = 0; index < count; index++)
133 nativeHost = table[index];
134 host = (Host*)(nativeHost->data);
138 if(!(host = new Host()))
140 // release all allocated memory.
141 for(int i = 0; i < index; i++)
142 delete (*(hosts)[i]);
144 throw BadAllocException("to fill the table of the hosts installed on your platform");
147 host->nativeHost = nativeHost;
148 nativeHost->data = (void*)host;
151 (*hosts)[index] = host;
157 const char* Host::getName(void) const
159 return nativeHost->name;
162 void Host::setData(void* data)
167 void* Host::getData(void) const
172 int Host::getRunningTaskNumber(void) const
174 return MSG_get_host_msgload(nativeHost);
177 double Host::getSpeed(void) const
179 return MSG_get_host_speed(nativeHost);
182 bool Host::hasData(void) const
184 return (NULL != this->data);
187 int Host::isAvailable(void) const
189 return SIMIX_host_get_state(nativeHost->simdata->smx_host);
192 void Host::put(int channel, Task* task)
193 throw(MsgException, InvalidArgumentException)
195 // checks the parameters
197 throw InvalidArgumentException("channel (must be more or equal to zero)");
199 if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, nativeHost, channel , -1.0))
200 throw MsgException("MSG_task_put_with_timeout() failed");
203 void Host::put(int channel, Task* task, double timeout)
204 throw(MsgException, InvalidArgumentException)
206 // checks the parameters
208 throw InvalidArgumentException("channel (must be more or equal to zero)");
210 if(timeout < 0.0 && timeout != -1.0)
211 throw InvalidArgumentException("timeout (must be more or equal to zero or equal to -1.0)");
214 if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, nativeHost, channel , timeout))
215 throw MsgException("MSG_task_put_with_timeout() failed");
218 void Host::putBounded(int channel, Task* task, double maxRate)
219 throw(MsgException, InvalidArgumentException)
221 // checks the parameters
223 throw InvalidArgumentException("channel (must be more or equal to zero)");
225 if(maxRate < 0.0 && maxRate != -1.0)
226 throw InvalidArgumentException("maxRate (must be more or equal to zero or equal to -1.0)");
228 if(MSG_OK != MSG_task_put_bounded(task->nativeTask, nativeHost, channel, maxRate))
229 throw MsgException("MSG_task_put_bounded() failed");
232 void Host::send(Task* task)
233 throw(MsgException, BadAllocException)
237 char* alias = (char*)calloc(strlen(this->getName())+ strlen(Process::currentProcess().getName()) + 2, sizeof(char));
240 throw BadAllocException("alias");
242 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
244 rv = MSG_task_send_with_timeout(task->nativeTask, alias, -1.0);
249 throw MsgException("MSG_task_send_with_timeout() failed");
252 void Host::send(const char* alias, Task* task)
253 throw(InvalidArgumentException, MsgException)
255 // check the parameters
257 throw InvalidArgumentException("alias (must not be NULL)");
259 if(MSG_OK != MSG_task_send_with_timeout(task->nativeTask, alias, -1.0))
260 throw MsgException("MSG_task_send_with_timeout() failed");
263 void Host::send(Task* task, double timeout)
264 throw(InvalidArgumentException, BadAllocException, MsgException)
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)");
272 char* alias = (char*)calloc(strlen(this->getName()) + strlen(Process::currentProcess().getName()) + 2, sizeof(char));
275 throw BadAllocException("alias");
277 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
280 rv = MSG_task_send_with_timeout(task->nativeTask, alias, timeout);
285 throw MsgException("MSG_task_send_with_timeout() failed");
288 void Host::send(const char* alias, Task* task, double timeout)
289 throw(InvalidArgumentException, MsgException)
291 // check the parameter
294 throw InvalidArgumentException("alias (must not be NULL)");
296 if(timeout < 0 && timeout != -1.0)
297 throw InvalidArgumentException("timeout (must be positive or equal to zero or equal to -1.0)");
299 if(MSG_OK != MSG_task_send_with_timeout(task->nativeTask, alias, timeout))
300 throw MsgException("MSG_task_send_with_timeout() failed");
304 void Host::sendBounded(Task* task, double maxRate)
305 throw(InvalidArgumentException, BadAllocException, MsgException)
307 if(maxRate < 0 && maxRate != -1.0)
308 throw InvalidArgumentException("maxRate (must be positive or equal to zero or equal to -1.0)");
312 char* alias = (char*)calloc(strlen(this->getName()) + strlen(Process::currentProcess().getName()) + 2, sizeof(char));
315 throw BadAllocException("alias");
317 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
319 rv = MSG_task_send_bounded(task->nativeTask, alias, maxRate);
324 throw MsgException("MSG_task_send_bounded() failed");
327 void Host::sendBounded(const char* alias, Task* task, double maxRate)
328 throw(InvalidArgumentException, MsgException)
330 // check the parameters
332 throw InvalidArgumentException("alias (must not be NULL)");
334 if(maxRate < 0 && maxRate != -1)
335 throw InvalidArgumentException("maxRate (must be positive or equal to zero or equal to -1.0)");
337 if(MSG_OK != MSG_task_send_bounded(task->nativeTask, alias, maxRate))
338 throw MsgException("MSG_task_send_bounded() failed");
342 } // namespace SimGrid