Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Explain the copious warnings of automake in this directory in the comment
[simgrid.git] / examples / gras / properties / properties.c
1 /*      $Id$     */
2
3 #include "gras.h"
4
5 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Simple Property example");
6
7 int alice(int argc, char *argv[]);
8 int bob(int argc, char *argv[]);
9
10 int alice(int argc, char *argv[]) {
11   gras_init(&argc,argv);
12
13   /* Get the properties */
14   xbt_dict_t process_props = gras_process_properties();
15   xbt_dict_t host_props = gras_os_host_properties();
16
17   xbt_dict_cursor_t cursor = NULL;
18   char *key,*data;
19   const char *value;
20
21   /* Let the other process change the host props */
22   gras_os_sleep(1); 
23    
24   INFO0("== Dump all the properties of current host");
25   xbt_dict_foreach(host_props,cursor,key,data)
26        INFO2("  Host property: '%s' has value: '%s'",key,data);
27
28   INFO0("== Dump all the properties of alice");
29   xbt_dict_foreach(process_props,cursor,key,data)
30     if (!strncmp(key,"SG_TEST_",8))
31        INFO2("  Process property: '%s' has value: '%s'",key,data);
32  
33   INFO0("== Try to get a process property that does not exist");
34   value = gras_process_property_value("Nonexisting");
35   xbt_assert0(!value, "nonexisting property exists!!");
36  
37   /* Modify an existing property. First check it exists */
38   INFO0("== Trying to modify a process property");
39   value = gras_process_property_value("new prop");
40   xbt_assert0(!value,"Property 'new prop' exists before I add it!");
41   xbt_dict_set(process_props, "new prop", xbt_strdup("new value"), xbt_free_f);
42  
43   /* Test if we have changed the value */
44   value = gras_process_property_value("new prop");
45   xbt_assert1(!strcmp(value,"new value"), "New property does have the value I've set ('%s' != 'new value')",value);
46
47   gras_exit();
48   return 0;
49 }
50
51 int bob(int argc, char *argv[]) {
52   gras_init(&argc,argv);
53
54   /* Get the properties */
55   xbt_dict_t host_props = gras_os_host_properties();
56   xbt_dict_cursor_t cursor = NULL;
57   char *key,*data;
58   const char *value;
59
60   INFO0("== Dump all the properties of host1");
61   xbt_dict_foreach(host_props,cursor,key,data)
62      INFO2("  Host property: '%s' has value: '%s'",key,data);
63  
64   INFO0("== Try to get a property that does not exist");
65   value = gras_os_host_property_value("non existing key");
66   xbt_assert1(value == NULL, "The key 'non existing key' exists (with value '%s')!!",value);
67
68   INFO0("== Set a host property that alice will try to retrieve in SG (from bob->hello)");
69   xbt_dict_set(host_props,"from bob",xbt_strdup("hello"), xbt_free_f);
70
71   INFO0("== Dump all the properties of host1 again to check the addition");
72   xbt_dict_foreach(host_props,cursor,key,data)
73      INFO2("  Host property: '%s' has value: '%s'",key,data);
74    
75   gras_os_sleep(1); /* KILLME once bug on empty main is solved */
76   gras_exit();
77   return 0;
78 }