Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : hash of regions in snapshot stored instead of all the data
[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"),
47                xbt_free_f);
48
49   /* Test if we have changed the value */
50   value = gras_process_property_value("new prop");
51   xbt_assert(!strcmp(value, "new value"),
52               "New property does have the value I've set ('%s' != 'new value')",
53               value);
54
55   gras_exit();
56   return 0;
57 }
58
59 int bob(int argc, char *argv[])
60 {
61   gras_init(&argc, argv);
62
63   /* Get the properties */
64   xbt_dict_t host_props = gras_os_host_properties();
65   xbt_dict_cursor_t cursor = NULL;
66   char *key, *data;
67   _XBT_GNUC_UNUSED const char *value;
68
69   XBT_INFO("== Dump all the properties of host1");
70   xbt_dict_foreach(host_props, cursor, key, data)
71       XBT_INFO("  Host property: '%s' has value: '%s'", key, data);
72
73   XBT_INFO("== Try to get a property that does not exist");
74   value = gras_os_host_property_value("non existing key");
75   xbt_assert(value == NULL,
76               "The key 'non existing key' exists (with value '%s')!!",
77               value);
78
79   XBT_INFO
80       ("== Set a host property that alice will try to retrieve in SG (from bob->hello)");
81   xbt_dict_set(host_props, "from bob", xbt_strdup("hello"), xbt_free_f);
82
83   XBT_INFO("== Dump all the properties of host1 again to check the addition");
84   xbt_dict_foreach(host_props, cursor, key, data)
85       XBT_INFO("  Host property: '%s' has value: '%s'", key, data);
86
87   gras_os_sleep(1);             /* KILLME once bug on empty main is solved */
88   gras_exit();
89   return 0;
90 }