Logo AND Algorithmique Numérique Distribuée

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