Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reorg examples/simdag for better user experience
[simgrid.git] / examples / simdag / properties / sd_properties.c
1 /* Copyright (c) 2007-2016. 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 "simgrid/simdag.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Property test");
10
11 int main(int argc, char **argv)
12 {
13   xbt_dict_cursor_t cursor = NULL;
14   char *key, *data;
15   char noexist[] = "NoProp";
16   const char *value;
17   char exist[] = "Hdd";
18
19   /* SD initialization */
20   SD_init(&argc, argv);
21   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s ../../platforms/prop.xml", argv[0], argv[0]);
22
23   SD_create_environment(argv[1]);
24
25   /* init of platform elements */
26   sg_host_t h1 = sg_host_by_name("host1");
27   sg_host_t h2 = sg_host_by_name("host2");
28   const char *name1 = sg_host_get_name(h1);
29   const char *name2 = sg_host_get_name(h2);
30
31   /* Get the property list of 'host1' */
32   XBT_INFO("Property list for host %s", name1);
33   xbt_dict_t props = sg_host_get_properties(h1);
34
35   /* Trying to set a new property */
36   xbt_dict_set(props, "NewProp", strdup("newValue"), xbt_free_f);
37
38   /* Print the properties of 'host1' */
39   xbt_dict_foreach(props, cursor, key, data) {
40     XBT_INFO("\tProperty: %s has value: %s", key, data);
41   }
42
43   /* Try to get a property that does not exist */
44   value = sg_host_get_property_value(h1, noexist);
45   XBT_INFO("\tProperty: %s has value: %s", noexist, value?value:"Undefined (NULL)");
46
47   /* Get the property list of 'host2' */
48   XBT_INFO("Property list for host %s", name2);
49   props = sg_host_get_properties(h2);
50
51   /* Print the properties of 'host2' */
52   xbt_dict_foreach(props, cursor, key, data) {
53     XBT_INFO("\tProperty: %s on host: %s", key, data);
54   }
55
56   /* Modify an existing property test. First check it exists */
57   XBT_INFO("Modify an existing property");
58
59   value = sg_host_get_property_value(h2, exist);
60   if (value == NULL)
61     XBT_INFO("\tProperty: %s is undefined", exist);
62   else {
63     XBT_INFO("\tProperty: %s old value: %s", exist, value);
64     xbt_dict_set(props, exist, strdup("250"), xbt_free_f);
65   }
66
67   /* Test if we have changed the value */
68   value = sg_host_get_property_value(h2, exist);
69   XBT_INFO("\tProperty: %s new value: %s", exist, value?value:"Undefined (NULL)");
70
71   /* Test if properties are displayed by sg_host_dump */
72   sg_host_dump(h2);
73
74   SD_exit();
75   return 0;
76 }