Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Slight cleanups in the lua console
[simgrid.git] / src / bindings / lua / lua_console.c
index eeda51d..3c709a5 100644 (file)
@@ -7,6 +7,7 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "simgrid_lua.h"
+#include "simgrid/platf.h"
 #include <string.h>
 #include <ctype.h>
 
@@ -16,15 +17,6 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_console, bindings, "Lua Bindings");
 //AS List
 static xbt_dynar_t as_list_d;
 
-/*
- * Initialize platform model routing
- */
-
-static void create_AS(const char *id, const char *mode)
-{
-  surf_AS_new(id, mode);
-}
-
 /**
  * create host resource via CPU model [for MSG]
  */
@@ -257,11 +249,225 @@ static int AS_add(lua_State *L)
        return 0;
 }
 
+/**
+ * set function to process
+ */
+static int Host_set_function(lua_State * L)     //(host,function,nb_args,list_args)
+{
+       p_AS_attr p_as;
+       p_host_attr p_host;
+       unsigned int i,j;
+       const char *host_id ;
+       const char *function_id;
+       const char *args;
+       char * tmp_arg;
+
+   if (lua_istable(L, -1)) {
+        // get Host id
+        lua_pushstring(L, "host");
+        lua_gettable(L, -2);
+        host_id = lua_tostring(L, -1);
+        lua_pop(L, 1);
+        // get Function Name
+        lua_pushstring(L, "fct");
+        lua_gettable(L, -2);
+     function_id = lua_tostring(L, -1);
+     lua_pop(L, 1);
+     //get args
+     lua_pushstring(L,"args");
+     lua_gettable(L, -2);
+     args = lua_tostring(L,-1);
+     lua_pop(L, 1);
+   }
+   else {
+          XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
+          return -1;
+   }
+
+  // look for the index of host in host_list for each AS
+   xbt_dynar_foreach(as_list_d, i, p_as)
+   {
+          xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
+                  if (p_host->id == host_id) {
+                          p_host->function = function_id;
+                          p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
+                          // split & fill the args list
+                          tmp_arg = strtok((char*)args,",");
+                          while (tmp_arg != NULL) {
+                                  xbt_dynar_push(p_host->args_list, &tmp_arg);
+                                  tmp_arg = strtok(NULL,",");
+                          }
+                          return 0;
+                  }
+       }
+   }
+         XBT_ERROR("Host : %s Not Found !!", host_id);
+         return 1;
+
+}
+
+static int Host_set_property(lua_State* L)
+{
+       p_AS_attr p_as;
+       p_host_attr p_host;
+       unsigned int i,j;
+       const char* host_id ="";
+       const char* prop_id = "";
+       const char* prop_value = "";
+       if (lua_istable(L, -1)) {
+                // get Host id
+                lua_pushstring(L, "host");
+                lua_gettable(L, -2);
+                host_id = lua_tostring(L, -1);
+                lua_pop(L, 1);
+                // get Function Name
+                lua_pushstring(L, "prop_id");
+                lua_gettable(L, -2);
+            prop_id = lua_tostring(L, -1);
+            lua_pop(L, 1);
+            //get args
+            lua_pushstring(L,"prop_value");
+            lua_gettable(L, -2);
+            prop_value = lua_tostring(L,-1);
+            lua_pop(L, 1);
+        }
+       xbt_dynar_foreach(as_list_d, i, p_as)
+          {
+                  xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
+                          if (p_host->id == host_id) {
+                                  xbt_dict_set(p_host->properties, prop_id, xbt_strdup(prop_value), free);
+                          }
+                  }
+      }
+       return 1;
 
+}
 /*
- * add new host to platform hosts list
+ * surf parse bypass platform
+ * through CPU/network Models
  */
