Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9706ed0d499c35a584cf48ed52304ccdb1df5eb2
[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
31   int i;
32
33   {                  /* Process organisation */
34     slaves_count = argc - 4;
35     slaves = xbt_new(m_host_t, sizeof(m_host_t) * slaves_count);
36     xbt_dict_t props;    
37     for (i = 4; i < argc; i++) {     
38       slaves[i-4] = MSG_get_host_by_name(argv[i]);
39       xbt_assert1(slaves[i-4]!=NULL, "Unknown host %s. Stopping Now! ", argv[i]);
40
41       /* Get the property list of the host */
42       props = MSG_host_get_properties(slaves[i-4]);
43       xbt_dict_cursor_t cursor=NULL;
44       char *key,*data;
45
46       /* Print the properties of the host */
47       xbt_dict_foreach(props,cursor,key,data) {
48          INFO3("Property: %s for host: %s has value: %s",key,argv[i],data);
49       }
50
51      /* Try to get a property that does not exist */
52      const char *noexist="Unknown";
53      const char*value = MSG_host_get_property_value(slaves[i-4], noexist);
54      if ( value == NULL) 
55        INFO2("Property: %s for host %s is undefined", noexist, argv[i]);
56      else
57        INFO3("Property: %s for host %s has value: %s",(char*) noexist, argv[i], value);
58
59       /* Modify an existing property test. First check it exists */\
60       INFO0("Trying to modify a host property");
61       char exist[]="Hdd";
62       value = MSG_host_get_property_value(slaves[i-4],exist);
63       xbt_assert1(value,"\tProperty %s is undefined", exist);
64       INFO2("\tProperty: %s old value: %s", exist, value);
65       xbt_dict_set(props, exist, xbt_strdup("250"), free);  
66  
67       /* Test if we have changed the value */
68       value = MSG_host_get_property_value(slaves[i-4],exist);
69       xbt_assert1(value,"\tProperty %s is undefined", exist);
70       INFO2("\tProperty: %s new value: %s", exist, value);
71     }
72   }
73   
74   free(slaves);
75   return 0;
76 } /* end_of_master */
77
78 /** Receiver function  */
79 int slave(int argc, char *argv[])
80 {
81   /* Get the property list of current slave process */
82   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
83   xbt_dict_cursor_t cursor=NULL;
84   char *key,*data;
85
86   /* Print the properties of the process */
87   xbt_dict_foreach(props,cursor,key,data) {
88      INFO3("Property: %s for process %s has value: %s",key,MSG_process_get_name(MSG_process_self()),data);
89   }
90
91   /* Try to get a property that does not exist */
92   const char *noexist="UnknownProcessProp";
93   const char *value = MSG_process_get_property_value(MSG_process_self(),noexist);
94   if ( value == NULL) 
95     INFO2("Property: %s for process %s is undefined", noexist, MSG_process_get_name(MSG_process_self()));
96   else
97     INFO3("Property: %s for process %s has value: %s", noexist, MSG_process_get_name(MSG_process_self()), value);
98
99   return 0;
100 } /* end_of_slave */
101
102 /** Test function */
103 MSG_error_t test_all(const char *platform_file,
104                             const char *application_file)
105 {
106   MSG_error_t res = MSG_OK;
107
108   /* MSG_config("surf_workstation_model","KCCFLN05"); */
109   {                             /*  Simulation setting */
110 //    MSG_set_channel_number(MAX_CHANNEL);
111     MSG_paje_output("msg_test.trace");
112     MSG_create_environment(platform_file);
113   }
114   {                            /*   Application deployment */
115     MSG_function_register("master", master);
116     MSG_function_register("slave", slave);
117     MSG_launch_application(application_file);
118   }
119   res = MSG_main();
120   
121   INFO1("Simulation time %g",MSG_get_clock());
122   return res;
123 } /* end_of_test_all */
124
125
126 /** Main function */
127 int main(int argc, char *argv[])
128 {
129   MSG_error_t res = MSG_OK;
130
131   MSG_global_init(&argc,argv);
132   if (argc < 3) {
133      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
134      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
135      exit(1);
136   }
137   res = test_all(argv[1],argv[2]);
138   MSG_clean();
139
140   if(res==MSG_OK)
141     return 0;
142   else
143     return 1;
144 } /* end_of_main */