Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6c88118de1278f9ef405932550d41f8e5305b76e
[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 forwarder(int argc, char *argv[]);
22 MSG_error_t test_all(const char *platform_file, const char *application_file);
23
24 int alice(int argc, char *argv[])
25 {
26   m_host_t host1 = MSG_get_host_by_name("host1");
27   xbt_dict_t props = MSG_host_get_properties(host1);
28   xbt_dict_cursor_t cursor = NULL;
29   char *key, *data;
30   const char *noexist = "Unknown";
31   const char *value;
32   char exist[] = "SG_TEST_Hdd";
33
34   INFO0("== Print the properties of the host");
35   xbt_dict_foreach(props, cursor, key, data)
36     INFO2("  Host property: '%s' -> '%s'", key, data);
37
38   INFO0("== Try to get a host property that does not exist");
39   value = MSG_host_get_property_value(host1, noexist);
40   xbt_assert0(!value, "The key exists (it's not supposed to)");
41
42   INFO0("== Try to get a host property that does exist");
43   value = MSG_host_get_property_value(host1, exist);
44   xbt_assert1(value, "\tProperty %s is undefined (where it should)", exist);
45   xbt_assert2(!strcmp(value, "180"),
46               "\tValue of property %s is defined to %s (where it should be 180)",
47               exist, value);
48   INFO2("   Property: %s old value: %s", exist, value);
49
50   INFO0("== Trying to modify a host property");
51   xbt_dict_set(props, exist, xbt_strdup("250"), xbt_free_f);
52
53   /* Test if we have changed the value */
54   value = MSG_host_get_property_value(host1, exist);
55   xbt_assert1(value, "Property %s is undefined (where it should)", exist);
56   xbt_assert2(!strcmp(value, "250"),
57               "Value of property %s is defined to %s (where it should be 250)",
58               exist, value);
59   INFO2("   Property: %s old value: %s", exist, value);
60
61   return 0;
62 }
63
64 int bob(int argc, char *argv[])
65 {
66   /* Get the property list of current bob process */
67   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
68   xbt_dict_cursor_t cursor = NULL;
69   char *key, *data;
70   const char *noexist = "UnknownProcessProp";
71   const char *value;
72
73   INFO0("== Print the properties of the process");
74   xbt_dict_foreach(props, cursor, key, data)
75     INFO2("   Process property: %s -> %s", key, data);
76
77   INFO0("== Try to get a process property that does not exist");
78
79   value = MSG_process_get_property_value(MSG_process_self(), noexist);
80   xbt_assert0(!value, "The property is defined (it shouldnt)");
81
82   return 0;
83 }
84
85 /** Test function */
86 MSG_error_t test_all(const char *platform_file, const char *application_file)
87 {
88   MSG_function_register("alice", alice);
89   MSG_function_register("bob", bob);
90
91   MSG_create_environment(platform_file);
92   MSG_launch_application(application_file);
93
94   return MSG_main();
95 }                               /* end_of_test_all */
96
97
98 /** Main function */
99 int main(int argc, char *argv[])
100 {
101   MSG_error_t res = MSG_OK;
102
103   MSG_global_init(&argc, argv);
104   if (argc < 3) {
105     printf("Usage: %s platform_file deployment_file\n", argv[0]);
106     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
107     exit(1);
108   }
109   res = test_all(argv[1], argv[2]);
110   MSG_clean();
111
112   if (res == MSG_OK)
113     return 0;
114   else
115     return 1;
116 }                               /* end_of_main */