1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /* SimGrid Lua bindings */
9 #include "lua_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_host, bindings, "Lua bindings (host module)");
14 #define HOST_MODULE_NAME "simgrid.host"
16 /* ********************************************************************************* */
17 /* simgrid.host API */
18 /* ********************************************************************************* */
21 * \brief Ensures that a value in the stack is a host and returns it.
22 * \param L a Lua state
23 * \param index an index in the Lua stack
24 * \return the C host corresponding to this Lua host
26 msg_host_t sglua_check_host(lua_State * L, int index)
29 luaL_checktype(L, index, LUA_TTABLE);
30 lua_getfield(L, index, "__simgrid_host");
31 pi = (msg_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
33 XBT_ERROR("luaL_checkudata() returned NULL");
36 luaL_error(L, "null Host");
42 * \brief Returns a host given its name.
43 * \param L a Lua state
44 * \return number of values returned to Lua
46 * - Argument 1 (string): name of a host
47 * - Return value (host): the corresponding host
49 static int l_host_get_by_name(lua_State * L)
51 const char *name = luaL_checkstring(L, 1);
52 XBT_DEBUG("Getting Host from name...");
53 msg_host_t msg_host = MSG_host_by_name(name);
55 XBT_ERROR("MSG_get_host_by_name failed, requested hostname: %s", name);
57 lua_newtable(L); /* create a table, put the userdata on top of it */
58 msg_host_t *lua_host = (msg_host_t *) lua_newuserdata(L, sizeof(msg_host_t));
60 luaL_getmetatable(L, HOST_MODULE_NAME);
61 lua_setmetatable(L, -2);
62 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
63 /* remove the args from the stack */
69 * \brief Returns the name of a host.
70 * \param L a Lua state
71 * \return number of values returned to Lua
73 * - Argument 1 (host): a host
74 * - Return value (string): name of this host
76 static int l_host_get_name(lua_State * L)
78 msg_host_t ht = sglua_check_host(L, 1);
79 lua_pushstring(L, MSG_host_get_name(ht));
84 * \brief Returns the number of existing hosts.
85 * \param L a Lua state
86 * \return number of values returned to Lua
88 * - Return value (number): number of hosts
90 static int l_host_number(lua_State * L)
92 xbt_dynar_t hosts = MSG_hosts_as_dynar();
93 lua_pushinteger(L, xbt_dynar_length(hosts));
94 xbt_dynar_free(&hosts);
99 * \brief Returns the host given its index.
100 * \param L a Lua state
101 * \return number of values returned to Lua
103 * - Argument 1 (number): an index (1 is the first)
104 * - Return value (host): the host at this index
106 static int l_host_at(lua_State * L)
108 int index = luaL_checkinteger(L, 1);
109 xbt_dynar_t hosts = MSG_hosts_as_dynar();
110 msg_host_t host = xbt_dynar_get_as(hosts,index - 1,msg_host_t);// lua indexing start by 1 (lua[1] <=> C[0])
111 lua_newtable(L); /* create a table, put the userdata on top of it */
112 msg_host_t *lua_host = (msg_host_t *) lua_newuserdata(L, sizeof(msg_host_t));
114 luaL_getmetatable(L, HOST_MODULE_NAME);
115 lua_setmetatable(L, -2);
116 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
117 xbt_dynar_free(&hosts);
122 * \brief Returns the host where the current process is located.
123 * \param L a Lua state
124 * \return number of values returned to Lua
126 * - Return value (host): the current host
128 static int l_host_self(lua_State * L)
131 msg_host_t host = MSG_host_self();
134 msg_host_t* lua_host = (msg_host_t*) lua_newuserdata(L, sizeof(msg_host_t));
137 luaL_getmetatable(L, HOST_MODULE_NAME);
139 lua_setmetatable(L, -2);
141 lua_setfield(L, -2, "__simgrid_host");
147 * \brief Returns the value of a host property.
148 * \param L a Lua state
149 * \return number of values returned to Lua
151 * - Argument 1 (host): a host
152 * - Argument 2 (string): name of the property to get
153 * - Return value (string): the value of this property
155 static int l_host_get_property_value(lua_State * L)
157 msg_host_t ht = sglua_check_host(L, 1);
158 const char *prop = luaL_checkstring(L, 2);
159 lua_pushstring(L,MSG_host_get_property_value(ht,prop));
164 * \brief Destroys a host.
165 * \param L a Lua state
166 * \return number of values returned to Lua
168 * - Argument 1 (host): the host to destroy
170 static int l_host_destroy(lua_State *L)
172 //msg_host_t ht = sglua_check_host(L, 1);
173 //FIXME: not working..__MSG_host_priv_free(MSG_host_priv(ht));
177 static const luaL_Reg host_functions[] = {
178 {"get_by_name", l_host_get_by_name},
179 {"name", l_host_get_name},
180 {"number", l_host_number},
182 {"self", l_host_self},
183 {"get_prop_value", l_host_get_property_value},
184 {"destroy", l_host_destroy},
185 // Bypass XML Methods
186 {"set_property", console_host_set_property},
191 * \brief Returns a string representation of a host.
192 * \param L a Lua state
193 * \return number of values returned to Lua
195 * - Argument 1 (userdata): a host
196 * - Return value (string): a string describing this host
198 static int l_host_tostring(lua_State * L)
200 lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
204 static const luaL_Reg host_meta[] = {
205 {"__tostring", l_host_tostring},
210 * \brief Registers the host functions into the table simgrid.host.
212 * Also initialize the metatable of the host userdata type.
214 * \param L a lua state
216 void sglua_register_host_functions(lua_State* L)
218 /* create a table simgrid.host and fill it with host functions */
219 lua_getglobal(L, "simgrid"); /* simgrid */
220 luaL_newlib(L, host_functions); /* simgrid simgrid.host */
221 lua_setfield(L, -2, "host"); /* simgrid */
222 lua_getfield(L, -1, "host"); /* simgrid simgrid.host */
224 /* create the metatable for host, add it to the Lua registry */
225 luaL_newmetatable(L, HOST_MODULE_NAME); /* simgrid simgrid.host mt */
227 /* fill the metatable */
228 luaL_setfuncs(L, host_meta, 0); /* simgrid simgrid.host mt */
231 * Copy the table and push it onto the stack.
232 * Required for the lua_setfield call below.
234 lua_getfield(L, -3, "host"); /* simgrid simgrid.host mt simgrid.host */
236 /* metatable.__index = simgrid.host
237 * we put the host functions inside the host userdata itself:
238 * this allows to write my_host:method(args) for
239 * simgrid.host.method(my_host, args) */
240 lua_setfield(L, -2, "__index"); /* simgrid simgrid.host mt */
242 lua_setmetatable(L, -2); /* simgrid simgrid.host */
243 lua_pop(L, 2); /* -- */