Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert host-on-off-recv
[simgrid.git] / teshsuite / s4u / host-on-off-recv / host-on-off-recv.cpp
diff --git a/teshsuite/s4u/host-on-off-recv/host-on-off-recv.cpp b/teshsuite/s4u/host-on-off-recv/host-on-off-recv.cpp
new file mode 100644 (file)
index 0000000..3dc299e
--- /dev/null
@@ -0,0 +1,69 @@
+/* Copyright (c) 2010-2020. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* 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. */
+
+#include "simgrid/s4u.hpp"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this example");
+
+static void master()
+{
+  simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("comm");
+  simgrid::s4u::Host* jupiter    = simgrid::s4u::Host::by_name("Jupiter");
+
+  XBT_INFO("Master starting");
+  simgrid::s4u::this_actor::sleep_for(0.5);
+
+  std::string* payload       = new std::string("COMM");
+  simgrid::s4u::CommPtr comm = mailbox->put_async(payload, 1E8);
+
+  simgrid::s4u::this_actor::sleep_for(0.5);
+
+  XBT_INFO("Turning off the worker host");
+  jupiter->turn_off();
+
+  try {
+    comm->wait();
+  } catch (const simgrid::NetworkFailureException&) {
+    delete payload;
+  }
+
+  XBT_INFO("Master has finished");
+}
+
+static void worker()
+{
+  const std::string* payload     = nullptr;
+  simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("comm");
+
+  XBT_INFO("Worker receiving");
+  try {
+    payload = static_cast<std::string*>(mailbox->get());
+  } catch (const simgrid::HostFailureException&) {
+    XBT_DEBUG("The host has been turned off, this was expected");
+    delete payload;
+    return;
+  }
+
+  XBT_ERROR("Worker should be off already.");
+}
+
+int main(int argc, char* argv[])
+{
+
+  simgrid::s4u::Engine e(&argc, argv);
+  xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s small_platform.xml\n", argv[0], argv[0]);
+
+  e.load_platform(argv[1]);
+
+  simgrid::s4u::Actor::create("master", simgrid::s4u::Host::by_name("Tremblay"), master);
+  simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Jupiter"), worker);
+
+  e.run();
+
+  XBT_INFO("Simulation time %g", e.get_clock());
+
+  return 0;
+}