Logo AND Algorithmique Numérique Distribuée

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