Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c2861eeb5f06b4de54c7ebb06fca7c93e6069204
[simgrid.git] / src / java / simgrid / msg / Process.java
1 /*
2  * simgrid.msg.Host.java        1.00 07/05/01
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) Write a default constructor and 
31  *      call the default constructor of the base class.
32  *      
33  *      public Slave() {
34  *              super();
35  *  }
36  *
37  *  (3) Override the method function 
38  *      public void main(String[] args) {
39  *              System.out.println("Hello MSG");
40  *      }
41  * }
42  * The name of your process must be declared in the deployment file of your simulation.
43  * For the exemple, for the previouse process Slave this file must contains a line :
44  * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
45  * Slave. All the process of your simulation are automaticaly launched and managed by Msg.
46  * A process use tasks to simulate communications or computations with another process. 
47  * For more information see Task and ParallelTask. For more information on host concept 
48  * see Host.
49  * 
50  * @author  Abdelmalek Cherier
51  * @author  Martin Quinson
52  * @version 1.00, 07/05/01
53  * @see Host
54  * @see Task
55  * @see ParallelTask
56  * @since SimGrid 3.3
57  * @since JDK1.5011
58  */
59
60 public abstract class Process extends Thread {
61     /**
62      * This attribute represents a bind between a java process object and
63      * a native process. Even if this attribute is public you must never
64      * access to it. It is set automaticatly during the build of the object.
65      */
66   public long bind;
67
68     /**
69      * Even if this attribute is public you must never access to it.
70      * It is used to compute the id of an MSG process.
71      */
72   public static long nextProcessId = 0;
73
74     /**
75      * Even if this attribute is public you must never access to it.
76      * It is compute automaticaly during the creation of the object. 
77      * The native functions use this identifier to synchronize the process.
78      */
79   public long id;
80
81     /**
82      * The name of the process.                                                 
83      */
84   protected String name;
85   public String msgName() {
86     return this.name;
87   }
88   /*
89    * The arguments of the method function of the process.     
90    */ public Vector args;
91
92   /* process synchronisation tools */
93   protected Sem schedBegin, schedEnd;
94
95     /**
96      * Default constructor. (used in ApplicationHandler to initialize it)
97      */
98   protected Process() {
99     super();
100     this.id = 0;
101     this.name = null;
102     this.bind = 0;
103     this.args = new Vector();
104     schedBegin = new Sem(0);
105     schedEnd = new Sem(0);
106   }
107
108
109     /**
110      * Constructs a new process from the name of a host and his name. The method
111      * function of the process doesn't have argument.
112      *
113      * @param hostName          The name of the host of the process to create.
114      * @param name                      The name of the process.
115      *
116      * @exception                       HostNotFoundException  if no host with this name exists.
117      *                              NullPointerException if the provided name is null
118      *                              JniException on JNI madness
119      *
120      */
121   public Process(String hostname, String name)
122   throws NullPointerException, HostNotFoundException, JniException,
123     NativeException {
124     this(Host.getByName(hostname), name, null);
125   }
126     /**
127      * Constructs a new process from the name of a host and his name. The arguments
128      * of the method function of the process are specified by the parameter args.
129      *
130      * @param hostName          The name of the host of the process to create.
131      * @param name                      The name of the process.
132      * @param args                      The arguments of the main function of the process.
133      *
134      * @exception                       HostNotFoundException  if no host with this name exists.
135      *                              NullPointerException if the provided name is null
136      *                              JniException on JNI madness
137      *
138      */ public Process(String hostname, String name, String args[])
139   throws NullPointerException, HostNotFoundException, JniException,
140     NativeException {
141     this(Host.getByName(hostname), name, args);
142   }
143     /**
144      * Constructs a new process from a host and his name. The method function of the 
145      * process doesn't have argument.
146      *
147      * @param host                      The host of the process to create.
148      * @param name                      The name of the process.
149      *
150      * @exception                       NullPointerException if the provided name is null
151      *                              JniException on JNI madness
152      *
153      */
154     public Process(Host host, String name) throws NullPointerException,
155     JniException {
156     this(host, name, null);
157   }
158     /**
159      * Constructs a new process from a host and his name, the arguments of here method function are
160      * specified by the parameter args.
161      *
162      * @param host                      The host of the process to create.
163      * @param name                      The name of the process.
164      * @param args                      The arguments of main method of the process.
165      *
166      * @exception                   NullPointerException if the provided name is null
167      *                              JniException on JNI madness
168      *
169      */
170     public Process(Host host, String name,
171                      String[]args) throws NullPointerException, JniException {
172     /* This is the constructor called by all others */
173
174     if (name == null)
175       throw new NullPointerException("Process name cannot be NULL");
176
177
178       this.args = new Vector();
179
180     if (null != args)
181         this.args.addAll(Arrays.asList(args));
182
183       this.name = name;
184       this.id = nextProcessId++;
185
186       schedBegin = new Sem(0);
187       schedEnd = new Sem(0);
188
189       Msg.processCreate(this, host);
190   }
191     /**
192      * This method kills all running process of the simulation.
193      *
194      * @param resetPID          Should we reset the PID numbers. A negative number means no reset
195      *                                          and a positive number will be used to set the PID of the next newly
196      *                                          created process.
197      *
198      * @return                          The function returns the PID of the next created process.
199      *                  
200      */ public static int killAll(int resetPID) {
201     return Msg.processKillAll(resetPID);
202   }
203
204     /**
205      * This method adds an argument in the list of the arguments of the main function
206      * of the process. 
207      *
208      * @param arg                       The argument to add.
209      */
210   protected void addArg(String arg) {
211     args.add(arg);
212   }
213
214     /**
215      * This method suspends the process by suspending the task on which it was
216      * waiting for the completion.
217      *
218      * @exception                       JniException on JNI madness
219      *                              NativeException on error in the native SimGrid code
220      */
221   public void pause() throws JniException, NativeException {
222     Msg.processSuspend(this);
223   }
224     /**
225      * This method resumes a suspended process by resuming the task on which it was
226      * waiting for the completion.
227      *
228      * @exception                       JniException on JNI madness
229      *                              NativeException on error in the native SimGrid code
230      *
231      */ public void restart() throws JniException, NativeException {
232     Msg.processResume(this);
233   }
234     /**
235      * This method tests if a process is suspended.
236      *
237      * @return                          The method returns true if the process is suspended.
238      *                                          Otherwise the method returns false.
239      *
240      * @exception                       JniException on JNI madness
241      */ public boolean isSuspended() throws JniException {
242     return Msg.processIsSuspended(this);
243   }
244     /**
245      * This method returns the host of a process.
246      *
247      * @return                          The host instance of the process.
248      *
249      * @exception                       JniException on JNI madness
250      *                              NativeException on error in the native SimGrid code
251      *
252      */ public Host getHost() throws JniException, NativeException {
253     return Msg.processGetHost(this);
254   }
255     /**
256      * This static method get a process from a PID.
257      *
258      * @param PID                       The process identifier of the process to get.
259      *
260      * @return                          The process with the specified PID.
261      *
262      * @exception                       NativeException on error in the native SimGrid code
263      */ public static Process fromPID(int PID) throws NativeException {
264     return Msg.processFromPID(PID);
265   }
266     /**
267      * This method returns the PID of the process.
268      *
269      * @return                          The PID of the process.
270      *
271      * @exception                       JniException on JNI madness
272      *                              NativeException on error in the native SimGrid code
273      */ public int getPID() throws JniException, NativeException {
274     return Msg.processGetPID(this);
275   }
276     /**
277      * This method returns the PID of the parent of a process.
278      *
279      * @return                          The PID of the parent of the process.
280      *
281      * @exception                       NativeException on error in the native SimGrid code
282      */ public int getPPID() throws NativeException {
283     return Msg.processGetPPID(this);
284   }
285     /**
286      * This static method returns the currently running process.
287      *
288      * @return                          The current process.
289      *
290      * @exception                       NativeException on error in the native SimGrid code
291      *
292      *
293      */ public static Process currentProcess() throws NativeException {
294     return Msg.processSelf();
295   }
296     /**
297      * This static method returns the PID of the currently running process.
298      *
299      * @return                          The PID of the current process.         
300      */ public static int currentProcessPID() {
301     return Msg.processSelfPID();
302   }
303
304     /**
305      * This static method returns the PID of the parent of the currently running process.
306      *
307      * @return                          The PID of the parent of current process.               
308      */
309   public static int currentProcessPPID() {
310     return Msg.processSelfPPID();
311   }
312
313     /**
314      * This function migrates a process to another host.
315      *
316      * @parm host                       The host where to migrate the process.
317      *
318      * @exception                       JniException on JNI madness
319      *                              NativeException on error in the native SimGrid code
320      */
321   public void migrate(Host host) throws JniException, NativeException {
322     Msg.processChangeHost(this, host);
323   }
324     /**
325      * This method makes the current process sleep until time seconds have elapsed.
326      *
327      * @param seconds           The time the current process must sleep.
328      *
329      * @exception                       NativeException on error in the native SimGrid code
330      */ public static void waitFor(double seconds) throws NativeException {
331     Msg.processWaitFor(seconds);
332   } public void showArgs() {
333     try {
334       Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
335                this.args.size());
336       for (int i = 0; i < this.args.size(); i++)
337         Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
338                  "] args[" + i + "]=" + (String) (this.args.get(i)));
339     } catch(MsgException e) {
340       Msg.info("Damn JNI stuff");
341       e.printStackTrace();
342       System.exit(1);
343     }
344   }
345     /**
346      * This method runs the process. Il calls the method function that you must overwrite.
347      */
348   public synchronized void run() {
349
350     try {
351       String[]args = null;      /* do not fill it before the signal or this.args will be empty */
352
353       //waitSignal(); /* wait for other people to fill the process in */
354
355
356       try {
357         schedBegin.acquire();
358       } catch(InterruptedException e) {
359       }
360
361
362
363       if (this.args.size() > 0) {
364
365         args = new String[this.args.size()];
366         this.args.toArray(args);
367       }
368
369       this.main(args);
370       Msg.processExit(this);
371       schedEnd.release();
372     }
373     catch(MsgException e) {
374       e.printStackTrace();
375       Msg.info("Unexpected behavior. Stopping now");
376       System.exit(1);
377     }
378   }
379
380     /**
381      * Process synchronization. The process wait the signal of the simulator to start.
382      *
383      * @exception                       JniException on JNI madness
384      */
385   private void waitSignal() throws JniException {
386     Msg.waitSignal(this);
387     unschedule();
388   }
389     /**
390          * The main function of the process (to implement).
391          */ public abstract void main(String[]args)
392   throws JniException, NativeException;
393
394
395   public void unschedule() {
396
397     try {
398       schedEnd.release();
399       schedBegin.acquire();
400     } catch(InterruptedException e) {
401
402     }
403   }
404
405   public void schedule() {
406
407     try {
408       schedBegin.release();
409       schedEnd.acquire();
410     } catch(InterruptedException e) {
411
412     }
413   }
414
415   public void taskSend(Channel channel, Task task,
416                        Host host) throws NativeException, JniException {
417     Msg.channelPut(channel, task, host);
418   } public Task taskReceive(Channel channel) throws NativeException,
419     JniException {
420     return Msg.channelGet(channel);
421 }}