-static int Host_new(lua_State * L)
+
+static int surf_parse_bypass_platform()
+{
+  unsigned int i,j;
+  p_AS_attr p_as;
+  p_host_attr p_host;
+  p_link_attr p_link;
+  p_route_attr p_route;
+
+
+  // Add AS
+  xbt_dynar_foreach(as_list_d, i,p_as)
+  {
+    sg_platf_new_AS_open(p_as->id,p_as->mode);
+         // add associated Hosts
+         xbt_dynar_foreach(p_as->host_list_d, j, p_host){
+                 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
+                                 p_host->power_trace, p_host->core, p_host->state_initial,
+                                 p_host->state_trace, p_host->properties);
+
+
+                  //FIXME: should use sg_platf instead. That would add to routing model host list, amongst other benefits
+         }
+         // add associated Links
+         xbt_dynar_foreach(p_as->link_list_d, j, p_link){
+                 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
+                                 p_link->latency, p_link->latency_trace,
+                                 p_link->state_initial, p_link->state_trace,
+                                 p_link->policy);
+         }
+         // add associated Routes
+         xbt_dynar_foreach(p_as->route_list_d, j, p_route){
+                 surf_routing_add_route((char *) p_route->src_id,
+                                            (char *) p_route->dest_id, p_route->links_id);
+         }
+
+         // Finalize AS
+         sg_platf_new_AS_close();
+  }
+
+  // add traces
+  surf_add_host_traces();
+  surf_add_link_traces();
+
+  return 0;                     // must return 0 ?!!
+
+}
+
+/**
+ *
+ * surf parse bypass platform
+ * through workstation_ptask_L07 Model
+ */
+
+static int surf_wsL07_parse_bypass_platform()
+{
+  unsigned int i,j;
+  p_AS_attr p_as, p_sub_as;
+  p_host_attr p_host;
+  p_link_attr p_link;
+  p_route_attr p_route;
+
+  xbt_dynar_foreach(as_list_d, i, p_as)
+  {
+         // Init AS
+    sg_platf_new_AS_open(p_as->id,p_as->mode);
+
+         // add Sub AS
+
+         // Add Hosts
+         xbt_dynar_foreach(p_as->sub_as_list_id, j, p_sub_as) {
+                         //...
+         }
+         xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
+           create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
+                             p_host->power_trace, p_host->state_initial,
+                             p_host->state_trace);
+           //add to routing model host list
+         }
+         //add Links
+         xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
+             create_link_wsL07(p_link->id, p_link->bandwidth,
+                               p_link->bandwidth_trace, p_link->latency,
+                               p_link->latency_trace, p_link->state_initial,
+                               p_link->state_trace, p_link->policy);
+           }
+           // add route
+         xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
+               surf_routing_add_route((char *) p_route->src_id,
+                                      (char *) p_route->dest_id, p_route->links_id);
+             }
+         /* </AS> */
+         // Finalize AS
+         sg_platf_new_AS_close();
+  }
+  // add traces
+  surf_wsL07_add_traces();
+  return 0;
+}
+
+/*
+ * surf parse bypass application for MSG Module
+ */
+static int surf_parse_bypass_application()
+{
+  unsigned int i,j;
+  p_AS_attr p_as;
+  p_host_attr p_host;
+  xbt_dynar_foreach(as_list_d, i, p_as)
+  {
+         xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
+                 if (p_host->function)
+                         MSG_set_function(p_host->id, p_host->function, p_host->args_list);
+         }
+  }
+  return 0;
+}
+
+/*
+ * Public Methods
+ */
+int console_add_host(lua_State *L)
 {
   p_host_attr host;
   unsigned int i;
@@ -274,11 +480,11 @@ static int Host_new(lua_State * L)
   int state_initial,core;
   //get values from the table passed as argument
   if (lua_istable(L, -1)) {
-       // get AS id
-       lua_pushstring(L, "AS");
-       lua_gettable(L, -2);
-       AS_id = lua_tostring(L, -1);
-       lua_pop(L,1);
+  // get AS id
+  lua_pushstring(L, "AS");
+  lua_gettable(L, -2);
+  AS_id = lua_tostring(L, -1);
+  lua_pop(L,1);
 
     // get Id Value
     lua_pushstring(L, "id");
@@ -327,16 +533,16 @@ static int Host_new(lua_State * L)
     return -1;
   }
   xbt_dynar_foreach(as_list_d, i, p_as){
-         if (p_as->id == AS_id){
-                 current_as = p_as;
-                 break;
-         }
+    if (p_as->id == AS_id){
+      current_as = p_as;
+      break;
+    }
   }
 
   if (!current_as)
   {
-         XBT_ERROR("No AS_id :%s found",AS_id);
-         return -2;
+    XBT_ERROR("No AS_id :%s found",AS_id);
+    return -2;
   }
 
   host = malloc(sizeof(host_attr));
@@ -354,11 +560,7 @@ static int Host_new(lua_State * L)
   return 0;
 }
 
