Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'MC_LTL'
[simgrid.git] / src / bindings / lua / lua_host.c
1 /* Copyright (c) 2010. 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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_host, bindings, "Lua bindings (host module)");
13
14 #define HOST_MODULE_NAME "simgrid.host"
15
16 /* ********************************************************************************* */
17 /*                                simgrid.host API                                   */
18 /* ********************************************************************************* */
19
20 /**
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
25  */
26 m_host_t sglua_check_host(lua_State * L, int index)
27 {
28   m_host_t *pi, ht;
29   luaL_checktype(L, index, LUA_TTABLE);
30   lua_getfield(L, index, "__simgrid_host");
31   pi = (m_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
32   if (pi == NULL)
33     luaL_typerror(L, index, HOST_MODULE_NAME);
34   ht = *pi;
35   if (!ht)
36     luaL_error(L, "null Host");
37   lua_pop(L, 1);
38   return ht;
39 }
40
41 /**
42  * \brief Returns a host given its name.
43  * \param L a Lua state
44  * \return number of values returned to Lua
45  *
46  * - Argument 1 (string): name of a host
47  * - Return value (host): the corresponding host
48  */
49 static int l_host_get_by_name(lua_State * L)
50 {
51   const char *name = luaL_checkstring(L, 1);
52   XBT_DEBUG("Getting Host from name...");
53   m_host_t msg_host = MSG_get_host_by_name(name);
54   if (!msg_host) {
55     luaL_error(L, "null Host : MSG_get_host_by_name failed");
56   }
57   lua_newtable(L);              /* create a table, put the userdata on top of it */
58   m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
59   *lua_host = msg_host;
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 */
64   lua_remove(L, 1);
65   return 1;
66 }
67
68 /**
69  * \brief Returns the name of a host.
70  * \param L a Lua state
71  * \return number of values returned to Lua
72  *
73  * - Argument 1 (host): a host
74  * - Return value (string): name of this host
75  */
76 static int l_host_get_name(lua_State * L)
77 {
78   m_host_t ht = sglua_check_host(L, 1);
79   lua_pushstring(L, MSG_host_get_name(ht));
80   return 1;
81 }
82
83 /**
84  * \brief Returns the number of existing hosts.
85  * \param L a Lua state
86  * \return number of values returned to Lua
87  *
88  * - Return value (number): number of hosts
89  */
90 static int l_host_number(lua_State * L)
91 {
92   lua_pushnumber(L, MSG_get_host_number());
93   return 1;
94 }
95
96 /**
97  * \brief Returns the host given its index.
98  * \param L a Lua state
99  * \return number of values returned to Lua
100  *
101  * - Argument 1 (number): an index (1 is the first)
102  * - Return value (host): the host at this index
103  */
104 static int l_host_at(lua_State * L)
105 {
106   int index = luaL_checkinteger(L, 1);
107   m_host_t host = MSG_get_host_table()[index - 1];      // lua indexing start by 1 (lua[1] <=> C[0])
108   lua_newtable(L);              /* create a table, put the userdata on top of it */
109   m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
110   *lua_host = host;
111   luaL_getmetatable(L, HOST_MODULE_NAME);
112   lua_setmetatable(L, -2);
113   lua_setfield(L, -2, "__simgrid_host");        /* put the userdata as field of the table */
114   return 1;
115 }
116
117 /**
118  * \brief Returns the host where the current process is located.
119  * \param L a Lua state
120  * \return number of values returned to Lua
121  *
122  * - Return value (host): the current host
123  */
124 static int l_host_self(lua_State * L)
125 {
126                                   /* -- */
127   m_host_t host = MSG_host_self();
128   lua_newtable(L);
129                                   /* table */
130   m_host_t* lua_host = (m_host_t*) lua_newuserdata(L, sizeof(m_host_t));
131                                   /* table ud */
132   *lua_host = host;
133   luaL_getmetatable(L, HOST_MODULE_NAME);
134                                   /* table ud mt */
135   lua_setmetatable(L, -2);
136                                   /* table ud */
137   lua_setfield(L, -2, "__simgrid_host");
138                                   /* table */
139   return 1;
140 }
141
142 /**
143  * \brief Returns the value of a host property.
144  * \param L a Lua state
145  * \return number of values returned to Lua
146  *
147  * - Argument 1 (host): a host
148  * - Argument 2 (string): name of the property to get
149  * - Return value (string): the value of this property
150  */
151 static int l_host_get_property_value(lua_State * L)
152 {
153   m_host_t ht = sglua_check_host(L, 1);
154   const char *prop = luaL_checkstring(L, 2);
155   lua_pushstring(L,MSG_host_get_property_value(ht,prop));
156   return 1;
157 }
158
159 /**
160  * \brief Makes the current process sleep for a while.
161  * \param L a Lua state
162  * \return number of values returned to Lua
163  *
164  * - Argument 1 (number): duration of the sleep
165  */
166 static int l_host_sleep(lua_State *L)
167 {
168   int time = luaL_checknumber(L, 1);
169   MSG_process_sleep(time);
170   return 0;
171 }
172
173 /**
174  * \brief Destroys a host.
175  * \param L a Lua state
176  * \return number of values returned to Lua
177  *
178  * - Argument 1 (host): the host to destroy
179  */
180 static int l_host_destroy(lua_State *L)
181 {
182   m_host_t ht = sglua_check_host(L, 1);
183   __MSG_host_destroy(ht);
184   return 0;
185 }
186
187 static const luaL_reg host_functions[] = {
188   {"get_by_name", l_host_get_by_name},
189   {"name", l_host_get_name},
190   {"number", l_host_number},
191   {"at", l_host_at},
192   {"self", l_host_self},
193   {"get_prop_value", l_host_get_property_value},
194   {"sleep", l_host_sleep},
195   {"destroy", l_host_destroy},
196   // Bypass XML Methods
197   {"set_function", console_set_function},
198   {"set_property", console_host_set_property},
199   {NULL, NULL}
200 };
201
202 /**
203  * \brief Returns a string representation of a host.
204  * \param L a Lua state
205  * \return number of values returned to Lua
206  *
207  * - Argument 1 (userdata): a host
208  * - Return value (string): a string describing this host
209  */
210 static int l_host_tostring(lua_State * L)
211 {
212   lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
213   return 1;
214 }
215
216 static const luaL_reg host_meta[] = {
217   {"__tostring", l_host_tostring},
218   {0, 0}
219 };
220
221 /**
222  * \brief Registers the host functions into the table simgrid.host.
223  *
224  * Also initialize the metatable of the host userdata type.
225  *
226  * \param L a lua state
227  */
228 void sglua_register_host_functions(lua_State* L)
229 {
230   /* create a table simgrid.host and fill it with host functions */
231   luaL_openlib(L, HOST_MODULE_NAME, host_functions, 0);
232                                   /* simgrid.host */
233
234   /* create the metatable for host, add it to the Lua registry */
235   luaL_newmetatable(L, HOST_MODULE_NAME);
236                                   /* simgrid.host mt */
237   /* fill the metatable */
238   luaL_openlib(L, NULL, host_meta, 0);
239                                   /* simgrid.host mt */
240   lua_pushvalue(L, -2);
241                                   /* simgrid.host mt simgrid.host */
242   /* metatable.__index = simgrid.host
243    * we put the host functions inside the host userdata itself:
244    * this allows to write my_host:method(args) for
245    * simgrid.host.method(my_host, args) */
246   lua_setfield(L, -2, "__index");
247                                   /* simgrid.host mt */
248   lua_pop(L, 2);
249                                   /* -- */
250 }
251