Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a test trying to access the properties of a remote host -- it works
[simgrid.git] / examples / msg / properties / msg_prop.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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
8 #include "xbt/sysdep.h"         /* calloc, printf */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Property test");
18
19 int alice(int argc, char *argv[]);
20 int bob(int argc, char *argv[]);
21 int carole(int argc, char *argv[]);
22 int forwarder(int argc, char *argv[]);
23 MSG_error_t test_all(const char *platform_file,
24                      const char *application_file);
25
26 static void test_host(const char*hostname) 
27 {
28   m_host_t thehost = MSG_get_host_by_name(hostname);
29   xbt_dict_t props = MSG_host_get_properties(thehost);
30   xbt_dict_cursor_t cursor = NULL;
31   char *key, *data;
32   const char *noexist = "Unknown";
33   const char *value;
34   char exist[] = "SG_TEST_Hdd";
35
36   XBT_INFO("== Print the properties of the host");
37   xbt_dict_foreach(props, cursor, key, data)
38       XBT_INFO("  Host property: '%s' -> '%s'", key, data);
39
40   XBT_INFO("== Try to get a host property that does not exist");
41   value = MSG_host_get_property_value(thehost, noexist);
42   xbt_assert(!value, "The key exists (it's not supposed to)");
43
44   XBT_INFO("== Try to get a host property that does exist");
45   value = MSG_host_get_property_value(thehost, exist);
46   xbt_assert(value, "\tProperty %s is undefined (where it should)",
47               exist);
48   xbt_assert(!strcmp(value, "180"),
49               "\tValue of property %s is defined to %s (where it should be 180)",
50               exist, value);
51   XBT_INFO("   Property: %s old value: %s", exist, value);
52
53   XBT_INFO("== Trying to modify a host property");
54   xbt_dict_set(props, exist, xbt_strdup("250"), xbt_free_f);
55
56   /* Test if we have changed the value */
57   value = MSG_host_get_property_value(thehost, exist);
58   xbt_assert(value, "Property %s is undefined (where it should)", exist);
59   xbt_assert(!strcmp(value, "250"),
60               "Value of property %s is defined to %s (where it should be 250)",
61               exist, value);
62   XBT_INFO("   Property: %s old value: %s", exist, value);
63    
64   /* Restore the value for the next test */
65   xbt_dict_set(props, exist, xbt_strdup("180"), xbt_free_f);
66 }
67
68 int alice(int argc, char *argv[]) { /* Dump what we have on the current host */
69   test_host("host1");
70   return 0;
71 }
72 int carole(int argc, char *argv[]) {/* Dump what we have on a remote host */
73   MSG_process_sleep(1); // Wait for alice to be done with its experiment
74   test_host("host1");
75   return 0;
76 }
77
78 int bob(int argc, char *argv[])
79 {
80   /* Get the property list of current bob process */
81   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
82   xbt_dict_cursor_t cursor = NULL;
83   char *key, *data;
84   const char *noexist = "UnknownProcessProp";
85   _XBT_GNUC_UNUSED const char *value;
86
87   XBT_INFO("== Print the properties of the process");
88   xbt_dict_foreach(props, cursor, key, data)
89       XBT_INFO("   Process property: %s -> %s", key, data);
90
91   XBT_INFO("== Try to get a process property that does not exist");
92
93   value = MSG_process_get_property_value(MSG_process_self(), noexist);
94   xbt_assert(!value, "The property is defined (it shouldnt)");
95
96   return 0;
97 }
98
99 /** Test function */
100 MSG_error_t test_all(const char *platform_file,
101                      const char *application_file)
102 {
103   MSG_function_register("alice", alice);
104   MSG_function_register("bob", bob);
105   MSG_function_register("carole", carole);
106
107   MSG_create_environment(platform_file);
108   MSG_launch_application(application_file);
109
110   return MSG_main();
111 }                               /* end_of_test_all */
112
113
114 /** Main function */
115 int main(int argc, char *argv[])
116 {
117   MSG_error_t res = MSG_OK;
118
119   MSG_global_init(&argc, argv);
120   if (argc < 3) {
121     printf("Usage: %s platform_file deployment_file\n", argv[0]);
122     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
123     exit(1);
124   }
125   res = test_all(argv[1], argv[2]);
126   MSG_clean();
127
128   if (res == MSG_OK)
129     return 0;
130   else
131     return 1;
132 }                               /* end_of_main */