Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove spurious else..
[simgrid.git] / src / bindings / lua / lua_console.c
index f578f13..234a8dd 100644 (file)
@@ -7,8 +7,10 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "simgrid_lua.h"
+#include <string.h>
+#include <ctype.h>
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(luax, bindings, "Lua Bindings");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_console, bindings, "Lua Bindings");
 
 static p_AS_attr AS;
 //using xbt_dynar_t :
@@ -30,16 +32,19 @@ static void create_AS(const char *id, const char *mode)
  */
 
 static void create_host(const char *id, double power_peak, double power_sc,
-                        const char *power_tr, int state_init,
+                        const char *power_tr,int core,int state_init,
                         const char *state_tr)
 {
 
   double power_scale = 1.0;
+  int core_nb = 1; //default value
   tmgr_trace_t power_trace = NULL;
   e_surf_resource_state_t state_initial;
   tmgr_trace_t state_trace;
   if (power_sc)                 // !=0
     power_scale = power_sc;
+  if (core)
+         core_nb = core; //default value
   if (state_init == -1)
     state_initial = SURF_RESOURCE_OFF;
   else
@@ -54,7 +59,7 @@ static void create_host(const char *id, double power_peak, double power_sc,
     state_trace = tmgr_trace_new("");
   current_property_set = xbt_dict_new();
   surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
-                            power_trace, state_initial, state_trace,
+                            power_trace, core_nb, state_initial, state_trace,
                             current_property_set);
 
 }
@@ -192,7 +197,7 @@ static int AS_new(lua_State * L)
     mode = lua_tostring(L, -1);
     lua_pop(L, 1);
   } else {
-    ERROR0
+    XBT_ERROR
         ("Bad Arguments to AS.new, Should be a table with named arguments");
     return -1;
   }
@@ -217,7 +222,7 @@ static int Host_new(lua_State * L)
   const char *power_trace;
   const char *state_trace;
   double power, power_scale;
-  int state_initial;
+  int state_initial,core;
   //get values from the table passed as argument
   if (lua_istable(L, -1)) {
 
@@ -245,6 +250,11 @@ static int Host_new(lua_State * L)
     power_trace = lua_tostring(L, -1);
     lua_pop(L, 1);
 
+    lua_pushstring(L, "core");
+    lua_gettable(L, -2);
+    core = lua_tonumber(L, -1);
+    lua_pop(L, 1);
+
     //get state initial
     lua_pushstring(L, "state_initial");
     lua_gettable(L, -2);
@@ -258,7 +268,7 @@ static int Host_new(lua_State * L)
     lua_pop(L, 1);
 
   } else {
-    ERROR0
+    XBT_ERROR
         ("Bad Arguments to create host, Should be a table with named arguments");
     return -1;
   }
@@ -268,6 +278,7 @@ static int Host_new(lua_State * L)
   host->power_peak = power;
   host->power_scale = power_scale;
   host->power_trace = power_trace;
+  host->core = core;
   host->state_initial = state_initial;
   host->state_trace = state_trace;
   host->function = NULL;
@@ -281,10 +292,12 @@ static int Host_new(lua_State * L)
  */
 static int Link_new(lua_State * L)      // (id,bandwidth,latency)
 {
+
   if (xbt_dynar_is_empty(link_list_d))
     link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
 
-  const char *id;
+
+  const char* id;
   double bandwidth, latency;
   const char *bandwidth_trace;
   const char *latency_trace;
@@ -337,7 +350,6 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
     state_initial = lua_tonumber(L, -1);
     lua_pop(L, 1);
 
-
     //get policy value
     lua_pushstring(L, "policy");
     lua_gettable(L, -2);
@@ -345,7 +357,7 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
     lua_pop(L, 1);
 
   } else {
-    ERROR0
+    XBT_ERROR
         ("Bad Arguments to create link, Should be a table with named arguments");
     return -1;
   }
@@ -360,6 +372,7 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
   link->state_initial = state_initial;
   link->policy = policy;
   xbt_dynar_push(link_list_d, &link);
+
   return 0;
 }
 