-/**
- * add link to AS links list
- */
-static int Link_new(lua_State * L)      // (id,bandwidth,latency)
-{
+int  console_add_link(lua_State *L) {
   const char *AS_id;
   unsigned int i;
   p_AS_attr p_as,current_as = NULL;
@@ -372,11 +574,11 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
   //get values from the table passed as argument
   if (lua_istable(L, -1)) {
 
-       //get AS id
-       lua_pushstring(L, "AS");
-       lua_gettable(L, -2);
-       AS_id = lua_tostring(L, -1);
-       lua_pop(L, 1);
+  //get AS id
+  lua_pushstring(L, "AS");
+  lua_gettable(L, -2);
+  AS_id = lua_tostring(L, -1);
+  lua_pop(L, 1);
 
     // get Id Value
     lua_pushstring(L, "id");
@@ -434,16 +636,16 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
     return -1;
   }
   xbt_dynar_foreach(as_list_d, i, p_as){
-         if (p_as->id == AS_id){
-                 current_as = p_as;
-                 break;
-         }
+    if (p_as->id == AS_id){
+      current_as = p_as;
+      break;
+    }
   }
 
   if (!current_as)
   {
-         XBT_ERROR("No AS_id :%s found",AS_id);
-         return -2;
+    XBT_ERROR("No AS_id :%s found",AS_id);
+    return -2;
   }
   p_link_attr link = malloc(sizeof(link_attr));
   link->id = id;
@@ -458,11 +660,48 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
 
   return 0;
 }
-
 /**
- * add route to AS routes list
+ * add Router to AS components
  */
-static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_table)
+int console_add_router(lua_State* L) {
+  p_router_attr router;
+  const char* AS_id;
+  unsigned int i;
+  p_AS_attr p_as,current_as = NULL;
+  const char* id;
+  if (lua_istable(L, -1)) {
+    // get AS id
+    lua_pushstring(L, "AS");
+    lua_gettable(L, -2);
+    AS_id = lua_tostring(L, -1);
+    lua_pop(L,1);
+
+    lua_pushstring(L, "id");
+    lua_gettable(L, -2);
+    id = lua_tostring(L, -1);
+    lua_pop(L,1);
+  }
+  xbt_dynar_foreach(as_list_d, i, p_as){
+      if (p_as->id == AS_id){
+        current_as = p_as;
+        break;
+      }
+    }
+
+    if (!current_as)
+    {
+      XBT_ERROR("No AS_id '%s' found",AS_id);
+      return -2;
+    }
+  router = malloc(sizeof(router_attr));
+  router->id = id;
+  xbt_dynar_push(current_as->router_list_d, &router);
+  return 0;
+
+}
+
+
+int console_add_route(lua_State *L)
 {
   const char* AS_id;
   unsigned int i;
@@ -474,25 +713,25 @@ static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_tab
 
   if (!lua_istable(L, 4)) { // if Route.new is declared as an indexed table (FIXME : we check the third arg if it's not a table)
 
-        //get AS_id
-        lua_pushstring(L, "AS");
-        lua_gettable(L, -2);
-        AS_id = lua_tostring(L, -1);
-        lua_pop(L, 1);
+   //get AS_id
+   lua_pushstring(L, "AS");
+   lua_gettable(L, -2);
+   AS_id = lua_tostring(L, -1);
+   lua_pop(L, 1);
 
-        xbt_dynar_foreach(as_list_d, i, p_as){
-         if (p_as->id == AS_id){
-                 current_as = p_as;
-                 break;
-         }
-        }
+   xbt_dynar_foreach(as_list_d, i, p_as){
+    if (p_as->id == AS_id){
+      current_as = p_as;
+      break;
+    }
+   }
 
-        if (!current_as)
-        {
-         XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
-         return -2;
-        }
-        // get Source Value
+   if (!current_as)
+   {
+    XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
+    return -2;
+   }
+   // get Source Value
      lua_pushstring(L, "src");
      lua_gettable(L, -2);
      route->src_id = lua_tostring(L, -1);
@@ -523,31 +762,31 @@ static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_tab
      return 0;
       }
   else { // Route.new is declared as a function
-        AS_id = luaL_checkstring(L, 1);
-        xbt_dynar_foreach(as_list_d, i, p_as){
-                 if (p_as->id == AS_id){
-                         current_as = p_as;
-                         break;
-                 }
-                }
-
-        if (!current_as)
-        {
-               XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
-               return -2;
-        }
+   AS_id = luaL_checkstring(L, 1);
+   xbt_dynar_foreach(as_list_d, i, p_as){
+      if (p_as->id == AS_id){
+        current_as = p_as;
+        break;
+      }
+     }
+
+   if (!current_as)
+   {
+    XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
+    return -2;
+   }
      route->src_id = luaL_checkstring(L, 2);
      route->dest_id = luaL_checkstring(L, 3);
      route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
      lua_pushnil(L);
      while (lua_next(L, 4) != 0)
-               {
-        link_id = lua_tostring(L, -1);
-        xbt_dynar_push(route->links_id, &link_id);
+    {
+       link_id = lua_tostring(L, -1);
+       xbt_dynar_push(route->links_id, &link_id);
          XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
-        lua_tostring(L, -1));
-        lua_pop(L, 1);
-       }
+       lua_tostring(L, -1));
+       lua_pop(L, 1);
+      }
     lua_pop(L, 1);
     //add route to platform's route list
     xbt_dynar_push(current_as->route_list_d, &route);
