Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Add a header file with forward declarations
[simgrid.git] / include / simgrid / s4u / actor.hpp
1 /* Copyright (c) 2006-2015. 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 #ifndef SIMGRID_S4U_ACTOR_HPP
7 #define SIMGRID_S4U_ACTOR_HPP
8
9 #include <xbt/base.h>
10 #include <simgrid/simix.h>
11 #include <simgrid/s4u/forward.hpp>
12
13 namespace simgrid {
14 namespace s4u {
15
16 /** @brief Simulation Agent
17  *
18  * An actor may be defined as a code executing in a location (host).
19  *
20  * All actors should be started from the XML deployment file (using the @link{s4u::Engine::loadDeployment()}),
21  * even if you can also start new actors directly.
22  * Separating the deployment in the XML from the logic in the code is a good habit as it makes your simulation easier
23  * to adapt to new settings.
24  *
25  * The code that you define for a given actor should be placed in the main method that is virtual.
26  * For example, a Worker actor should be declared as follows:
27  *
28  * \verbatim
29  * #include "s4u/actor.hpp"
30  *
31  * class Worker : simgrid::s4u::Actor {
32  *
33  *        int main(int argc, char **argv) {
34  *              printf("Hello s4u");
35  *        }
36  * };
37  * \endverbatim
38  *
39  */
40 XBT_PUBLIC_CLASS Actor {
41         friend Comm;
42         Actor(smx_process_t smx_proc);
43 public:
44         Actor(const char*name, s4u::Host *host, int argc, char **argv);
45         Actor(const char*name, s4u::Host *host, int argc, char **argv, double killTime);
46         virtual ~Actor() {}
47
48         /** The main method of your agent */
49         virtual int main(int argc, char **argv);
50
51         /** The Actor that is currently running */
52         static Actor *current();
53         /** Retrieves the actor that have the given PID (or NULL if not existing) */
54         static Actor *byPid(int pid);
55
56         /** Retrieves the name of that actor */
57         const char*getName();
58         /** Retrieves the host on which that actor is running */
59         s4u::Host *getHost();
60         /** Retrieves the PID of that actor */
61         int getPid();
62
63         /** If set to true, the actor will automatically restart when its host reboots */
64         void setAutoRestart(bool autorestart);
65         /** Sets the time at which that actor should be killed */
66         void setKillTime(double time);
67         /** Retrieves the time at which that actor will be killed (or -1 if not set) */
68         double getKillTime();
69
70         /** Ask kindly to all actors to die. Only the issuer will survive. */
71         static void killAll();
72         /** Ask the actor to die.
73          *
74          * It will only notice your request when doing a simcall next time (a communication or similar).
75          * SimGrid sometimes have issues when you kill actors that are currently communicating and such. We are working on it to fix the issues.
76          */
77         void kill();
78
79         /** Block the actor sleeping for that amount of seconds (may throws hostFailure) */
80         void sleep(double duration);
81
82         /** Block the actor, computing the given amount of flops */
83         e_smx_state_t execute(double flop);
84
85         /** Block the actor until it gets a message from the given mailbox.
86          *
87          * See \ref Comm for the full communication API (including non blocking communications).
88          */
89         void *recv(Mailbox &chan);
90
91         /** Block the actor until it delivers a message of the given simulated size to the given mailbox
92          *
93          * See \ref Comm for the full communication API (including non blocking communications).
94         */
95         void send(Mailbox &chan, void*payload, size_t simulatedSize);
96
97 protected:
98         smx_process_t getInferior() {return p_smx_process;}
99 private:
100         smx_process_t p_smx_process;
101 };
102 }} // namespace simgrid::s4u
103
104 #endif /* SIMGRID_S4U_ACTOR_HPP */
105
106 #if 0
107
108 public abstract class Actor implements Runnable {
109         /** Suspends the process. See {@link #resume()} to resume it afterward */
110         public native void suspend();
111         /** Resume a process that was suspended by {@link #suspend()}. */
112         public native void resume();    
113         /** Tests if a process is suspended. */
114         public native boolean isSuspended();
115         
116         /**
117          * Returns the value of a given process property. 
118          */
119         public native String getProperty(String name);
120
121
122         /**
123          * Migrates a process to another host.
124          *
125          * @param host                  The host where to migrate the process.
126          *
127          */
128         public native void migrate(Host host);  
129
130         public native void exit();    
131         /**
132          * This static method returns the current amount of processes running
133          *
134          * @return                      The count of the running processes
135          */ 
136         public native static int getCount();
137
138 }
139 #endif