diff --git a/examples/java/ExampleApp.java b/examples/java/ExampleApp.java index 00612a7..23889e8 100644 --- a/examples/java/ExampleApp.java +++ b/examples/java/ExampleApp.java @@ -26,8 +26,11 @@ // Simple Java example for libzt using JNI -import zerotier.*; -import java.net.*; +import com.zerotier.libzt.ZeroTier; +import com.zerotier.libzt.ZTSocketAddress; +import com.zerotier.libzt.ZTFDSet; + +//import java.net.*; import java.lang.Thread; public class ExampleApp { @@ -46,14 +49,14 @@ public class ExampleApp { public static void main(String[] args) { - final ZeroTier libzt = new ZeroTier(); + final ZeroTier zt = new ZeroTier(); new Thread(new Runnable() { public void run() { String path = "/Users/joseph/op/zt/libzt/ztjni"; // Where node's config files are stored - long nwid = 0xa09acf0233e4b070L; + long nwid = 0xa09acf7233e4b071L; // Test modes boolean blocking_start_call = true; @@ -61,6 +64,7 @@ public class ExampleApp { boolean tcp = false; boolean loop = true; // RX/TX multiple times boolean idle = false; // Idle loop after node comes online. For testing reachability + boolean use_select = true; int fd = -1, client_fd = -1, err, r, w, lengthToRead = 0, flags = 0; byte[] rxBuffer; @@ -77,14 +81,14 @@ public class ExampleApp { // Blocking call that waits for all components of the service to start System.out.println("Starting ZT service..."); if (blocking_start_call) { - libzt.startjoin(path, nwid); + zt.startjoin(path, nwid); } // METHOD 2 // Optional. Non-blocking call to start service. You'll have to use the below process to determine // when you are allowed to start making socket calls. if (!blocking_start_call) { - libzt.start(path, true); - while(!libzt.ready()) { + zt.start(path, true); + while(!zt.ready()) { try { // Wait for core service to start Thread.sleep(250); } @@ -93,9 +97,9 @@ public class ExampleApp { } } System.out.println("Core started. Networks can be joined after this point"); - libzt.join(nwid); + zt.join(nwid); // Wait for userspace stack to start, we trigger this by joining a network - while(!libzt.stack_running()) { + while(!zt.stack_running()) { try { Thread.sleep(1000); } @@ -106,18 +110,18 @@ public class ExampleApp { } System.out.println("ZT service ready."); // Device/Node address info - System.out.println("path=" + libzt.get_path()); - long nodeId = libzt.get_node_id(); + System.out.println("path=" + zt.get_path()); + long nodeId = zt.get_node_id(); System.out.println("nodeId=" + Long.toHexString(nodeId)); - int numAddresses = libzt.get_num_assigned_addresses(nwid); + int numAddresses = zt.get_num_assigned_addresses(nwid); System.out.println("this node has (" + numAddresses + ") assigned addresses on network " + Long.toHexString(nwid)); for (int i=0; i 0) { + System.out.println("select detected something to read on"); + for (int i = 0; i < nfds; i++) + { + if (master_set.ISSET(i)) + { + System.out.println("attempting to read from: " + i); + r = zt.recvfrom(fd, rxBuffer, rxBuffer.length, flags, remoteAddr); + System.out.println("read (" + r + ") bytes from " + remoteAddr.toString() + ", buffer = " + new String(rxBuffer)); + } + } + } + } + } + if (!use_select) + { + while(true) { + addr = new ZTSocketAddress(); + r = zt.recvfrom(fd, rxBuffer, rxBuffer.length, flags, remoteAddr); + System.out.println("read (" + r + ") bytes from " + remoteAddr.toString() + ", buffer = " + new String(rxBuffer)); + } } } } - libzt.close(client_fd); - libzt.close(fd); + zt.close(client_fd); + zt.close(fd); } }).start(); diff --git a/examples/java/zerotier/ZTFD.java b/examples/java/com/zerotier/libzt/ZTFDSet.java similarity index 76% rename from examples/java/zerotier/ZTFD.java rename to examples/java/com/zerotier/libzt/ZTFDSet.java index 866a0e2..cf5bd7b 100644 --- a/examples/java/zerotier/ZTFD.java +++ b/examples/java/com/zerotier/libzt/ZTFDSet.java @@ -24,9 +24,31 @@ * of your own application. */ -package zerotier; +package com.zerotier.libzt; -public class fd_set +public class ZTFDSet { - byte[] fds_bits[1024 / 8]; + byte[] fds_bits = new byte[1024]; + + public void CLR(int fd) + { + fds_bits[fd] = 0x00; + } + + public boolean ISSET(int fd) + { + return fds_bits[fd] == 0x01; + } + + public void SET(int fd) + { + fds_bits[fd] = 0x01; + } + + public void ZERO() + { + for (int i=0; i<1024; i++) { + fds_bits[i] = 0x00; + } + } } \ No newline at end of file diff --git a/examples/java/zerotier/ZTSocketAddress.java b/examples/java/com/zerotier/libzt/ZTSocketAddress.java similarity index 90% rename from examples/java/zerotier/ZTSocketAddress.java rename to examples/java/com/zerotier/libzt/ZTSocketAddress.java index 68117dd..c922251 100644 --- a/examples/java/zerotier/ZTSocketAddress.java +++ b/examples/java/com/zerotier/libzt/ZTSocketAddress.java @@ -24,9 +24,11 @@ * of your own application. */ -package zerotier; +package com.zerotier.libzt; -import java.net.*; +import com.zerotier.libzt.ZeroTier; + +import java.net.InetAddress; // Designed to transport address information across the JNI boundary public class ZTSocketAddress @@ -34,9 +36,6 @@ public class ZTSocketAddress public byte[] _ip6 = new byte[16]; public byte[] _ip4 = new byte[4]; - public long _ipdata; - public long _ipdata_ext; - public int _family; public int _port; // Also reused for netmask or prefix @@ -45,7 +44,7 @@ public class ZTSocketAddress public ZTSocketAddress(String ipStr, int port) { if(ipStr.contains(":")) { - _family = zerotier.ZeroTier.AF_INET6; + _family = ZeroTier.AF_INET6; try { InetAddress ip = InetAddress.getByName(ipStr); _ip6 = ip.getAddress(); @@ -53,7 +52,7 @@ public class ZTSocketAddress catch (Exception e) { } } else if(ipStr.contains(".")) { - _family = zerotier.ZeroTier.AF_INET; + _family = ZeroTier.AF_INET; try { InetAddress ip = InetAddress.getByName(ipStr); _ip4 = ip.getAddress(); @@ -69,7 +68,7 @@ public class ZTSocketAddress private String ipString() { - if (_family == zerotier.ZeroTier.AF_INET) { + if (_family == ZeroTier.AF_INET) { try { InetAddress inet = InetAddress.getByAddress(_ip4); return "" + inet.getHostAddress(); @@ -77,7 +76,7 @@ public class ZTSocketAddress System.out.println(e); } } - if (_family == zerotier.ZeroTier.AF_INET6) { + if (_family == ZeroTier.AF_INET6) { try { InetAddress inet = InetAddress.getByAddress(_ip6); return "" + inet.getHostAddress(); diff --git a/examples/java/zerotier/ZeroTier.java b/examples/java/com/zerotier/libzt/ZeroTier.java similarity index 95% rename from examples/java/zerotier/ZeroTier.java rename to examples/java/com/zerotier/libzt/ZeroTier.java index 8c7d060..f6c3c01 100644 --- a/examples/java/zerotier/ZeroTier.java +++ b/examples/java/com/zerotier/libzt/ZeroTier.java @@ -24,7 +24,7 @@ * of your own application. */ -package zerotier; +package com.zerotier.libzt; import java.net.*; @@ -78,4 +78,5 @@ public class ZeroTier { public native boolean getsockname(int fd, ZTSocketAddress addr); public native int getpeername(int fd, ZTSocketAddress addr); public native int fcntl(int sock, int cmd, int flag); + public native int select(int nfds, ZTFDSet readfds, ZTFDSet writefds, ZTFDSet exceptfds, int timeout_sec, int timeout_usec); } \ No newline at end of file diff --git a/examples/java/libzt.jar b/examples/java/libzt.jar deleted file mode 100644 index 3a5e8f5..0000000 Binary files a/examples/java/libzt.jar and /dev/null differ