Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1c484dd9092e27ed32c5185958199673da2fd15b
[simgrid.git] / src / bindings / lua / Msglua.h
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 "xbt/sysdep.h"        
9 #include "xbt/log.h"
10 #include "xbt/asserts.h"
11
12
13
14
15 /*
16 =============================================
17
18         Example Lua Bindings for Msg
19
20 =============================================
21 */
22
23
24
25
26 #define TASK "Task"
27 #define HOST "Host"
28 #define PROCESS "Process"
29
30 typedef m_task_t Task;
31 typedef m_host_t Host;
32 typedef m_process_t Process;
33
34 //**********************************************************TASK SECTION***************************************************
35
36
37 static Task toTask(lua_State *L,int index)
38         {
39         Task *pi = (Task*) lua_touserdata(L,index);
40         if(pi == NULL) luaL_typerror(L,index,TASK);
41         return *pi;
42         }
43 /**
44 *checkTask ensures that a userdata on the stack is the correct type, and returns the Task pointer inside the userdata
45 */
46
47 static Task checkTask (lua_State *L,int index)
48         {
49         Task *pi,tk;
50         luaL_checktype(L,index,LUA_TUSERDATA);
51         pi = (Task*)luaL_checkudata(L,index,TASK);
52         if(pi == NULL ) luaL_typerror(L,index,TASK);
53         tk = *pi;
54         if(!tk)
55                 luaL_error(L,"null Task");
56         return  tk;
57         }
58
59 /**
60 *pushTask leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata.
61 */
62
63 static Task *pushTask (lua_State *L,Task tk)
64         {
65         Task *pi = (Task*)lua_newuserdata(L,sizeof(Task));
66         *pi=tk;
67         luaL_getmetatable(L,TASK);
68         lua_setmetatable(L,-2);
69         return pi;
70
71         }
72
73 /**
74 *Task.new is a constructor that returns a userdata containing a pointer the Task to be manipulated
75 */
76
77
78 static int Task_new(lua_State* L)
79         {
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));
85         return 1;               
86
87         }
88
89 /**
90 * Function to return the Task Name
91 */
92
93
94 static int Task_name(lua_State *L)
95         {
96         Task tk = checkTask(L,1);
97         lua_pushstring(L,MSG_task_get_name(tk));
98         return 1;
99         }
100
101
102 /**
103 * Function to return the Computing Size of a Task
104 */
105
106 static int Task_comp(lua_State *L)
107         {
108         Task tk = checkTask(L,1);
109         lua_pushnumber(L,MSG_task_get_compute_duration (tk));
110         return 1;
111         }
112
113
114 /**
115 *Function to Excute a Task
116 */
117
118 static int Task_execute(lua_State *L)
119         {
120         Task tk = checkTask(L,1);
121         int res = MSG_task_execute(tk);
122         lua_pushnumber(L,res);
123         return 1;
124         }
125
126 /**
127 *Function ro Desroy a Task
128 */
129 static int Task_destroy(lua_State *L)
130         {       
131         Task tk = checkTask(L,1);
132         int res = MSG_task_destroy(tk);
133         lua_pushnumber(L,res);
134         return 1;
135         }
136
137
138
139
140
141
142 //***************************************************************  HOST SECTION  ***************************************************************************
143
144
145
146 static Host toHost(lua_State *L,int index)
147         {
148         Host *pi = (Host*) lua_touserdata(L,index);
149         if(pi == NULL) luaL_typerror(L,index,HOST);
150         return *pi;
151         }
152
153
154
155 static Host checkHost (lua_State *L,int index)
156         {
157         Host *pi,ht;
158         luaL_checktype(L,index,LUA_TUSERDATA);
159         pi = (Host*)luaL_checkudata(L,index,HOST);
160         if(pi == NULL ) luaL_typerror(L,index,HOST);
161         ht = *pi;
162         if(!ht)
163                 luaL_error(L,"null Host");
164         return  ht;
165         }
166
167
168
169 static Host *pushHost (lua_State *L,Host ht)
170         {
171         Host *pi = (Host*)lua_newuserdata(L,sizeof(Host));
172         *pi=ht;
173         luaL_getmetatable(L,HOST);
174         lua_setmetatable(L,-2);
175         return pi;
176
177         }
178
179
180 static int Host_new(lua_State* L)
181         {
182         char *host_name=luaL_checkstring(L,1);
183         pushHost(L,MSG_get_host_by_name(host_name));
184         return 1;               
185         }
186
187
188
189 static int Host_name(lua_State* L)
190         {
191         Host ht = checkHost(L,1);
192         lua_pushstring(L,MSG_host_get_name(ht));        
193         return 1;
194         }
195
196
197 //*******************************************************   PROCESS SECTION      ***************************************************************************
198 static Process toProcess(lua_State *L,int index)
199         {
200         Process *pi = (Process*) lua_touserdata(L,index);
201         if(pi == NULL) luaL_typerror(L,index,PROCESS);
202         return *pi;
203         }
204
205
206
207 static Process checkProcess (lua_State *L,int index)
208         {
209         Process *pi,ps;
210         luaL_checktype(L,index,LUA_TUSERDATA);
211         pi = (Process*)luaL_checkudata(L,index,PROCESS);
212         if(pi == NULL ) luaL_typerror(L,index,PROCESS);
213         ps = *pi;
214         if(!ps)
215                 luaL_error(L,"null Process");
216         return  ps;
217         }
218
219
220
221 static Process *pushProcess (lua_State *L,Process ps)
222         {
223         Process *pi = (Process*)lua_newuserdata(L,sizeof(Process));
224         *pi=ps;
225         luaL_getmetatable(L,PROCESS);
226         lua_setmetatable(L,-2);
227         return pi;
228
229         }
230
231 /*
232 m_process_t MSG_process_create          (       const char *     name,
233                 xbt_main_func_t         code,
234                 void *          data,
235                 m_host_t        host     
236         )       
237 */
238
239
240
241 static int Process_new(lua_State* L)
242         {
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));
249         return 1;               
250
251         }
252
253
254 static int Process_kill(lua_State *L)
255         {
256         Process ps = checkProcess (L,1);
257         MSG_process_kill(ps);
258         return 0;
259         }
260
261
262
263 //**********************************************************MSG Operating System Functions******************************************************************
264 /**
265 * Function to Send a Task
266 */
267 static void Task_send(lua_State *L)     {
268   Task tk = checkTask(L,1);
269         char *mailbox = luaL_checkstring(L,2);
270         
271   int res = MSG_task_send(tk,mailbox);
272 }
273 /**
274 * Function to Get ( Recieve !! ) a Task
275 */
276 static int Task_recv(lua_State *L)      {
277   m_task_t tk = NULL;
278
279         char *mailbox = luaL_checkstring(L,1);
280         int res = MSG_task_receive(&tk,mailbox);
281         pushTask(L,tk);
282         return 1;
283 }
284
285
286 //*******************************************************   PLATFORM & APPLICATION SECTION  ****************************************************************
287
288
289  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WE WONT NEED IT (!!??) 
290 /**
291 * Launch Application Function
292 */
293
294 static int launch_application(lua_State *L)
295         {
296
297         const char * file = luaL_checkstring(L,1);
298         MSG_launch_application(file);
299         return 0;
300
301         }
302
303 /**
304 * Create Environment Function
305 */
306
307 static int create_environment(lua_State *L)
308         {
309
310         const char *file = luaL_checkstring(L,1);
311         MSG_create_environment(file);
312         return 0;
313
314         }
315
316 /**
317 * Function Register
318 */
319
320 static int function_register()
321         {
322
323         //Code ??!!!
324
325         }
326
327 //******************************************************************       REGISTERING          ************************************************************
328 /**
329 *Registering Into Lua
330 */
331
332
333 static const luaL_reg Task_methods[] = {
334 {"new",         Task_new},
335 {"name",        Task_name},
336 {"comp",        Task_comp},
337 {"execute",     Task_execute},
338 {"destroy",     Task_destroy},
339 {"send",                Task_send},
340 {"recv",                Task_recv},
341 {0,0}
342 };
343
344
345 static const luaL_reg Host_methods[] = {
346 {"new",         Host_new},
347 {"name",        Host_name},
348 {0,0}
349 };
350
351
352 static const luaL_reg Process_methods[] = {
353 {"new",         Process_new},
354 {"kill",        Process_kill},
355 {0,0}
356 };
357
358
359
360
361 /**
362 * Task_meta
363 */
364 static int Task_gc(lua_State *L)
365         {
366         Task tk=toTask(L,1);
367         if (tk) MSG_task_destroy(tk);
368         printf("GoodBye Task(%p)\n",lua_touserdata(L,1));
369         return 0; 
370         }
371
372 static int Task_tostring(lua_State *L)
373         {
374         lua_pushfstring(L,"Task :%p",lua_touserdata(L,1));
375         return 1;       
376         }
377
378
379 static const luaL_reg Task_meta[] = {
380         {"__gc",        Task_gc},
381         {"__tostring",  Task_tostring},
382         {0,0}
383 };
384
385
386
387
388 /**
389 * Host_meta
390 */
391 static int Host_gc(lua_State *L)
392         {
393         Host ht=toHost(L,1);
394         if (ht) ht=NULL;
395         printf("GoodBye Host(%p)\n",lua_touserdata(L,1));
396         return 0; 
397         }
398
399 static int Host_tostring(lua_State *L)
400         {
401         lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
402         return 1;       
403         }
404
405
406 static const luaL_reg Host_meta[] = {
407         {"__gc",        Host_gc},
408         {"__tostring",  Host_tostring},
409         {0,0}
410 };
411
412
413 /**
414 * Process_meta
415 */
416 static int Process_gc(lua_State *L)
417         {
418         Process ps=toProcess(L,1);
419         if (ps) MSG_process_kill(ps) ;
420         printf("GoodBye Process(%p)\n",lua_touserdata(L,1));
421         return 0; 
422         }
423
424 static int Process_tostring(lua_State *L)
425         {
426         lua_pushfstring(L,"Process :%p",lua_touserdata(L,1));
427         return 1;       
428         }
429
430
431 static const luaL_reg Process_meta[] = {
432         {"__gc",        Process_gc},
433         {"__tostring",  Process_tostring},
434         {0,0}
435 };
436
437
438 /**
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. 
440 */
441
442 /**
443 * Task Register
444 */
445
446 int Task_register(lua_State *L)
447         {       
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
458         return 1;
459         }
460           
461 /**
462 * Host Register
463 */
464
465 int Host_register(lua_State *L)
466         {       
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");
471         lua_pushvalue(L,-3);    
472         lua_rawset(L,-3);       
473         lua_pushliteral(L,"__metatable");
474         lua_pushvalue(L,-3);    
475         lua_rawset(L,-3);       
476         lua_pop(L,1);           
477         return 1;
478         }
479
480 /**
481 *  Process Register
482 */
483
484 int Process_register(lua_State *L)
485         {       
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");
490         lua_pushvalue(L,-3);    
491         lua_rawset(L,-3);       
492         lua_pushliteral(L,"__metatable");
493         lua_pushvalue(L,-3);    
494         lua_rawset(L,-3);       
495         lua_pop(L,1);           
496         return 1;
497         }
498