2 * JNI interface to C code for MSG.
4 * Copyright 2006-2012 The SimGrid Team.
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.
12 package org.simgrid.msg;
14 import java.io.FileOutputStream;
15 import java.io.InputStream;
16 import java.io.OutputStream;
20 public final class Msg {
21 /* Statically load the library which contains all native functions used in here */
22 static private boolean isNativeInited = false;
23 public static void nativeInit() {
27 /* prefer the version on disk, if existing */
28 System.loadLibrary("SG_java");
29 } catch (UnsatisfiedLinkError e) {
30 /* If not found, unpack the one bundled into the jar file and use it */
34 isNativeInited = true;
39 private static void loadLib (String name) {
40 String Path = "NATIVE/"+System.getProperty("os.name")+"/"+System.getProperty("os.arch")+"/";
42 InputStream in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
45 filename = "lib"+name+".so";
46 in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
49 filename = name+".dll";
50 in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
53 filename = "lib"+name+".dll";
54 in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
57 filename = "lib"+name+".dylib";
58 in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
61 throw new RuntimeException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine.");
63 // Caching the file on disk: desactivated because it could fool the users
64 // if (new File(filename).isFile()) {
65 // // file was already unpacked -- use it directly
66 // System.load(new File(".").getAbsolutePath()+File.separator+filename);
70 // We must write the lib onto the disk before loading it -- stupid operating systems
71 File fileOut = new File(filename);
72 // if (!new File(".").canWrite()) {
73 // System.out.println("Cannot write in ."+File.separator+filename+"; unpacking the library into a temporary file instead");
74 fileOut = File.createTempFile("simgrid-", ".tmp");
75 // don't leak the file on disk, but remove it on JVM shutdown
76 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(fileOut.getAbsolutePath())));
78 // System.out.println("Unpacking SimGrid native library to " + fileOut.getAbsolutePath());
79 OutputStream out = new FileOutputStream(fileOut);
81 /* copy the library in position */
82 byte[] buffer = new byte[4096];
84 while ((bytes_read = in.read(buffer)) != -1) // Read until EOF
85 out.write(buffer, 0, bytes_read);
87 /* close all file descriptors, and load that shit */
90 System.load(fileOut.getAbsolutePath());
91 } catch (Exception e) {
92 System.err.println("Cannot load the bindings to the simgrid library: ");
94 System.err.println("This jar file does not seem to fit your system, sorry");
98 /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
99 private static class FileCleaner implements Runnable {
100 private String target;
101 public FileCleaner(String name) {
106 new File(target).delete();
107 } catch(Exception e) {
108 System.out.println("Unable to clean temporary file "+target+" during shutdown.");
114 /** Retrieve the simulation time
117 public final static native double getClock();
119 * Issue a debug logging message.
120 * @param s message to log.
122 public final static native void debug(String s);
124 * Issue an verbose logging message.
125 * @param s message to log.
127 public final static native void verb(String s);
129 /** Issue an information logging message
132 public final static native void info(String s);
134 * Issue an warning logging message.
135 * @param s message to log.
137 public final static native void warn(String s);
139 * Issue an error logging message.
140 * @param s message to log.
142 public final static native void error(String s);
144 * Issue an critical logging message.
145 * @param s message to log.
147 public final static native void critical(String s);
149 /*********************************************************************************
150 * Deployment and initialization related functions *
151 *********************************************************************************/
154 * The natively implemented method to initialize a MSG simulation.
156 * @param args The arguments of the command line of the simulation.
160 public final static native void init(String[]args);
163 * Run the MSG simulation.
165 * The simulation is not cleaned afterward (see
166 * {@link #clean()} if you really insist on cleaning the C side), so you can freely
167 * retrieve the informations that you want from the simulation. In particular, retrieving the status
168 * of a process or the current date is perfectly ok.
172 public final static native void run() ;
174 /** This function is useless nowadays, just stop calling it. */
176 public final static void clean(){}
179 * The native implemented method to create the environment of the simulation.
181 * @param platformFile The XML file which contains the description of the environment of the simulation
184 public final static native void createEnvironment(String platformFile);
187 * The method to deploy the simulation.
190 * @param deploymentFile
192 public final static native void deployApplication(String deploymentFile);
194 /** Example launcher. You can use it or provide your own launcher, as you wish
196 * @throws MsgException
198 static public void main(String[]args) throws MsgException {
199 /* initialize the MSG simulation. Must be done before anything else (even logging). */
202 if (args.length < 2) {
203 Msg.info("Usage: Msg platform_file deployment_file");
207 /* Load the platform and deploy the application */
208 Msg.createEnvironment(args[0]);
209 Msg.deployApplication(args[1]);
210 /* Execute the simulation */