Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Just declare the variable at the beginning of the function (strict ansi)
[simgrid.git] / examples / msg / properties / msg_prop.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007. SimGrid Team. 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 "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Property test");
19
20 int master(int argc, char *argv[]);
21 int slave(int argc, char *argv[]);
22 int forwarder(int argc, char *argv[]);
23 MSG_error_t test_all(const char *platform_file, const char *application_file);
24
25 /** Emitter function  */
26 int master(int argc, char *argv[])
27 {
28   int slaves_count = 0;
29   m_host_t *slaves = NULL;
30   xbt_dict_t props; 
31   xbt_dict_cursor_t cursor=NULL;
32   int i;
33   char *key,*data;
34   const char *noexist="Unknown";
35   const char*value;
36   char exist[]="Hdd";
37
38   {                  /* Process organisation */
39     slaves_count = argc - 4;
40     slaves = xbt_new(m_host_t, sizeof(m_host_t) * slaves_count);
41       
42     for (i = 4; i < argc; i++) {     
43       slaves[i-4] = MSG_get_host_by_name(argv[i]);
44       xbt_assert1(slaves[i-4]!=NULL, "Unknown host %s. Stopping Now! ", argv[i]);
45
46       /* Get the property list of the host */
47       props = MSG_host_get_properties(slaves[i-4]);
48      
49       
50
51       /* Print the properties of the host */
52       xbt_dict_foreach(props,cursor,key,data) {
53          INFO3("Property: %s for host: %s has value: %s",key,argv[i],data);
54       }
55
56      /* Try to get a property that does not exist */
57      
58      value = MSG_host_get_property_value(slaves[i-4], noexist);
59      if ( value == NULL) 
60        INFO2("Property: %s for host %s is undefined", noexist, argv[i]);
61      else
62        INFO3("Property: %s for host %s has value: %s",(char*) noexist, argv[i], value);
63
64       /* Modify an existing property test. First check it exists */\
65       INFO0("Trying to modify a host property");
66       
67       value = MSG_host_get_property_value(slaves[i-4],exist);
68       xbt_assert1(value,"\tProperty %s is undefined", exist);
69       INFO2("\tProperty: %s old value: %s", exist, value);
70       xbt_dict_set(props, exist, xbt_strdup("250"), free);  
71  
72       /* Test if we have changed the value */
73       value = MSG_host_get_property_value(slaves[i-4],exist);
74       xbt_assert1(value,"\tProperty %s is undefined", exist);
75       INFO2("\tProperty: %s new value: %s", exist, value);
76     }
77   }
78   
79   free(slaves);
80   return 0;
81 } /* end_of_master */
82
83 /** Receiver function  */
84 int slave(int argc, char *argv[])
85 {
86   /* Get the property list of current slave process */
87   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
88   xbt_dict_cursor_t cursor=NULL;
89   char *key,*data;
90   const char *noexist="UnknownProcessProp";
91   const char *value;
92
93   /* Print the properties of the process */
94   xbt_dict_foreach(props,cursor,key,data) {
95      INFO3("Property: %s for process %s has value: %s",key,MSG_process_get_name(MSG_process_self()),data);
96   }
97
98   /* Try to get a property that does not exist */
99  
100   value = MSG_process_get_property_value(MSG_process_self(),noexist);
101   if ( value == NULL) 
102     INFO2("Property: %s for process %s is undefined", noexist, MSG_process_get_name(MSG_process_self()));
103   else
104     INFO3("Property: %s for process %s has value: %s", noexist, MSG_process_get_name(MSG_process_self()), value);
105
106   return 0;
107 } /* end_of_slave */
108
109 /** Test function */
110 MSG_error_t test_all(const char *platform_file,
111                             const char *application_file)
112 {
113   MSG_error_t res = MSG_OK;
114
115   /* MSG_config("surf_workstation_model","KCCFLN05"); */
116   {                             /*  Simulation setting */
117 //    MSG_set_channel_number(MAX_CHANNEL);
118     MSG_paje_output("msg_test.trace");
119     MSG_create_environment(platform_file);
120   }
121   {                            /*   Application deployment */
122     MSG_function_register("master", master);
123     MSG_function_register("slave", slave);
124     MSG_launch_application(application_file);
125   }
126   res = MSG_main();
127   
128   INFO1("Simulation time %g",MSG_get_clock());
129   return res;
130 } /* end_of_test_all */
131
132
133 /** Main function */
134 int main(int argc, char *argv[])
135 {
136   MSG_error_t res = MSG_OK;
137
138   MSG_global_init(&argc,argv);
139   if (argc < 3) {
140      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
141      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
142      exit(1);
143   }
144   res = test_all(argv[1],argv[2]);
145   MSG_clean();
146
147   if(res==MSG_OK)
148     return 0;
149   else
150     return 1;
151 } /* end_of_main */