Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9a57dae2abb0a55cc28b17432956c2f1fd6fe46c
[simgrid.git] / src / bindings / lua / tasktest.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 // *** Testing Stuff !!
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(lua,"Lua bindings");
18
19 char *lua_file;
20 //***************************** LOAD LUA *************************************************
21 static int load_lua(char * luaFile, lua_State *L) {
22   luaL_openlibs(L);
23
24
25   if (luaL_loadfile(L, luaFile) || lua_pcall(L, 0, 0, 0)) {
26     printf("error while parsing %s: %s", luaFile, lua_tostring(L, -1));
27     exit(1);
28   }
29
30   return 0;
31
32 }
33
34 int lua_wrapper(int argc, char *argv[]) {
35   lua_State *L = lua_open();
36   load_lua(lua_file, L);
37
38   // Seek the right lua function
39   lua_getglobal(L,argv[0]);
40   if(!lua_isfunction(L,-1)) {
41     lua_pop(L,1);
42     ERROR1("The lua function %s does not seem to exist",argv[0]);
43     return -1;
44   }
45
46   // push arguments onto the stack
47   int i;
48   for(i=1;i<argc;i++)
49     lua_pushstring(L,argv[i]);
50
51   // Call the function
52   lua_call(L,argc-1,0); // takes argc-1 argument, returns nothing
53
54   // User process terminated
55   lua_pop(L, 1);
56   lua_close(L);
57   return 0;
58 }
59
60
61
62 //*****************************************************************************
63
64 extern const char*xbt_ctx_factory_to_use; /*Hack: let msg load directly the right factory */
65
66 int main(int argc,char * argv[]) {
67   MSG_error_t res = MSG_OK;
68   void *lua_factory;
69
70   xbt_ctx_factory_to_use = "lua";
71   MSG_global_init(&argc, argv);
72
73
74   if(argc < 4) {
75     printf("Usage: %s platform_file deployment_file lua_script\n", argv[0]);
76     printf("example: %s msg_platform.xml msg_deployment.xml script_lua.lua\n", argv[0]);
77     exit(1);
78
79   }
80
81   /* MSG_config("surf_workstation_model","KCCFLN05"); */
82   SIMIX_ctx_lua_factory_loadfile(argv[3]);
83
84   MSG_create_environment(argv[1]);
85   MSG_launch_application(argv[2]);
86
87   res = MSG_main();
88
89   fflush(stdout);
90   INFO1("Simulation time %g", MSG_get_clock());
91   MSG_clean();
92
93   if (res == MSG_OK)
94     return 0;
95   else
96     return 1;
97 }     
98
99
100
101
102
103