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 <InvalidArgumentException.hpp>
\r
18 #include <BadAllocException.hpp>
\r
19 #include <HostNotFoundException.hpp>
\r
20 #include <MsgException.hpp>
\r
23 #include <Process.hpp>
\r
28 #include <msg/msg.h>
\r
29 #include <msg/private.h>
\r
31 #include <xbt/fifo.h>
\r
43 Host::Host(const Host& rHost)
\r
45 this->nativeHost = rHost.nativeHost;
\r
46 this->data = rHost.getData();
\r
55 Host& Host::getByName(const char* hostName)
\r
56 throw(HostNotFoundException, InvalidArgumentException, BadAllocException)
\r
58 // check the parameters
\r
60 throw InvalidArgumentException("hostName");
\r
62 m_host_t nativeHost = NULL; // native host.
\r
63 Host* host = NULL; // wrapper host.
\r
65 if(!(nativeHost = MSG_get_host_by_name(hostName)))
\r
66 throw HostNotFoundException(hostName);
\r
68 if(!nativeHost->data)
\r
69 { // native host not associated yet with its wrapper
\r
71 // instanciate a new wrapper
\r
72 if(!(host = new Host()))
\r
73 throw BadAllocException(hostName);
\r
75 host->nativeHost = nativeHost;
\r
77 // the native host data field is set with its wrapper returned
\r
78 nativeHost->data = (void*)host;
\r
81 // return the reference to cxx wrapper
\r
82 return *((Host*)nativeHost->data);
\r
85 int Host::getNumber(void)
\r
87 return MSG_get_host_number();
\r
90 Host& Host::currentHost(void)
\r
93 m_host_t nativeHost = MSG_host_self();
\r
95 if(!nativeHost->data)
\r
97 // the native host not yet associated with its wrapper
\r
99 // instanciate a new wrapper
\r
102 host->nativeHost = nativeHost;
\r
104 nativeHost->data = (void*)host;
\r
108 host = (Host*)nativeHost->data;
\r
114 void Host::all(Host*** hosts, int* len)
\r
115 throw(InvalidArgumentException, BadAllocException)
\r
117 // check the parameters
\r
119 throw InvalidArgumentException("hosts");
\r
122 throw InvalidArgumentException("len parameter must be positive");
\r
124 int count = xbt_fifo_size(msg_global->host);
\r
127 throw InvalidArgumentException("len parameter must be more than the number of installed host\n (use Host::getNumber() to get the number of hosts)");
\r
130 m_host_t nativeHost;
\r
133 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
\r
135 for(index = 0; index < count; index++)
\r
137 nativeHost = table[index];
\r
138 host = (Host*)(nativeHost->data);
\r
142 if(!(host = new Host()))
\r
144 // release all allocated memory.
\r
145 for(int i = 0; i < index; i++)
\r
146 delete (*(hosts)[i]);
\r
148 throw BadAllocException("to fill the table of the hosts installed on your platform");
\r
151 host->nativeHost = nativeHost;
\r
152 nativeHost->data = (void*)host;
\r
155 (*hosts)[index] = host;
\r
161 const char* Host::getName(void) const
\r
163 return nativeHost->name;
\r
166 void Host::setData(void* data)
\r
171 void* Host::getData(void) const
\r
176 int Host::getRunningTaskNumber(void) const
\r
178 return MSG_get_host_msgload(nativeHost);
\r
181 double Host::getSpeed(void) const
\r
183 return MSG_get_host_speed(nativeHost);
\r
186 bool Host::hasData(void) const
\r
188 return (NULL != this->data);
\r
191 int Host::isAvailable(void) const
\r
193 return SIMIX_host_get_state(nativeHost->simdata->smx_host);
\r
196 void Host::put(int channel, const Task& rTask)
\r
197 throw(MsgException, InvalidArgumentException)
\r
199 // checks the parameters
\r
201 throw InvalidArgumentException("channel (must be more or equal to zero)");
\r
203 if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , -1.0))
\r
204 throw MsgException("MSG_task_put_with_timeout() failed");
\r
207 void Host::put(int channel, Task* task)
\r
208 throw(MsgException, InvalidArgumentException)
\r
210 // checks the parameters
\r
212 throw InvalidArgumentException("channel (must be more or equal to zero)");
\r
214 if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, nativeHost, channel , -1.0))
\r
215 throw MsgException("MSG_task_put_with_timeout() failed");
\r
218 void Host::put(int channel, const Task& rTask, double timeout)
\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(timeout < 0.0 && timeout != -1.0)
\r
226 throw InvalidArgumentException("timeout (must be more or equal to zero or equal to -1.0)");
\r
229 if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , timeout))
\r
230 throw MsgException("MSG_task_put_with_timeout() failed");
\r
233 void Host::putBounded(int channel, const Task& rTask, double maxRate)
\r
234 throw(MsgException, InvalidArgumentException)
\r
236 // checks the parameters
\r
238 throw InvalidArgumentException("channel (must be more or equal to zero)");
\r
240 if(maxRate < 0.0 && maxRate != -1.0)
\r
241 throw InvalidArgumentException("maxRate (must be more or equal to zero or equal to -1.0)");
\r
243 if(MSG_OK != MSG_task_put_bounded(rTask.nativeTask, nativeHost, channel, maxRate))
\r
244 throw MsgException("MSG_task_put_bounded() failed");
\r
247 void Host::send(const Task& rTask)
\r
248 throw(MsgException)
\r
252 char* alias = (char*)calloc(strlen(this->getName())+ strlen(Process::currentProcess().getName()) + 2, sizeof(char));
\r
255 throw BadAllocException("alias");
\r
257 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
\r
259 rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0);
\r
264 throw MsgException("MSG_task_send_with_timeout() failed");
\r
267 void Host::send(const char* alias, const Task& rTask)
\r
268 throw(InvalidArgumentException, MsgException)
\r
270 // check the parameters
\r
272 throw InvalidArgumentException("alias (must not be NULL)");
\r
274 if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0))
\r
275 throw MsgException("MSG_task_send_with_timeout() failed");
\r
278 void Host::send(const Task& rTask, double timeout)
\r
279 throw(InvalidArgumentException, BadAllocException, MsgException)
\r
281 // check the parameters
\r
282 if(timeout < 0 && timeout != -1.0)
\r
283 throw InvalidArgumentException("timeout (must be positive or equal to zero or equal to -1.0)");
\r
287 char* alias = (char*)calloc(strlen(this->getName()) + strlen(Process::currentProcess().getName()) + 2, sizeof(char));
\r
290 throw BadAllocException("alias");
\r
292 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
\r
295 rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout);
\r
300 throw MsgException("MSG_task_send_with_timeout() failed");
\r
303 void Host::send(const char* alias, const Task& rTask, double timeout)
\r
304 throw(InvalidArgumentException, MsgException)
\r
306 // check the parameter
\r
309 throw InvalidArgumentException("alias (must not be NULL)");
\r
311 if(timeout < 0 && timeout != -1.0)
\r
312 throw InvalidArgumentException("timeout (must be positive or equal to zero or equal to -1.0)");
\r
314 if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout))
\r
315 throw MsgException("MSG_task_send_with_timeout() failed");
\r
319 void Host::sendBounded(const Task& rTask, double maxRate)
\r
320 throw(InvalidArgumentException, BadAllocException, MsgException)
\r
322 if(maxRate < 0 && maxRate != -1.0)
\r
323 throw InvalidArgumentException("maxRate (must be positive or equal to zero or equal to -1.0)");
\r
327 char* alias = (char*)calloc(strlen(this->getName()) + strlen(Process::currentProcess().getName()) + 2, sizeof(char));
\r
330 throw BadAllocException("alias");
\r
332 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
\r
334 rv = MSG_task_send_bounded(rTask.nativeTask, alias, maxRate);
\r
339 throw MsgException("MSG_task_send_bounded() failed");
\r
342 void Host::sendBounded(const char* alias, const Task& rTask, double maxRate)
\r
343 throw(InvalidArgumentException, MsgException)
\r
345 // check the parameters
\r
347 throw InvalidArgumentException("alias (must not be NULL)");
\r
349 if(maxRate < 0 && maxRate != -1)
\r
350 throw InvalidArgumentException("maxRate (must be positive or equal to zero or equal to -1.0)");
\r
352 if(MSG_OK != MSG_task_send_bounded(rTask.nativeTask, alias, maxRate))
\r
353 throw MsgException("MSG_task_send_bounded() failed");
\r
357 } // namespace SimGrid