Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A string utility class and a new Exception (used to throw out of band exception)
[simgrid.git] / src / cxx / Host.hpp
1 /*\r
2  * Host.hpp\r
3  *\r
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           \r
5  * All right reserved. \r
6  *\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
10  *\r
11  */  \r
12 #ifndef MSG_HOST_HPP\r
13 #define MSG_HOST_HPP\r
14 \r
15 \r
16 /*! \brief Host class declaration.\r
17  *\r
18  * An host instance represents a location (any possible place) where a process may run. \r
19  * Thus it is represented as a physical resource with computing capabilities, some \r
20  * mailboxes to enable running process to communicate with remote ones, and some private \r
21  * data that can be only accessed by local process. An instance of this class is always \r
22  * binded with the corresponding native host. All the native hosts are automaticaly created \r
23  * during the call of the static method Msg::createEnvironment(). This method takes as parameter\r
24  * the platform file which describes all elements of the platform (host, link, root..).\r
25  * You never need to create an host your self.\r
26  *\r
27  * The best way to get an host is to call the static method \r
28  * Host.getByName() which returns a reference.\r
29  *\r
30  * For example to get the instance of the host. If your platform\r
31  * file description contains an host named "Jacquelin" :\r
32  *\r
33  * \verbatim\r
34 using namespace SimGrid.Msg;\r
35 \r
36 Host jacquelin;\r
37 \r
38 try \r
39\r
40         jacquelin = Host::getByName("Jacquelin");\r
41 }\r
42 catch(HostNotFoundException e) \r
43 {\r
44         cerr << e.toString();\r
45 }\r
46 ...\r
47 \endverbatim\r
48  *\r
49  */\r
50 \r
51 #ifndef __cplusplus\r
52         #error Host.hpp requires C++ compilation (use a .cxx suffix)\r
53 #endif\r
54 \r
55 #include <msg/datatypes.h>\r
56 \r
57 #include <InvalidArgumentException.hpp>\r
58 #include <BadAllocException.hpp>\r
59 #include <HostNotFoundException.hpp>\r
60 #include <MsgException.hpp>\r
61 #include <NullPointerException.hpp>\r
62 \r
63 \r
64 \r
65 // namespace SimGrid::Msg\r
66 namespace SimGrid\r
67 {\r
68         namespace Msg\r
69         {\r
70                 class Task;\r
71                 class Process;\r
72 \r
73                 // Declaration of the class SimGrid::Msg::Host.\r
74                 class SIMGRIDX_EXPORT Host // final class.\r
75                 {\r
76                         friend class Process;\r
77                         friend class Task;\r
78 \r
79                         // Desable the default constructor.\r
80                         // The best way to get an host instance is to use the static method Host::getByName().\r
81                         \r
82                         public :\r
83                                 \r
84                         // Default constructor (desabled).\r
85                                 Host();\r
86                         \r
87                         public: \r
88                                 \r
89                         // Copy constructor (desabled).\r
90                                 Host(const Host& rHost);\r
91                                 \r
92                         // Destructor (desable).\r
93                                 virtual ~Host();\r
94                         \r
95                         // Operations\r
96                         \r
97                                 /*! \brief Host::getByName() - returns an host by its name\r
98                                  *\r
99                                  * This static method returns a reference to the host instance associated \r
100                                  * with a native host of your platform. This is the best way to get a host.\r
101                                  *\r
102                                  * \param hostName      The name of the host.\r
103                                  *\r
104                                  * \return                      If successful the method returns a reference to the instance\r
105                                  *                                      associated with the native host having the name specified\r
106                                  *                                      as parameter of your platform. Otherwise the method throws\r
107                                  *                                      one of the exceptions detailed below.\r
108                                  *\r
109                                  * \exception           [HostNotFoundException]         if no host with the specified name\r
110                                  *                                                                                              was found.\r
111                                  *                                      [InvalidArgumentException]      if the hostName parameter is invalid (NULL).\r
112                                  *                                      [BadAllocException]                     if there is not enough memory to allocate the host.\r
113                                  */ \r
114                                 static Host& getByName(const char* hostName)\r
115                                 throw(HostNotFoundException, NullPointerException, BadAllocException);\r
116                                 \r
117                                 /*! \brief Host::getNumber() - returns the number of the installed hosts.\r
118                                  *\r
119                                  * \return                      The number of hosts installed.\r
120                                  */\r
121                                 static int getNumber(void);\r
122                                 \r
123                                 \r
124                                 /*! \brief Host::currentHost() - This static method returns the location on which the current \r
125                                  * process is executed.\r
126                                  *\r
127                                  * \return                      The host of the current process.\r
128                                  *\r
129                                  * \see                         Process::currentProcess().\r
130                                  */\r
131                                 static Host& currentHost(void);\r
132                                 \r
133                                 /*! \brief Host::all() - This static method retrieves all of the hosts of the installed platform.\r
134                                  *\r
135                                  * \param hosts         A pointer to array of Host pointers that receives all the hosts of the platform.\r
136                                  *\r
137                                  * \param len           A pointer to the length of the table of pointers.\r
138                                  *\r
139                                  * \return                      If successful the hosts table is filled and\r
140                                  *                                      the parameter len is set with the number of hosts of the platform.\r
141                                  *                                      Otherwise the method throw one of the exception described below.\r
142                                  *\r
143                                  * \exception           [InvalidArgumentException]      if the parameter hosts is invalid or\r
144                                  *                                                                                              if the parameter len is negative or\r
145                                  *                                                                                              less than the number of hosts installed\r
146                                  *                                                                                              on the current platform.\r
147                                  *                                      [BadAllocException]                     If the method can't allocate memory to fill\r
148                                  *                                                                                              the table of hosts.\r
149                                  *\r
150                                  *\r
151                                  * \remark                      To get the number of hosts installed on your platform use the static method\r
152                                  *                                      Host::getNumber().\r
153                                  *\r
154                                  * \see                         Host::getNumber().\r
155                                  *\r
156                                  *\verbatim\r
157                                  * // This example show how to use this method to get the list of hosts installed on your platform.\r
158                                  *\r
159                                  *      using namespace SimGrid::Msg;\r
160                                  *      using <iostream>\r
161                                  *\r
162                                  *      // (1) get the number of hosts.\r
163                                  *      int number = Host::getNumber();\r
164                                  *      \r
165                                  *      // (2) allocate the array that receives the list of hosts.\r
166                                  *      HostPtr* ar = new HostPtr[number];      // HostPtr is defined as (typedef Host* HostPtr at the end of the\r
167                                  *                                                                              // declaration of this class.\r
168                                  *\r
169                                  *      // (3) call the method\r
170                                  *      try\r
171                                  *      {\r
172                                  *              Host::all(&ar, &number);\r
173                                  *      }\r
174                                  *      catch(BadAllocException e)\r
175                                  *      {\r
176                                  *              cerr << e.toString() << endl;\r
177                                  *              ...\r
178                                  *      }\r
179                                  *      catch(InvalidArgumentException e)\r
180                                  *      {\r
181                                  *              cerr << e.toString() << endl;\r
182                                  *              ..\r
183                                  *      } \r
184                                  *\r
185                                  *      // (4) use the table of host (for example print all the name of all the hosts);\r
186                                  *      \r
187                                  *      for(int i = 0; i < number ; i++)\r
188                                  *              cout << ar[i]->getName() << endl;\r
189                                  *\r
190                                  *      ...\r
191                                  *              \r
192                                  *      // (5) release the allocate table\r
193                                  *      \r
194                                  *      delete[] ar;\r
195                                  *\r
196                                  */ \r
197                                 static void all(Host*** hosts /*in|out*/, int* len /*in|out*/)\r
198                                 throw(InvalidArgumentException, BadAllocException) ;\r
199                                 \r
200                                 /*! \brief Host::getName() - This method return the name of the Msg host object.\r
201                                  *\r
202                                  * \return                      The name of the host object.\r
203                                  */\r
204                                 const char* getName(void) const;\r
205                                 \r
206                                 /*! \brief Host::setData() - Set the user data of an host object.\r
207                                  *\r
208                                  * \param data          The user data to set.\r
209                                  */\r
210                                 void setData(void* data);\r
211                                 \r
212                                 /*! \brief Host::getData() - Get the user data of a host object.\r
213                                  *\r
214                                  * \return                      The user data of the host object.\r
215                                  */\r
216                                 void* getData(void) const;\r
217                                 \r
218                                 /*! \brief Host::hasData() - Test if an host object has some data.\r
219                                  *\r
220                                  * \return                      This method returns true if the host object has some user data.\r
221                                  *                                      Otherwise the method returns false.\r
222                                  */\r
223                                 bool hasData(void) const;\r
224                                 \r
225                                 /*! \brief Host::getRunningTaskNumber() - returns the number of tasks currently running on a host.\r
226                                  *\r
227                                  * \return                      The number of task currently running of the host object.\r
228                                  *\r
229                                  * \remark                      The external load is not taken in account.\r
230                                  */\r
231                                 int getRunningTaskNumber(void) const;\r
232                                 \r
233                                 /*! \brief Host::getSpeed() - returns the speed of the processor of a host,\r
234                                  * regardless of the current load of the machine.\r
235                                  *\r
236                                  * \return                      The speed of the processor of the host in flops.\r
237                                  */ \r
238                                 double getSpeed(void) const;\r
239                                 \r
240                                 /*! \brief Host::isAvailable - tests if an host is availabled.\r
241                                  * \r
242                                  * \return                      Is the host is availabled the method returns\r
243                                  *                                      1. Otherwise the method returns 0.\r
244                                  */ \r
245                                 int isAvailable(void) const;\r
246                                 \r
247                                 /* ! \brief Host::put() - put a task on the given channel of a host .\r
248                                  *\r
249                                  * \param channel       The channel where to put the task.\r
250                                  * \param rTask         A refercence to the task object containing the native task to\r
251                                  *                                      put on the channel specified by the parameter channel.\r
252                                  *\r
253                                  * \return                      If successful the task is puted on the specified channel. Otherwise\r
254                                  *                                      the method throws one of the exceptions described below.\r
255                                  *\r
256                                  * \exception           [MsgException]                          if an internal error occurs.\r
257                                  *                                      [InvalidArgumentException]      if the value of the channel specified as\r
258                                  *                                                                                              parameter is negative.\r
259                                  */\r
260                                 void put(int channel, Task* task) \r
261                                 throw(MsgException, InvalidArgumentException);\r
262                                 \r
263                                 /* ! \brief Host::put() - put a task on the given channel of a host object (waiting at most timeout seconds).\r
264                                  *\r
265                                  * \param channel       The channel where to put the task.\r
266                                  * \param rTask         A refercence to the task object containing the native task to\r
267                                  *                                      put on the channel specified by the parameter channel.\r
268                                  * \param timeout       The timeout in seconds.\r
269                                  *\r
270                                  * \return                      If successful the task is puted on the specified channel. Otherwise\r
271                                  *                                      the method throws one of the exceptions described below.\r
272                                  *\r
273                                  * \exception           [MsgException]                          if an internal error occurs.\r
274                                  *                                      [InvalidArgumentException]      if the value of the channel specified as\r
275                                  *                                                                                              parameter is negative or if the timeout value\r
276                                  *                                                                                              is less than zero and différent of -1.\r
277                                  *\r
278                                  * \remark                      To specify no timeout set the timeout value with -1.0.\r
279                                  */\r
280                                 void put(int channel, Task* task, double timeout) \r
281                                 throw(MsgException, InvalidArgumentException);\r
282                                 \r
283                                 /* ! \brief Host::putBounded() - put a task on the given channel of a host object (capping the emission rate to maxrate).\r
284                                  *\r
285                                  * \param channel       The channel where to put the task.\r
286                                  * \param rTask         A refercence to the task object containing the native task to\r
287                                  *                                      put on the channel specified by the parameter channel.\r
288                                  * \param maxRate       The maximum rate.\r
289                                  *\r
290                                  * \return                      If successful the task is puted on the specified channel. Otherwise\r
291                                  *                                      the method throws one of the exceptions described below.\r
292                                  *\r
293                                  * \exception           [MsgException]                          if an internal error occurs.\r
294                                  *                                      [InvalidArgumentException]      if the value of the channel specified as\r
295                                  *                                                                                              parameter is negative or if the maxRate parameter value\r
296                                  *                                                                                              is less than zero and différent of -1.0.\r
297                                  *\r
298                                  * \remark                      To specify no rate set the maxRate parameter value with -1.0.\r
299                                  */\r
300                                 void putBounded(int channel, Task* task, double maxRate) \r
301                                 throw(MsgException, InvalidArgumentException);\r
302                                 \r
303                                 /* ! brief Host::send() - sends the given task to mailbox identified by the default alias.\r
304                                  *\r
305                                  * \param rTask         A reference to the task object containing the native msg task to send.\r
306                                  *\r
307                                  * \return                      If successful the task is sended to the default mailbox. Otherwise the\r
308                                  *                                      method throws one of the exceptions described below.\r
309                                  *\r
310                                  * \exception           [BadAllocException]                     if there is not enough memory to allocate\r
311                                  *                                                                                              the default alias variable.\r
312                                  *                                      [MsgException]                          if an internal error occurs.\r
313                                  */\r
314                                 void send(Task* task) \r
315                                 throw(MsgException, BadAllocException);\r
316                                 \r
317                                 /* ! brief Host::send() - sends the given task to mailbox identified by the specified alias parameter.\r
318                                  *\r
319                                  * \param rTask         A reference to the task object containing the native msg task to send.\r
320                                  * \param alias         The alias of the mailbox where to send the task.\r
321                                  *\r
322                                  * \return                      If successful the task is sended to the default mailbox. Otherwise the\r
323                                  *                                      method throws one of the exceptions described below.\r
324                                  *\r
325                                  * \exception           [InvalidArgumentException]      if alias parameter is invalid (NULL).\r
326                                  *                                      [BadAllocException]                     if there is not enough memory to allocate\r
327                                  *                                                                                              the default alias variable.\r
328                                  *                                      [MsgException]                          if an internal error occurs.\r
329                                  */\r
330                                 void send(const char* alias, Task* task) \r
331                                 throw(InvalidArgumentException, MsgException);\r
332                                 \r
333                                 /* ! brief Host::send() - sends the given task to mailbox identified by the default alias\r
334                                  *  (waiting at most timeout seconds).\r
335                                  *\r
336                                  * \param rTask         A reference to the task object containing the native msg task to send.\r
337                                  * \param timeout       The timeout value to wait for.\r
338                                  *\r
339                                  * \return                      If successful the task is sended to the default mailbox. Otherwise the\r
340                                  *                                      method throws one of the exceptions described below.\r
341                                  *\r
342                                  * \exception           [BadAllocException]                     if there is not enough memory to allocate\r
343                                  *                                                                                              the default alias variable.\r
344                                  *                                      [InvalidArgumentException]      if the timeout value is negative and different of\r
345                                  *                                                                                              -1.0.                   \r
346                                  *                                      [MsgException]                          if an internal error occurs.\r
347                                  *\r
348                                  * \remark                      To specify no timeout set the timeout value with -1.0 or use the previous \r
349                                  *                                      version of this method.\r
350                                  *\r
351                                  */\r
352                                 void send(Task* task, double timeout) \r
353                                 throw(InvalidArgumentException, BadAllocException, MsgException);\r
354                                 \r
355                                 /* ! brief Host::send() - sends the given task to mailbox identified by the parameter alias\r
356                                  *  (waiting at most timeout seconds).\r
357                                  *\r
358                                  * \param alias         The alias of the mailbox to send the task.\r
359                                  * \param rTask         A reference to the task object containing the native msg task to send.\r
360                                  * \param timeout       The timeout value to wait for.\r
361                                  *\r
362                                  * \return                      If successful the task is sended to the default mailbox. Otherwise the\r
363                                  *                                      method throws one of the exceptions described below.\r
364                                  *\r
365                                  * \exception           [InvalidArgumentException]      if the timeout value is negative and different of\r
366                                  *                                                                                              -1.0 or if the alias parameter is invalid (NULL).                       \r
367                                  *                                      [MsgException]                          if an internal error occurs.\r
368                                  *\r
369                                  * \remark                      To specify no timeout set the timeout value with -1.0 or use the previous \r
370                                  *                                      version of this method.\r
371                                  *\r
372                                  */\r
373                                 void send(const char* alias, Task* task, double timeout) \r
374                                 throw(InvalidArgumentException, MsgException);\r
375                                 \r
376                                 /* ! brief Host::sendBounded() - sends the given task to mailbox associated to the default alias\r
377                                  *  (capping the emission rate to maxRate).\r
378                                  *\r
379                                  * \param rTask         A reference to the task object containing the native msg task to send.\r
380                                  * \param maxRate       The maximum rate value.\r
381                                  *\r
382                                  * \return                      If successful the task is sended to the default mailbox. Otherwise the\r
383                                  *                                      method throws one of the exceptions described below.\r
384                                  *\r
385                                  * \exception           [InvalidArgumentException]      if the maximum rate value is negative and different of\r
386                                  *                                                                                              -1.0.                   \r
387                                  *                                      [MsgException]                          if an internal error occurs.\r
388                                  *                                      [BadAllocException]                     if there is not enough memory to allocate\r
389                                  *                                                                                              the default alias variable.\r
390                                  *\r
391                                  * \remark                      To specify no rate set its value with -1.0.\r
392                                  *\r
393                                  */\r
394                                 void sendBounded(Task* task, double maxRate) \r
395                                 throw(InvalidArgumentException, BadAllocException, MsgException);\r
396                                 \r
397                                 /* ! brief Host::sendBounded() - sends the given task to mailbox identified by the parameter alias\r
398                                  *  (capping the emission rate to maxRate).\r
399                                  *\r
400                                  * \param alias         The alias of the mailbox where to send the task.\r
401                                  * \param rTask         A reference to the task object containing the native msg task to send.\r
402                                  * \param maxRate       The maximum rate value.\r
403                                  *\r
404                                  * \return                      If successful the task is sended to the default mailbox. Otherwise the\r
405                                  *                                      method throws one of the exceptions described below.\r
406                                  *\r
407                                  * \exception           [InvalidArgumentException]      if the maximum rate value is negative and different of\r
408                                  *                                                                                              -1.0 or if the alias parameter is invalid (NULL).                       \r
409                                  *                                      [MsgException]                          if an internal error occurs.\r
410                                  *\r
411                                  * \remark                      To specify no rate set its value with -1.0.\r
412                                  *\r
413                                  */\r
414                                 void sendBounded(const char* alias, Task* task, double maxRate) \r
415                                 throw(InvalidArgumentException, MsgException);\r
416                         \r
417                         protected:\r
418                         // Attributes.\r
419                         \r
420                                 /**\r
421                                  * This attribute represents the msg native host object. \r
422                                  * It is set automaticatly during the call of the static \r
423                                  * method Host::getByName().\r
424                                  *\r
425                                  * \see                         Host::getByName().\r
426                                  */ \r
427                                 m_host_t nativeHost;\r
428                         \r
429                         private:\r
430                                 /**\r
431                                  * User host data. \r
432                                  */ \r
433                                 void* data;\r
434                 };\r
435                 \r
436                 typedef Host* HostPtr;\r
437         } // namespace Msg\r
438 } // namespace SimGrid\r
439 \r
440 #endif // !MSG_HOST_HPP\r