Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / bindings / lua / lua_host.cpp
1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* SimGrid Lua bindings                                                     */
8
9 #include "lua_private.h"
10 #include <simgrid/host.h>
11 extern "C" {
12 #include <lauxlib.h>
13 }
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_host, "Lua Host module");
16
17 #define HOST_MODULE_NAME "simgrid.host"
18 #define HOST_FIELDNAME   "__simgrid_host"
19 /* ********************************************************************************* */
20 /*                                simgrid.host API                                   */
21 /* ********************************************************************************* */
22
23 /** @brief Ensures that the pointed stack value is an host userdatum and returns it.
24  *
25  * \param L a Lua state
26  * \param index an index in the Lua stack
27  * \return the C host corresponding to this Lua host
28  */
29 sg_host_t sglua_check_host(lua_State * L, int index)
30 {
31   luaL_checktype(L, index, LUA_TTABLE);
32   lua_getfield(L, index, HOST_FIELDNAME);
33   sg_host_t *pi = (sg_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
34   lua_pop(L, 1);
35   if (pi == NULL)
36     XBT_ERROR("luaL_checkudata() returned NULL");
37   sg_host_t ht = *pi;
38   if (!ht)
39     luaL_error(L, "null Host");
40   return ht;
41 }
42
43 /**
44  * \brief Returns a host given its name. This is a lua function.
45  *
46  * \param L a Lua state
47  * \return number of values returned to Lua
48  *
49  * - Argument 1 (string): name of a host
50  * - Return value (host): the corresponding host will be pushed onto the stack
51  */
52 static int l_host_get_by_name(lua_State * L)
53 {
54   const char *name = luaL_checkstring(L, 1);
55   lua_remove(L, 1); /* remove the args from the stack */
56
57   sg_host_t host = sg_host_by_name(name);
58   lua_ensure(host, "No host name '%s' found.", name);
59
60   lua_newtable(L);                        /* table */
61   sg_host_t *lua_host = (sg_host_t *) lua_newuserdata(L, sizeof(sg_host_t)); /* table userdatum */
62   *lua_host = host;
63   luaL_getmetatable(L, HOST_MODULE_NAME); /* table userdatum metatable */
64   lua_setmetatable(L, -2);                /* table userdatum */
65   lua_setfield(L, -2, HOST_FIELDNAME);    /* table -- put the userdata as field of the table */
66
67   return 1;
68 }
69
70 /**
71  * \brief Returns the name of a host.
72  * \param L a Lua state
73  * \return number of values returned to Lua
74  *
75  * - Argument 1 (host): a host
76  * - Return value (string): name of this host
77  */
78 static int l_host_get_name(lua_State * L)
79 {
80   sg_host_t ht = sglua_check_host(L, 1);
81   lua_pushstring(L, sg_host_get_name(ht));
82   return 1;
83 }
84
85 /**
86  * \brief Returns the number of existing hosts.
87  * \param L a Lua state
88  * \return number of values returned to Lua
89  *
90  * - Return value (number): number of hosts
91  */
92 static int l_host_number(lua_State * L)
93 {
94   xbt_dynar_t hosts = sg_hosts_as_dynar();
95   lua_pushinteger(L, xbt_dynar_length(hosts));
96   xbt_dynar_free(&hosts);
97   return 1;
98 }
99
100 /**
101  * \brief Returns the host given its index.
102  * \param L a Lua state
103  * \return number of values returned to Lua
104  *
105  * - Argument 1 (number): an index (1 is the first)
106  * - Return value (host): the host at this index
107  */
108 static int l_host_at(lua_State * L)
109 {
110   int index = luaL_checkinteger(L, 1);
111   xbt_dynar_t hosts = sg_hosts_as_dynar();
112   sg_host_t host = xbt_dynar_get_as(hosts,index - 1,sg_host_t);// lua indexing start by 1 (lua[1] <=> C[0])
113   lua_newtable(L);              /* create a table, put the userdata on top of it */
114   sg_host_t *lua_host = (sg_host_t *) lua_newuserdata(L, sizeof(sg_host_t));
115   *lua_host = host;
116   luaL_getmetatable(L, HOST_MODULE_NAME);
117   lua_setmetatable(L, -2);
118   lua_setfield(L, -2, HOST_FIELDNAME);        /* put the userdata as field of the table */
119   xbt_dynar_free(&hosts);
120   return 1;
121 }
122
123 /**
124  * \brief Returns the value of a host property.
125  * \param L a Lua state
126  * \return number of values returned to Lua
127  *
128  * - Argument 1 (host): a host
129  * - Argument 2 (string): name of the property to get
130  * - Return value (string): the value of this property
131  */
132 static int l_host_get_property_value(lua_State * L)
133 {
134   sg_host_t ht = sglua_check_host(L, 1);
135   const char *prop = luaL_checkstring(L, 2);
136   lua_pushstring(L, sg_host_get_property_value(ht,prop));
137   return 1;
138 }
139
140 /**
141  * \brief Destroys a host.
142  * \param L a Lua state
143  * \return number of values returned to Lua
144  *
145  * - Argument 1 (host): the host to destroy
146  */
147 static int l_host_destroy(lua_State *L)
148 {
149   //sg_host_t ht = sglua_check_host(L, 1);
150   //FIXME: not working..__MSG_host_priv_free(MSG_host_priv(ht));
151   return 0;
152 }
153
154 static const luaL_Reg host_functions[] = {
155   {"get_by_name", l_host_get_by_name},
156   {"name", l_host_get_name},
157   {"number", l_host_number},
158   {"at", l_host_at},
159   {"get_prop_value", l_host_get_property_value},
160   {"destroy", l_host_destroy},
161   // Bypass XML Methods
162   {"set_property", console_host_set_property},
163   {NULL, NULL}
164 };
165
166 /**
167  * \brief Returns a string representation of a host.
168  * \param L a Lua state
169  * \return number of values returned to Lua
170  *
171  * - Argument 1 (userdata): a host
172  * - Return value (string): a string describing this host
173  */
174 static int l_host_tostring(lua_State * L)
175 {
176   lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
177   return 1;
178 }
179
180 static const luaL_Reg host_meta[] = {
181   {"__tostring", l_host_tostring},
182   {0, 0}
183 };
184
185 /**
186  * \brief Registers the host functions into the table simgrid.host.
187  *
188  * Also initialize the metatable of the host userdata type.
189  *
190  * \param L a lua state
191  */
192 void sglua_register_host_functions(lua_State* L)
193 {
194   /* create a table simgrid.host and fill it with host functions */
195   lua_getglobal(L, "simgrid"); /* simgrid */
196   luaL_newlib(L, host_functions); /* simgrid simgrid.host */
197   lua_setfield(L, -2, "host"); /* simgrid */
198   lua_getfield(L, -1, "host");    /* simgrid simgrid.host */
199
200   /* create the metatable for host, add it to the Lua registry */
201   luaL_newmetatable(L, HOST_MODULE_NAME); /* simgrid simgrid.host mt */
202
203   /* fill the metatable */
204   luaL_setfuncs(L, host_meta, 0);         /* simgrid simgrid.host mt */
205
206   /**
207    * Copy the table and push it onto the stack.
208    * Required for the lua_setfield call below.
209    */
210   lua_getfield(L, -3, "host");                   /* simgrid simgrid.host mt simgrid.host */
211
212   /* metatable.__index = simgrid.host
213    * we put the host functions inside the host userdata itself:
214    * this allows to write my_host:method(args) for
215    * simgrid.host.method(my_host, args) */
216   lua_setfield(L, -2, "__index");         /* simgrid simgrid.host mt */
217
218   lua_setmetatable(L, -2);                /* simgrid simgrid.host */
219   lua_pop(L, 2);                          /* -- */
220 }
221