Updated Java JNI example

This commit is contained in:
Joseph Henry
2017-10-13 13:14:28 -07:00
parent 83419fb89e
commit 63fc3ad8ab
6 changed files with 86 additions and 49 deletions

View File

@@ -3,7 +3,12 @@
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes> <attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="/Users/Joseph/op/code/zerotier/ZeroTierSDK/examples/java/ZeroTierHelloWorld/lib"/> <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="/Users/joseph/op/zt/libzt"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/Users/joseph/op/zt/libzt/examples/java/ZeroTierHelloWorld">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="."/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>

View File

@@ -1,3 +0,0 @@
/MyClass$1.class
/zerotier/
/MyClass.class

View File

@@ -1,4 +1,30 @@
// Hello World example for the ZeroTierSDK /*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
// Simple Java example for libzt using JNI
import zerotier.*; import zerotier.*;
@@ -8,33 +34,26 @@ public class MyClass {
public native void startOneService(); public native void startOneService();
static { static {
System.loadLibrary("zt"); System.load("/Users/joseph/op/zt/libzt/build/darwin/libzt.so");
} }
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Welcome to the Machine");
final ZeroTier z = new ZeroTier(); final ZeroTier z = new ZeroTier();
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
// Calls to JNI code System.out.println("starting libzt");
z.start("/Users/Joseph/op/code/zerotier/ZeroTierSDK/zt1"); z.startjoin("/Users/joseph/op/zt/libzt/ztjni", "1212121212121212");
// start(path) will not block
// startjoin(path, nwid) will block
} }
}).start(); }).start();
//while(!z.running()) { }
while(true) while(true)
{ {
try { try { Thread.sleep(3000); }
System.out.println("Welcome to the Machine"); catch (InterruptedException e) { e.printStackTrace(); }
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }
} }

View File

@@ -1,3 +1,29 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
package zerotier; package zerotier;
import java.math.BigInteger; import java.math.BigInteger;

View File

@@ -1,6 +1,6 @@
/* /*
* ZeroTier One - Network Virtualization Everywhere * ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -17,12 +17,11 @@
* *
* -- * --
* *
* ZeroTier may be used and distributed under the terms of the GPLv3, which * You can be released from the requirements of the license by purchasing
* are available at: http://www.gnu.org/licenses/gpl-3.0.html * a commercial license. Buying such a license is mandatory as soon as you
* * develop commercial closed-source software that incorporates or links
* If you would like to embed ZeroTier into a commercial application or * directly against ZeroTier software without disclosing the source code
* redistribute it in a modified binary form, please contact ZeroTier Networks * of your own application.
* LLC. Start here: http://www.zerotier.com/
*/ */
package zerotier; package zerotier;
@@ -35,9 +34,8 @@ import java.util.zip.ZipError;
public class ZeroTier { public class ZeroTier {
public static String Version() public static String Version() {
{ return "1.1.5";
return "1.2.2";
} }
// Socket families // Socket families
@@ -60,14 +58,15 @@ public class ZeroTier {
public static int F_SETFL = 4; public static int F_SETFL = 4;
// Loads JNI code // Loads JNI code
static { System.loadLibrary("zt"); } //static { System.loadLibrary("zt"); }
// ZeroTier service controls // ZeroTier service controls
public native void ztjni_start(String homeDir); public native void ztjni_start(String homeDir);
public void start(String homeDir) { public void start(String homeDir) { ztjni_start(homeDir); }
ztjni_start(homeDir);
}
public native void ztjni_startjoin(String homeDir, String nwid);
public void startjoin(String homeDir, String nwid) { ztjni_startjoin(homeDir, nwid); }
public native void ztjni_join(String nwid); public native void ztjni_join(String nwid);
public void join(String nwid) { public void join(String nwid) {
ztjni_join(nwid); ztjni_join(nwid);
@@ -96,20 +95,13 @@ public class ZeroTier {
} }
public native boolean ztjni_running(); public native boolean ztjni_running();
public boolean running() { public boolean running() { return ztjni_running(); }
return ztjni_running();
}
public native int ztjni_socket(int family, int type, int protocol); public native int ztjni_socket(int family, int type, int protocol);
public int socket(int family, int type, int protocol) { public int socket(int family, int type, int protocol) { return ztjni_socket(family, type, protocol); }
return ztjni_socket(family, type, protocol);
}
public native int ztjni_connect(int fd, String addr, int port); public native int ztjni_connect(int fd, String addr, int port);
public int connect(int sock, Address zaddr, String nwid) { return connect(sock, zaddr.Address(), zaddr.Port(), nwid); }
public int connect(int sock, Address zaddr, String nwid) {
return connect(sock, zaddr.Address(), zaddr.Port(), nwid);
}
public int connect(int sock, String addr, int port, String nwid) public int connect(int sock, String addr, int port, String nwid)
{ {
@@ -131,10 +123,8 @@ public class ZeroTier {
} }
public native int ztjni_bind(int fd, String addr, int port); public native int ztjni_bind(int fd, String addr, int port);
public int bind(int sock, Address zaddr, String nwid) { return bind(sock, zaddr.Address(), zaddr.Port(), nwid); }
public int bind(int sock, Address zaddr, String nwid) {
return bind(sock, zaddr.Address(), zaddr.Port(), nwid);
}
public int bind(int sock, String addr, int port, String nwid) { public int bind(int sock, String addr, int port, String nwid) {
int err = -1; int err = -1;
ArrayList<String> addresses; ArrayList<String> addresses;