@@ -556,281 +795,6 @@ static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_tab
 
   return -1;
 }
-/**
- * add Router to AS components
- */
-static int Router_new(lua_State* L)
-{
-       p_router_attr router;
-       const char* AS_id;
-       unsigned int i;
-       p_AS_attr p_as,current_as = NULL;
-       const char* id;
-       if (lua_istable(L, -1)) {
-               // get AS id
-               lua_pushstring(L, "AS");
-               lua_gettable(L, -2);
-               AS_id = lua_tostring(L, -1);
-               lua_pop(L,1);
-
-               lua_pushstring(L, "id");
-               lua_gettable(L, -2);
-               id = lua_tostring(L, -1);
-               lua_pop(L,1);
-       }
-       xbt_dynar_foreach(as_list_d, i, p_as){
-                 if (p_as->id == AS_id){
-                         current_as = p_as;
-                         break;
-                 }
-         }
-
-         if (!current_as)
-         {
-                 XBT_ERROR("No AS_id :%s found",AS_id);
-                 return -2;
-         }
-       router = malloc(sizeof(router_attr));
-       router->id = id;
-       xbt_dynar_push(current_as->router_list_d, &router);
-       return 0;
-
-}
-
-/**
- * set function to process
- */
-static int Host_set_function(lua_State * L)     //(host,function,nb_args,list_args)
-{
-       p_AS_attr p_as;
-       p_host_attr p_host;
-       unsigned int i,j;
-       const char *host_id ;
-       const char *function_id;
-       const char *args;
-       char * tmp_arg;
-
-   if (lua_istable(L, -1)) {
-        // get Host id
-        lua_pushstring(L, "host");
-        lua_gettable(L, -2);
-        host_id = lua_tostring(L, -1);
-        lua_pop(L, 1);
-        // get Function Name
-        lua_pushstring(L, "fct");
-        lua_gettable(L, -2);
-     function_id = lua_tostring(L, -1);
-     lua_pop(L, 1);
-     //get args
-     lua_pushstring(L,"args");
-     lua_gettable(L, -2);
-     args = lua_tostring(L,-1);
-     lua_pop(L, 1);
-   }
-   else {
-          XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
-          return -1;
-   }
-
-  // look for the index of host in host_list for each AS
-   xbt_dynar_foreach(as_list_d, i, p_as)
-   {
-          xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
-                  if (p_host->id == host_id) {
-                          p_host->function = function_id;
-                          p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
-                          // split & fill the args list
-                          tmp_arg = strtok((char*)args,",");
-                          while (tmp_arg != NULL) {
-                                  xbt_dynar_push(p_host->args_list, &tmp_arg);
-                                  tmp_arg = strtok(NULL,",");
-                          }
-                          return 0;
-                  }
-       }
-   }
-         XBT_ERROR("Host : %s Not Found !!", host_id);
-         return 1;
-
-}
-
-static int Host_set_property(lua_State* L)
-{
-       p_AS_attr p_as;
-       p_host_attr p_host;
-       unsigned int i,j;
-       const char* host_id ="";
-       const char* prop_id = "";
-       const char* prop_value = "";
-       if (lua_istable(L, -1)) {
-                // get Host id
-                lua_pushstring(L, "host");
-                lua_gettable(L, -2);
-                host_id = lua_tostring(L, -1);
-                lua_pop(L, 1);
-                // get Function Name
-                lua_pushstring(L, "prop_id");
-                lua_gettable(L, -2);
-            prop_id = lua_tostring(L, -1);
-            lua_pop(L, 1);
-            //get args
-            lua_pushstring(L,"prop_value");
-            lua_gettable(L, -2);
-            prop_value = lua_tostring(L,-1);
-            lua_pop(L, 1);
-        }
-       xbt_dynar_foreach(as_list_d, i, p_as)
-          {
-                  xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
-                          if (p_host->id == host_id) {
-                                  xbt_dict_set(p_host->properties, prop_id, xbt_strdup(prop_value), free);
-                          }
-                  }
-      }
-       return 1;
-
-}
-/*
- * surf parse bypass platform
- * through CPU/network Models
- */
-
-static int surf_parse_bypass_platform()
-{
-  unsigned int i,j;
-  p_AS_attr p_as;
-  p_host_attr p_host;
-  p_link_attr p_link;
-  p_route_attr p_route;
-
-
-  // Add AS
-  xbt_dynar_foreach(as_list_d, i,p_as)
-  {
-         create_AS(p_as->id, p_as->mode);
-         // add associated Hosts
-         xbt_dynar_foreach(p_as->host_list_d, j, p_host){
-                 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
-                                 p_host->power_trace, p_host->core, p_host->state_initial,
-                                 p_host->state_trace, p_host->properties);
-
-
-                  //add to routing model host list
-                  surf_route_add_host((char *) p_host->id);
-         }
-         // add associated Links
-         xbt_dynar_foreach(p_as->link_list_d, j, p_link){
-                 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
-                                 p_link->latency, p_link->latency_trace,
-                                 p_link->state_initial, p_link->state_trace,
-                                 p_link->policy);
-         }
-         // add associated Routes
-         xbt_dynar_foreach(p_as->route_list_d, j, p_route){
-                 surf_routing_add_route((char *) p_route->src_id,
-                                            (char *) p_route->dest_id, p_route->links_id);
-         }
-
-         // Finalize AS
-         surf_AS_finalize(p_as->id);
-  }
-
-  // add traces
-  surf_add_host_traces();
-  surf_add_link_traces();
-
-  return 0;                     // must return 0 ?!!
-
-}
-
-/**
- *
- * surf parse bypass platform
- * through workstation_ptask_L07 Model
- */
-
-static int surf_wsL07_parse_bypass_platform()
-{
-  unsigned int i,j;
-  p_AS_attr p_as, p_sub_as;
-  p_host_attr p_host;
-  p_link_attr p_link;
-  p_route_attr p_route;
-
-  xbt_dynar_foreach(as_list_d, i, p_as)
-  {
-         // Init AS
-         create_AS(p_as->id, p_as->mode);
-
-         // add Sub AS
-
-         // Add Hosts
-         xbt_dynar_foreach(p_as->sub_as_list_id, j, p_sub_as) {
-                         //...
-         }
-         xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
-           create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
-                             p_host->power_trace, p_host->state_initial,
-                             p_host->state_trace);
-           //add to routing model host list
-           surf_route_add_host((char *) p_host->id);
-         }
-         //add Links
-         xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
-             create_link_wsL07(p_link->id, p_link->bandwidth,
-                               p_link->bandwidth_trace, p_link->latency,
-                               p_link->latency_trace, p_link->state_initial,
-                               p_link->state_trace, p_link->policy);
-           }
-           // add route
-         xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
-               surf_routing_add_route((char *) p_route->src_id,
-                                      (char *) p_route->dest_id, p_route->links_id);
-             }
-         /* </AS> */
-         // Finalize AS
-         surf_AS_finalize(p_as->id);
-  }
-  // add traces
-  surf_wsL07_add_traces();
-  return 0;
-}
-
-/*
- * surf parse bypass application for MSG Module
- */
-static int surf_parse_bypass_application()
-{
-  unsigned int i,j;
-  p_AS_attr p_as;
-  p_host_attr p_host;
-  xbt_dynar_foreach(as_list_d, i, p_as)
-  {
-         xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
-                 if (p_host->function)
-                         MSG_set_function(p_host->id, p_host->function, p_host->args_list);
-         }
-  }
-  return 0;
-}
-
-/*
- * Public Methods
- */
-int console_add_host(lua_State *L)
-{
-       return Host_new(L);
-}
-
-int  console_add_link(lua_State *L)
-{
-       return Link_new(L);
-}
-
-int console_add_route(lua_State *L)
-{
-       return Route_new(L);
-}
 
 int console_add_AS(lua_State *L)
 {