Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
java: code simplification: no need for an extra boolean here
[simgrid.git] / src / bindings / java / org / simgrid / msg / Process.java
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package org.simgrid.msg;
8
9 import java.util.Arrays;
10 import java.util.ArrayList;
11
12 /**
13  * A process may be defined as a code, with some private data, executing 
14  * in a location (host). All the process used by your simulation must be
15  * declared in the deployment file (XML format).
16  * To create your own process you must inherit your own process from this 
17  * class and override the method "main()". For example if you want to use 
18  * a process named Slave proceed as it :
19  *
20  * (1) import the class Process of the package simgrid.msg
21  * import simgrid.msg.Process;
22  * 
23  * public class Slave extends simgrid.msg.Process {
24  *
25  *  (2) Override the method function
26  * 
27  *  \verbatim
28  *      public void main(String[] args) {
29  *              System.out.println("Hello MSG");
30  *      }
31  *  \endverbatim
32  * }
33  * The name of your process must be declared in the deployment file of your simulation.
34  * For the example, for the previous process Slave this file must contains a line :
35  * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
36  * Slave. All the process of your simulation are automatically launched and managed by Msg.
37  * A process use tasks to simulate communications or computations with another process. 
38  * For more information see Task. For more information on host concept 
39  * see Host.
40  * 
41  */
42
43 public abstract class Process implements Runnable {
44         /**
45          * This attribute represents a bind between a java process object and
46          * a native process. Even if this attribute is public you must never
47          * access to it. It is set automatically during the build of the object.
48          */
49         private long bind = 0;
50         /** Indicates if the process is started */
51         /**
52          * Even if this attribute is public you must never access to it.
53          * It is used to compute the id of an MSG process.
54          */
55         private static long nextProcessId = 0;
56
57         /**
58          * Even if this attribute is public you must never access to it.
59          * It is compute automatically during the creation of the object. 
60          * The native functions use this identifier to synchronize the process.
61          */
62         private long id;
63
64         /** Time at which the process should be created  */
65         protected double startTime = 0;
66         /** Time at which the process should be killed.
67          * 
68          * Set at creation, and used internally by SimGrid
69          */
70         private double killTime = -1;
71
72         private String name = null;
73         
74         private int pid = -1;
75         private int ppid = -1;
76         private Host host = null;
77
78         /** The arguments of the method function of the process. */
79         private ArrayList<String> args = new ArrayList<>();
80
81         /**
82          * Constructs a new process from the name of a host and his name. The method
83          * function of the process doesn't have argument.
84          *
85          * @param hostname              Where to create the process.
86          * @param name                  The name of the process.
87          *
88          * @exception                   HostNotFoundException  if no host with this name exists.
89          *                      
90          *
91          */
92         public Process(String hostname, String name) throws HostNotFoundException {
93                 this(Host.getByName(hostname), name, null);
94         }
95         /**
96          * Constructs a new process from the name of a host and his name. The arguments
97          * of the method function of the process are specified by the parameter args.
98          *
99          * @param hostname              Where to create the process.
100          * @param name                  The name of the process.
101          * @param args                  The arguments of the main function of the process.
102          *
103          * @exception                   HostNotFoundException  if no host with this name exists.
104          *
105          */ 
106         public Process(String hostname, String name, String[] args) throws HostNotFoundException {
107                 this(Host.getByName(hostname), name, args);
108         }
109         /**
110          * Constructs a new process from a host and his name. The method function of the 
111          * process doesn't have argument.
112          *
113          * @param host                  Where to create the process.
114          * @param name                  The name of the process.
115          *
116          */
117         public Process(Host host, String name) {
118                 this(host, name, null);
119         }
120         /**
121          * Constructs a new process from a host and his name, the arguments of here method function are
122          * specified by the parameter args.
123          *
124          * @param host                  Where to create the process.
125          * @param name                  The name of the process.
126          * @param args                  The arguments of main method of the process.
127          */     
128         public Process(Host host, String name, String[]args) 
129         {
130                 if (host == null)
131                         throw new NullPointerException("Cannot create a process on the null host");
132                 if (name == null)
133                         throw new NullPointerException("Process name cannot be null");
134                 
135                 this.id = nextProcessId++;
136                 this.host = host;
137                 this.name = name;
138
139                 this.args = new ArrayList<>();
140                 if (null != args)
141                         this.args.addAll(Arrays.asList(args));
142         }
143         /**
144          * Constructs a new process from a host and his name, the arguments of here method function are
145          * specified by the parameter args.
146          *
147          * @param host                  Where to create the process.
148          * @param name                  The name of the process.
149          * @param args                  The arguments of main method of the process.
150          * @param startTime             Start time of the process
151          * @param killTime              Kill time of the process
152          *
153          */
154         public Process(Host host, String name, String[]args, double startTime, double killTime) {
155                 this(host, name, args);
156                 this.startTime = startTime;
157                 this.killTime = killTime;
158         }
159         /**
160          * The natively implemented method to create an MSG process.
161          * @param hostName    A valid (bound) host where create the process.
162          */
163         protected native void create(String hostName) throws HostNotFoundException;
164         /**
165          * This method kills all running process of the simulation.
166          *
167          * @param resetPID              Should we reset the PID numbers. A negative number means no reset
168          *                                              and a positive number will be used to set the PID of the next newly
169          *                                              created process.
170          *
171          * @return                              The function returns the PID of the next created process.
172          *                      
173          */ 
174         public static native int killAll(int resetPID);
175
176         /** Simply kills the receiving process.
177          *
178          * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
179          */
180         public native void kill();
181         public static void kill(Process p) {
182                 p.kill();
183         }
184         
185         /** Suspends the process. See {@link #resume()} to resume it afterward */
186         public native void suspend();
187         /** Resume a process that was suspended by {@link #suspend()}. */
188         public native void resume();    
189         /** Tests if a process is suspended.
190          *
191          * @see #suspend()
192          * @see #resume()
193          */
194         public native boolean isSuspended();
195         
196         /** Yield the current process. All other processes that are ready at the same timestamp will be executed first */
197         public static native void yield();
198         
199         /**
200          * Specify whether the process should restart when its host restarts after a failure
201          * 
202          * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
203          * Otherwise, it just disappears when the host stops.
204          */
205         public native void setAutoRestart(boolean autoRestart);
206         /** Restarts the process from the beginning */
207         public native void restart();
208         /**
209          * Returns the name of the process
210          */
211         public String getName() {
212                 return this.name;
213         }
214         /**
215          * Returns the host of the process.
216          * @return                              The host instance of the process.
217          */ 
218         public Host getHost() {
219                 return this.host;
220         }
221         /**
222          * This static method gets a process from a PID.
223          *
224          * @param pid                   The process identifier of the process to get.
225          *
226          * @return                              The process with the specified PID.
227          */ 
228         public static native Process fromPID(int pid);
229         /**
230          * This method returns the PID of the process.
231          *
232          * @return                              The PID of the process.
233          *
234          */ 
235         public int getPID()  {
236                 return pid;
237         }
238         /**
239          * This method returns the PID of the parent of a process.
240          *
241          * @return                              The PID of the parent of the process.
242          *
243          */ 
244         public int getPPID()  {
245                 return ppid;
246         }
247         /**
248          * Returns the value of a given process property. 
249          */
250         public native String getProperty(String name);
251
252         /**
253          * Set the kill time of the process
254          * @param killTime the time when the process is killed
255          */
256         public native void setKillTime(double killTime);
257
258         /**
259          * This static method returns the currently running process.
260          *
261          * @return                              The current process.
262          *
263          */ 
264         public static native Process getCurrentProcess();
265         /**
266          * Migrates a process to another host.
267          *
268          * @param host                  The host where to migrate the process.
269          *
270          */
271         public native void migrate(Host host);  
272         /**
273          * Makes the current process sleep until millis milliseconds have elapsed.
274          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
275          * FIXME: Not optimal, maybe we should have two native functions.
276          * @param millis the length of time to sleep in milliseconds.
277          */
278         public static void sleep(long millis) throws HostFailureException  {
279                 sleep(millis,0);
280         }
281         /**
282          * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
283          * have elapsed.
284          * Unlike {@link #waitFor(double)} which takes seconds, this method takes 
285          * milliseconds and nanoseconds.
286          * Overloads Thread.sleep.
287          * @param millis the length of time to sleep in milliseconds.
288          * @param nanos additional nanoseconds to sleep.
289          */
290         public static native void sleep(long millis, int nanos) throws HostFailureException;
291         /**
292          * Makes the current process sleep until time seconds have elapsed.
293          * @param seconds               The time the current process must sleep.
294          */ 
295         public native void waitFor(double seconds) throws HostFailureException;    
296         /**
297          * This method actually creates and run the process.
298          * It is a noop if the process is already launched.
299          * @throws HostNotFoundException
300          */
301         public final void start() throws HostNotFoundException {
302                 if (bind != 0)
303                         create(host.getName());
304         }
305
306         /** This method runs the process. It calls the method function that you must overwrite. */
307         @Override
308         public void run() {
309
310                 String[] args = null;      /* do not fill it before the signal or this.args will be empty */
311
312                 try {
313                         args = new String[this.args.size()];
314                         if (!this.args.isEmpty()) {
315                                 this.args.toArray(args);
316                         }
317
318                         this.main(args);
319                 }
320                 catch(MsgException e) {
321                         e.printStackTrace();
322                         Msg.info("Unexpected behavior. Stopping now");
323                         System.exit(1);
324                 }
325                 catch(ProcessKilledError pk) {
326                         /* The process was killed before its end. With a kill() or something. */
327                 }       
328         }
329
330         /**
331          * The main function of the process (to implement).
332          *
333          * @param args
334          * @throws MsgException
335          */
336         public abstract void main(String[]args) throws MsgException;
337
338         /** Stops the execution of the current actor */
339         public void exit() {
340                 this.kill();
341         }
342         /**
343          * Class initializer, to initialize various JNI stuff
344          */
345         private static native void nativeInit();
346         static {
347                 org.simgrid.NativeLib.nativeInit();
348                 nativeInit();
349         }
350         /**
351          * This static method returns the current amount of processes running
352          *
353          * @return                      The count of the running processes
354          */ 
355         public static native int getCount();
356
357 }