@@ -368,64 +381,120 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
  */
 static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_table)
 {
-  if (xbt_dynar_is_empty(route_list_d))
-    route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
-  const char *link_id;
-  p_route_attr route = malloc(sizeof(route_attr));
-  route->src_id = luaL_checkstring(L, 1);
-  route->dest_id = luaL_checkstring(L, 2);
-  route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
-  lua_pushnil(L);
-  while (lua_next(L, 3) != 0) {
-    link_id = lua_tostring(L, -1);
-    xbt_dynar_push(route->links_id, &link_id);
-    DEBUG2("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
-           lua_tostring(L, -1));
-    lua_pop(L, 1);
-  }
-  lua_pop(L, 1);
+ if (xbt_dynar_is_empty(route_list_d))
+           route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
+
+        const char *links;
+        const char* link_id;
+        p_route_attr route = malloc(sizeof(route_attr));
+
+
+  if (!lua_istable(L, 3)) { // if Route.new is declared as an indexed table (FIXME : we check the third arg if it's not a table)
+     // get Source Value
+     lua_pushstring(L, "src");
+     lua_gettable(L, -2);
+     route->src_id = lua_tostring(L, -1);
+     lua_pop(L, 1);
+
+     // get Destination Value
+     lua_pushstring(L, "dest");
+     lua_gettable(L, -2);
+     route->dest_id = lua_tostring(L, -1);
+     lua_pop(L, 1);
+
+     // get Links Table (char* to be splited later)
+     lua_pushstring(L, "links");
+     lua_gettable(L, -2);
+     links = lua_tostring(L, -1);
+     lua_pop(L,1);
+
+     route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
+
+     char *tmp_links = xbt_strdup(links);
+     link_id = strtok(tmp_links,",");   //tmp_link = strtok((char*)links,",");
+     while(link_id != NULL)
+       {
+          xbt_dynar_push(route->links_id, &link_id);
+          link_id = strtok(NULL,","); //Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
+        }
+     xbt_dynar_push(route_list_d, &route);
+     return 0;
 
-  //add route to platform's route list
-  xbt_dynar_push(route_list_d, &route);
-  return 0;
-}
+      }
+  else { // Route.new is declared as a function
+     //const char* link_id;
+     route->src_id = luaL_checkstring(L, 1);
+     route->dest_id = luaL_checkstring(L, 2);
+     route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
+     lua_pushnil(L);
+     while (lua_next(L, 3) != 0)
+               {
+        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_pop(L, 1);
+         //add route to platform's route list
+         xbt_dynar_push(route_list_d, &route);
+         return 0;
+    }
 
+  return -1;
+}
 /**
  * set function to process
  */
 static int Host_set_function(lua_State * L)     //(host,function,nb_args,list_args)
 {
-  // look for the index of host in host_list
-  const char *host_id = luaL_checkstring(L, 1);
-  const char *argument;
-  unsigned int i;
-  p_host_attr p_host;
+       p_host_attr p_host;
+       const char *host;
+       const char *function;
+       const char *args;
+       char * tmp_arg;
+       unsigned int i;
+
+   if (lua_istable(L, -1)) {
+        // get Host id
+        lua_pushstring(L, "host");
+        lua_gettable(L, -2);
+        host = lua_tostring(L, -1);
+        lua_pop(L, 1);
+        // get Function Name
+        lua_pushstring(L, "fct");
+        lua_gettable(L, -2);
+     function = 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
   xbt_dynar_foreach(host_list_d, i, p_host) {
-    if (p_host->id == host_id) {
-      p_host->function = luaL_checkstring(L, 2);
-      if (lua_istable(L, 3)) {
-        p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
-        // fill the args list
-        lua_pushnil(L);
-        int j = 0;
-        while (lua_next(L, 3) != 0) {
-          argument = lua_tostring(L, -1);
-          xbt_dynar_push(p_host->args_list, &argument);
-          DEBUG2("index = %f , Arg_id = %s \n", lua_tonumber(L, -2),
-                 lua_tostring(L, -1));
-          j++;
-          lua_pop(L, 1);
+         if (p_host->id == host) {
+      p_host->function = function;
+      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,",");
         }
-      }
-      lua_pop(L, 1);
       return 0;
-    }
-  }
-  ERROR1("Host : %s Not Found !!", host_id);
-  return 1;
-}
+          }
+      }
+         XBT_ERROR("Host : %s Not Found !!", host);
+         return 1;
 
+}
 /*
  * surf parse bypass platform
  * through CPU/network Models
@@ -441,11 +510,10 @@ static int surf_parse_bypass_platform()
   // Init routing mode
   create_AS(AS->id, AS->mode);
 
-
   // Add Hosts
   xbt_dynar_foreach(host_list_d, i, p_host) {
     create_host(p_host->id, p_host->power_peak, p_host->power_scale,
-                p_host->power_trace, p_host->state_initial,
+                p_host->power_trace, p_host->core, p_host->state_initial,
                 p_host->state_trace);
     //add to routing model host list
     surf_route_add_host((char *) p_host->id);
@@ -485,6 +553,7 @@ static int surf_parse_bypass_platform()
 
 static int surf_wsL07_parse_bypass_platform()
 {
+
   unsigned int i;
   p_host_attr p_host;
   p_link_attr p_link;
@@ -538,7 +607,6 @@ static int surf_parse_bypass_application()
   return 0;
 }
 
-
 /*
  * Public Methods
  */