Logo AND Algorithmique Numérique Distribuée

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