Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge back master branch
[simgrid.git] / examples / gras / properties / properties.c
index 2315240..358df12 100644 (file)
@@ -1,76 +1,90 @@
-/*     $Id$     */
+/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
 
-#include "gras.h"
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Simple Property example");
+#include "gras.h"
 
-int master(int argc, char *argv[]);
-int slave(int argc, char *argv[]);
+XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Simple Property example");
 
+int alice(int argc, char *argv[]);
+int bob(int argc, char *argv[]);
 
-int slave(int argc, char *argv[]) {
-  gras_init(&argc,argv);
+int alice(int argc, char *argv[])
+{
+  gras_init(&argc, argv);
 
   /* Get the properties */
-  xbt_dict_t props = gras_process_properties();
+  xbt_dict_t process_props = gras_process_properties();
+  xbt_dict_t host_props = gras_os_host_properties();
+
   xbt_dict_cursor_t cursor = NULL;
-  char *key,*data;
-
-  /* Print the properties of the workstation 1 */
-  xbt_dict_foreach(props,cursor,key,data) {
-    INFO2("Process property: %s has value: %s",key,data);
-  }
-  /* Try to get a property that does not exist */
-  const char *noexist="Nonexisting";
-  const char *value = gras_process_property_value(noexist);
-  if ( value == NULL) 
-    INFO1("Process property: %s is undefined", noexist);
-  else
-    INFO2("Process property: %s has value: %s", noexist, value);
-   /* Modify an existing property. First check it exists */
-    INFO0("Trying to modify a process property");
-    const char *exist="otherprop";
-    value = gras_process_property_value(exist);
-    if (value == NULL) 
-      INFO1("\tProperty: %s is undefined", exist);
-    else {
-      INFO2("\tProperty: %s old value: %s", exist, value);
-      xbt_dict_set(props, exist, xbt_strdup("newValue"), free);  
-    }
-    /* Test if we have changed the value */
-    value = gras_process_property_value(exist);
-    if (value == NULL) 
-      INFO1("\tProperty: %s is undefined", exist);
-    else
-      INFO2("\tProperty: %s new value: %s", exist, value);
+  char *key, *data;
+  const char *value;
 
+  /* Let the other process change the host props */
   gras_os_sleep(1);
+
+  XBT_INFO("== Dump all the properties of current host");
+  xbt_dict_foreach(host_props, cursor, key, data)
+      XBT_INFO("  Host property: '%s' has value: '%s'", key, data);
+
+  XBT_INFO("== Dump all the properties of alice");
+  xbt_dict_foreach(process_props, cursor, key, data)
+      if (!strncmp(key, "SG_TEST_", 8))
+    XBT_INFO("  Process property: '%s' has value: '%s'", key, data);
+
+  XBT_INFO("== Try to get a process property that does not exist");
+  value = gras_process_property_value("Nonexisting");
+  xbt_assert(!value, "nonexisting property exists!!");
+
+  /* Modify an existing property. First check it exists */
+  XBT_INFO("== Trying to modify a process property");
+  value = gras_process_property_value("new prop");
+  xbt_assert(!value, "Property 'new prop' exists before I add it!");
+  xbt_dict_set(process_props, "new prop", xbt_strdup("new value"),
+               xbt_free_f);
+
+  /* Test if we have changed the value */
+  value = gras_process_property_value("new prop");
+  xbt_assert(!strcmp(value, "new value"),
+              "New property does have the value I've set ('%s' != 'new value')",
+              value);
+
   gras_exit();
   return 0;
 }
 
-int master(int argc, char *argv[]) {
-  gras_init(&argc,argv);
+int bob(int argc, char *argv[])
+{
+  gras_init(&argc, argv);
 
   /* Get the properties */
-  xbt_dict_t props = gras_os_host_properties();
+  xbt_dict_t host_props = gras_os_host_properties();
   xbt_dict_cursor_t cursor = NULL;
-  char *key,*data;
-
-  /* Print the properties of the workstation 1 */
-  xbt_dict_foreach(props,cursor,key,data) {
-    INFO2("Host property: %s has value: %s",key,data);
-  }
-  /* Try to get a property that does not exist */
-  const char *noexist="Nonexisting";
-  const char *value = gras_os_host_property_value(noexist);
-  xbt_assert2(value == NULL, "Host property: %s has value: %s", noexist, value);
-  
-  gras_os_sleep(1);
+  char *key, *data;
+  const char *value;
+
+  XBT_INFO("== Dump all the properties of host1");
+  xbt_dict_foreach(host_props, cursor, key, data)
+      XBT_INFO("  Host property: '%s' has value: '%s'", key, data);
+
+  XBT_INFO("== Try to get a property that does not exist");
+  value = gras_os_host_property_value("non existing key");
+  xbt_assert(value == NULL,
+              "The key 'non existing key' exists (with value '%s')!!",
+              value);
+
+  XBT_INFO
+      ("== Set a host property that alice will try to retrieve in SG (from bob->hello)");
+  xbt_dict_set(host_props, "from bob", xbt_strdup("hello"), xbt_free_f);
+
+  XBT_INFO("== Dump all the properties of host1 again to check the addition");
+  xbt_dict_foreach(host_props, cursor, key, data)
+      XBT_INFO("  Host property: '%s' has value: '%s'", key, data);
+
+  gras_os_sleep(1);             /* KILLME once bug on empty main is solved */
   gras_exit();
   return 0;
 }