Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert maestro-set to S4U
[simgrid.git] / examples / s4u / maestro-set / s4u-maestro-set.cpp
@@ -3,7 +3,7 @@
 /* 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. */
 
-/** @addtogroup MSG_examples
+/** @addtogroup S4U_examples
  *
  *  - <b>maestro-set/maestro-set.cpp: Switch the system thread hosting our maestro</b>.
  *    That's a very advanced example in which we move the maestro thread to another process.
  *    expected. You can still use that feature from a C code.
  */
 
-#include "simgrid/msg.h"
+#include "simgrid/Exception.hpp"
+#include "simgrid/actor.h"
+#include "simgrid/s4u.hpp"
 
+#include <string>
 #include <thread>
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
 
 std::thread::id root_id;
 
-static void ensure_root_tid() {
+static void ensure_root_tid()
+{
   std::thread::id this_id = std::this_thread::get_id();
   xbt_assert(root_id == this_id, "I was supposed to be the main thread");
   XBT_INFO("I am the main thread, as expected");
 }
-static void ensure_other_tid() {
+static void ensure_other_tid()
+{
   std::thread::id this_id = std::this_thread::get_id();
   xbt_assert(this_id != root_id, "I was NOT supposed to be the main thread");
   XBT_INFO("I am not the main thread, as expected");
 }
 
-
-
-static int sender(int argc, char *argv[])
+static void sender()
 {
   ensure_root_tid();
-
-  msg_task_t task_la = MSG_task_create("Some task", 0.0, 10e8, NULL);
-  MSG_task_send(task_la, "some mailbox");
-
-  return 0;
+  std::string* payload = new std::string("some message");
+  simgrid::s4u::Mailbox::by_name("some mailbox")->put((void*)payload, 10e8);
 }
 
-static int receiver(int argc, char *argv[])
+static void receiver()
 {
   ensure_other_tid();
 
-  msg_task_t task_la = NULL;
-  xbt_assert(MSG_task_receive(&task_la,"some mailbox") == MSG_OK);
+  std::string* payload = static_cast<std::string*>(simgrid::s4u::Mailbox::by_name("some mailbox")->get());
   XBT_INFO("Task received");
-  MSG_task_destroy(task_la);
-
-  return 0;
+  delete payload;
 }
 
 static void maestro(void* data)
 {
   ensure_other_tid();
-  MSG_process_create("receiver",&receiver,NULL,MSG_host_by_name("Jupiter"));
-  MSG_main();
+  simgrid::s4u::Actor::create("receiver", simgrid::s4u::Host::by_name("Jupiter"), receiver);
+  simgrid::s4u::Engine::get_instance()->run();
 }
 
 /** Main function */
-int main(int argc, char *argv[])
+int main(int argc, charargv[])
 {
   root_id = std::this_thread::get_id();
 
   SIMIX_set_maestro(maestro, NULL);
-  MSG_init(&argc, argv);
+  simgrid::s4u::Engine e(&argc, argv);
 
   if (argc != 2) {
     XBT_CRITICAL("Usage: %s platform_file\n", argv[0]);
-    xbt_die("example: %s msg_platform.xml\n",argv[0]);
+    xbt_die("example: %s msg_platform.xml\n", argv[0]);
   }
 
-  MSG_create_environment(argv[1]);
+  e.load_platform(argv[1]);
 
   /* Become one of the simulated process.
    *
    * This must be done after the creation of the platform because we are depending attaching to a host.*/
-  MSG_process_attach("sender", NULL, MSG_host_by_name("Tremblay"), NULL);
+  sg_actor_attach("sender", nullptr, simgrid::s4u::Host::by_name("Tremblay"), nullptr);
   ensure_root_tid();
 
   // Execute the sender code:
-  const char* subargv[3] = { "sender", "Jupiter", NULL };
-  sender(2, (char**) subargv);
+  sender();
 
-  MSG_process_detach(); // Become root thread again
+  sg_actor_detach(); // Become root thread again
   XBT_INFO("Detached");
   ensure_root_tid();