1 /* Copyright (c) 2009 The SimGrid team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "surf_private.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");// FIXME: connect this under windows
13 s_routing_t generic_routing;
14 xbt_dynar_t *routing_table;
17 }s_routing_full_t,*routing_full_t;
19 routing_t used_routing = NULL;
20 /* ************************************************************************** */
21 /* *************************** FULL ROUTING ********************************* */
23 #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
28 static void routing_full_parse_Shost(void) {
29 int *val = xbt_malloc(sizeof(int));
30 DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
31 *val = used_routing->host_count++;
32 xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
34 static int src_id = -1;
35 static int dst_id = -1;
36 static void routing_full_parse_Sroute_set_endpoints(void)
38 src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
39 dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
40 DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
41 route_action = A_surfxml_route_action;
43 static void routing_full_parse_Eroute(void)
46 if (src_id != -1 && dst_id != -1) {
47 name = bprintf("%x#%x", src_id, dst_id);
48 manage_route(route_table, name, route_action, 0);
54 static void routing_full_parse_end(void) {
55 routing_full_t routing = (routing_full_t) used_routing;
58 xbt_dict_cursor_t cursor = NULL;
59 char *key, *data, *end;
60 const char *sep = "#";
61 xbt_dynar_t links, keys;
62 char *link_name = NULL;
65 int host_count = routing->generic_routing.host_count;
67 /* Create the routing table */
68 routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
69 for (i=0;i<host_count;i++)
70 for (j=0;j<host_count;j++)
71 ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
73 /* Put the routes in position */
74 xbt_dict_foreach(route_table, cursor, key, data) {
76 links = (xbt_dynar_t) data;
77 keys = xbt_str_split_str(key, sep);
79 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
80 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
81 xbt_dynar_free(&keys);
83 DEBUG4("Handle %d %d (from %d hosts): %ld links",
84 src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
85 xbt_dynar_foreach(links, cpt, link_name) {
86 void* link = xbt_dict_get_or_null(link_set, link_name);
88 xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
90 THROW1(mismatch_error,0,"Link %s not found", link_name);
94 /* Add the loopback if needed */
95 for (i = 0; i < host_count; i++)
96 if (!xbt_dynar_length(ROUTE_FULL(i, i)))
97 xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
99 /* Shrink the dynar routes (save unused slots) */
100 for (i=0;i<host_count;i++)
101 for (j=0;j<host_count;j++)
102 xbt_dynar_shrink(ROUTE_FULL(i,j),0);
108 static xbt_dynar_t routing_full_get_route(int src,int dst) {
109 return ROUTE_FULL(src,dst);
112 static void routing_full_finalize(void) {
113 routing_full_t routing = (routing_full_t)used_routing;
117 for (i = 0; i < used_routing->host_count; i++)
118 for (j = 0; j < used_routing->host_count; j++)
119 xbt_dynar_free(&ROUTE_FULL(i, j));
120 free(routing->routing_table);
121 xbt_dict_free(&used_routing->host_id);
127 routing_t routing_model_full_create(size_t size_of_link,void *loopback) {
128 /* initialize our structure */
129 routing_full_t res = xbt_new0(s_routing_full_t,1);
130 res->generic_routing.name = "Full";
131 res->generic_routing.host_count = 0;
132 res->generic_routing.get_route = routing_full_get_route;
133 res->generic_routing.finalize = routing_full_finalize;
134 res->size_of_link = size_of_link;
135 res->loopback = loopback;
137 /* Set it in position */
138 used_routing = (routing_t) res;
140 /* Setup the parsing callbacks we need */
141 used_routing->host_id = xbt_dict_new();
142 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
143 surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
144 surfxml_add_callback(STag_surfxml_route_cb_list,
145 &routing_full_parse_Sroute_set_endpoints);
146 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);