Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
deperlify the Java process creation: no need to pass the hostname where we can use...
[simgrid.git] / src / bindings / java / org / simgrid / msg / Process.java
index 29a69ca..15cd7e5 100644 (file)
@@ -7,7 +7,7 @@
 package org.simgrid.msg;
 
 import java.util.Arrays;
-import java.util.Vector;
+import java.util.ArrayList;
 
 /**
  * A process may be defined as a code, with some private data, executing 
@@ -46,16 +46,13 @@ public abstract class Process implements Runnable {
         * a native process. Even if this attribute is public you must never
         * access to it. It is set automatically during the build of the object.
         */
-       private long bind;
-       /**
-        * Indicates if the process is started
-        */
-       boolean started;
+       private long bind = 0;
+       /** Indicates if the process is started */
        /**
         * Even if this attribute is public you must never access to it.
         * It is used to compute the id of an MSG process.
         */
-       public static long nextProcessId = 0;
+       private static long nextProcessId = 0;
 
        /**
         * Even if this attribute is public you must never access to it.
@@ -66,39 +63,26 @@ public abstract class Process implements Runnable {
 
        /** Time at which the process should be created  */
        protected double startTime = 0;
-       /** Time at which th
-        * Kill time of the process
+       /** Time at which the process should be killed.
         * 
         * Set at creation, and used internally by SimGrid
         */
        private double killTime = -1;
 
-       private String name;
+       private String name = null;
        
        private int pid = -1;
        private int ppid = -1;
        private Host host = null;
 
-       /** The arguments of the method function of the process. */     
-       public Vector<String> args;
-
-
-       /**
-        * Default constructor
-        */
-       protected Process() {
-               this.id = nextProcessId++;
-               this.name = null;
-               this.bind = 0;
-               this.args = new Vector<String>();
-       }
-
+       /** The arguments of the method function of the process. */
+       private ArrayList<String> args = new ArrayList<>();
 
        /**
         * Constructs a new process from the name of a host and his name. The method
         * function of the process doesn't have argument.
         *
-        * @param hostname              The name of the host of the process to create.
+        * @param hostname              Where to create the process.
         * @param name                  The name of the process.
         *
         * @exception                   HostNotFoundException  if no host with this name exists.
@@ -112,23 +96,21 @@ public abstract class Process implements Runnable {
         * Constructs a new process from the name of a host and his name. The arguments
         * of the method function of the process are specified by the parameter args.
         *
-        * @param hostname              The name of the host of the process to create.
+        * @param hostname              Where to create the process.
         * @param name                  The name of the process.
         * @param args                  The arguments of the main function of the process.
         *
         * @exception                   HostNotFoundException  if no host with this name exists.
-        *                      NativeException
-        * @throws NativeException
         *
         */ 
-       public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
+       public Process(String hostname, String name, String[] args) throws HostNotFoundException {
                this(Host.getByName(hostname), name, args);
        }
        /**
         * Constructs a new process from a host and his name. The method function of the 
         * process doesn't have argument.
         *
-        * @param host                  The host of the process to create.
+        * @param host                  Where to create the process.
         * @param name                  The name of the process.
         *
         */
