Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow reinitialization after Martin's last memleak plug.
[simgrid.git] / src / surf / surfxml_parse.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/misc.h"
9 #include "xbt/log.h"
10 #include "xbt/str.h"
11 #include "xbt/dict.h"
12 #include "surf/surfxml_parse_private.h"
13 #include "surf/surf_private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
16                                 "Logging specific to the SURF parsing module");
17
18 #undef CLEANUP
19 #include "simgrid_dtd.c"
20
21 /* Initialize the parsing globals */
22 int route_action = 0;
23 xbt_dict_t traces_set_list = NULL;
24 //xbt_dynar_t traces_connect_list = NULL;
25 xbt_dict_t trace_connect_list_host_avail = NULL;
26 xbt_dict_t trace_connect_list_power = NULL;
27 xbt_dict_t trace_connect_list_link_avail = NULL;
28 xbt_dict_t trace_connect_list_bandwidth = NULL;
29 xbt_dict_t trace_connect_list_latency = NULL;
30
31 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
32 char* old_buff;
33 /* Stores the set name reffered to by the foreach tag */
34 static const char* foreach_set_name;
35 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
36 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
37 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
38 static xbt_dynar_t main_ETag_surfxml_link_cb_list = NULL;
39
40 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
41 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
42 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
43 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
44 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
45 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
46 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
47 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
48 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
49 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
50 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
51 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
73
74 /* Stores the sets defined in the XML */
75 xbt_dict_t set_list = NULL;
76
77 xbt_dict_t current_property_set = NULL;
78
79 /* For the route:multi tag */
80 xbt_dict_t route_table = NULL;
81 xbt_dict_t route_multi_table = NULL;
82 xbt_dynar_t route_multi_elements = NULL;
83 static xbt_dynar_t route_link_list = NULL;
84 xbt_dynar_t links = NULL;
85 xbt_dynar_t keys = NULL;
86
87 xbt_dict_t random_data_list = NULL;
88
89 static xbt_dynar_t surf_input_buffer_stack = NULL;
90 static xbt_dynar_t surf_file_to_parse_stack = NULL;
91
92 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
93
94 YY_BUFFER_STATE surf_input_buffer;
95 FILE *surf_file_to_parse = NULL;
96
97 static void convert_route_multi_to_routes(void);
98 static void parse_route_elem(void);
99
100 void surf_parse_free_callbacks(void)
101 {
102   xbt_dynar_free(&STag_surfxml_platform_cb_list);
103   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
104   xbt_dynar_free(&STag_surfxml_host_cb_list);
105   xbt_dynar_free(&ETag_surfxml_host_cb_list);
106   xbt_dynar_free(&STag_surfxml_router_cb_list);
107   xbt_dynar_free(&ETag_surfxml_router_cb_list);
108   xbt_dynar_free(&STag_surfxml_link_cb_list);
109   xbt_dynar_free(&ETag_surfxml_link_cb_list);
110   xbt_dynar_free(&STag_surfxml_route_cb_list);
111   xbt_dynar_free(&ETag_surfxml_route_cb_list);
112   xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
113   xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
114   xbt_dynar_free(&STag_surfxml_process_cb_list);
115   xbt_dynar_free(&ETag_surfxml_process_cb_list);
116   xbt_dynar_free(&STag_surfxml_argument_cb_list);
117   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
118   xbt_dynar_free(&STag_surfxml_prop_cb_list);
119   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
120   xbt_dynar_free(&STag_surfxml_set_cb_list);
121   xbt_dynar_free(&ETag_surfxml_set_cb_list);
122   xbt_dynar_free(&STag_surfxml_foreach_cb_list);
123   xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
124   xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
125   xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
126   xbt_dynar_free(&STag_surfxml_cluster_cb_list);
127   xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
128   xbt_dynar_free(&STag_surfxml_trace_cb_list);
129   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
130   xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
131   xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
132   xbt_dynar_free(&STag_surfxml_random_cb_list);
133   xbt_dynar_free(&ETag_surfxml_random_cb_list);
134 }
135
136 void surf_parse_reset_parser(void)
137 {
138   surf_parse_free_callbacks();
139   STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
140   ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
141   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
142   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
143   STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
144   ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
145   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
146   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
147   STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
148   ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
149   STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150   ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151   STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152   ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153   STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154   ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155   STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156   ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157   STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158   ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
159   STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
160   ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161   STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
162   ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
163   STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
164   ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
165   STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
166   ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
167   STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
168   ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
169   STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
170   ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
171 }
172
173 void STag_surfxml_include(void)
174 {
175   xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
176   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
177
178   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
179   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
180               A_surfxml_include_file);
181   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
182   surf_parse__switch_to_buffer(surf_input_buffer);
183   printf("STAG\n");
184   fflush(NULL);
185 }
186
187 void ETag_surfxml_include(void)
188 {
189   printf("ETAG\n");
190   fflush(NULL);
191   surf_parse__delete_buffer(surf_input_buffer);
192   fclose(surf_file_to_parse);
193   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
194   xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
195 }
196
197 void STag_surfxml_platform(void)
198 {
199   double version = 0.0;
200
201   sscanf(A_surfxml_platform_version, "%lg", &version);
202
203   xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
204               "You're using an ancient XML file. "
205               "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
206               "instead of MBytes, MFlops and seconds. "
207               "A script (surfxml_update.pl) to help you convert your old "
208               "platform files "
209               "is available in the contrib/platform_generation directory "
210               "of the simgrid repository. Please check also out the "
211               "SURF section of the ChangeLog for the 3.1 version. "
212               "Last, do not forget to also update your values for "
213               "the calls to MSG_task_create (if any).");
214   xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
215               "You're using an old XML file. "
216               "A script (surfxml_update.pl) to help you convert your old "
217               "platform files "
218               "is available in the contrib/platform_generation directory "
219               "of the simgrid repository.");
220
221   if (set_list == NULL) set_list = xbt_dict_new(); 
222
223   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
224
225 }
226
227 void ETag_surfxml_platform(void)
228 {
229   convert_route_multi_to_routes();
230
231   surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
232
233   xbt_dict_free(&random_data_list);
234   xbt_dict_free(&set_list);
235 }
236
237 void STag_surfxml_host(void)
238 {
239   surfxml_call_cb_functions(STag_surfxml_host_cb_list);
240 }
241
242 void ETag_surfxml_host(void)
243 {
244   surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
245 }
246
247 void STag_surfxml_router(void)
248 {
249   surfxml_call_cb_functions(STag_surfxml_router_cb_list);
250 }
251
252 void ETag_surfxml_router(void)
253 {
254   surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
255 }
256
257 void STag_surfxml_link(void)
258 {
259   surfxml_call_cb_functions(STag_surfxml_link_cb_list);
260 }
261
262 void ETag_surfxml_link(void)
263 {
264   surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
265 }
266
267 void STag_surfxml_route(void)
268 {
269   surfxml_call_cb_functions(STag_surfxml_route_cb_list);
270 }
271
272 void ETag_surfxml_route(void)
273 {
274   surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
275 }
276
277 void STag_surfxml_link_c_ctn(void)
278 {
279   surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
280 }
281
282 void ETag_surfxml_link_c_ctn(void)
283 {
284   surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
285 }
286
287 void STag_surfxml_process(void)
288 {
289   surfxml_call_cb_functions(STag_surfxml_process_cb_list);
290 }
291
292 void ETag_surfxml_process(void)
293 {
294   surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
295 }
296
297 void STag_surfxml_argument(void)
298 {
299   surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
300 }
301
302 void ETag_surfxml_argument(void)
303 {
304   surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
305 }
306
307 void STag_surfxml_prop(void)
308 {
309   surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
310 }
311 void ETag_surfxml_prop(void)
312 {
313   surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
314 }
315
316 void STag_surfxml_set(void)
317 {
318   surfxml_call_cb_functions(STag_surfxml_set_cb_list);
319 }
320
321 void ETag_surfxml_set(void)
322 {
323   surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
324 }
325
326 void STag_surfxml_foreach(void)
327 {
328   /* Save the current buffer */
329   old_buff = surfxml_bufferstack;
330   surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
331 }
332
333 void ETag_surfxml_foreach(void)
334
335   surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
336
337   /* free the temporary dynar and restore original */
338   xbt_dynar_free(&STag_surfxml_host_cb_list);
339   xbt_dynar_free(&ETag_surfxml_host_cb_list);
340
341   STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
342   ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
343
344   /* free the temporary dynar and restore original */
345   xbt_dynar_free(&STag_surfxml_link_cb_list);
346   xbt_dynar_free(&ETag_surfxml_link_cb_list);
347
348   STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
349   ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
350
351 }
352
353 void STag_surfxml_route_c_multi(void)
354 {
355   surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
356 }
357
358 void ETag_surfxml_route_c_multi(void)
359 {
360   surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
361 }
362
363 void STag_surfxml_cluster(void)
364 {
365   surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
366 }
367
368 void ETag_surfxml_cluster(void)
369 {
370   surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
371 }
372
373 void STag_surfxml_trace(void)
374 {
375   surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
376 }
377
378 void ETag_surfxml_trace(void)
379 {
380   surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
381 }
382
383 void STag_surfxml_trace_c_connect(void)
384 {
385   surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
386 }
387
388 void ETag_surfxml_trace_c_connect(void)
389 {
390   surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
391 }
392
393 void STag_surfxml_random(void)
394 {
395   surfxml_call_cb_functions(STag_surfxml_random_cb_list);
396 }
397
398 void ETag_surfxml_random(void)
399 {
400   surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
401 }
402
403 void surf_parse_open(const char *file)
404 {
405   static int warned = 0;        /* warn only once */
406   if (!file) {
407     if (!warned) {
408       WARN0
409           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
410       warned = 1;
411     }
412     return;
413   }
414   if (!surf_input_buffer_stack)
415     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
416   if (!surf_file_to_parse_stack)
417     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
418
419   surf_file_to_parse = surf_fopen(file, "r");
420   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
421   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
422   surf_parse__switch_to_buffer(surf_input_buffer);
423   surf_parse_lineno = 1;
424 }
425
426 void surf_parse_close(void)
427 {
428   if (surf_input_buffer_stack)
429     xbt_dynar_free(&surf_input_buffer_stack);
430   if (surf_file_to_parse_stack)
431     xbt_dynar_free(&surf_file_to_parse_stack);
432
433   if (surf_file_to_parse) {
434     surf_parse__delete_buffer(surf_input_buffer);
435     fclose(surf_file_to_parse);
436   }
437 }
438
439
440 static int _surf_parse(void)
441 {
442   return surf_parse_lex();
443 }
444
445 int_f_void_t surf_parse = _surf_parse;
446
447 void surf_parse_get_double(double *value, const char *string)
448 {
449   int ret = 0;
450
451   ret = sscanf(string, "%lg", value);
452   xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
453               surf_parse_lineno, string);
454 }
455
456 void surf_parse_get_int(int *value, const char *string)
457 {
458   int ret = 0;
459
460   ret = sscanf(string, "%d", value);
461   xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
462               surf_parse_lineno, string);
463 }
464
465 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
466 {
467   if ((!string) || (strcmp(string, "") == 0))
468     *trace = NULL;
469   else
470     *trace = tmgr_trace_new(string);
471 }
472
473 void parse_properties(void)
474 {
475   char *value = NULL;
476
477   if(!current_property_set) current_property_set = xbt_dict_new();
478
479    value = xbt_strdup(A_surfxml_prop_value);  
480    xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
481 }
482
483 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
484 {
485   xbt_dynar_push(cb_list, &function);
486 }
487
488 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
489 {
490   unsigned int iterator;
491   void_f_void_t fun;
492   xbt_dynar_foreach(cb_list, iterator, fun){
493        DEBUG2("call %p %p",fun,*fun);
494        (*fun)();
495     }
496 }
497
498 static void parse_route_set_endpoints(void) {
499   route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
500 }
501
502 static void init_data(void)
503 {
504   xbt_dict_free(&route_table);
505   xbt_dynar_free(&route_link_list);
506   route_table = xbt_dict_new();
507
508   route_multi_table = xbt_dict_new();
509   route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
510   traces_set_list = xbt_dict_new();
511    
512   trace_connect_list_host_avail = xbt_dict_new();
513   trace_connect_list_power = xbt_dict_new();
514   trace_connect_list_link_avail = xbt_dict_new();
515   trace_connect_list_bandwidth = xbt_dict_new();
516   trace_connect_list_latency = xbt_dict_new();
517
518   random_data_list = xbt_dict_new();
519   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
520   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
521 }
522
523 static void free_data(void) 
524 {
525   char *key,*data;
526   xbt_dict_cursor_t cursor = NULL;
527
528   xbt_dict_foreach(route_table, cursor, key, data) {
529     xbt_dynar_t links = (xbt_dynar_t)data;
530     xbt_dynar_free(&links);
531   }
532   xbt_dict_free(&route_table);
533   route_link_list = NULL;
534
535   xbt_dict_free(&trace_connect_list_host_avail);
536   xbt_dict_free(&trace_connect_list_power);
537   xbt_dict_free(&trace_connect_list_link_avail);
538   xbt_dict_free(&trace_connect_list_bandwidth);
539   xbt_dict_free(&trace_connect_list_latency);
540   xbt_dict_free(&traces_set_list);
541 }
542
543 void parse_platform_file(const char* file)
544 {
545   surf_parse_open(file);
546   init_data();
547   xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
548   free_data();
549   surf_parse_close();
550 }
551
552 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
553
554 static void parse_make_temporary_route(const char *src, const char *dst, int action)
555 {
556   int AX_ptr = 0;
557   surfxml_bufferstack = xbt_new0(char, 2048);
558   
559   A_surfxml_route_action = action;
560   SURFXML_BUFFER_SET(route_src,                     src);
561   SURFXML_BUFFER_SET(route_dst,                     dst);
562 }
563
564 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
565                                         const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
566 {
567   int AX_ptr = 0;
568   surfxml_bufferstack = xbt_new0(char, 2048);
569  
570   SURFXML_BUFFER_SET(host_id,                     hostName);
571   SURFXML_BUFFER_SET(host_power,                  surfxml_host_power /*hostPower*/);
572   SURFXML_BUFFER_SET(host_availability,           surfxml_host_availability);
573   SURFXML_BUFFER_SET(host_availability_file,      surfxml_host_availability_file);
574   SURFXML_BUFFER_SET(host_state_file,             surfxml_host_state_file);
575 }
576
577 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
578                                         const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
579 {
580   int AX_ptr = 0;
581   surfxml_bufferstack = xbt_new0(char, 2048);
582  
583   SURFXML_BUFFER_SET(link_id,                linkName);
584   SURFXML_BUFFER_SET(link_bandwidth,         surfxml_link_bandwidth);
585   SURFXML_BUFFER_SET(link_bandwidth_file,    surfxml_link_bandwidth_file);
586   SURFXML_BUFFER_SET(link_latency,           surfxml_link_latency);
587   SURFXML_BUFFER_SET(link_latency_file,      surfxml_link_latency_file);
588   SURFXML_BUFFER_SET(link_state_file,        surfxml_link_state_file);
589 }
590
591 /**
592 * \brief Restores the original surfxml buffer
593 */
594 static void parse_restore_original_buffer(void)
595 {
596   free(surfxml_bufferstack);
597   surfxml_bufferstack = old_buff;
598 }
599
600 /* Functions for the sets and foreach tags */
601
602 void parse_sets(void)
603 {
604   char *id, *suffix, *prefix, *radical;
605   int start, end;
606   xbt_dynar_t radical_ends;
607   xbt_dynar_t current_set;
608   char *value;
609   int i;
610
611   id = xbt_strdup(A_surfxml_set_id);
612   prefix = xbt_strdup(A_surfxml_set_prefix);
613   suffix = xbt_strdup(A_surfxml_set_suffix);
614   radical = xbt_strdup(A_surfxml_set_radical);
615   
616   xbt_assert1(!xbt_dict_get_or_null(set_list, id),
617               "Set '%s' declared several times in the platform file.",id);  
618   radical_ends = xbt_str_split(radical, "-");
619   xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
620
621   surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
622   surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
623
624   current_set = xbt_dynar_new(sizeof(char*), NULL);
625
626   
627   for (i=start; i<=end; i++) {
628      value = bprintf("%s%d%s", prefix, i, suffix);
629      xbt_dynar_push(current_set, &value);
630   } 
631   
632   xbt_dict_set(set_list, id, current_set, NULL);
633 }
634
635 static const char* surfxml_host_power;
636 static const char* surfxml_host_availability;
637 static const char* surfxml_host_availability_file;
638 static const char* surfxml_host_state_file;
639
640 static void parse_host_foreach(void)
641 {
642   surfxml_host_power = A_surfxml_host_power;
643   surfxml_host_availability = A_surfxml_host_availability;
644   surfxml_host_availability_file = A_surfxml_host_availability_file;
645   surfxml_host_state_file = A_surfxml_host_state_file;
646 }
647
648 static void finalize_host_foreach(void)
649 {
650   xbt_dynar_t names = NULL;
651   unsigned int cpt = 0;
652   char *name;
653   xbt_dict_cursor_t cursor = NULL;
654   char *key,*data; 
655
656   xbt_dict_t cluster_host_props = current_property_set;
657   
658   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
659               "Set name '%s' reffered by foreach tag not found.", foreach_set_name);  
660
661   xbt_assert1((strcmp(A_surfxml_host_id, "$1") == 0), "The id of the host within the foreach should point to the foreach set_id (use $1). Your value: %s", A_surfxml_host_id);
662
663         
664   /* foreach name in set call the main host callback */
665   xbt_dynar_foreach (names, cpt, name) {
666     parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
667                                         surfxml_host_availability_file, surfxml_host_state_file);
668     surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
669
670     xbt_dict_foreach(cluster_host_props,cursor,key,data) {
671            xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
672     }
673
674     surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
675     free(surfxml_bufferstack);
676   }
677
678   current_property_set = xbt_dict_new();
679
680   surfxml_bufferstack = old_buff;
681   
682 }
683
684 static const char* surfxml_link_bandwidth;
685 static const char* surfxml_link_bandwidth_file;
686 static const char* surfxml_link_latency;
687 static const char* surfxml_link_latency_file;
688 static const char* surfxml_link_state_file;
689
690 static void parse_link_foreach(void)
691 {
692   surfxml_link_bandwidth = A_surfxml_link_bandwidth;
693   surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
694   surfxml_link_latency = A_surfxml_link_latency;
695   surfxml_link_latency_file = A_surfxml_link_latency_file;
696   surfxml_link_state_file = A_surfxml_link_state_file;
697 }
698
699 static void finalize_link_foreach(void)
700 {
701   xbt_dynar_t names = NULL;
702   unsigned int cpt = 0;
703   char *name;
704   xbt_dict_cursor_t cursor = NULL;
705   char *key,*data; 
706
707   xbt_dict_t cluster_link_props = current_property_set;
708
709   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
710               "Set name '%s' reffered by foreach tag not found.", foreach_set_name); 
711
712   xbt_assert1((strcmp(A_surfxml_link_id, "$1") == 0), "The id of the link within the foreach should point to the foreach set_id (use $1). Your value: %s", A_surfxml_link_id);
713
714
715   /* for each name in set call the main link callback */
716   xbt_dynar_foreach (names, cpt, name) {
717     parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
718                                         surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
719     surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
720
721     xbt_dict_foreach(cluster_link_props,cursor,key,data) {
722            xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
723     }
724
725     surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
726    free(surfxml_bufferstack);
727
728   }
729
730   current_property_set = xbt_dict_new();
731
732   surfxml_bufferstack = old_buff;
733 }
734
735 void parse_foreach(void)
736 {
737   /* save the host & link callbacks */
738   main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
739   main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
740   main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
741   main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
742
743   /* define host & link callbacks to be used only by the foreach tag */
744   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
745   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
746   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
747   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
748
749   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
750   surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
751   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
752   surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
753
754   /* get set name */
755   foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id); 
756 }
757
758 /* Route:multi functions */
759
760 static int route_multi_size=0;
761 static char* src_name, *dst_name;
762 static int is_symmetric_route;
763
764 static void parse_route_elem(void)
765 {
766   char *val;
767
768   val = xbt_strdup(A_surfxml_link_c_ctn_id);
769   
770   xbt_dynar_push(route_link_list, &val);
771 }
772
773 void parse_route_multi_set_endpoints(void)
774 {
775   src_name = xbt_strdup(A_surfxml_route_c_multi_src); 
776   dst_name = xbt_strdup(A_surfxml_route_c_multi_dst); 
777   route_action = A_surfxml_route_c_multi_action;
778   is_symmetric_route = A_surfxml_route_c_multi_symmetric;
779   route_multi_size++;
780
781   route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
782 }
783
784 static int contains(xbt_dynar_t list, const char* value)
785 {
786  unsigned int cpt;
787  char * val;
788  xbt_dynar_foreach(list, cpt, val) {
789    if (strcmp(val, value) == 0)
790       return 1;
791   }
792   return 0;
793 }
794
795 /* 
796    This function is used to append or override the contents of an alread existing route in the case a new one with its name is found.
797    The decision is based upon the value of action specified in the xml route:multi attribute action
798  */
799 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
800 {
801   unsigned int cpt;
802   xbt_dynar_t links;
803   char *value;
804
805   /* get already existing list if it exists */
806   links = xbt_dict_get_or_null(routing_table, route_name);
807   DEBUG1("ROUTE: %s", route_name);
808   if (links != NULL) {
809      switch (action) {
810         case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
811                                     xbt_dynar_foreach(links, cpt, value) {      
812                                        xbt_dynar_push(route_link_list,&value);
813                                     }
814                                     break;
815         case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */ 
816                                     xbt_dynar_foreach(route_link_list, cpt, value) {
817                                        xbt_dynar_push(links,&value);
818                                     }
819                                     xbt_dynar_free(&route_link_list);
820                                     route_link_list = links;
821                                     break;
822         case A_surfxml_route_action_OVERRIDE:
823                                     break;
824         default:break;
825      }
826   }
827   /* this is the final route; do not add if name is a set; add only if name is in set list */
828   if (!isMultiRoute){    
829     xbt_dict_set(routing_table, route_name, route_link_list, NULL);
830   }
831 }
832
833 void parse_route_multi_set_route(void)
834 {
835   char* route_name;
836
837   route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
838
839   xbt_dynar_push(route_multi_elements, &route_name);
840
841   /* Add route */
842   xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
843   /* add symmetric if it is the case */ 
844   if (is_symmetric_route == 1) {
845     char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
846   
847     xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
848     xbt_dynar_push(route_multi_elements, &symmetric_name);
849     is_symmetric_route = 0;
850   }
851   free(src_name);
852   free(dst_name);
853 }
854
855 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
856 {
857   unsigned int cpt;
858   char* value, *val;
859    parse_make_temporary_route(src_name, dst_name, route_action);
860    surfxml_call_cb_functions(STag_surfxml_route_cb_list);
861    DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
862    /* Build link list */ 
863    xbt_dynar_foreach(links, cpt, value) {
864      if (strcmp(value, src) == 0)
865        val =  xbt_strdup(src_name);
866      else if (strcmp(value, dst) == 0)
867        val = xbt_strdup(dst_name);
868      else if (strcmp(value, "$dst") == 0)
869        val = xbt_strdup(dst_name);
870      else if (strcmp(value, "$src") == 0)
871        val = xbt_strdup(src_name);
872      else
873        val = xbt_strdup(value);
874      DEBUG1("\t\tELEMENT: %s", val);
875      xbt_dynar_push(route_link_list, &val);
876    }    
877    surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
878    free(surfxml_bufferstack);
879 }
880
881 static void convert_route_multi_to_routes(void)
882 {
883   xbt_dict_cursor_t cursor_w;
884   int symmetric;
885   unsigned int cpt, cpt2, cursor;
886   char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w; 
887   const char* sep="#";
888   xbt_dict_t set;
889   xbt_dynar_t src_names = NULL, dst_names = NULL, links;
890
891   if (!route_multi_elements) return;
892
893   set = cpu_set;
894   DEBUG1("%d", xbt_dict_length(workstation_set));                               
895   if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
896      set = workstation_set;
897   
898
899   old_buff = surfxml_bufferstack;
900   /* Get all routes in the exact order they were entered in the platform file */
901   xbt_dynar_foreach(route_multi_elements, cursor, key) {
902      /* Get links for the route */     
903      links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
904      keys = xbt_str_split_str(key, sep);
905      /* Get route ends */
906      src = xbt_dynar_get_as(keys, 0, char*);
907      dst = xbt_dynar_get_as(keys, 1, char*);
908      route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
909      symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
910
911     /* Create the dynar of src and dst hosts for the new routes */ 
912     /* NOTE: src and dst can be either set names or simple host names */
913     src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
914     dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
915     /* Add to dynar even if they are simple names */
916     if (src_names == NULL) {
917        src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
918        val = xbt_strdup(src);
919        xbt_dynar_push(src_names, &val);
920        if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
921          THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
922     }
923     if (dst_names == NULL) {
924        dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
925        val = xbt_strdup(dst);
926        if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
927          THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
928        xbt_dynar_push(dst_names, &val);
929     }
930
931     /* Build the routes */
932     DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
933     xbt_dynar_foreach(src_names, cpt, src_host_name) {
934       xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
935         /* If dst is $* then set this route to have its dst point to all hosts */
936         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
937                   xbt_dict_foreach(set, cursor_w, key_w, data_w) {
938                           //int n = xbt_dynar_member(src_names, (char*)key_w);
939                             add_multi_links(src, dst, links, src_host_name, key_w);               
940                   }
941         }
942         /* If src is $* then set this route to have its dst point to all hosts */
943         if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
944                   xbt_dict_foreach(set, cursor_w, key_w, data_w) {
945                      // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
946                         add_multi_links(src, dst, links, key_w, dst_host_name);               
947                 }
948         }
949         /* if none of them are equal to $* */
950         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
951                         add_multi_links(src, dst, links, src_host_name, dst_host_name);               
952         }     
953       }
954     }
955   }  
956   surfxml_bufferstack = old_buff;
957   xbt_dict_free(&route_multi_table);
958   xbt_dynar_free(&route_multi_elements);
959 }
960
961 /* Cluster tag functions */
962
963 void parse_cluster(void)
964 {  
965    static int AX_ptr = 0;
966    static int surfxml_bufferstack_size = 2048;
967  
968    char* cluster_id = A_surfxml_cluster_id;
969    char* cluster_prefix = A_surfxml_cluster_prefix;
970    char* cluster_suffix = A_surfxml_cluster_suffix;
971    char* cluster_radical = A_surfxml_cluster_radical;
972    char* cluster_power = A_surfxml_cluster_power;
973    char* cluster_bw = A_surfxml_cluster_bw;
974    char* cluster_lat = A_surfxml_cluster_lat;
975    char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
976    char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
977  
978    char* saved_buff = surfxml_bufferstack;
979
980    char * backbone_name;
981
982    surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
983
984    /* Make set */
985    SURFXML_BUFFER_SET(set_id, cluster_id);
986    SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
987    SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
988    SURFXML_BUFFER_SET(set_radical, cluster_radical);
989   
990    SURFXML_START_TAG(set);
991    SURFXML_END_TAG(set);
992
993    /* Make foreach */
994    SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
995
996    SURFXML_START_TAG(foreach);
997
998      /* Make host for the foreach */
999      parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1000      A_surfxml_host_state = A_surfxml_host_state_ON;
1001
1002      SURFXML_START_TAG(host);
1003      SURFXML_END_TAG(host);
1004
1005      /* Make link for the foreach */
1006      parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
1007      A_surfxml_link_state = A_surfxml_link_state_ON;
1008      A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1009  
1010      SURFXML_START_TAG(link);
1011      SURFXML_END_TAG(link);
1012
1013    SURFXML_END_TAG(foreach);
1014
1015    /* Make backbone link */
1016    backbone_name = bprintf("%s_bb", cluster_id);
1017    parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
1018    A_surfxml_link_state = A_surfxml_link_state_ON;
1019    A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1020  
1021    SURFXML_START_TAG(link);
1022    SURFXML_END_TAG(link);
1023
1024    /* Make route multi with the outside world, i.e. cluster->$* */
1025    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1026    SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1027    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1028    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
1029    
1030    SURFXML_START_TAG(route_c_multi);
1031
1032      SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1033     
1034      SURFXML_START_TAG(link_c_ctn);
1035      SURFXML_END_TAG(link_c_ctn);
1036
1037    SURFXML_END_TAG(route_c_multi);
1038
1039    /* Make route multi between cluster hosts, i.e. cluster->cluster */
1040    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1041    SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1042    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1043    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1044    
1045    SURFXML_START_TAG(route_c_multi);
1046     
1047      SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1048     
1049      SURFXML_START_TAG(link_c_ctn);
1050      SURFXML_END_TAG(link_c_ctn);
1051     
1052    SURFXML_END_TAG(route_c_multi);
1053
1054
1055    /* Restore buff */
1056    free(surfxml_bufferstack);
1057    surfxml_bufferstack = saved_buff;
1058 }
1059
1060 /* Trace management functions */
1061
1062 static double trace_periodicity = -1.0;
1063 static char* trace_file = NULL;
1064 static char* trace_id;
1065
1066 void parse_trace_init(void)
1067 {
1068    trace_id = strdup(A_surfxml_trace_id);
1069    trace_file = strdup(A_surfxml_trace_file);
1070    surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1071 }
1072
1073 void parse_trace_finalize(void)
1074 {
1075   tmgr_trace_t trace;
1076   if (!trace_file || strcmp(trace_file,"") != 0) {
1077     surf_parse_get_trace(&trace, trace_file);
1078   }
1079   else {
1080     if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1081     else
1082       trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);  
1083   }
1084   xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1085 }
1086
1087 void parse_trace_c_connect(void)
1088 {
1089    xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
1090               "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
1091    
1092    switch (A_surfxml_trace_c_connect_kind) {
1093     case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1094       xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1095       break;
1096     case A_surfxml_trace_c_connect_kind_POWER:
1097       xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1098       break;
1099     case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1100       xbt_dict_set(trace_connect_list_link_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1101       break;
1102     case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1103       xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1104       break;
1105     case A_surfxml_trace_c_connect_kind_LATENCY:
1106       xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1107       break;
1108     default:
1109       xbt_die("kind of trace unknown");
1110    }   
1111 }
1112
1113 /* Random tag functions */
1114
1115 double get_cpu_power(const char *power)
1116
1117   double power_scale = 0.0;
1118   const char *p, *q;
1119   char *generator;
1120   random_data_t random = NULL; 
1121   /* randomness is inserted like this: power="$rand(my_random)" */
1122   if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1123      if (p < q) {
1124        generator = xbt_malloc(q - (p + 6) + 1);
1125        memcpy(generator, p + 6, q - (p + 6)); 
1126        generator[q - (p + 6)] = '\0';
1127        xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1128               "Random generator %s undefined", generator);
1129        power_scale = random_generate(random);
1130      }
1131   }
1132   else {
1133     surf_parse_get_double(&power_scale, power);
1134   }
1135   return power_scale;
1136 }
1137
1138 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1139 char *random_id;
1140
1141 void init_randomness(void)
1142 {
1143   random_id = A_surfxml_random_id;
1144   surf_parse_get_int(&random_min, A_surfxml_random_min);
1145   surf_parse_get_int(&random_max, A_surfxml_random_max);
1146   surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1147   surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1148   random_generator = A_surfxml_random_generator;
1149 }
1150
1151 void add_randomness(void)
1152 {
1153    /* If needed aditional properties can be added by using the prop tag */
1154   random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
1155    xbt_dict_set(random_data_list, random_id, (void *)random, NULL);
1156 }
1157