Logo AND Algorithmique Numérique Distribuée

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