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
27 Host::Host(const Host& rHost)
\r
29 this->nativeHost = rHost.nativeHost;
\r
30 this->data = rHost.getData();
\r
39 Host& Host::getByName(const char* hostName)
\r
40 throw(HostNotFoundException, InvalidParameterException, BadAllocException)
\r
42 // check the parameters
\r
44 throw InvalidParmeterException("hostName");
\r
46 m_host_t nativeHost = NULL; // native host.
\r
47 Host* host = NULL; // wrapper host.
\r
49 if(!(nativeHost = MSG_get_host_by_name(hostName)))
\r
50 throw HostNotFoundException(hostName);
\r
52 if(!nativeHost->data)
\r
53 { // native host not associated yet with its wrapper
\r
55 // instanciate a new wrapper
\r
56 if(!(host = new Host())
\r
57 throw BadAllocException(hostName);
\r
59 host->nativeHost = nativeHost;
\r
61 // the native host data field is set with its wrapper returned
\r
62 nativeHost->data = (void*)host;
\r
65 // return the reference to cxx wrapper
\r
66 return *((Host*)nativeHost->data);
\r
69 int Host::getNumber(void)
\r
71 return MSG_get_host_number();
\r
74 Host& Host::currentHost(void)
\r
77 m_host_t nativeHost = MSG_host_self();
\r
79 if(!nativeHost->data)
\r
81 // the native host not yet associated with its wrapper
\r
83 // instanciate a new wrapper
\r
86 host->nativeHost = nativeHost;
\r
88 nativeHost->data = (void*)host;
\r
92 host = (Host*)nativeHost->data;
\r
98 void Host::all(Host*** hosts, int* len)
\r
99 throw(InvalidParameterException, BadAllocException)
\r
101 /* check the parameters */
\r
103 throw InvalidParameterException("hosts");
\r
106 throw InvalidParameterException("len parameter must be positive");
\r
108 int count = xbt_fifo_size(msg_global->host);
\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
114 m_host_t nativeHost;
\r
117 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
\r
119 for(index = 0; index < count; index++)
\r
121 nativeHost = table[index];
\r
122 host = (Host*)(nativeHost->data);
\r
126 if(!(host = new Host())
\r
128 // release all allocated memory.
\r
129 for(int i = 0; i < index; i++)
\r
130 delete (*(hosts)[i]);
\r
132 throw BadAllocException("to fill the table of the hosts installed on your platform");
\r
135 host->nativeHost = nativeHost;
\r
136 nativeHost->data = (void*)host;
\r
139 (*hosts)[index] = host;
\r
145 const char* Host::getName(void) const
\r
147 return nativeHost->name;
\r
150 void Host::setData(void* data)
\r
155 void* Host::getData(void) const
\r
160 int Host::getRunningTaskNumber(void) const
\r
162 return MSG_get_host_msgload(nativeHost);
\r
165 double Host::getSpeed(void) const
\r
167 return MSG_get_host_speed(nativeHost->simdata->smx_host);
\r
170 bool Host::hasData(void) const
\r
172 return (NULL != this->data);
\r
175 bool Host::isAvailable(void) const
\r
177 return (bool)SIMIX_host_get_state(nativeHost->simdata->smx_host);
\r
180 void Host::put(int channel, const Task& rTask)
\r
181 throw(MsgException, InvalidParameterException)
\r
183 // checks the parameters
\r
185 throw InvalidParameterException("channel (must be more or equal to zero)");
\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
191 void Host::put(int channel, const Task& rTask, double timeout)
\r
192 throw(MsgException, InvalidParameterException)
\r
194 // checks the parameters
\r
196 throw InvalidParameterException("channel (must be more or equal to zero)");
\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
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
206 void Host::putBounded(int channel, const Task& rTask, double maxRate)
\r
207 throw(MsgException, InvalidParameterException)
\r
209 // checks the parameters
\r
211 throw InvalidParameterException("channel (must be more or equal to zero)");
\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
216 if(MSG_OK != MSG_task_put_bounded(rTask.nativeTask, nativeHost, channel, maxRate))
\r
217 throw MsgException("MSG_task_put_bounded() failed");
\r
220 void Host::send(const Task& rTask)
\r
221 throw(NativeException)
\r
225 char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2);
\r
228 throw BadAllocException("alias");
\r
230 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
\r
232 rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0);
\r
237 throw MsgException("MSG_task_send_with_timeout() failed");
\r
240 void Host::send(const char* alias, const Task& rTask)
\r
241 throw(InvalidParameterException, MsgException)
\r
243 // check the parameters
\r
245 throw InvalidParmeterException("alias (must not be NULL)");
\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
251 void Host::send(const Task& rTask, double timeout)
\r
252 throw(InvalidParameterException, BadAllocException, MsgException)
\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
259 char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2);
\r
262 throw BadAllocException("alias");
\r
264 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
\r
267 rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout);
\r
272 throw MsgException("MSG_task_send_with_timeout() failed");
\r
275 void Host::send(const char* alias, const Task& rTask, double timeout)
\r
276 throw(InvalidParameterException, MsgException)
\r
278 // check the parameter
\r
281 throw InvalidParmeterException("alias (must not be NULL)");
\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
286 if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout))
\r
287 throw MsgException("MSG_task_send_with_timeout() failed");
\r
291 void Host::sendBounded(const Task& rTask, double maxRate)
\r
292 throw(InvalidParameterException, BadAllocException, MsgException)
\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
299 char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2);
\r
302 throw BadAllocException("alias");
\r
304 sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());
\r
306 rv = MSG_task_send_bounded(rTask.nativeTask, alias, maxRate);
\r
311 throw MsgException("MSG_task_send_bounded() failed");
\r
314 void Host::sendBounded(const char* alias, const Task& rTask, double maxRate)
\r
315 throw(InvalidParameterException, MsgException)
\r
317 // check the parameters
\r
319 throw InvalidParameterException("alias (must not be NULL)");
\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
324 if(MSG_OK != MSG_task_send_bounded(rTask.nativeTask, alias, maxRate))
\r
325 throw MsgException("MSG_task_send_bounded() failed");
\r
329 } // namespace SimGrid