@@ -139,26 +121,30 @@ public abstract class Process implements Runnable {
         * Constructs a new process from a host and his name, the arguments of here method function are
         * specified by the parameter args.
         *
-        * @param host                  The host of the process to create.
+        * @param host                  Where to create the process.
         * @param name                  The name of the process.
         * @param args                  The arguments of main method of the process.
         */     
-       public Process(Host host, String name, String[]args) {
-               this();
-               this.host = host;
+       public Process(Host host, String name, String[]args) 
+       {
+               if (host == null)
+                       throw new NullPointerException("Cannot create a process on the null host");
                if (name == null)
-                       throw new NullPointerException("Process name cannot be NULL");
+                       throw new NullPointerException("Process name cannot be null");
+               
+               this.id = nextProcessId++;
+               this.host = host;
                this.name = name;
 
-               this.args = new Vector<String>();
+               this.args = new ArrayList<>();
                if (null != args)
                        this.args.addAll(Arrays.asList(args));
-       }       
+       }
        /**
         * Constructs a new process from a host and his name, the arguments of here method function are
         * specified by the parameter args.
         *
-        * @param host                  The host of the process to create.
+        * @param host                  Where to create the process.
         * @param name                  The name of the process.
         * @param args                  The arguments of main method of the process.
         * @param startTime             Start time of the process
@@ -166,24 +152,16 @@ public abstract class Process implements Runnable {
         *
         */
        public Process(Host host, String name, String[]args, double startTime, double killTime) {
-               this();
-               this.host = host;
-               if (name == null)
-                       throw new NullPointerException("Process name cannot be NULL");
-               this.name = name;
-
-               this.args = new Vector<String>();
-               if (null != args)
-                       this.args.addAll(Arrays.asList(args));
-
+               this(host, name, args);
                this.startTime = startTime;
-               this.killTime = killTime;               
+               this.killTime = killTime;
        }
        /**
-        * The natively implemented method to create an MSG process.
-        * @param hostName    A valid (bound) host where create the process.
+        * The native method to create an MSG process.
+        * @param host    where to create the process.
         */
-       protected native void create(String hostName) throws HostNotFoundException;
+       protected native void create(Host host);
+       
        /**
         * This method kills all running process of the simulation.
         *
@@ -201,6 +179,9 @@ public abstract class Process implements Runnable {
         * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
         */
        public native void kill();
+       public static void kill(Process p) {
+               p.kill();
+       }
        
        /** Suspends the process. See {@link #resume()} to resume it afterward */
        public native void suspend();
@@ -208,10 +189,14 @@ public abstract class Process implements Runnable {
        public native void resume();    
        /** Tests if a process is suspended.
         *
-        * @see {@link #suspend()} and {@link #resume()}
-        */ 
+        * @see #suspend()
+        * @see #resume()
+        */
        public native boolean isSuspended();
        
+       /** Yield the current process. All other processes that are ready at the same timestamp will be executed first */
+       public static native void yield();
+       
        /**
         * Specify whether the process should restart when its host restarts after a failure
         * 
@@ -237,13 +222,11 @@ public abstract class Process implements Runnable {
        /**
         * This static method gets a process from a PID.
         *
-        * @param PID                   The process identifier of the process to get.
+        * @param pid                   The process identifier of the process to get.
         *
         * @return                              The process with the specified PID.
-        *
-        * @exception                   NativeException on error in the native SimGrid code
         */ 
-       public static native Process fromPID(int PID) throws NativeException;
+       public static native Process fromPID(int pid);
        /**
         * This method returns the PID of the process.
         *
@@ -288,7 +271,7 @@ public abstract class Process implements Runnable {
         */
        public native void migrate(Host host);  
        /**
-        * Makes the current process sleep until millis millisecondes have elapsed.
+        * Makes the current process sleep until millis milliseconds have elapsed.
         * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
         * FIXME: Not optimal, maybe we should have two native functions.
         * @param millis the length of time to sleep in milliseconds.
@@ -303,9 +286,9 @@ public abstract class Process implements Runnable {
         * milliseconds and nanoseconds.
         * Overloads Thread.sleep.
         * @param millis the length of time to sleep in milliseconds.
-        * @param nanos additionnal nanoseconds to sleep.
+        * @param nanos additional nanoseconds to sleep.
         */
-       public native static void sleep(long millis, int nanos) throws HostFailureException;
+       public static native void sleep(long millis, int nanos) throws HostFailureException;
        /**
         * Makes the current process sleep until time seconds have elapsed.
         * @param seconds               The time the current process must sleep.
@@ -317,35 +300,32 @@ public abstract class Process implements Runnable {
         * @throws HostNotFoundException
         */
        public final void start() throws HostNotFoundException {
-               if (!started) {
-                       started = true;
-                       create(host.getName());
-               }
+               if (bind == 0)
+                        create(host);
        }
 
-       /**
-        * This method runs the process. Il calls the method function that you must overwrite.
-        */
+       /** This method runs the process. It calls the method function that you must overwrite. */
+       @Override
        public void run() {
 
                String[] args = null;      /* do not fill it before the signal or this.args will be empty */
-               //waitSignal(); /* wait for other people to fill the process in */
 
                try {
                        args = new String[this.args.size()];
-                       if (this.args.size() > 0) {
+                       if (!this.args.isEmpty()) {
                                this.args.toArray(args);
                        }
 
                        this.main(args);
-               } catch(MsgException e) {
+               }
+               catch(MsgException e) {
                        e.printStackTrace();
                        Msg.info("Unexpected behavior. Stopping now");
                        System.exit(1);
                }
                catch(ProcessKilledError pk) {
+                       /* The process was killed before its end. With a kill() or something. */
                }       
-               exit();
        }
 
        /**
@@ -356,13 +336,16 @@ public abstract class Process implements Runnable {
         */
        public abstract void main(String[]args) throws MsgException;
 
-       public native void exit();    
+       /** Stops the execution of the current actor */
+       public void exit() {
+               this.kill();
+       }
        /**
         * Class initializer, to initialize various JNI stuff
         */
        private static native void nativeInit();
        static {
-               Msg.nativeInit();
+               org.simgrid.NativeLib.nativeInit();
                nativeInit();
        }
        /**
@@ -370,6 +353,6 @@ public abstract class Process implements Runnable {
         *
         * @return                      The count of the running processes
         */ 
-       public native static int getCount();
+       public static native int getCount();
 
 }