Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added support for using lua platform files from C
[simgrid.git] / src / bindings / lua / lua_platf.c
index 7514c71..e21b30e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010. The SimGrid Team.
+/* Copyright (c) 2010, 2012-2015. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -41,13 +41,12 @@ static const luaL_reg platf_functions[] = {
 int console_open(lua_State *L) {
   sg_platf_init();
   sg_platf_begin();
-  surf_parse_init_callbacks();
-  
-  storage_register_callbacks();  
+
+  storage_register_callbacks();
   routing_register_callbacks();
 
   gpu_register_callbacks();
-  
+
   return 0;
 }
 
@@ -62,10 +61,12 @@ int console_close(lua_State *L) {
   /* Initialize MSG and WKS hosts */
   XBT_DEBUG("Initialize MSG and WKS hosts");
   xbt_lib_foreach(host_lib, cursor, name, data) {
-    if(data[SURF_WKS_LEVEL]){
+    if(data[SURF_HOST_LEVEL]){
       XBT_DEBUG("\tSee surf host %s",name);
-      SIMIX_host_create(name, data[SURF_WKS_LEVEL], NULL);
-      __MSG_host_create((smx_host_t)data[SIMIX_HOST_LEVEL]);
+      SIMIX_host_create(name);
+      // THIS IS BRAINDEAD. There is no sg_host_t in that level, but a smx_host_priv. So commenting out for now.
+      // Lua is broken anyway. Christian will fix it
+      // __MSG_host_create((sg_host_t)data[SIMIX_HOST_LEVEL]);
     }
   }
 
@@ -93,7 +94,8 @@ int console_add_host(lua_State *L) {
   // get power value
   lua_pushstring(L, "power");
   lua_gettable(L, -2);
-  host.power_peak = lua_tonumber(L, -1);
+  host.power_peak = xbt_dynar_new(sizeof(double), NULL);
+  xbt_dynar_push_as(host.power_peak, double, lua_tonumber(L, -1));
   lua_pop(L, 1);
 
   // get core
@@ -108,7 +110,7 @@ int console_add_host(lua_State *L) {
   //get power_scale
   lua_pushstring(L, "power_scale");
   lua_gettable(L, -2);
-  if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value  
+  if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value
   else host.power_scale = lua_tonumber(L, -1);
   lua_pop(L, 1);
 
@@ -246,14 +248,13 @@ int console_add_router(lua_State* L) {
 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
 
 int console_add_route(lua_State *L) {
+  XBT_DEBUG("Adding route");
   s_sg_platf_route_cbarg_t route;
   memset(&route,0,sizeof(route));
 
   /* allocating memory for the buffer, I think 2kB should be enough */
   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
 
-  int is_symmetrical;
-
   if (! lua_istable(L, -1)) {
     XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
     return -1;
@@ -276,24 +277,29 @@ int console_add_route(lua_State *L) {
     xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
   lua_pop(L,1);
 
+  /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
+   * Et ouais mon pote. That's the way it goes. F34R.
+   */
   lua_pushstring(L,"symmetrical");
   lua_gettable(L,-2);
-  is_symmetrical = lua_tointeger(L, -1);
+  if (lua_isstring(L, -1)) {
+    const char* value = lua_tostring(L, -1);
+    if (strcmp("YES", value) == 0) {
+      route.symmetrical = TRUE;
+    }
+    else
+      route.symmetrical = FALSE;
+  }
+  else {
+    route.symmetrical = TRUE;
+  }
   lua_pop(L,1);
 
   route.gw_src = NULL;
   route.gw_dst = NULL;
 
-  /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
-   * Et ouais mon pote. That's the way it goes. F34R.
-   */
-  if (is_symmetrical)
-    route.symmetrical = TRUE;
-  else
-    route.symmetrical = FALSE;
-
   sg_platf_new_route(&route);
-  
+
   return 0;
 }
 
@@ -301,6 +307,8 @@ int console_AS_open(lua_State *L) {
  const char *id;
  const char *mode;
 
+ XBT_DEBUG("Opening AS");
+
  if (! lua_istable(L, 1)) {
    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
    return -1;
@@ -321,7 +329,6 @@ int console_AS_open(lua_State *L) {
  else if(!strcmp(mode,"Floyd")) mode_int = A_surfxml_AS_routing_Floyd;
  else if(!strcmp(mode,"Dijkstra")) mode_int = A_surfxml_AS_routing_Dijkstra;
  else if(!strcmp(mode,"DijkstraCache")) mode_int = A_surfxml_AS_routing_DijkstraCache;
- else if(!strcmp(mode,"RuleBased")) mode_int = A_surfxml_AS_routing_RuleBased;
  else if(!strcmp(mode,"Vivaldi")) mode_int = A_surfxml_AS_routing_Vivaldi;
  else if(!strcmp(mode,"Cluster")) mode_int = A_surfxml_AS_routing_Cluster;
  else if(!strcmp(mode,"none")) mode_int = A_surfxml_AS_routing_None;
@@ -335,6 +342,7 @@ int console_AS_open(lua_State *L) {
  return 0;
 }
 int console_AS_close(lua_State *L) {
+  XBT_DEBUG("Closing AS");
   sg_platf_new_AS_end();
   return 0;
 }
@@ -365,16 +373,16 @@ int console_set_function(lua_State *L) {
   //get args
   lua_pushstring(L,"args");
   lua_gettable(L, -2);
-  args = xbt_str_split_quoted( lua_tostring(L,-1) );
+  args = xbt_str_split_str( lua_tostring(L,-1) , ",");
   lua_pop(L, 1);
 
-  // FIXME: hackish to go under MSG that way
-  msg_host_t host = xbt_lib_get_or_null(host_lib,host_id,MSG_HOST_LEVEL);
+  msg_host_t host = MSG_host_by_name(host_id);
   if (!host) {
     XBT_ERROR("no host '%s' found",host_id);
     return -1;
   }
 
+  // FIXME: use sg_platf_new_process directly (warning: find a way to check hostname)
   MSG_set_function(host_id, function_id, args);
 
   return 0;
@@ -407,8 +415,7 @@ int console_host_set_property(lua_State *L) {
   prop_value = lua_tostring(L,-1);
   lua_pop(L, 1);
 
-  // FIXME: hackish to go under MSG that way
-  msg_host_t host = xbt_lib_get_or_null(host_lib,name,MSG_HOST_LEVEL);
+  msg_host_t host = MSG_host_by_name(name);
   if (!host) {
     XBT_ERROR("no host '%s' found",name);
     return -1;