8 #include "xbt/sysdep.h"
10 #include "xbt/asserts.h"
16 =============================================
18 Example Lua Bindings for Msg
20 =============================================
28 #define PROCESS "Process"
30 typedef m_task_t Task;
31 typedef m_host_t Host;
32 typedef m_process_t Process;
34 //**********************************************************TASK SECTION***************************************************
37 static Task toTask(lua_State *L,int index)
39 Task *pi = (Task*) lua_touserdata(L,index);
40 if(pi == NULL) luaL_typerror(L,index,TASK);
44 *checkTask ensures that a userdata on the stack is the correct type, and returns the Task pointer inside the userdata
47 static Task checkTask (lua_State *L,int index)
50 luaL_checktype(L,index,LUA_TUSERDATA);
51 pi = (Task*)luaL_checkudata(L,index,TASK);
52 if(pi == NULL ) luaL_typerror(L,index,TASK);
55 luaL_error(L,"null Task");
60 *pushTask leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata.
63 static Task *pushTask (lua_State *L,Task tk)
65 Task *pi = (Task*)lua_newuserdata(L,sizeof(Task));
67 luaL_getmetatable(L,TASK);
68 lua_setmetatable(L,-2);
74 *Task.new is a constructor that returns a userdata containing a pointer the Task to be manipulated
78 static int Task_new(lua_State* L)
80 char *name=luaL_checkstring(L,1);
81 int comp_size = luaL_checkint(L,2);
82 int msg_size = luaL_checkint(L,3);
83 // We Will Set The Data as a Null for The Moment
84 pushTask(L,MSG_task_create(name,comp_size,msg_size,NULL));
90 * Function to return the Task Name
94 static int Task_name(lua_State *L)
96 Task tk = checkTask(L,1);
97 lua_pushstring(L,MSG_task_get_name(tk));
103 * Function to return the Computing Size of a Task
106 static int Task_comp(lua_State *L)
108 Task tk = checkTask(L,1);
109 lua_pushnumber(L,MSG_task_get_compute_duration (tk));
115 *Function to Excute a Task
118 static int Task_execute(lua_State *L)
120 Task tk = checkTask(L,1);
121 int res = MSG_task_execute(tk);
122 lua_pushnumber(L,res);
127 *Function ro Desroy a Task
129 static int Task_destroy(lua_State *L)
131 Task tk = checkTask(L,1);
132 int res = MSG_task_destroy(tk);
133 lua_pushnumber(L,res);
142 //*************************************************************** HOST SECTION ***************************************************************************
146 static Host toHost(lua_State *L,int index)
148 Host *pi = (Host*) lua_touserdata(L,index);
149 if(pi == NULL) luaL_typerror(L,index,HOST);
155 static Host checkHost (lua_State *L,int index)
158 luaL_checktype(L,index,LUA_TUSERDATA);
159 pi = (Host*)luaL_checkudata(L,index,HOST);
160 if(pi == NULL ) luaL_typerror(L,index,HOST);
163 luaL_error(L,"null Host");
169 static Host *pushHost (lua_State *L,Host ht)
171 Host *pi = (Host*)lua_newuserdata(L,sizeof(Host));
173 luaL_getmetatable(L,HOST);
174 lua_setmetatable(L,-2);
180 static int Host_new(lua_State* L)
182 char *host_name=luaL_checkstring(L,1);
183 pushHost(L,MSG_get_host_by_name(host_name));
189 static int Host_name(lua_State* L)
191 Host ht = checkHost(L,1);
192 lua_pushstring(L,MSG_host_get_name(ht));
197 //******************************************************* PROCESS SECTION ***************************************************************************
198 static Process toProcess(lua_State *L,int index)
200 Process *pi = (Process*) lua_touserdata(L,index);
201 if(pi == NULL) luaL_typerror(L,index,PROCESS);
207 static Process checkProcess (lua_State *L,int index)
210 luaL_checktype(L,index,LUA_TUSERDATA);
211 pi = (Process*)luaL_checkudata(L,index,PROCESS);
212 if(pi == NULL ) luaL_typerror(L,index,PROCESS);
215 luaL_error(L,"null Process");
221 static Process *pushProcess (lua_State *L,Process ps)
223 Process *pi = (Process*)lua_newuserdata(L,sizeof(Process));
225 luaL_getmetatable(L,PROCESS);
226 lua_setmetatable(L,-2);
232 m_process_t MSG_process_create ( const char * name,
233 xbt_main_func_t code,
241 static int Process_new(lua_State* L)
243 char *name=luaL_checkstring(L,1);
244 //int code = luaL_checkint(L,2);
245 //xbt_main_func_t << code
246 // We Will Set The Data as a Null for The Moment
247 Host ht = checkHost(L,4);
248 pushProcess(L,MSG_process_create(name,NULL,NULL,ht));
254 static int Process_kill(lua_State *L)
256 Process ps = checkProcess (L,1);
257 MSG_process_kill(ps);
263 //**********************************************************MSG Operating System Functions******************************************************************
265 * Function to Send a Task
267 static void Task_send(lua_State *L) {
268 Task tk = checkTask(L,1);
269 char *mailbox = luaL_checkstring(L,2);
271 int res = MSG_task_send(tk,mailbox);
274 * Function to Get ( Recieve !! ) a Task
276 static int Task_recv(lua_State *L) {
279 char *mailbox = luaL_checkstring(L,1);
280 int res = MSG_task_receive(&tk,mailbox);
286 //******************************************************* PLATFORM & APPLICATION SECTION ****************************************************************
289 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WE WONT NEED IT (!!??)
291 * Launch Application Function
294 static int launch_application(lua_State *L)
297 const char * file = luaL_checkstring(L,1);
298 MSG_launch_application(file);
304 * Create Environment Function
307 static int create_environment(lua_State *L)
310 const char *file = luaL_checkstring(L,1);
311 MSG_create_environment(file);
320 static int function_register()
327 //****************************************************************** REGISTERING ************************************************************
329 *Registering Into Lua
333 static const luaL_reg Task_methods[] = {
337 {"execute", Task_execute},
338 {"destroy", Task_destroy},
345 static const luaL_reg Host_methods[] = {
352 static const luaL_reg Process_methods[] = {
353 {"new", Process_new},
354 {"kill", Process_kill},
364 static int Task_gc(lua_State *L)
367 if (tk) MSG_task_destroy(tk);
368 printf("GoodBye Task(%p)\n",lua_touserdata(L,1));
372 static int Task_tostring(lua_State *L)
374 lua_pushfstring(L,"Task :%p",lua_touserdata(L,1));
379 static const luaL_reg Task_meta[] = {
381 {"__tostring", Task_tostring},
391 static int Host_gc(lua_State *L)
395 printf("GoodBye Host(%p)\n",lua_touserdata(L,1));
399 static int Host_tostring(lua_State *L)
401 lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
406 static const luaL_reg Host_meta[] = {
408 {"__tostring", Host_tostring},
416 static int Process_gc(lua_State *L)
418 Process ps=toProcess(L,1);
419 if (ps) MSG_process_kill(ps) ;
420 printf("GoodBye Process(%p)\n",lua_touserdata(L,1));
424 static int Process_tostring(lua_State *L)
426 lua_pushfstring(L,"Process :%p",lua_touserdata(L,1));
431 static const luaL_reg Process_meta[] = {
432 {"__gc", Process_gc},
433 {"__tostring", Process_tostring},
439 *The metatable for the userdata is put in the registry, and the __index field points to the table of methods so that the object:method() syntax will work. The methods table is stored in the table of globals so that scripts can add methods written in Lua.
446 int Task_register(lua_State *L)
448 luaL_openlib(L,TASK,Task_methods,0); //create methods table,add it to the globals
449 luaL_newmetatable(L,TASK); //create metatable for Task,add it to the Lua registry
450 luaL_openlib(L,0,Task_meta,0);// fill metatable
451 lua_pushliteral(L,"__index");
452 lua_pushvalue(L,-3); //dup methods table
453 lua_rawset(L,-3); //matatable.__index = methods
454 lua_pushliteral(L,"__metatable");
455 lua_pushvalue(L,-3); //dup methods table
456 lua_rawset(L,-3); //hide metatable:metatable.__metatable = methods
457 lua_pop(L,1); //drop metatable
465 int Host_register(lua_State *L)
467 luaL_openlib(L,HOST,Host_methods,0);
468 luaL_newmetatable(L,HOST);
469 luaL_openlib(L,0,Host_meta,0);
470 lua_pushliteral(L,"__index");
473 lua_pushliteral(L,"__metatable");
484 int Process_register(lua_State *L)
486 luaL_openlib(L,PROCESS,Process_methods,0);
487 luaL_newmetatable(L,PROCESS);
488 luaL_openlib(L,0,Host_meta,0);
489 lua_pushliteral(L,"__index");
492 lua_pushliteral(L,"__metatable");