2 * JNI interface to C code for MSG.
4 * Copyright (c) 2006-2013. 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("simgrid-java");
29 } catch (UnsatisfiedLinkError e) {
30 /* If not found, unpack the one bundled into the jar file and use it */
32 loadLib("simgrid-java");
34 isNativeInited = true;
39 private static void loadLib (String name) {
40 String Os = System.getProperty("os.name");
41 String Arch = System.getProperty("os.arch");
42 // Some OS/Arch may be different between Java and Cmake,
43 // which generated the path
44 if (Os.toLowerCase().startsWith("win"))
46 else if (Os.contains("OS X"))
48 if (Arch.equalsIgnoreCase("x86"))
50 else if (Arch.equalsIgnoreCase("x86_64"))
52 String Path = "NATIVE/" + Os + "/" + Arch + "/";
55 InputStream in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
58 filename = "lib"+name+".so";
59 in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
62 filename = name+".dll";
63 in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
66 filename = "lib"+name+".dll";
67 in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
70 filename = "lib"+name+".dylib";
71 in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
74 throw new RuntimeException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine.");
76 // Caching the file on disk: desactivated because it could fool the users
77 // if (new File(filename).isFile()) {
78 // // file was already unpacked -- use it directly
79 // System.load(new File(".").getAbsolutePath()+File.separator+filename);
83 // We must write the lib onto the disk before loading it -- stupid operating systems
84 File fileOut = new File(filename);
85 // if (!new File(".").canWrite()) {
86 // System.out.println("Cannot write in ."+File.separator+filename+"; unpacking the library into a temporary file instead");
87 fileOut = File.createTempFile("simgrid-", ".tmp");
88 // don't leak the file on disk, but remove it on JVM shutdown
89 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(fileOut.getAbsolutePath())));
91 // System.out.println("Unpacking SimGrid native library to " + fileOut.getAbsolutePath());
92 OutputStream out = new FileOutputStream(fileOut);
94 /* copy the library in position */
95 byte[] buffer = new byte[4096];
97 while ((bytes_read = in.read(buffer)) != -1) // Read until EOF
98 out.write(buffer, 0, bytes_read);
100 /* close all file descriptors, and load that shit */
103 System.load(fileOut.getAbsolutePath());
104 } catch (Exception e) {
105 System.err.println("Cannot load the bindings to the simgrid library: ");
107 System.err.println("This jar file does not seem to fit your system, sorry");
111 /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
112 private static class FileCleaner implements Runnable {
113 private String target;
114 public FileCleaner(String name) {
119 new File(target).delete();
120 } catch(Exception e) {
121 System.out.println("Unable to clean temporary file "+target+" during shutdown.");
127 /** Retrieve the simulation time
128 * @return The simulation time.
130 public final static native double getClock();
132 * Issue a debug logging message.
133 * @param s message to log.
135 public final static native void debug(String s);
137 * Issue an verbose logging message.
138 * @param s message to log.
140 public final static native void verb(String s);
142 /** Issue an information logging message
145 public final static native void info(String s);
147 * Issue an warning logging message.
148 * @param s message to log.
150 public final static native void warn(String s);
152 * Issue an error logging message.
153 * @param s message to log.
155 public final static native void error(String s);
157 * Issue an critical logging message.
158 * @param s message to log.
160 public final static native void critical(String s);
162 /*********************************************************************************
163 * Deployment and initialization related functions *
164 *********************************************************************************/
167 * The natively implemented method to initialize a MSG simulation.
169 * @param args The arguments of the command line of the simulation.
171 public final static native void init(String[]args);
174 * Run the MSG simulation.
176 * The simulation is not cleaned afterward (see
177 * {@link #clean()} if you really insist on cleaning the C side), so you can freely
178 * retrieve the informations that you want from the simulation. In particular, retrieving the status
179 * of a process or the current date is perfectly ok.
181 public final static native void run() ;
183 /** This function is useless nowadays, just stop calling it. */
185 public final static void clean(){}
188 * The native implemented method to create the environment of the simulation.
190 * @param platformFile The XML file which contains the description of the environment of the simulation
193 public final static native void createEnvironment(String platformFile);
195 public final static native As environmentGetRoutingRoot();
198 * The method to deploy the simulation.
201 * @param deploymentFile
203 public final static native void deployApplication(String deploymentFile);
205 /** Example launcher. You can use it or provide your own launcher, as you wish
207 * @throws MsgException
209 static public void main(String[]args) throws MsgException {
210 /* initialize the MSG simulation. Must be done before anything else (even logging). */
213 if (args.length < 2) {
214 Msg.info("Usage: Msg platform_file deployment_file");
218 /* Load the platform and deploy the application */
219 Msg.createEnvironment(args[0]);
220 Msg.deployApplication(args[1]);
221 /* Execute the simulation */