Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove a teshsuite example that is superseeded by a proper example
[simgrid.git] / examples / s4u / actor-create / s4u_actor-create.cpp
1 /* Copyright (c) 2006-2016. 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 declare and start your actors.
7  *
8  * The first step is to declare the code of your actors (what they do exactly does not matter to this example) and then
9  * you ask SimGrid to start your actors. There is three ways of doing so:
10  *  - Directly, by instantiating your actor as paramter to Actor::create()
11  *  - By first registering your actors before instantiating it;
12  *  - Through the deployment file.
13  *
14  * This example shows all these solutions, even if you obviously should use only one of these solutions to start your
15  * actors. The most advised solution is to use a deployment file, as it creates a clear separation between your
16  * application and the settings to test it. This is a better scientific methodology. Actually, starting an actor with
17  * Actor::create() is mostly useful to start an actor from another actor.
18  */
19
20 #include <simgrid/s4u.hpp>
21
22 // This declares a logging channel so that XBT_INFO can be used later
23 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_create, "The logging channel used in this example");
24
25 /* Declares a first class of actors which sends a message to the mailbox 'mb42'.
26  * The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default)
27  *
28  * Later, this actor class is instantiated twice in the simulation.
29  */
30 class Sender {
31 public:
32   std::string msg = "GaBuZoMeu";
33   explicit Sender() = default;
34   explicit Sender(std::vector<std::string> args)
35   {
36     /* This constructor is used when we pass parameters to the actor */
37     if (args.size() > 0)
38       msg = args[0];
39   }
40   void operator()()
41   {
42     XBT_INFO("Hello s4u, I have something to send");
43     simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("mb42");
44
45     mailbox->put(xbt_strdup(msg.c_str()), msg.size());
46     XBT_INFO("I'm done. See you.");
47   }
48 };
49
50 /* Declares a second class of actor which receive two messages on the mailbox which
51  * name is passed as parameter ('thingy' by default, ie the wrong one).
52  *
53  * Later, this actor class is instantiated once in the simulation.
54  */
55 class Receiver {
56 public:
57   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("thingy");
58
59   explicit Receiver() = default;
60   explicit Receiver(std::vector<std::string> args)
61   {
62     /* This constructor is used when we pass parameters to the actor */
63     /* as with argc/argv, args[0] is the actor's name, so the first parameter is args[1] */
64
65     /* FIXME: this is a bug as this does not happen when starting the process directly
66      * We should fix it by not adding the process name as argv[0] from the deployment file,
67      * which is useless anyway since it's always the function name in this setting.
68      * But this will break MSG...
69      */
70     if (args.size() > 1)
71       mailbox = simgrid::s4u::Mailbox::byName(args[1]);
72   }
73   void operator()()
74   {
75     XBT_INFO("Hello s4u, I'm ready to get any message you'd want on %s", mailbox->getName());
76
77     char* msg1 = static_cast<char*>(mailbox->get());
78     char* msg2 = static_cast<char*>(mailbox->get());
79     XBT_INFO("I received '%s' and '%s'", msg1, msg2);
80     xbt_free(msg1);
81     xbt_free(msg2);
82     XBT_INFO("I'm done. See you.");
83   }
84 };
85
86 /* Here comes the main function of your program */
87 int main(int argc, char** argv)
88 {
89   /* When your program starts, you have to first start a new simulation engine, as follows */
90   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
91
92   /* Then you should load a platform file, describing your simulated platform */
93   e->loadPlatform("../../platforms/small_platform.xml");
94
95   /* And now you have to ask SimGrid to actually start your actors.
96    *
97    * You can first directly start your actor, as follows. Note the last parameter: 'Sender()',
98    * as if you would call the Sender function.
99    */
100   simgrid::s4u::Actor::createActor("sender1", simgrid::s4u::Host::by_name("Tremblay"), Sender());
101
102   /* The second way is to first register your function, and then retrieve it */
103   e->registerFunction<Sender>("sender"); // The sender is passed as a template parameter here
104   std::vector<std::string> args;         // Here we declare the parameter that the actor will get
105   args.push_back("GloubiBoulga");        // Add a parameter to the set (we could have done it in the first approach too)
106
107   simgrid::s4u::Actor::createActor("sender2", simgrid::s4u::Host::by_name("Jupiter"), "sender", args);
108
109   /* The third way to start your actors is to use a deployment file. */
110   e->registerFunction<Receiver>("receiver");   // You first have to register the actor as with the second approach
111   e->loadDeployment("s4u_actor-create_d.xml"); // And then, you load the deployment file
112
113   /* Once every actors are started in the engine, the simulation can start */
114   e->run();
115
116   /* Once the simulation is done, the program is ended */
117   delete e;
118
119   return 0;
120 }