Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3b4dd08bef3701265887c8f2eff7ea049a6c5df7
[simgrid.git] / src / bindings / lua / Msglua.c
1 #include <stdio.h>
2 #include "lauxlib.h"
3 #include "lualib.h"
4
5 // Msg Includes
6 #include <stdio.h>
7 #include "msg/msg.h"
8 #include "msg/datatypes.h"
9 #include "xbt/sysdep.h"        
10 #include "xbt/log.h"
11 #include "xbt/asserts.h"
12
13
14
15
16 /*
17 =============================================
18
19         Example Lua Bindings for Msg
20
21 =============================================
22 */
23
24 XBT_LOG_NEW_DEFAULT_CATEGORY(lua,"Lua bindings");
25
26
27 #define TASK "Msg.Task"
28 #define HOST "Msg.Host"
29 #define PROCESS "Msg.Process"
30
31 typedef m_task_t Task;
32 typedef m_host_t Host;
33 typedef m_process_t Process;
34
35
36 static void stackDump (lua_State *L) {
37   int i;
38   int top = lua_gettop(L);
39   void *p;
40   fflush(stdout);
41   return;
42   INFO0("That's me");
43   printf("STACK(top=%d): ",top);
44   for (i = 1; i <= top; i++) {  /* repeat for each level */
45     int t = lua_type(L, i);
46     switch (t) {
47
48       case LUA_TSTRING:  /* strings */
49         printf("`%s'", lua_tostring(L, i));
50         break;
51
52       case LUA_TBOOLEAN:  /* booleans */
53         printf(lua_toboolean(L, i) ? "true" : "false");
54         break;
55
56       case LUA_TNUMBER:  /* numbers */
57         printf("%g", lua_tonumber(L, i));
58         break;
59
60       default:  /* other values */
61         if ((p = luaL_checkudata(L,i,TASK))) {
62           printf("task");
63         } else {
64           printf("%s", lua_typename(L, t));
65         }
66         break;
67
68     }
69     printf("  ");  /* put a separator */
70   }
71   printf("\n");  /* end the listing */
72   fflush(stdout);
73 }
74
75 //**********************************************************TASK SECTION***************************************************
76
77
78 static Task toTask(lua_State *L,int index)
79         {
80         Task *pi = (Task*) lua_touserdata(L,index);
81         if(pi == NULL) luaL_typerror(L,index,TASK);
82         return *pi;
83         }
84 /**
85 *checkTask ensures that a userdata on the stack is the correct type, and returns the Task pointer inside the userdata
86 */
87
88 static Task checkTask (lua_State *L,int index)
89         {
90         Task *pi,tk;
91         luaL_checktype(L,index,LUA_TUSERDATA);
92         pi = (Task*)luaL_checkudata(L,index,TASK);
93         if(pi == NULL ) luaL_typerror(L,index,TASK);
94         tk = *pi;
95         if(!tk)
96                 luaL_error(L,"null Task");
97         return  tk;
98         }
99
100 /**
101 *pushTask leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata.
102 */
103
104 static Task *pushTask (lua_State *L,Task tk)
105         {
106         Task *pi = (Task*)lua_newuserdata(L,sizeof(Task));
107         *pi=tk;
108         luaL_getmetatable(L,TASK);
109         lua_setmetatable(L,-2);
110         return pi;
111
112         }
113
114 /**
115 *Task.new is a constructor that returns a userdata containing a pointer the Task to be manipulated
116 */
117
118
119 static int Task_new(lua_State* L)
120         {
121         const char *name=luaL_checkstring(L,1);
122         int comp_size = luaL_checkint(L,2);
123         int msg_size = luaL_checkint(L,3);
124         // We Will Set The Data as a Null for The Moment
125         pushTask(L,MSG_task_create(name,comp_size,msg_size,NULL));
126         return 1;               
127
128         }
129
130 /**
131 * Function to return the Task Name
132 */
133
134
135 static int Task_name(lua_State *L)
136         {
137         Task tk = checkTask(L,1);
138         lua_pushstring(L,MSG_task_get_name(tk));
139         return 1;
140         }
141
142
143 /**
144 * Function to return the Computing Size of a Task
145 */
146
147 static int Task_comp(lua_State *L)
148         {
149         Task tk = checkTask(L,1);
150         lua_pushnumber(L,MSG_task_get_compute_duration (tk));
151         return 1;
152         }
153
154
155 /**
156 *Function to Excute a Task
157 */
158
159 static int Task_execute(lua_State *L)
160         {
161         Task tk = checkTask(L,1);
162         int res = MSG_task_execute(tk);
163         lua_pushnumber(L,res);
164         return 1;
165         }
166
167 /**
168 *Function ro Desroy a Task
169 */
170 static int Task_destroy(lua_State *L)
171         {       
172         Task tk = checkTask(L,1);
173         int res = MSG_task_destroy(tk);
174         lua_pushnumber(L,res);
175         return 1;
176         }
177
178
179
180
181
182
183 //***************************************************************  HOST SECTION  ***************************************************************************
184
185
186
187 static Host toHost(lua_State *L,int index)
188         {
189         Host *pi = (Host*) lua_touserdata(L,index);
190         if(pi == NULL) luaL_typerror(L,index,HOST);
191         return *pi;
192         }
193
194
195
196 static Host checkHost (lua_State *L,int index)
197         {
198         Host *pi,ht;
199         luaL_checktype(L,index,LUA_TUSERDATA);
200         pi = (Host*)luaL_checkudata(L,index,HOST);
201         if(pi == NULL ) luaL_typerror(L,index,HOST);
202         ht = *pi;
203         if(!ht)
204                 luaL_error(L,"null Host");
205         return  ht;
206         }
207
208
209
210 static Host *pushHost (lua_State *L,Host ht)
211         {
212         Host *pi = (Host*)lua_newuserdata(L,sizeof(Host));
213         *pi=ht;
214         luaL_getmetatable(L,HOST);
215         lua_setmetatable(L,-2);
216         return pi;
217
218         }
219
220
221 static int Host_new(lua_State* L)
222         {
223         const char *host_name=luaL_checkstring(L,1);
224         pushHost(L,MSG_get_host_by_name(host_name));
225         return 1;               
226         }
227
228
229
230 static int Host_name(lua_State* L)
231         {
232         Host ht = checkHost(L,1);
233         lua_pushstring(L,MSG_host_get_name(ht));        
234         return 1;
235         }
236
237
238 //*******************************************************   PROCESS SECTION      ***************************************************************************
239 static Process toProcess(lua_State *L,int index)
240         {
241         Process *pi = (Process*) lua_touserdata(L,index);
242         if(pi == NULL) luaL_typerror(L,index,PROCESS);
243         return *pi;
244         }
245
246
247
248 static Process checkProcess (lua_State *L,int index)
249         {
250         Process *pi,ps;
251         luaL_checktype(L,index,LUA_TUSERDATA);
252         pi = (Process*)luaL_checkudata(L,index,PROCESS);
253         if(pi == NULL ) luaL_typerror(L,index,PROCESS);
254         ps = *pi;
255         if(!ps)
256                 luaL_error(L,"null Process");
257         return  ps;
258         }
259
260
261
262 static Process *pushProcess (lua_State *L,Process ps)
263         {
264         Process *pi = (Process*)lua_newuserdata(L,sizeof(Process));
265         *pi=ps;
266         luaL_getmetatable(L,PROCESS);
267         lua_setmetatable(L,-2);
268         return pi;
269
270         }
271
272 /*
273 m_process_t MSG_process_create          (       const char *     name,
274                 xbt_main_func_t         code,
275                 void *          data,
276                 m_host_t        host     
277         )       
278 */
279
280
281
282 static int Process_new(lua_State* L)
283         {
284         const char *name=luaL_checkstring(L,1);
285         //int code = luaL_checkint(L,2);
286         //xbt_main_func_t << code  
287         // We Will Set The Data as a Null for The Moment
288         Host ht = checkHost(L,4); 
289         pushProcess(L,MSG_process_create(name,NULL,NULL,ht));
290         return 1;               
291
292         }
293
294
295 static int Process_kill(lua_State *L)
296         {
297         Process ps = checkProcess (L,1);
298         MSG_process_kill(ps);
299         return 0;
300         }
301
302
303
304 //**********************************************************MSG Operating System Functions******************************************************************
305 /**
306 * Function to Send a Task
307 */
308 static int Task_send(lua_State *L)      {
309   Task tk = checkTask(L,1);
310   const char *mailbox = luaL_checkstring(L,2);
311   stackDump(L);
312   int res = MSG_task_send(tk,mailbox);
313   stackDump(L);
314   return 0;
315 }
316 /**
317 * Function Recieve a Task
318 */
319 static int Task_recv2(lua_State *L)     {
320
321         Task *tk = (Task *)checkTask(L,1);
322         const char *mailbox = luaL_checkstring(L,2);
323         MSG_task_receive(tk,mailbox);
324         printf("Task Name Within Receive Fct: >>>%s\n",MSG_task_get_name(*tk));
325         
326         return 0;
327 }
328
329
330 static int Task_recv(lua_State *L)      {
331   Task tk = NULL;
332   //stackDump(L);
333         const char *mailbox = luaL_checkstring(L,1);
334         int res = MSG_task_receive(&tk,mailbox);
335         INFO1("Task Name : >>>%s",MSG_task_get_name(tk));
336         pushTask(L,tk);
337 //      stackDump(L);
338         return 1;
339 }
340
341 //*******************************************************   PLATFORM & APPLICATION SECTION  ****************************************************************
342
343
344  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WE WONT NEED IT (!!??) 
345 /**
346 * Launch Application Function
347 */
348
349 static int launch_application(lua_State *L)
350         {
351
352         const char * file = luaL_checkstring(L,1);
353         MSG_launch_application(file);
354         return 0;
355
356         }
357
358 /**
359 * Create Environment Function
360 */
361
362 static int create_environment(lua_State *L)
363         {
364
365         const char *file = luaL_checkstring(L,1);
366         MSG_create_environment(file);
367         return 0;
368
369         }
370
371 /**
372 * Function Register
373 */
374
375 static int function_register()
376         {
377
378         //Code ??!!!
379
380         }
381
382 //******************************************************************       REGISTERING          ************************************************************
383 /**
384 *Registering Into Lua
385 */
386
387
388 static const luaL_reg Task_methods[] = {
389 {"new",         Task_new},
390 {"name",        Task_name},
391 {"comp",        Task_comp},
392 {"execute",     Task_execute},
393 {"destroy",     Task_destroy},
394 {"send",                Task_send},
395 {"recv",                Task_recv},
396 {"recv2",               Task_recv2},
397 {0,0}
398 };
399
400
401 static const luaL_reg Host_methods[] = {
402 {"new",         Host_new},
403 {"name",        Host_name},
404 {0,0}
405 };
406
407
408 static const luaL_reg Process_methods[] = {
409 {"new",         Process_new},
410 {"kill",        Process_kill},
411 {0,0}
412 };
413
414
415
416
417 /**
418 * Task_meta
419 */
420 static int Task_gc(lua_State *L)
421         {
422         Task tk=toTask(L,1);
423         //if (tk) MSG_task_destroy(tk);
424         //printf("GoodBye Task(%p)\n",lua_touserdata(L,1));
425         return 0; 
426         }
427
428 static int Task_tostring(lua_State *L)
429         {
430         lua_pushfstring(L,"Task :%p",lua_touserdata(L,1));
431         return 1;       
432         }
433
434
435 static const luaL_reg Task_meta[] = {
436         {"__gc",        Task_gc},
437         {"__tostring",  Task_tostring},
438         {0,0}
439 };
440
441
442
443
444 /**
445 * Host_meta
446 */
447 static int Host_gc(lua_State *L)
448         {
449         Host ht=toHost(L,1);
450         if (ht) ht=NULL;
451         printf("GoodBye Host(%p)\n",lua_touserdata(L,1));
452         return 0; 
453         }
454
455 static int Host_tostring(lua_State *L)
456         {
457         lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
458         return 1;       
459         }
460
461
462 static const luaL_reg Host_meta[] = {
463         {"__gc",        Host_gc},
464         {"__tostring",  Host_tostring},
465         {0,0}
466 };
467
468
469 /**
470 * Process_meta
471 */
472 static int Process_gc(lua_State *L)
473         {
474         Process ps=toProcess(L,1);
475         if (ps) MSG_process_kill(ps) ;
476         printf("GoodBye Process(%p)\n",lua_touserdata(L,1));
477         return 0; 
478         }
479
480 static int Process_tostring(lua_State *L)
481         {
482         lua_pushfstring(L,"Process :%p",lua_touserdata(L,1));
483         return 1;       
484         }
485
486
487 static const luaL_reg Process_meta[] = {
488         {"__gc",        Process_gc},
489         {"__tostring",  Process_tostring},
490         {0,0}
491 };
492
493
494 /**
495 *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. 
496 */
497
498 /**
499 * Task Register
500 */
501
502 int Task_register(lua_State *L)
503         {       
504         luaL_openlib(L,TASK,Task_methods,0); //create methods table,add it to the globals
505         luaL_newmetatable(L,TASK); //create metatable for Task,add it to the Lua registry
506         luaL_openlib(L,0,Task_meta,0);// fill metatable
507         lua_pushliteral(L,"__index");
508         lua_pushvalue(L,-3);    //dup methods table
509         lua_rawset(L,-3);       //matatable.__index = methods
510         lua_pushliteral(L,"__metatable");
511         lua_pushvalue(L,-3);    //dup methods table
512         lua_rawset(L,-3);       //hide metatable:metatable.__metatable = methods 
513         lua_pop(L,1);           //drop metatable
514         return 1;
515         }
516           
517 /**
518 * Host Register
519 */
520
521 int Host_register(lua_State *L)
522         {       
523         luaL_openlib(L,HOST,Host_methods,0); 
524         luaL_newmetatable(L,HOST); 
525         luaL_openlib(L,0,Host_meta,0);
526         lua_pushliteral(L,"__index");
527         lua_pushvalue(L,-3);    
528         lua_rawset(L,-3);       
529         lua_pushliteral(L,"__metatable");
530         lua_pushvalue(L,-3);    
531         lua_rawset(L,-3);       
532         lua_pop(L,1);           
533         return 1;
534         }
535
536 /**
537 *  Process Register
538 */
539
540 int Process_register(lua_State *L)
541         {       
542         luaL_openlib(L,PROCESS,Process_methods,0); 
543         luaL_newmetatable(L,PROCESS); 
544         luaL_openlib(L,0,Host_meta,0);
545         lua_pushliteral(L,"__index");
546         lua_pushvalue(L,-3);    
547         lua_rawset(L,-3);       
548         lua_pushliteral(L,"__metatable");
549         lua_pushvalue(L,-3);    
550         lua_rawset(L,-3);       
551         lua_pop(L,1);           
552         return 1;
553         }
554