Logo AND Algorithmique Numérique Distribuée

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