Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
first draft of the action runner
[simgrid.git] / src / msg / msg_actions.c
1 /* Copyright (c) 2009 The SimGrid team. All rights reserved.                */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "msg/private.h"
7 #include "xbt/str.h"
8 #include "xbt/dynar.h"
9
10 static xbt_dict_t action_funs;
11 static xbt_dynar_t action_list;
12
13
14 /** \ingroup msg_actions
15  * \brief Registers a function to handle a kind of action
16  *
17  * Registers a function to handle a kind of action
18  * This table is then used by #MSG_action_trace_run
19  *
20  * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
21  *
22  * \param name the reference name of the action.
23  * \param code the function; prototype given by the type: void...(xbt_dynar_t action)
24  */
25 void MSG_action_register(const char*action_name, msg_action_fun function) {
26         xbt_dict_set(action_funs,action_name,function,NULL);
27 }
28 /** \ingroup msg_actions
29  * \brief Unregisters a function, which handled a kind of action
30  *
31  * \param name the reference name of the action.
32  */
33 void MSG_action_unregister(const char*action_name) {
34         xbt_dict_remove(action_funs,action_name);
35 }
36
37 static int MSG_action_runner(int argc, char* argv[]) {
38         xbt_dynar_t evt;
39         unsigned int cursor;
40
41         xbt_dynar_foreach(action_list,cursor,evt) {
42                 if (!strcmp(xbt_dynar_get_as(evt,0,char*),argv[0])) {
43                         msg_action_fun function = xbt_dict_get(action_funs,xbt_dynar_get_as(evt,1,char*));
44                         (*function)(evt);
45                 }
46         }
47
48         return 0;
49 }
50
51 void _MSG_action_init() {
52         action_funs = xbt_dict_new();
53         action_list = xbt_dynar_new(sizeof(xbt_dynar_t),xbt_dynar_free_voidp);
54         MSG_function_register_default(MSG_action_runner);
55 }
56
57 /** \ingroup msg_actions
58  * \brief A trace loader
59  *
60  *  Load a trace file containing actions, and execute them.
61  */
62 MSG_error_t MSG_action_trace_run(char*path) {
63         MSG_error_t res;
64
65     FILE * fp;
66     char * line = NULL;
67     size_t len = 0;
68     ssize_t read;
69
70     xbt_dynar_t evt;
71
72     fp = fopen(path, "r");
73     xbt_assert2(fp != NULL,"Cannot open %s: %s",path,strerror(errno));
74
75     while ((read = getline(&line, &len, fp)) != -1) {
76         char *comment = strchr(line,'#');
77         if (comment!=NULL)
78                 *comment='\0';
79         xbt_str_trim(line,NULL);
80         if (line[0]=='\0')
81                 continue;
82         evt = xbt_str_split_quoted(line);
83         xbt_dynar_push(action_list,&evt);
84     }
85
86     if (line)
87         free(line);
88     fclose(fp);
89
90         res = MSG_main();
91         xbt_dynar_free(&action_list);
92         action_list = xbt_dynar_new(sizeof(xbt_dynar_t),xbt_dynar_free_voidp);
93
94         return res;
95 }