Logo AND Algorithmique Numérique Distribuée

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