Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[java] unpack dll/so to a tempDir instead of messing with their name
[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.Vector;
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;
50         /**
51          * Indicates if the process is started
52          */
53         boolean started;
54         /**
55          * Even if this attribute is public you must never access to it.
56          * It is used to compute the id of an MSG process.
57          */
58         public static long nextProcessId = 0;
59
60         /**
61          * Even if this attribute is public you must never access to it.
62          * It is compute automatically during the creation of the object. 
63          * The native functions use this identifier to synchronize the process.
64          */
65         private long id;
66
67         /** Time at which the process should be created  */
68         protected double startTime = 0;
69         /** Time at which th
70          * Kill time of the process
71          * 
72          * Set at creation, and used internally by SimGrid
73          */
74         private double killTime = -1;
75
76         private String name;
77         
78         private int pid = -1;
79         private int ppid = -1;
80         private Host host = null;
81
82         /** The arguments of the method function of the process. */     
83         public Vector<String> args;
84
85
86         /**
87          * Default constructor
88          */
89         protected Process() {
90                 this.id = nextProcessId++;
91                 this.name = null;
92                 this.bind = 0;
93                 this.args = new Vector<String>();
94         }
95
96
97         /**
98          * Constructs a new process from the name of a host and his name. The method
99          * function of the process doesn't have argument.
100          *
101          * @param hostname              The name of the host of the process to create.
102          * @param name                  The name of the process.
103          *
104          * @exception                   HostNotFoundException  if no host with this name exists.
105          *                      
106          *
107          */
108         public Process(String hostname, String name) throws HostNotFoundException {
109                 this(Host.getByName(hostname), name, null);
110         }
111         /**
112          * Constructs a new process from the name of a host and his name. The arguments
113          * of the method function of the process are specified by the parameter args.
114          *
115          * @param hostname              The name of the host of the process to create.
116          * @param name                  The name of the process.
117          * @param args                  The arguments of the main function of the process.
118          *
119          * @exception                   HostNotFoundException  if no host with this name exists.
120          *                      NativeException
121          * @throws NativeException
122          *
123          */ 
124         public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
125                 this(Host.getByName(hostname), name, args);
126         }
127         /**
128          * Constructs a new process from a host and his name. The method function of the 
129          * process doesn't have argument.
130          *
131          * @param host                  The host of the process to create.
132          * @param name                  The name of the process.
133          *
134          */
135         public Process(Host host, String name) {
136                 this(host, name, null);
137         }
138         /**
139          * Constructs a new process from a host and his name, the arguments of here method function are
140          * specified by the parameter args.
141          *
142          * @param host                  The host of the process to create.
143          * @param name                  The name of the process.
144          * @param args                  The arguments of main method of the process.
145          */     
146         public Process(Host host, String name, String[]args) {
147                 this();
148                 this.host = host;
149                 if (name == null)
150                         throw new NullPointerException("Process name cannot be NULL");
151                 this.name = name;
152
153                 this.args = new Vector<String>();
154                 if (null != args)
155                         this.args.addAll(Arrays.asList(args));
156         }       
157         /**
158          * Constructs a new process from a host and his name, the arguments of here method function are
159          * specified by the parameter args.
160          *
161          * @param host                  The host of the process to create.
162          * @param name                  The name of the process.
163          * @param args                  The arguments of main method of the process.
164          * @param startTime             Start time of the process
165          * @param killTime              Kill time of the process
166          *
167          */
168         public Process(Host host, String name, String[]args, double startTime, double killTime) {
169                 this();
170                 this.host = host;
171                 if (name == null)
172                         throw new NullPointerException("Process name cannot be NULL");
173                 this.name = name;
174
175                 this.args = new Vector<String>();
176                 if (null != args)
177                         this.args.addAll(Arrays.asList(args));
178
179                 this.startTime = startTime;
180                 this.killTime = killTime;               
181         }
182         /**
183          * The natively implemented method to create an MSG process.
184          * @param hostName    A valid (bound) host where create the process.
185          */
186         protected native void create(String hostName) throws HostNotFoundException;
187         /**
188          * This method kills all running process of the simulation.
189          *
190          * @param resetPID              Should we reset the PID numbers. A negative number means no reset
191          *                                              and a positive number will be used to set the PID of the next newly
192          *                                              created process.
193          *
194          * @return                              The function returns the PID of the next created process.
195          *                      
196          */ 
197         public static native int killAll(int resetPID);
198
199         /** Simply kills the receiving process.
200          *
201          * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
202          */
203         public native void kill();
204         
205         /** Suspends the process. See {@link #resume()} to resume it afterward */
206         public native void suspend();
207         /** Resume a process that was suspended by {@link #suspend()}. */
208         public native void resume();    
209         /** Tests if a process is suspended.
210          *
211          * @see #suspend()
212          * @see #resume()
213          */
214         public native boolean isSuspended();
215         
216         /**
217          * Specify whether the process should restart when its host restarts after a failure
218          * 
219          * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
220          * Otherwise, it just disappears when the host stops.
221          */
222         public native void setAutoRestart(boolean autoRestart);
223         /** Restarts the process from the beginning */
224         public native void restart();
225         /**
226          * Returns the name of the process
227          */
228         public String getName() {
229                 return this.name;
230         }
231         /**
232          * Returns the host of the process.
233          * @return                              The host instance of the process.
234          */ 
235         public Host getHost() {
236                 return this.host;
237         }
238         /**
239          * This static method gets a process from a PID.
240          *
241          * @param PID                   The process identifier of the process to get.
242          *
243          * @return                              The process with the specified PID.
244          *
245          * @exception                   NativeException on error in the native SimGrid code
246          */ 
247         public static native Process fromPID(int PID) throws NativeException;
248         /**
249          * This method returns the PID of the process.
250          *
251          * @return                              The PID of the process.
252          *
253          */ 
254         public int getPID()  {
255                 return pid;
256         }
257         /**
258          * This method returns the PID of the parent of a process.
259          *
260          * @return                              The PID of the parent of the process.
261          *
262          */ 
263         public int getPPID()  {
264                 return ppid;
265         }
266         /**
267          * Returns the value of a given process property. 
268          */
269         public native String getProperty(String name);
270
271         /**
272          * Set the kill time of the process
273          * @param killTime the time when the process is killed
274          */
275         public native void setKillTime(double killTime);
276
277         /**
278          * This static method returns the currently running process.
279          *
280          * @return                              The current process.
281          *
282          */ 
283         public static native Process getCurrentProcess();
284         /**
285          * Migrates a process to another host.
286          *
287          * @param host                  The host where to migrate the process.
288          *
289          */
290         public native void migrate(Host host);  
291         /**
292          * Makes the current process sleep until millis millisecondes have elapsed.
293          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
294          * FIXME: Not optimal, maybe we should have two native functions.
295          * @param millis the length of time to sleep in milliseconds.
296          */
297         public static void sleep(long millis) throws HostFailureException  {
298                 sleep(millis,0);
299         }
300         /**
301          * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
302          * have elapsed.
303          * Unlike {@link #waitFor(double)} which takes seconds, this method takes 
304          * milliseconds and nanoseconds.
305          * Overloads Thread.sleep.
306          * @param millis the length of time to sleep in milliseconds.
307          * @param nanos additionnal nanoseconds to sleep.
308          */
309         public native static void sleep(long millis, int nanos) throws HostFailureException;
310         /**
311          * Makes the current process sleep until time seconds have elapsed.
312          * @param seconds               The time the current process must sleep.
313          */ 
314         public native void waitFor(double seconds) throws HostFailureException;    
315         /**
316          * This method actually creates and run the process.
317          * It is a noop if the process is already launched.
318          * @throws HostNotFoundException
319          */
320         public final void start() throws HostNotFoundException {
321                 if (!started) {
322                         started = true;
323                         create(host.getName());
324                 }
325         }
326
327         /**
328          * This method runs the process. Il calls the method function that you must overwrite.
329          */
330         public void run() {
331
332                 String[] args = null;      /* do not fill it before the signal or this.args will be empty */
333                 //waitSignal(); /* wait for other people to fill the process in */
334
335                 try {
336                         args = new String[this.args.size()];
337                         if (this.args.size() > 0) {
338                                 this.args.toArray(args);
339                         }
340
341                         this.main(args);
342                 } catch(MsgException e) {
343                         e.printStackTrace();
344                         Msg.info("Unexpected behavior. Stopping now");
345                         System.exit(1);
346                 }
347                 catch(ProcessKilledError pk) {
348                 }       
349                 exit();
350         }
351
352         /**
353          * The main function of the process (to implement).
354          *
355          * @param args
356          * @throws MsgException
357          */
358         public abstract void main(String[]args) throws MsgException;
359
360         public native void exit();    
361         /**
362          * Class initializer, to initialize various JNI stuff
363          */
364         private static native void nativeInit();
365         static {
366                 org.simgrid.NativeLib.nativeInit();
367                 nativeInit();
368         }
369         /**
370          * This static method returns the current amount of processes running
371          *
372          * @return                      The count of the running processes
373          */ 
374         public native static int getCount();
375
376 }