Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / s4u / comm-suspend / s4u-comm-suspend.cpp
1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* This example shows how to suspend and resume an asynchronous communication. */
7
8 #include "simgrid/s4u.hpp"
9 #include <cstdlib>
10 #include <iostream>
11 #include <string>
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_wait, "Messages specific for this s4u example");
14
15 static void sender(int argc, char**)
16 {
17   xbt_assert(argc == 1, "Expecting no parameter from the XML deployment file but got %d", argc - 1);
18
19   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name("receiver");
20
21   // Copy the data we send: the 'msg_content' variable is not a stable storage location.
22   // It will be destroyed when this actor leaves the loop, ie before the receiver gets the data
23   auto* payload = new std::string("Sent message");
24
25   /* Create a communication representing the ongoing communication and then */
26   simgrid::s4u::CommPtr comm = mbox->put_init(payload, 13194230);
27   XBT_INFO("Suspend the communication before it starts (remaining: %.0f bytes) and wait a second.",
28            comm->get_remaining());
29   simgrid::s4u::this_actor::sleep_for(1);
30   XBT_INFO("Now, start the communication (remaining: %.0f bytes) and wait another second.", comm->get_remaining());
31   comm->start();
32   simgrid::s4u::this_actor::sleep_for(1);
33
34   XBT_INFO("There is still %.0f bytes to transfer in this communication. Suspend it for one second.",
35            comm->get_remaining());
36   comm->suspend();
37   XBT_INFO("Now there is %.0f bytes to transfer. Resume it and wait for its completion.", comm->get_remaining());
38   comm->resume();
39   comm->wait();
40   XBT_INFO("There is %f bytes to transfer after the communication completion.", comm->get_remaining());
41   XBT_INFO("Suspending a completed activity is a no-op.");
42   comm->suspend();
43 }
44
45 static void receiver(int, char**)
46 {
47   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name("receiver");
48   XBT_INFO("Wait for the message.");
49   auto received = mbox->get_unique<std::string>();
50
51   XBT_INFO("I got '%s'.", received->c_str());
52 }
53
54 int main(int argc, char* argv[])
55 {
56   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
57
58   simgrid::s4u::Engine e(&argc, argv);
59   e.register_function("sender", &sender);
60   e.register_function("receiver", &receiver);
61
62   e.load_platform(argv[1]);
63   e.load_deployment(argv[2]);
64   e.run();
65
66   return 0;
67 }