root / ide / org.omnetpp.ide.nativelibs / loadlib.i @ 9d023582
History | View | Annotate | Download (3.14 KB)
1 |
// |
---|---|
2 |
// Load the native library when any method from it is referenced. |
3 |
// |
4 |
// Note: check exceptions before. gcc shared libs must NOT be linked with "-static", |
5 |
// because throw/catch won't work (program exits with "terminate called..."). |
6 |
// See http://www.gnu.org/software/gcc/faq.html#dso |
7 |
// As a safeguard against incorrectly built libs, we test exceptions first thing |
8 |
// after loading the library. |
9 |
// --Andras |
10 |
// |
11 |
|
12 |
%{ |
13 |
#include "exception.h" |
14 |
static void testExceptionHandling() { |
15 |
try { |
16 |
throw opp_runtime_error("if you see this exception, omnetpp's native library (\"opplibs\") was built incorrectly"); |
17 |
} catch (std::exception&) { /*OK!*/ } |
18 |
} |
19 |
%} |
20 |
void testExceptionHandling(); |
21 |
|
22 |
%pragma(java) jniclassimports=%{ |
23 |
import org.eclipse.core.runtime.Platform; |
24 |
import org.eclipse.jface.dialogs.MessageDialog; |
25 |
import org.eclipse.swt.widgets.Display; |
26 |
%} |
27 |
|
28 |
%pragma(java) jniclasscode=%{ |
29 |
static { |
30 |
if (Platform.getOS().equals(Platform.OS_WIN32)) { |
31 |
try { |
32 |
// mingwm10.dll is a dependency of opplibs.dll. We need to load it |
33 |
// explicitly, because for some reason it is not found otherwise |
34 |
// when placed next to opplibs.dll. --Andras |
35 |
System.loadLibrary("mingwm10"); |
36 |
} |
37 |
catch (UnsatisfiedLinkError e) { |
38 |
System.err.println("Warning: could not load mingwm10.dll: " + e.getClass().getName() + ": " + e.toString()); |
39 |
} |
40 |
} |
41 |
try { |
42 |
System.loadLibrary("opplibs"); |
43 |
} |
44 |
catch (UnsatisfiedLinkError e) { |
45 |
displayError(e); |
46 |
throw e; |
47 |
} |
48 |
testExceptionHandling(); |
49 |
testThreads(); |
50 |
} |
51 |
|
52 |
private static void testThreads() { |
53 |
// test that exception handling is thread-safe in our shared lib (gcc -mthreads) |
54 |
for (int i=0; i<3; i++) { |
55 |
Thread t = new Thread() { |
56 |
@Override |
57 |
public void run() { |
58 |
long start = System.currentTimeMillis(); |
59 |
while (System.currentTimeMillis() - start < 50) |
60 |
testExceptionHandling(); |
61 |
} |
62 |
}; |
63 |
t.start(); |
64 |
} |
65 |
} |
66 |
|
67 |
private static void displayError(final UnsatisfiedLinkError e) { |
68 |
runNowOrSyncInUIThread(new Runnable() { |
69 |
public void run() { |
70 |
MessageDialog.openError(null, "Fatal Error", |
71 |
"FATAL: The OMNeT++ IDE native library failed to load, " + |
72 |
"all OMNeT++-related functionality will be unaccessible.\n\n" + |
73 |
"Details:\n\n" + |
74 |
e.getClass().getSimpleName() + ": " + e.getMessage() + "\n\n" + |
75 |
(Platform.getOS().equals(Platform.OS_LINUX) ? |
76 |
"Try upgrading your Linux installation to a more recent version, " + |
77 |
"or installing newer versions of libc and libstdc++." : "") |
78 |
); |
79 |
} |
80 |
}); |
81 |
} |
82 |
|
83 |
private static void runNowOrSyncInUIThread(Runnable runnable) { |
84 |
if (Display.getCurrent() == null) |
85 |
Display.getDefault().syncExec(runnable); |
86 |
else |
87 |
runnable.run(); |
88 |
} |
89 |
%} |
90 |
|
91 |
|