Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into MC_LTL
[simgrid.git] / examples / gras / properties / properties.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "gras.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Simple Property example");
10
11 int alice(int argc, char *argv[]);
12 int bob(int argc, char *argv[]);
13
14 int alice(int argc, char *argv[])
15 {
16   gras_init(&argc, argv);
17
18   /* Get the properties */
19   xbt_dict_t process_props = gras_process_properties();
20   xbt_dict_t host_props = gras_os_host_properties();
21
22   xbt_dict_cursor_t cursor = NULL;
23   char *key, *data;
24   _XBT_GNUC_UNUSED const char *value;
25
26   /* Let the other process change the host props */
27   gras_os_sleep(1);
28
29   XBT_INFO("== Dump all the properties of current host");
30   xbt_dict_foreach(host_props, cursor, key, data)
31       XBT_INFO("  Host property: '%s' has value: '%s'", key, data);
32
33   XBT_INFO("== Dump all the properties of alice");
34   xbt_dict_foreach(process_props, cursor, key, data)
35       if (!strncmp(key, "SG_TEST_", 8))
36     XBT_INFO("  Process property: '%s' has value: '%s'", key, data);
37
38   XBT_INFO("== Try to get a process property that does not exist");
39   value = gras_process_property_value("Nonexisting");
40   xbt_assert(!value, "nonexisting property exists!!");
41
42   /* Modify an existing property. First check it exists */
43   XBT_INFO("== Trying to modify a process property");
44   value = gras_process_property_value("new prop");
45   xbt_assert(!value, "Property 'new prop' exists before I add it!");
46   xbt_dict_set(process_props, "new prop", xbt_strdup("new value"), NULL);
47
48   /* Test if we have changed the value */
49   value = gras_process_property_value("new prop");
50   xbt_assert(!strcmp(value, "new value"),
51               "New property does have the value I've set ('%s' != 'new value')",
52               value);
53
54   gras_exit();
55   return 0;
56 }
57
58 int bob(int argc, char *argv[])
59 {
60   gras_init(&argc, argv);
61
62   /* Get the properties */
63   xbt_dict_t host_props = gras_os_host_properties();
64   xbt_dict_cursor_t cursor = NULL;
65   char *key, *data;
66   _XBT_GNUC_UNUSED const char *value;
67
68   XBT_INFO("== Dump all the properties of host1");
69   xbt_dict_foreach(host_props, cursor, key, data)
70       XBT_INFO("  Host property: '%s' has value: '%s'", key, data);
71
72   XBT_INFO("== Try to get a property that does not exist");
73   value = gras_os_host_property_value("non existing key");
74   xbt_assert(value == NULL,
75               "The key 'non existing key' exists (with value '%s')!!",
76               value);
77
78   XBT_INFO
79       ("== Set a host property that alice will try to retrieve in SG (from bob->hello)");
80   xbt_dict_set(host_props, "from bob", xbt_strdup("hello"), NULL);
81
82   XBT_INFO("== Dump all the properties of host1 again to check the addition");
83   xbt_dict_foreach(host_props, cursor, key, data)
84       XBT_INFO("  Host property: '%s' has value: '%s'", key, data);
85
86   gras_os_sleep(1);             /* KILLME once bug on empty main is solved */
87   gras_exit();
88   return 0;
89 }