Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove all indefinite variables
[simgrid.git] / src / java / simgrid / msg / Process.java
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
5  * All right reserved. 
6  *
7  * This program is free software; you can redistribute 
8  * it and/or modify it under the terms of the license 
9  *(GNU LGPL) which comes with this package. 
10  */
11
12 package simgrid.msg;
13
14 import java.lang.Thread;
15 import java.util.*;
16
17 /**
18  * A process may be defined as a code, with some private data, executing 
19  * in a location (host). All the process used by your simulation must be
20  * declared in the deployment file (XML format).
21  * To create your ouwn process you must inherite your own process from this 
22  * class and override the method "main()". For example if you want to use 
23  * a process named Slave proceed as it :
24  *
25  * (1) import the class Process of the package simgrid.msg
26  * import simgrid.msg.Process;
27  * 
28  * public class Slave extends simgrid.msg.Process {
29  *
30  *  (2) Override the method function
31  * 
32  *  \verbatim
33  *      public void main(String[] args) {
34  *              System.out.println("Hello MSG");
35  *      }
36  *  \endverbatim
37  * }
38  * The name of your process must be declared in the deployment file of your simulation.
39  * For the exemple, for the previouse process Slave this file must contains a line :
40  * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
41  * Slave. All the process of your simulation are automaticaly launched and managed by Msg.
42  * A process use tasks to simulate communications or computations with another process. 
43  * For more information see Task. For more information on host concept 
44  * see Host.
45  * 
46  * @author  Abdelmalek Cherier
47  * @author  Martin Quinson
48  * @since SimGrid 3.3
49  */
50
51 public abstract class Process extends Thread {
52     /**
53      * This attribute represents a bind between a java process object and
54      * a native process. Even if this attribute is public you must never
55      * access to it. It is set automaticatly during the build of the object.
56      */
57   public long bind;
58
59     /**
60      * Even if this attribute is public you must never access to it.
61      * It is used to compute the id of an MSG process.
62      */
63   public static long nextProcessId = 0;
64
65     /**
66      * Even if this attribute is public you must never access to it.
67      * It is compute automaticaly during the creation of the object. 
68      * The native functions use this identifier to synchronize the process.
69      */
70   public long id;
71   
72   public Hashtable properties;
73
74     /**
75      * The name of the process.                                                 
76      */
77   protected String name;
78   public String msgName() {
79     return this.name;
80   }
81   /*
82    * The arguments of the method function of the process.     
83    */ public Vector args;
84
85   /* process synchronisation tools */
86   protected Sem schedBegin, schedEnd;
87
88     /**
89      * Default constructor. (used in ApplicationHandler to initialize it)
90      */
91   protected Process() {
92     super();
93     this.id = 0;
94     this.name = null;
95     this.bind = 0;
96     this.args = new Vector();
97     this.properties = null;
98     schedBegin = new Sem(0);
99     schedEnd = new Sem(0);
100   }
101
102
103     /**
104      * Constructs a new process from the name of a host and his name. The method
105      * function of the process doesn't have argument.
106      *
107      * @param hostname          The name of the host of the process to create.
108      * @param name                      The name of the process.
109      *
110      * @exception                       HostNotFoundException  if no host with this name exists.
111      *                              NullPointerException if the provided name is null
112      *                              JniException on JNI madness
113      *
114      */
115   public Process(String hostname, String name)
116   throws NullPointerException, HostNotFoundException, JniException,
117     NativeException {
118     this(Host.getByName(hostname), name, null);
119   }
120     /**
121      * Constructs a new process from the name of a host and his name. The arguments
122      * of the method function of the process are specified by the parameter args.
123      *
124      * @param hostname          The name of the host of the process to create.
125      * @param name                      The name of the process.
126      * @param args                      The arguments of the main function of the process.
127      *
128      * @exception                       HostNotFoundException  if no host with this name exists.
129      *                              NullPointerException if the provided name is null
130      *                              JniException on JNI madness
131      *
132      */ 
133    public Process(String hostname, String name, String args[])
134      throws NullPointerException, HostNotFoundException, JniException,
135     NativeException {
136     this(Host.getByName(hostname), name, args);
137   }
138     /**
139      * Constructs a new process from a host and his name. The method function of the 
140      * process doesn't have argument.
141      *
142      * @param host                      The host of the process to create.
143      * @param name                      The name of the process.
144      *
145      * @exception                       NullPointerException if the provided name is null
146      *                              JniException on JNI madness
147      *
148      */
149     public Process(Host host, String name) throws NullPointerException,
150     JniException {
151     this(host, name, null);
152   }
153     /**
154      * Constructs a new process from a host and his name, the arguments of here method function are
155      * specified by the parameter args.
156      *
157      * @param host                      The host of the process to create.
158      * @param name                      The name of the process.
159      * @param args                      The arguments of main method of the process.
160      *
161      * @exception                   NullPointerException if the provided name is null
162      *                              JniException on JNI madness
163      *
164      */
165     public Process(Host host, String name,
166                      String[]args) throws NullPointerException, JniException {
167     /* This is the constructor called by all others */
168
169     if (name == null)
170       throw new NullPointerException("Process name cannot be NULL");
171
172           this.properties = null;
173           
174       this.args = new Vector();
175
176     if (null != args)
177         this.args.addAll(Arrays.asList(args));
178
179       this.name = name;
180       this.id = nextProcessId++;
181
182       schedBegin = new Sem(0);
183       schedEnd = new Sem(0);
184
185       MsgNative.processCreate(this, host);
186   }
187   
188  
189     /**
190      * This method kills all running process of the simulation.
191      *
192      * @param resetPID          Should we reset the PID numbers. A negative number means no reset
193      *                                          and a positive number will be used to set the PID of the next newly
194      *                                          created process.
195      *
196      * @return                          The function returns the PID of the next created process.
197      *                  
198      */ public static int killAll(int resetPID) {
199     return MsgNative.processKillAll(resetPID);
200   }
201
202     /**
203      * This method adds an argument in the list of the arguments of the main function
204      * of the process. 
205      *
206      * @param arg                       The argument to add.
207      */
208   protected void addArg(String arg) {
209     args.add(arg);
210   }
211
212     /**
213      * This method suspends the process by suspending the task on which it was
214      * waiting for the completion.
215      *
216      * @exception                       JniException on JNI madness
217      *                              NativeException on error in the native SimGrid code
218      */
219   public void pause() throws JniException, NativeException {
220     MsgNative.processSuspend(this);
221   }
222     /**
223      * This method resumes a suspended process by resuming the task on which it was
224      * waiting for the completion.
225      *
226      * @exception                       JniException on JNI madness
227      *                              NativeException on error in the native SimGrid code
228      *
229      */ public void restart() throws JniException, NativeException {
230     MsgNative.processResume(this);
231   }
232     /**
233      * This method tests if a process is suspended.
234      *
235      * @return                          The method returns true if the process is suspended.
236      *                                          Otherwise the method returns false.
237      *
238      * @exception                       JniException on JNI madness
239      */ public boolean isSuspended() throws JniException {
240     return MsgNative.processIsSuspended(this);
241   }
242     /**
243      * This method returns the host of a process.
244      *
245      * @return                          The host instance of the process.
246      *
247      * @exception                       JniException on JNI madness
248      *                              NativeException on error in the native SimGrid code
249      *
250      */ public Host getHost() throws JniException, NativeException {
251     return MsgNative.processGetHost(this);
252   }
253     /**
254      * This static method get a process from a PID.
255      *
256      * @param PID                       The process identifier of the process to get.
257      *
258      * @return                          The process with the specified PID.
259      *
260      * @exception                       NativeException on error in the native SimGrid code
261      */ public static Process fromPID(int PID) throws NativeException {
262     return MsgNative.processFromPID(PID);
263   }
264     /**
265      * This method returns the PID of the process.
266      *
267      * @return                          The PID of the process.
268      *
269      * @exception                       JniException on JNI madness
270      *                              NativeException on error in the native SimGrid code
271      */ public int getPID() throws JniException, NativeException {
272     return MsgNative.processGetPID(this);
273   }
274     /**
275      * This method returns the PID of the parent of a process.
276      *
277      * @return                          The PID of the parent of the process.
278      *
279      * @exception                       NativeException on error in the native SimGrid code
280      */ public int getPPID() throws NativeException {
281     return MsgNative.processGetPPID(this);
282   }
283     /**
284      * This static method returns the currently running process.
285      *
286      * @return                          The current process.
287      *
288      * @exception                       NativeException on error in the native SimGrid code
289      *
290      *
291      */ public static Process currentProcess() throws NativeException {
292     return MsgNative.processSelf();
293   }
294     /**
295      * This static method returns the PID of the currently running process.
296      *
297      * @return                          The PID of the current process.         
298      */ public static int currentProcessPID() {
299     return MsgNative.processSelfPID();
300   }
301
302     /**
303      * This static method returns the PID of the parent of the currently running process.
304      *
305      * @return                          The PID of the parent of current process.               
306      */
307   public static int currentProcessPPID() {
308     return MsgNative.processSelfPPID();
309   }
310
311     /**
312      * This function migrates a process to another host.
313      *
314      * @param host                      The host where to migrate the process.
315      *
316      * @exception                       JniException on JNI madness
317      *                              NativeException on error in the native SimGrid code
318      */
319   public void migrate(Host host) throws JniException, NativeException {
320     MsgNative.processChangeHost(this, host);
321   }
322     /**
323      * This method makes the current process sleep until time seconds have elapsed.
324      *
325      * @param seconds           The time the current process must sleep.
326      *
327      * @exception                       NativeException on error in the native SimGrid code
328      */ public static void waitFor(double seconds) throws NativeException {
329     MsgNative.processWaitFor(seconds);
330   } public void showArgs() {
331     try {
332       Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
333                this.args.size());
334       for (int i = 0; i < this.args.size(); i++)
335         Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
336                  "] args[" + i + "]=" + (String) (this.args.get(i)));
337     } catch(MsgException e) {
338       Msg.info("Damn JNI stuff");
339       e.printStackTrace();
340       System.exit(1);
341     }
342   }
343     /**
344      * This method runs the process. Il calls the method function that you must overwrite.
345      */
346   public synchronized void run() {
347
348       String[]args = null;      /* do not fill it before the signal or this.args will be empty */
349
350       //waitSignal(); /* wait for other people to fill the process in */
351
352
353      try {
354         schedBegin.acquire();
355      } catch(InterruptedException e) {
356      }
357
358
359
360     try {
361       args = new String[this.args.size()];
362       if (this.args.size() > 0) {
363         this.args.toArray(args);
364       }
365
366       this.main(args);
367       MsgNative.processExit(this);
368       schedEnd.release();
369     }
370     catch(MsgException e) {
371       e.printStackTrace();
372       Msg.info("Unexpected behavior. Stopping now");
373       System.exit(1);
374     }
375   }
376
377     /**
378      * The main function of the process (to implement).
379      */
380   public abstract void main(String[]args)
381   throws JniException, NativeException;
382
383
384   public void unschedule() {
385     try {
386       schedEnd.release();
387       schedBegin.acquire();
388     } catch(InterruptedException e) {
389     }
390   }
391
392   public void schedule() {
393     try {
394       schedBegin.release();
395       schedEnd.acquire();
396     } catch(InterruptedException e) {
397     }
398   }
399   
400   /** Send the given task to given host on given channel */
401   public void taskPut(Host host, int channel,
402                        Task task) throws NativeException, JniException {
403     MsgNative.hostPut(host, channel, task, -1);
404   }
405   
406    /** Send the given task to given host on given channel (waiting at most given time)*/
407    public void taskPut(Host host, int channel,
408                        Task task, double timeout) throws NativeException, JniException {
409     MsgNative.hostPut(host, channel, task, timeout);
410   }
411    /** Receive a task on given channel */
412     public Task taskGet(int channel) throws NativeException,
413     JniException {
414     return MsgNative.taskGet(channel, -1, null);
415   }
416    /** Receive a task on given channel (waiting at most given time) */
417     public Task taskGet(int channel,
418                             double timeout) throws NativeException,
419     JniException {
420     return MsgNative.taskGet(channel, timeout, null);
421   }
422    /** Receive a task on given channel from given sender */
423     public Task taskGet(int channel, Host host) throws NativeException,
424     JniException {
425     return MsgNative.taskGet(channel, -1, host);
426   }
427    /** Receive a task on given channel from given sender (waiting at most given time) */
428     public Task taskGet(int channel, double timeout,
429                             Host host) throws NativeException, JniException {
430     return MsgNative.taskGet(channel, timeout, host);
431   }
432   
433   /** Send the given task in the mailbox associated with the specified alias  (waiting at most given time) */
434   public void taskSend(String alias,
435                        Task task, double timeout) throws NativeException, JniException {
436     MsgNative.taskSend(alias, task, timeout);
437   }
438   
439   /** Send the given task in the mailbox associated with the specified alias*/
440   public void taskSend(String alias,
441                        Task task) throws NativeException, JniException {
442     MsgNative.taskSend(alias, task, -1);
443   }
444   
445   /** Send the given task in the mailbox associated with the default alias  (defaultAlias = "hostName:processName") */
446   public void taskSend(Task task) throws NativeException, JniException {
447         
448         String alias = Host.currentHost().getName() + ":" + this.msgName();
449     MsgNative.taskSend(alias, task, -1);
450   }
451   
452   /** Send the given task in the mailbox associated with the default alias (waiting at most given time) */
453   public void taskSend(Task task, double timeout) throws NativeException, JniException {
454         
455         String alias = Host.currentHost().getName() + ":" + this.msgName();
456     MsgNative.taskSend(alias, task, timeout);
457   }
458   
459   
460    /** Receive a task on mailbox associated with the specified alias */
461     public Task taskReceive(String alias) throws NativeException,
462     JniException {
463     return MsgNative.taskReceive(alias, -1.0, null);
464   }
465   
466   /** Receive a task on mailbox associated with the default alias */
467    public Task taskReceive() throws NativeException,
468     JniException {
469     String alias = Host.currentHost().getName() + ":" + this.msgName();
470     return MsgNative.taskReceive(alias, -1.0, null);
471   }
472   
473   /** Receive a task on mailbox associated with the specified alias (waiting at most given time) */
474     public Task taskReceive(String alias,
475                             double timeout) throws NativeException,
476     JniException {
477     return MsgNative.taskReceive(alias, timeout, null);
478   }
479   
480   /** Receive a task on mailbox associated with the default alias (waiting at most given time) */
481    public Task taskReceive(double timeout) throws NativeException,
482     JniException {
483     String alias = Host.currentHost().getName() + ":" + this.msgName();
484     return MsgNative.taskReceive(alias, timeout, null);
485   }
486   
487    /** Receive a task on mailbox associated with the specified alias from given sender */
488     public Task taskReceive(String alias,
489                             double timeout, Host host) throws NativeException,
490     JniException {
491     return MsgNative.taskReceive(alias, timeout, host);
492   }
493   
494   /** Receive a task on mailbox associated with the default alias from given sender  (waiting at most given time) */
495   public Task taskReceive(double timeout, Host host) throws NativeException,
496     JniException {
497     String alias = Host.currentHost().getName() + ":" + this.msgName();
498     return MsgNative.taskReceive(alias, timeout, host);
499   }
500   
501    /** Receive a task on mailbox associated with the specified alias from given sender*/
502      public Task taskReceive(String alias,
503                             Host host) throws NativeException,
504     JniException {
505     return MsgNative.taskReceive(alias, -1.0, host);
506   }
507    /** Receive a task on mailbox associated with the default alias from given sender */
508    public Task taskReceive( Host host) throws NativeException,
509     JniException {
510         String alias = Host.currentHost().getName() + ":" + this.msgName();
511     return MsgNative.taskReceive(alias, -1.0, host);
512   }
513 }