Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c77484224577639af447af42e3406dfeafce29a1
[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 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
17     "Messages specific for this msg example");
18
19 int master_lua(int argc, char *argv[]); 
20 int slave_lua(int argc, char *argv[]); 
21 //int load_lua(char * file);
22 //int forwarder(int argc, char *argv[]); LUA
23 MSG_error_t test_all(const char *platform_file, const char *application_file);
24
25 typedef enum {
26
27   PORT_22 = 0,
28       MAX_CHANNEL
29
30 } channel_t;
31
32 char *lua_file;
33 //***************************** LOAD LUA *************************************************
34 static int load_lua(char * luaFile, lua_State *L) {
35   luaL_openlibs(L);
36
37   // Lua Stuff
38   Task_register(L);
39   Host_register(L);
40   Process_register(L);
41
42   if (luaL_loadfile(L, luaFile) || lua_pcall(L, 0, 0, 0)) {
43     printf("error while parsing %s: %s", luaFile, lua_tostring(L, -1));
44     return -1;
45   }
46
47   return 0;
48
49 }
50
51 int lua_wrapper(int argc, char *argv[]) {
52   lua_State *L = lua_open();
53   load_lua(lua_file, L);
54
55   // Seek the right lua function
56   lua_getglobal(L,argv[0]);
57   if(!lua_isfunction(L,-1)) {
58     lua_pop(L,1);
59     ERROR1("The lua function %s does not seem to exist",argv[0]);
60     return -1;
61   }
62
63   // push arguments onto the stack
64   int i;
65   for(i=1;i<argc;i++)
66     lua_pushstring(L,argv[i]);
67
68   // Call the function
69   lua_call(L,argc-1,0); // takes argc-1 argument, returns nothing
70
71   // User process terminated
72   lua_pop(L, 1);
73   lua_close(L);
74   return 0;
75 }
76
77
78
79 //*****************************************************************************
80
81 int main(int argc,char * argv[])
82 {
83
84
85   MSG_error_t res = MSG_OK;
86
87   MSG_global_init(&argc, argv);
88
89   if(argc < 4)
90   {
91     printf("Usage: %s platform_file deployment_file lua_script\n", argv[0]);
92     printf("example: %s msg_platform.xml msg_deployment.xml script_lua.lua\n", argv[0]);
93     exit(1);
94
95   }
96
97   lua_file=argv[3];
98 //  load_lua(argv[3]);
99
100   /* MSG_config("surf_workstation_model","KCCFLN05"); */
101   MSG_create_environment(argv[1]);
102   MSG_function_register_default(&lua_wrapper);
103   MSG_launch_application(argv[2]);
104   res = MSG_main();
105
106   fflush(stdout);
107   INFO1("Simulation time %g", MSG_get_clock());
108
109   MSG_clean();
110
111   if (res == MSG_OK)
112     return 0;
113   else
114     return 1;
115 }     
116
117
118
119
120
121