Logo AND Algorithmique Numérique Distribuée

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