Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ca19687633b8e3278e8642d1b913a27a4e1ad83c
[simgrid.git] / examples / msg / properties / msg_prop.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007. SimGrid Team. All rights reserved.                   */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Property test");
19
20 int alice(int argc, char *argv[]);
21 int bob(int argc, char *argv[]);
22 int forwarder(int argc, char *argv[]);
23 MSG_error_t test_all(const char *platform_file, const char *application_file);
24
25 int alice(int argc, char *argv[])
26 {
27   m_host_t host1 = MSG_get_host_by_name("host1");
28   xbt_dict_t props = MSG_host_get_properties(host1);
29   xbt_dict_cursor_t cursor=NULL;
30   char *key,*data;
31   const char *noexist="Unknown";
32   const char*value;
33   char exist[]="SG_TEST_Hdd";
34
35   INFO0("== Print the properties of the host");
36   xbt_dict_foreach(props,cursor,key,data)
37     INFO2("  Host property: '%s' -> '%s'",key,data);
38
39   INFO0("== Try to get a host property that does not exist");     
40   value = MSG_host_get_property_value(host1, noexist);
41   xbt_assert0(!value, "The key exists (it's not supposed to)");
42
43   INFO0("== Try to get a host property that does exist");     
44   value = MSG_host_get_property_value(host1,exist);
45   xbt_assert1(value,"\tProperty %s is undefined (where it should)", exist);
46   xbt_assert2(!strcmp(value,"180"),"\tValue of property %s is defined to %s (where it should be 180)", exist, value);
47   INFO2("   Property: %s old value: %s", exist, value);
48
49   INFO0("== Trying to modify a host property");
50   xbt_dict_set(props, exist, xbt_strdup("250"), xbt_free_f);
51   
52   /* Test if we have changed the value */
53   value = MSG_host_get_property_value(host1,exist);
54   xbt_assert1(value,"Property %s is undefined (where it should)", exist);
55   xbt_assert2(!strcmp(value,"250"),"Value of property %s is defined to %s (where it should be 250)", exist, value);
56   INFO2("   Property: %s old value: %s", exist, value);
57
58   return 0;
59 }
60
61 int bob(int argc, char *argv[])
62 {
63   /* Get the property list of current bob process */
64   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
65   xbt_dict_cursor_t cursor=NULL;
66   char *key,*data;
67   const char *noexist="UnknownProcessProp";
68   const char *value;
69
70   INFO0("== Print the properties of the process");
71   xbt_dict_foreach(props,cursor,key,data)
72     INFO2("   Process property: %s -> %s",key,data);
73
74   INFO0("== Try to get a process property that does not exist");
75  
76   value = MSG_process_get_property_value(MSG_process_self(),noexist);
77   xbt_assert0(!value,"The property is defined (it shouldnt)");
78
79   return 0;
80
81
82 /** Test function */
83 MSG_error_t test_all(const char *platform_file,
84                             const char *application_file)
85 {
86   MSG_function_register("alice", alice);
87   MSG_function_register("bob", bob);
88    
89   MSG_create_environment(platform_file);
90   MSG_launch_application(application_file);
91
92   return MSG_main();
93 } /* end_of_test_all */
94
95
96 /** Main function */
97 int main(int argc, char *argv[])
98 {
99   MSG_error_t res = MSG_OK;
100
101   MSG_global_init(&argc,argv);
102   if (argc < 3) {
103      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
104      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
105      exit(1);
106   }
107   res = test_all(argv[1],argv[2]);
108   MSG_clean();
109
110   if(res==MSG_OK)
111     return 0;
112   else
113     return 1;
114 } /* end_of_main */