Logo AND Algorithmique Numérique Distribuée

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