Logo AND Algorithmique Numérique Distribuée

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