Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
debug debug debug
[simgrid.git] / src / msg / deployment.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include"private.h"
9 #include"xbt/sysdep.h"
10 #include "xbt/error.h"
11 #include "surf/surf_parse.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(deployment, msg,
13                                 "Logging specific to MSG (environment)");
14
15 extern char *yytext;
16
17
18 /** \ingroup msg_easier_life
19  * \brief An application deployer.
20  *
21  * Creates the process described in \a file.
22  * @param file a file containing a simple description of the application.
23  * The format of each line is the following : host_name function_name arguments
24  * You can use C-style comments and C-style strings.
25  * Here is a simple exemple
26  * 
27  \verbatim
28 <DEPLOYMENT>
29  // Here is the master
30  the-doors.ens-lyon.fr  master "moby.cri2000.ens-lyon.fr" canaria.ens-lyon.fr popc.ens-lyon.fr
31  // And here are the slaves
32  moby.cri2000.ens-lyon.fr slave
33  canaria.ens-lyon.fr slave
34  popc.ens-lyon.fr slave 
35 </DEPLOYMENT>
36 \endverbatim
37  */
38 void MSG_launch_application(const char *file) 
39 {
40   char *host_name = NULL;
41   int argc = 0 ;
42   char **argv = NULL;
43   m_process_t process = NULL ;
44   m_host_t host = NULL;
45   m_process_code_t code = NULL;
46   e_surf_token_t token;
47
48   MSG_global_init();
49   
50   find_section(file, "DEPLOYMENT");
51
52   while(1) {
53     token = surf_parse();
54
55     if (token == TOKEN_END_SECTION)
56       break;
57     if (token == TOKEN_NEWLINE)
58       continue;
59
60     if (token == TOKEN_WORD) {
61       surf_parse_deployment_line(&host_name,&argc,&argv);
62       xbt_assert0(argc,"No function to execute");
63
64       code = MSG_get_registered_function(argv[0]);
65       xbt_assert1(code, "Unknown function %s",argv[0]);
66
67       host = MSG_get_host_by_name(host_name);
68       xbt_assert1(host, "Unknown host %s",host_name);
69
70      process = MSG_process_create(argv[0], code, NULL, host);
71      MSG_set_arguments(process, argc, argv);
72      xbt_free(host_name); 
73     }
74     else {
75       CRITICAL1("Parse error line %d\n", surf_line_pos);
76       xbt_abort();
77     }
78   }
79
80   close_section("DEPLOYMENT");
81 }
82
83 /** \ingroup msg_easier_life
84  * \brief Registers a ::m_process_code_t code in a global table.
85  *
86  * Registers a code function in a global table. 
87  * This table is then used by ::MSG_launch_application. 
88  * \param name the reference name of the function.
89  * \param code the function
90  */
91 void MSG_function_register(const char *name,m_process_code_t code)
92 {
93   MSG_global_init();
94
95   xbt_dict_set(msg_global->registered_functions,name,code,NULL);
96 }
97
98 /** \ingroup msg_easier_life
99  * \brief Registers a ::m_process_t code in a global table.
100  *
101  * Registers a code function in a global table. 
102  * This table is then used by ::MSG_launch_application. 
103  * \param name the reference name of the function.
104  */
105 m_process_code_t MSG_get_registered_function(const char *name)
106 {
107   m_process_code_t code = NULL;
108
109   MSG_global_init();
110  
111   xbt_dict_get(msg_global->registered_functions,name,(void **) &code);
112
113   return code;
114 }
115
116 /** \ingroup msg_easier_life
117  * \brief Get the arguments of the current process.
118  * \deprecated{Not useful since m_process_code_t is int (*)(int argc, char *argv[])}
119  *
120  * This functions returns the values set for the current process 
121  * using ::MSG_set_arguments or ::MSG_launch_application.
122  * \param argc the number of arguments
123  * \param argv the arguments table
124  */
125 MSG_error_t MSG_get_arguments(int *argc, char ***argv)
126 {
127   m_process_t process = MSG_process_self();
128   simdata_process_t simdata = NULL;
129
130   xbt_assert0((argc) && (argv), "Invalid parameters");
131   simdata = process->simdata;
132   *argc = simdata->argc;
133   *argv = simdata->argv;
134
135   return MSG_OK;
136 }
137
138 /** \ingroup msg_easier_life
139  * \brief Set the arguments of a process.
140  *
141  * This functions sets the argument number and the arguments table for a
142  * proces.
143  * \param process is the process you want to modify
144  * \param argc the number of arguments
145  * \param argv the arguments table
146  */
147 MSG_error_t MSG_set_arguments(m_process_t process,int argc, char *argv[])
148 {
149   simdata_process_t simdata = NULL;
150
151   xbt_assert0((process) && (process->simdata), "Invalid parameters");
152   simdata = process->simdata;
153   simdata->argc = argc;
154   simdata->argv = argv;
155   return MSG_OK;
156 }
157