Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5a0fafc52a74ec7665bd6d0ffb96d83a8bda668a
[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 own process you must inherit 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 example, for the previous 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 automatically 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  */
47
48 public abstract class Process extends Thread {
49         /**
50          * This attribute represents a bind between a java process object and
51          * a native process. Even if this attribute is public you must never
52          * access to it. It is set automatically during the build of the object.
53          */
54         public long bind;
55
56         /**
57          * Even if this attribute is public you must never access to it.
58          * It is used to compute the id of an MSG process.
59          */
60         public static long nextProcessId = 0;
61
62         /**
63          * Even if this attribute is public you must never access to it.
64          * It is compute automatically during the creation of the object. 
65          * The native functions use this identifier to synchronize the process.
66          */
67         public long id;
68
69         public Hashtable<String,String> properties;
70
71         /**
72          * The name of the process.                                                     
73          */
74         protected String name;
75         public String msgName() {
76                 return this.name;
77         }
78         /** The arguments of the method function of the process. */     
79         public Vector<String> args;
80
81         /* process synchronization tools */
82         protected Sem schedBegin, schedEnd;
83
84         /**
85          * Default constructor (used in ApplicationHandler to initialize it)
86          */
87         protected Process() {
88                 super();
89                 this.id = nextProcessId++;
90                 this.name = null;
91                 this.bind = 0;
92                 this.args = new Vector<String>();
93                 this.properties = null;
94                 schedBegin = new Sem(0);
95                 schedEnd = new Sem(0);
96         }
97
98
99         /**
100          * Constructs a new process from the name of a host and his name. The method
101          * function of the process doesn't have argument.
102          *
103          * @param hostname              The name of the host of the process to create.
104          * @param name                  The name of the process.
105          *
106          * @exception                   HostNotFoundException  if no host with this name exists.
107          *                      NativeException
108          *
109          */
110         public Process(String hostname, String name) throws HostNotFoundException, NativeException {
111                 this(Host.getByName(hostname), name, null);
112         }
113         /**
114          * Constructs a new process from the name of a host and his name. The arguments
115          * of the method function of the process are specified by the parameter args.
116          *
117          * @param hostname              The name of the host of the process to create.
118          * @param name                  The name of the process.
119          * @param args                  The arguments of the main function of the process.
120          *
121          * @exception                   HostNotFoundException  if no host with this name exists.
122          *                      NativeException
123          *
124          */ 
125         public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
126                 this(Host.getByName(hostname), name, args);
127         }
128         /**
129          * Constructs a new process from a host and his name. The method function of the 
130          * process doesn't have argument.
131          *
132          * @param host                  The host of the process to create.
133          * @param name                  The name of the process.
134          *
135          */
136         public Process(Host host, String name) {
137                 this(host, name, null);
138         }
139         /**
140          * Constructs a new process from a host and his name, the arguments of here method function are
141          * specified by the parameter args.
142          *
143          * @param host                  The host of the process to create.
144          * @param name                  The name of the process.
145          * @param args                  The arguments of main method of the process.
146          *
147          */
148         public Process(Host host, String name, String[]args) {
149                 /* This is the constructor called by all others */
150                 this();
151                 
152                 if (name == null)
153                         throw new NullPointerException("Process name cannot be NULL");
154                 this.name = name;
155
156                 this.args = new Vector<String>();
157                 if (null != args)
158                         this.args.addAll(Arrays.asList(args));
159
160                 MsgNative.processCreate(this, host);
161         }
162
163
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 int killAll(int resetPID) {
175                 return MsgNative.processKillAll(resetPID);
176         }
177
178         /**
179          * This method adds an argument in the list of the arguments of the main function
180          * of the process. 
181          *
182          * @param arg                   The argument to add.
183          */
184         @Deprecated
185         protected void addArg(String arg) {
186                 args.add(arg);
187         }
188
189         /**
190          * Suspends the process by suspending the task on which it was
191          * waiting for the completion.
192          *
193          * @exception                   NativeException on error in the native SimGrid code
194          */
195         public void pause() throws NativeException {
196                 MsgNative.processSuspend(this);
197         }
198         /**
199          * Resumes a suspended process by resuming the task on which it was
200          * waiting for the completion.
201          *
202          * @exception                   NativeException on error in the native SimGrid code
203          *
204          */ 
205         public void restart() throws NativeException {
206                 MsgNative.processResume(this);
207         }
208         /**
209          * Tests if a process is suspended.
210          *
211          * @return                              The method returns true if the process is suspended.
212          *                                              Otherwise the method returns false.
213          */ 
214         public boolean isSuspended() {
215                 return MsgNative.processIsSuspended(this);
216         }
217         /**
218          * Returns the host of a process.
219          *
220          * @return                              The host instance of the process.
221          *
222          * @exception                   NativeException on error in the native SimGrid code
223          *
224          */ 
225         public Host getHost() throws NativeException {
226                 return MsgNative.processGetHost(this);
227         }
228         /**
229          * This static method gets a process from a PID.
230          *
231          * @param PID                   The process identifier of the process to get.
232          *
233          * @return                              The process with the specified PID.
234          *
235          * @exception                   NativeException on error in the native SimGrid code
236          */ 
237         public static Process fromPID(int PID) throws NativeException {
238                 return MsgNative.processFromPID(PID);
239         }
240         /**
241          * This method returns the PID of the process.
242          *
243          * @return                              The PID of the process.
244          *
245          * @exception                   NativeException on error in the native SimGrid code
246          */ 
247         public int getPID() throws NativeException {
248                 return MsgNative.processGetPID(this);
249         }
250         /**
251          * This method returns the PID of the parent of a process.
252          *
253          * @return                              The PID of the parent of the process.
254          *
255          * @exception                   NativeException on error in the native SimGrid code
256          */ 
257         public int getPPID() throws NativeException {
258                 return MsgNative.processGetPPID(this);
259         }
260         /**
261          * This static method returns the currently running process.
262          *
263          * @return                              The current process.
264          *
265          * @exception                   NativeException on error in the native SimGrid code
266          */ 
267         public static Process currentProcess() throws NativeException {
268                 return MsgNative.processSelf();
269         }
270         /**
271          * Migrates a process to another host.
272          *
273          * @param host                  The host where to migrate the process.
274          *
275          * @exception                   NativeException on error in the native SimGrid code
276          */
277         public void migrate(Host host) throws NativeException {
278                 MsgNative.processChangeHost(this, host);
279         }
280         /**
281          * Makes the current process sleep until time seconds have elapsed.
282          *
283          * @param seconds               The time the current process must sleep.
284          *
285          * @exception                   NativeException on error in the native SimGrid code
286          */ 
287         public static void waitFor(double seconds) throws NativeException {
288                 MsgNative.processWaitFor(seconds);
289         } 
290         public void showArgs() {
291                 try {
292                         Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
293                                         this.args.size());
294                         for (int i = 0; i < this.args.size(); i++)
295                                 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
296                                                 "] args[" + i + "]=" + (String) (this.args.get(i)));
297                 } catch(MsgException e) {
298                         Msg.info("Damn JNI stuff");
299                         e.printStackTrace();
300                         System.exit(1);
301                 }
302         }
303         /**
304          * This method runs the process. Il calls the method function that you must overwrite.
305          */
306         public void run() {
307
308                 String[]args = null;      /* do not fill it before the signal or this.args will be empty */
309
310                 //waitSignal(); /* wait for other people to fill the process in */
311
312
313                 try {
314                         schedBegin.acquire();
315                 } catch(InterruptedException e) {
316                 }
317
318                 try {
319                         args = new String[this.args.size()];
320                         if (this.args.size() > 0) {
321                                 this.args.toArray(args);
322                         }
323
324                         this.main(args);
325                         MsgNative.processExit(this);
326                         schedEnd.release();
327                 } catch(MsgException e) {
328                         e.printStackTrace();
329                         Msg.info("Unexpected behavior. Stopping now");
330                         System.exit(1);
331                 }
332         }
333
334         /**
335          * The main function of the process (to implement).
336          */
337         public abstract void main(String[]args) throws NativeException;
338
339
340         public void unschedule() {
341                 try {
342                         schedEnd.release();
343                         schedBegin.acquire();
344                 } catch(InterruptedException e) {
345                 }
346         }
347
348         public void schedule() {
349                 try {
350                         schedBegin.release();
351                         schedEnd.acquire();
352                 } catch(InterruptedException e) {
353                 }
354         }
355
356         /** Send the given task in the mailbox associated with the specified alias  (waiting at most given time) */
357         public void taskSend(String mailbox, Task task, double timeout) throws NativeException {
358                 MsgNative.taskSend(mailbox, task, timeout);
359         }
360
361         /** Send the given task in the mailbox associated with the specified alias*/
362         public void taskSend(String mailbox, Task task) throws NativeException {
363                 MsgNative.taskSend(mailbox, task, -1);
364         }
365
366         /** Receive a task on mailbox associated with the specified mailbox */
367         public Task taskReceive(String mailbox) throws NativeException {
368                 return MsgNative.taskReceive(mailbox, -1.0, null);
369         }
370
371         /** Receive a task on mailbox associated with the specified alias (waiting at most given time) */
372         public Task taskReceive(String mailbox, double timeout) throws NativeException {
373                 return MsgNative.taskReceive(mailbox, timeout, null);
374         }
375
376         /** Receive a task on mailbox associated with the specified alias from given sender */
377         public Task taskReceive(String mailbox, double timeout, Host host) throws NativeException {
378                 return MsgNative.taskReceive(mailbox, timeout, host);
379         }
380
381         /** Receive a task on mailbox associated with the specified alias from given sender*/
382         public Task taskReceive(String mailbox, Host host) throws NativeException {
383                 return MsgNative.taskReceive(mailbox, -1.0, host);
384         }
385 }