Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
documentation improvement
[simgrid.git] / src / cxx / Host.cxx
1 #include <Host.hpp>\r
2 \r
3 namespace SimGrid\r
4 {\r
5 namespace Msg\r
6 {\r
7 \r
8 \r
9 Host::Host()\r
10 {\r
11         nativeHost = NULL;\r
12         data = NULL;\r
13 }\r
14 \r
15 Host::Host(const Host& rHost)\r
16 {\r
17         this->nativeHost = rHost.nativeHost;\r
18         this->data = rHost.getData();   \r
19 }\r
20 \r
21 Host::~Host()\r
22 {\r
23         // NOTHING TODO\r
24 }\r
25 \r
26 \r
27 Host& Host::getByName(const char* hostName)\r
28 throw(HostNotFoundException, InvalidParameterException, BadAllocException)\r
29 {\r
30         // check the parameters\r
31         if(!hostName)\r
32                 throw InvalidParmeterException("hostName");\r
33                 \r
34         m_host_t nativeHost = NULL;     // native host.\r
35         Host* host = NULL;                      // wrapper host.\r
36         \r
37         if(!(nativeHost = MSG_get_host_by_name(hostName))) \r
38                 throw HostNotFoundException(hostName);\r
39         \r
40         if(!nativeHost->data) \r
41         { // native host not associated yet with  its wrapper\r
42         \r
43                 // instanciate a new wrapper \r
44                 if(!(host = new Host())\r
45                         throw BadAllocException(hostName);\r
46                 \r
47                 host->nativeHost = nativeHost; \r
48         \r
49                 // the native host data field is set with its wrapper returned \r
50                 nativeHost->data = (void*)host;\r
51         }\r
52         \r
53         // return the reference to cxx wrapper\r
54         return *((Host*)nativeHost->data);                              \r
55 }\r
56 \r
57 int Host::getNumber(void)\r
58 {\r
59         return MSG_get_host_number();\r
60 }\r
61 \r
62 Host& Host::currentHost(void)\r
63 {\r
64         Host* host = NULL;\r
65         m_host_t nativeHost = MSG_host_self();\r
66         \r
67         if(!nativeHost->data) \r
68         {\r
69                 // the native host not yet associated with its wrapper\r
70         \r
71                 // instanciate a new wrapper\r
72                 host = new Host();\r
73         \r
74                 host->nativeHost = nativeHost;\r
75         \r
76                 nativeHost->data = (void*)host;\r
77         } \r
78         else \r
79         {\r
80                 host = (Host*)nativeHost->data;\r
81         }\r
82         \r
83         return *host;\r
84 }\r
85 \r
86 void Host::all(Host*** hosts, int* len) \r
87 throw(InvalidParameterException, BadAllocException) \r
88 {\r
89         /* check the parameters */\r
90         if(!hosts)\r
91                 throw InvalidParameterException("hosts");\r
92                 \r
93         if(len < 0)\r
94                 throw InvalidParameterException("len parameter must be positive");\r
95         \r
96         int count = xbt_fifo_size(msg_global->host);\r
97         \r
98         if(*len < count)\r
99                 throw InvalidParameterException("len parameter must be more than the number of installed host\n (use Host::getNumber() to get the number of hosts)");\r
100         \r
101         int index;\r
102         m_host_t nativeHost;\r
103         Host* host;\r
104 \r
105         m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);\r
106         \r
107         for(index = 0; index < count; index++) \r
108         {\r
109                 nativeHost = table[index];\r
110                 host = (Host*)(nativeHost->data);\r
111         \r
112                 if(!host) \r
113                 {\r
114                         if(!(host = new Host())\r
115                         {\r
116                                 // release all allocated memory.\r
117                                 for(int i = 0; i < index; i++)\r
118                                         delete (*(hosts)[i]);\r
119                                 \r
120                                 throw BadAllocException("to fill the table of the hosts installed on your platform");\r
121                         }\r
122                         \r
123                         host->nativeHost = nativeHost;\r
124                         nativeHost->data = (void*)host;\r
125                 }\r
126                 \r
127                 (*hosts)[index] = host;\r
128   }\r
129 \r
130   *len = count;  \r
131 }\r
132 \r
133 \r
134 const char* Host::getName(void) const\r
135 {\r
136         return nativeHost->name;\r
137 }\r
138 \r
139 \r
140 void Host::setData(void* data)\r
141 {\r
142         this->data = data;\r
143 }\r
144 \r
145 // Getters/setters\r
146 \r
147 void* Host::getData(void) const\r
148 {\r
149         return this->data;\r
150 }\r
151 \r
152 int Host::getRunningTaskNumber(void) const\r
153 {\r
154         return MSG_get_host_msgload(nativeHost); \r
155 }\r
156 \r
157 double Host::getSpeed(void) const\r
158 {\r
159         return MSG_get_host_speed(nativeHost->simdata->smx_host);\r
160 }\r
161 \r
162 bool Host::hasData(void) const\r
163 {\r
164         return (NULL != this->data);\r
165 }\r
166 \r
167 bool Host::isAvailable(void) const\r
168 {\r
169         return (bool)SIMIX_host_get_state(nativeHost->simdata->smx_host);\r
170 }\r
171 \r
172 void Host::put(int channel, const Task& rTask) \r
173 throw(NativeException)\r
174 {\r
175         if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , -1.0))\r
176         {\r
177                 // TODO throw NativeException\r
178         }\r
179\r
180 \r
181 void Host::put(int channel, const Task& rTask, double timeout) \r
182 throw(NativeException) \r
183 {\r
184     if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , timeout))\r
185         {\r
186                 // TODO throw NativeException\r
187         }\r
188 }\r
189 \r
190 void Host::putBounded(int channel, const Task& rTask, double maxRate) \r
191 throw(NativeException)\r
192 {\r
193     MsgNative.hostPutBounded(this, channel, task, maxrate);\r
194     \r
195         if(MSG_OK != MSG_task_put_bounded(rTask.nativeTask, nativeHost, channel, maxRate))\r
196         {\r
197                 // TODO throw NativeException\r
198         }\r
199 }\r
200 \r
201 void Host::send(const Task& rTask) \r
202 throw(NativeException)  \r
203 {       \r
204         MSG_error_t rv;\r
205         \r
206         char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2);\r
207         sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());\r
208                 \r
209                 \r
210         rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0);\r
211         \r
212         free(alias);\r
213         \r
214         if(MSG_OK != rv)\r
215         {\r
216                 // TODO throw NativeException\r
217         }\r
218\r
219 \r
220 void Host::send(const char* alias, const Task& rTask) \r
221 throw(NativeException) \r
222 {\r
223         \r
224         if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0))\r
225         {\r
226                 // TODO throw NativeException\r
227         }\r
228 }\r
229 \r
230 void Host::send(const Task& rTask, double timeout) \r
231 throw(NativeException) \r
232 {\r
233         MSG_error_t rv;\r
234         \r
235         char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2);\r
236         sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());\r
237                 \r
238                 \r
239         rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout);\r
240         \r
241         free(alias);\r
242         \r
243         if(MSG_OK != rv)\r
244         {\r
245                 // TODO throw NativeException\r
246         }\r
247 }\r
248 \r
249 void Host::send(const char* alias, const Task& rTask, double timeout) \r
250 throw(NativeException) \r
251 {\r
252         if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout))\r
253         {\r
254                 // TODO throw NativeException\r
255         }\r
256 }\r
257 \r
258 \r
259 void Host::sendBounded(const Task& rTask, double maxRate) \r
260 throw(NativeException) \r
261 {\r
262         \r
263         MSG_error_t rv;\r
264         \r
265         char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2);\r
266         sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName());\r
267                 \r
268         rv = MSG_task_send_bounded(rTask.nativeTask, alias, maxRate);\r
269         \r
270         free(alias);\r
271         \r
272         if(MSG_OK != rv)\r
273         {\r
274                 // TODO throw NativeException\r
275         }\r
276 }  \r
277 \r
278 void Host::sendBounded(const char* alias, const Task& rTask, double maxRate) \r
279 throw(NativeException) \r
280 {\r
281         if(MSG_OK != MSG_task_send_bounded(rTask.nativeTask, alias, maxRate))\r
282         {\r
283                 // TODO throw NativeException\r
284         }\r
285\r
286 \r
287         \r
288 } // namspace Msg\r
289 } // namespace SimGrid