Logo AND Algorithmique Numérique Distribuée

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