Logo AND Algorithmique Numérique Distribuée

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