Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix some conflicts with modifications introduced in the svn branch
[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 {
27   xbt_dict_set(action_funs, action_name, function, NULL);
28 }
29
30 /** \ingroup msg_actions
31  * \brief Unregisters a function, which handled a kind of action
32  *
33  * \param name the reference name of the action.
34  */
35 void MSG_action_unregister(const char *action_name)
36 {
37   xbt_dict_remove(action_funs, action_name);
38 }
39
40 static int MSG_action_runner(int argc, char *argv[])
41 {
42   xbt_dynar_t evt;
43   unsigned int cursor;
44
45   xbt_dynar_foreach(action_list, cursor, evt) {
46     if (!strcmp(xbt_dynar_get_as(evt, 0, char *), argv[0])) {
47       msg_action_fun function =
48         xbt_dict_get(action_funs, xbt_dynar_get_as(evt, 1, char *));
49       (*function) (evt);
50     }
51   }
52
53   return 0;
54 }
55
56 void _MSG_action_init()
57 {
58   action_funs = xbt_dict_new();
59   action_list = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
60   MSG_function_register_default(MSG_action_runner);
61 }
62
63 /** \ingroup msg_actions
64  * \brief A trace loader
65  *
66  *  Load a trace file containing actions, and execute them.
67  */
68 MSG_error_t MSG_action_trace_run(char *path)
69 {
70   MSG_error_t res;
71
72   FILE *fp;
73   char *line = NULL;
74   size_t len = 0;
75   ssize_t read;
76
77   xbt_dynar_t evt;
78
79   fp = fopen(path, "r");
80   xbt_assert2(fp != NULL, "Cannot open %s: %s", path, strerror(errno));
81
82   while ((read = getline(&line, &len, fp)) != -1) {
83     char *comment = strchr(line, '#');
84     if (comment != NULL)
85       *comment = '\0';
86     xbt_str_trim(line, NULL);
87     if (line[0] == '\0')
88       continue;
89     evt = xbt_str_split_quoted(line);
90     xbt_dynar_push(action_list, &evt);
91   }
92
93   if (line)
94     free(line);
95   fclose(fp);
96
97   res = MSG_main();
98   xbt_dynar_free(&action_list);
99   action_list = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
100
101   return res;
102 }