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