Logo AND Algorithmique Numérique Distribuée

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