Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implementing recursive call to get_onelink_routes
[simgrid.git] / src / surf / surf_routing.c
index c5fabbb..a0b74ee 100644 (file)
@@ -1,11 +1,17 @@
-/* Copyright (c) 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2009, 2010. The SimGrid Team. 
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+
+
 #include <float.h>
-#include <pcre.h> /* regular expresion library */
+#include "gras_config.h"
+
+#ifdef HAVE_PCRE_LIB
+       #include <pcre.h> /* regular expresion library */
+#endif
 #include "surf_private.h"
 #include "xbt/dynar.h"
 #include "xbt/str.h"
@@ -86,6 +92,8 @@ static void generic_set_bypassroute(routing_component_t rc, const char* src, con
 /* ************************************************************************** */
 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
 
+static xbt_dynar_t generic_get_onelink_routes (routing_component_t rc);
+static int generic_is_router (const char *name);
 static route_extended_t generic_get_bypassroute(routing_component_t rc, const char* src, const char* dst);
 
 /* ************************************************************************** */
@@ -218,7 +226,7 @@ static void parse_S_bypassRoute_new_and_endpoints(void) {
 /**
  * \brief Set a new link on the actual list of link for a route or ASroute
  */
-static void parse_E_link_c_ctn_new_elem(char *link_id) {
+static void parse_E_link_ctn_new_elem(char *link_id) {
   char *val;
   val = xbt_strdup(link_id);
   xbt_dynar_push(link_list, &val);
@@ -228,9 +236,9 @@ static void parse_E_link_c_ctn_new_elem(char *link_id) {
  * \brief Set a new link on the actual list of link for a route or ASroute from XML
  */
 
-static void parse_E_link_c_ctn_new_elem_XML(void)
+static void parse_E_link_ctn_new_elem_XML(void)
 {
-       parse_E_link_c_ctn_new_elem(A_surfxml_link_c_ctn_id);
+       parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
 }
 
 /**
@@ -238,7 +246,7 @@ static void parse_E_link_c_ctn_new_elem_XML(void)
  */
 static void parse_E_link_c_ctn_new_elem_lua(char *link_id) {
 
-       parse_E_link_c_ctn_new_elem(link_id);
+       parse_E_link_ctn_new_elem(link_id);
 }
 
 /**
@@ -669,14 +677,43 @@ static void finalize(void) {
   xbt_free(global_routing);
 }
 
-static void get_onelink_routes(void)
+static xbt_dynar_t recursive_get_onelink_routes (routing_component_t rc)
+{
+  xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
+
+  //adding my one link routes
+  unsigned int cpt;
+  void *link;
+  xbt_dynar_t onelink_mine = rc->get_onelink_routes (rc);
+  if (onelink_mine){
+    xbt_dynar_foreach(onelink_mine, cpt, link) {
+      xbt_dynar_push(ret,&link);
+    }
+  }
+
+  //recursing
+  char *key;
+  xbt_dict_cursor_t cursor=NULL;
+  routing_component_t rc_child;
+  xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
+    xbt_dynar_t onelink_child = recursive_get_onelink_routes (rc_child);//->get_onelink_routes (rc_child);
+    if (onelink_child){
+      xbt_dynar_foreach(onelink_child, cpt, link) {
+        xbt_dynar_push(ret,&link);
+      }
+    }
+  }
+  return ret;
+}
+
+static xbt_dynar_t get_onelink_routes(void)
 {
-       xbt_die("get_onelink_routes function not implemented yet!!!");
+  return recursive_get_onelink_routes (global_routing->root);
 }
 
 static int is_router(const char *name)
 {
-       xbt_die("is_router function not implemented yet!!!");
+       xbt_die("global \"is_router\" function not implemented yet");
 }
 
 /**
@@ -691,6 +728,8 @@ void routing_model_create(size_t size_of_links, void* loopback) {
   global_routing->where_network_elements = xbt_dict_new();
   global_routing->root = NULL;
   global_routing->get_route = get_route;
+  global_routing->get_onelink_routes = get_onelink_routes;
+  global_routing->is_router = is_router;
   global_routing->finalize = finalize;
   global_routing->loopback = loopback;
   global_routing->size_of_link = size_of_links;
@@ -707,7 +746,7 @@ void routing_model_create(size_t size_of_links, void* loopback) {
   surfxml_add_callback(STag_surfxml_ASroute_cb_list, &parse_S_ASroute_new_and_endpoints);
   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list, &parse_S_bypassRoute_new_and_endpoints);
   
-  surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_E_link_c_ctn_new_elem_XML);
+  surfxml_add_callback(ETag_surfxml_link_ctn_cb_list, &parse_E_link_ctn_new_elem_XML);
   
   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_E_route_store_route);
   surfxml_add_callback(ETag_surfxml_ASroute_cb_list, &parse_E_ASroute_store_route);
@@ -736,6 +775,40 @@ typedef struct {
 } s_routing_component_full_t,*routing_component_full_t;
 
 /* Business methods */
+static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
+{
+  xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
+
+  routing_component_full_t routing = (routing_component_full_t)rc;
+  int table_size = xbt_dict_length(routing->to_index);
+  xbt_dict_cursor_t c1 = NULL, c2 = NULL;
+  char *k1, *d1, *k2, *d2;
+  xbt_dict_foreach(routing->to_index, c1, k1, d1) {
+    xbt_dict_foreach (routing->to_index, c2, k2, d2) {
+      int *src_id = xbt_dict_get_or_null(routing->to_index, k1);
+      int *dst_id = xbt_dict_get_or_null(routing->to_index, k2);
+      xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",src,dst);
+      route_extended_t route = TO_ROUTE_FULL(*src_id,*dst_id);
+      if (route){
+        if (xbt_dynar_length(route->generic_route.link_list) == 1){
+          void *link = *(void**)xbt_dynar_get_ptr(route->generic_route.link_list,0);
+
+          onelink_t onelink = xbt_new0 (s_onelink_t, 1);
+          onelink->src = xbt_strdup (k1);
+          onelink->dst = xbt_strdup (k2);
+          onelink->link_ptr = link;
+          xbt_dynar_push (ret, &onelink);
+        }
+      }
+    }
+  }
+  return ret;
+}
+
+static int full_is_router(const char *name)
+{
+  xbt_die("\"full_is_router\" function not implemented yet");
+}
 
 static route_extended_t full_get_route(routing_component_t rc, const char* src,const char* dst) {
   xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
@@ -797,6 +870,8 @@ static void* model_full_create(void) {
   new_component->generic_routing.set_ASroute = generic_set_ASroute;
   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
   new_component->generic_routing.get_route = full_get_route;
+  new_component->generic_routing.get_onelink_routes = full_get_onelink_routes;
+  new_component->generic_routing.is_router = full_is_router;
   new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
   new_component->generic_routing.finalize = full_finalize;
   new_component->to_index = xbt_dict_new();
@@ -895,6 +970,15 @@ typedef struct {
 } s_routing_component_floyd_t,*routing_component_floyd_t;
 
 /* Business methods */
+static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
+{
+  xbt_die("\"floyd_get_onelink_routes\" function not implemented yet");
+}
+
+static int floyd_is_router(const char *name)
+{
+  xbt_die("\"floyd_is_router\" function not implemented yet");
+}
 
 static route_extended_t floyd_get_route(routing_component_t rc, const char* src,const char* dst) {
   xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
@@ -995,6 +1079,8 @@ static void* model_floyd_create(void) {
   new_component->generic_routing.set_ASroute = generic_set_ASroute;
   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
   new_component->generic_routing.get_route = floyd_get_route;
+  new_component->generic_routing.get_onelink_routes = floyd_get_onelink_routes;
+  new_component->generic_routing.is_router = floyd_is_router;
   new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
   new_component->generic_routing.finalize = floyd_finalize;
   new_component->to_index = xbt_dict_new();
@@ -1243,6 +1329,15 @@ static void add_loopback_dijkstra(routing_component_dijkstra_t rc) {
 }
 
 /* Business methods */
+static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
+{
+  xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
+}
+
+static int dijkstra_is_router(const char *name)
+{
+  xbt_die("\"dijkstra_is_router\" function not implemented yet");
+}
 
 static route_extended_t dijkstra_get_route(routing_component_t rc, const char* src,const char* dst) {
   xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
@@ -1449,6 +1544,8 @@ static void* model_dijkstra_both_create(int cached) {
   new_component->generic_routing.set_ASroute = generic_set_ASroute;
   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
   new_component->generic_routing.get_route = dijkstra_get_route;
+  new_component->generic_routing.get_onelink_routes = dijkstra_get_onelink_routes;
+  new_component->generic_routing.is_router = dijkstra_is_router;
   new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
   new_component->generic_routing.finalize = dijkstra_finalize;
   new_component->cached = cached;
@@ -1691,6 +1788,16 @@ static char* remplace(char* value, const char** src_list, int src_size, const ch
   return xbt_strdup(result_result);
 }
 
+static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
+{
+  xbt_die("\"rulebased_get_onelink_routes\" function not implemented yet");
+}
+
+static int rulebased_is_router(const char *name)
+{
+  xbt_die("\"rulebased_is_router\" function not implemented yet");
+}
+
 /* Business methods */
 static route_extended_t rulebased_get_route(routing_component_t rc, const char* src,const char* dst) {
   xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
@@ -1793,6 +1900,8 @@ static void* model_rulebased_create(void) {
   new_component->generic_routing.set_route = model_rulebased_set_route;
   new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
   new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
+  new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
+  new_component->generic_routing.is_router = rulebased_is_router;
   new_component->generic_routing.get_route = rulebased_get_route;
   new_component->generic_routing.get_bypass_route = NULL; //rulebased_get_bypass_route;
   new_component->generic_routing.finalize = rulebased_finalize;
@@ -1824,6 +1933,12 @@ typedef struct {
 } s_routing_component_none_t,*routing_component_none_t;
 
 /* Business methods */
+static xbt_dynar_t none_get_onelink_routes(routing_component_t rc){
+  return NULL;
+}
+static int none_is_router(const char *name){
+  return -1;
+}
 static route_extended_t none_get_route(routing_component_t rc, const char* src,const char* dst){
   return NULL;
 }
@@ -1846,6 +1961,8 @@ static void* model_none_create(void) {
   new_component->generic_routing.set_ASroute = NULL;
   new_component->generic_routing.set_bypassroute = NULL;
   new_component->generic_routing.get_route = none_get_route;
+  new_component->generic_routing.get_onelink_routes = none_get_onelink_routes;
+  new_component->generic_routing.is_router = none_is_router;
   new_component->generic_routing.get_bypass_route = none_get_bypass_route;
   new_component->generic_routing.finalize = none_finalize;
   return new_component;
@@ -2044,6 +2161,16 @@ static void generic_set_bypassroute(routing_component_t rc, const char* src, con
 /* ************************************************************************** */
 /* *********************** GENERIC BUSINESS METHODS ************************* */
 
+static xbt_dynar_t generic_get_onelink_routes (routing_component_t rc)
+{
+  xbt_die("\"generic_get_onelink_routes\" not implemented yet");
+}
+
+static int generic_is_router (const char *name)
+{
+  xbt_die("\"generic_is_router\" not implemented yet");
+}
+
 static route_extended_t generic_get_bypassroute(routing_component_t rc, const char* src, const char* dst) {
   model_type_t modeltype = rc->routing;
   xbt_dict_t dict_bypassRoutes;
@@ -2055,7 +2182,7 @@ static route_extended_t generic_get_bypassroute(routing_component_t rc, const ch
   } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
           modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
     dict_bypassRoutes = ((routing_component_dijkstra_t)rc)->bypassRoutes;
-  } else xbt_die("\"generic_set_bypassroute\" not supported");
+  } else xbt_die("\"generic_get_bypassroute\" not supported");
  
 
   routing_component_t src_as, dst_as;
@@ -2309,6 +2436,11 @@ static void routing_full_parse_Scluster(void)
        int start, end, i;
        xbt_dynar_t radical_elements;
        xbt_dynar_t radical_ends;
+       #ifndef HAVE_PCRE_LIB
+               xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
+               char *route_src,*route_dst;
+               int j;
+       #endif
 
        static unsigned int surfxml_buffer_stack_stack_ptr = 1;
        static unsigned int surfxml_buffer_stack_stack[1024];
@@ -2317,9 +2449,14 @@ static void routing_full_parse_Scluster(void)
 
        surfxml_bufferstack_push(1);
 
-       DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">",cluster_id);
        SURFXML_BUFFER_SET(AS_id, cluster_id);
+#ifdef HAVE_PCRE_LIB
        SURFXML_BUFFER_SET(AS_routing, "RuleBased");
+       DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">",cluster_id);
+#else
+       SURFXML_BUFFER_SET(AS_routing, "Full");
+       DEBUG1("<AS id=\"%s\"\trouting=\"Full\">",cluster_id);
+#endif
        SURFXML_START_TAG(AS);
 
        radical_elements = xbt_str_split(cluster_radical, ",");
@@ -2331,6 +2468,9 @@ static void routing_full_parse_Scluster(void)
                        case 1:
                          surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
                          host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
+                         #ifndef HAVE_PCRE_LIB
+                         xbt_dynar_push_as(tab_elements_num, int, start);
+                         #endif
                          link_id = bprintf("%s_link_%d", cluster_id, start);
 
                          DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>",host_id,cluster_power);
@@ -2369,6 +2509,9 @@ static void routing_full_parse_Scluster(void)
                          for (i = start; i <= end; i++)
                          {
                                  host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
+                                 #ifndef HAVE_PCRE_LIB
+                                 xbt_dynar_push_as(tab_elements_num, int, i);
+                                 #endif
                                  link_id = bprintf("%s_link_%d", cluster_id, i);
 
                                  DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>",host_id,cluster_power);
@@ -2454,28 +2597,67 @@ static void routing_full_parse_Scluster(void)
 
        DEBUG0(" ");
 
+#ifdef HAVE_PCRE_LIB
+
        DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">",route_src_dst,route_src_dst);
        SURFXML_BUFFER_SET(route_src, route_src_dst);
        SURFXML_BUFFER_SET(route_dst, route_src_dst);
        SURFXML_START_TAG(route);
 
        DEBUG1("<link:ctn\tid=\"%s_link_$1src\"/>",cluster_id);
-       SURFXML_BUFFER_SET(link_c_ctn_id, bprintf("%s_link_$1src",cluster_id));
-       SURFXML_START_TAG(link_c_ctn);
-       SURFXML_END_TAG(link_c_ctn);
+       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src",cluster_id));
+       SURFXML_START_TAG(link_ctn);
+       SURFXML_END_TAG(link_ctn);
 
        DEBUG1("<link:ctn\tid=\"%s_backbone\"/>",cluster_id);
-       SURFXML_BUFFER_SET(link_c_ctn_id, bprintf("%s_backbone",cluster_id));
-       SURFXML_START_TAG(link_c_ctn);
-       SURFXML_END_TAG(link_c_ctn);
+       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone",cluster_id));
+       SURFXML_START_TAG(link_ctn);
+       SURFXML_END_TAG(link_ctn);
 
        DEBUG1("<link:ctn\tid=\"%s_link_$1dst\"/>",cluster_id);
-       SURFXML_BUFFER_SET(link_c_ctn_id, bprintf("%s_link_$1dst",cluster_id));
-       SURFXML_START_TAG(link_c_ctn);
-       SURFXML_END_TAG(link_c_ctn);
+       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst",cluster_id));
+       SURFXML_START_TAG(link_ctn);
+       SURFXML_END_TAG(link_ctn);
 
        DEBUG0("</route>");
        SURFXML_END_TAG(route);
+#else
+       for(i=0 ; i<=tab_elements_num->elmsize ; i++)
+       {
+               for(j=0 ; j<=tab_elements_num->elmsize ; j++)
+               {
+                       route_src = bprintf("%s%d%s",cluster_prefix,xbt_dynar_get_as(tab_elements_num,i,int),cluster_suffix);
+                       route_dst = bprintf("%s%d%s",cluster_prefix,xbt_dynar_get_as(tab_elements_num,j,int),cluster_suffix);
+
+                       DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">",route_src,route_dst);
+                       SURFXML_BUFFER_SET(route_src, route_src);
+                       SURFXML_BUFFER_SET(route_dst, route_dst);
+                       SURFXML_START_TAG(route);
+
+                       route_src = bprintf("%s_link_%d",cluster_id,xbt_dynar_get_as(tab_elements_num,i,int));
+                       route_dst = bprintf("%s_link_%d",cluster_id,xbt_dynar_get_as(tab_elements_num,j,int));
+
+                       DEBUG2("<link:ctn\tid=\"%s_link_%d\"/>",cluster_id,xbt_dynar_get_as(tab_elements_num,i,int));
+                       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_%d",cluster_id,xbt_dynar_get_as(tab_elements_num,i,int)));
+                       SURFXML_START_TAG(link_ctn);
+                       SURFXML_END_TAG(link_ctn);
+
+                       DEBUG1("<link:ctn\tid=\"%s_backbone\"/>",cluster_id);
+                       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone",cluster_id));
+                       SURFXML_START_TAG(link_ctn);
+                       SURFXML_END_TAG(link_ctn);
+
+                       DEBUG2("<link:ctn\tid=\"%s_link_%d\"/>",cluster_id,xbt_dynar_get_as(tab_elements_num,j,int));
+                       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_%d",cluster_id,xbt_dynar_get_as(tab_elements_num,j,int)));
+                       SURFXML_START_TAG(link_ctn);
+                       SURFXML_END_TAG(link_ctn);
+
+                       DEBUG0("</route>");
+                       SURFXML_END_TAG(route);
+               }
+       }
+       xbt_dynar_free(&tab_elements_num);
+#endif
 
        DEBUG0("</AS>");
        SURFXML_END_TAG(AS);