Add WIP Java language bindings
This commit is contained in:
@@ -1,161 +1,138 @@
|
||||
//package com.zerotier.libzt.javasimpleexample;
|
||||
|
||||
import com.zerotier.libzt.ZeroTier;
|
||||
import com.zerotier.libzt.ZeroTierEventListener;
|
||||
import com.zerotier.sdk.*;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class Example {
|
||||
public class selftest {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.err.println(args.length);
|
||||
if (args.length < 4 || args.length > 5) {
|
||||
System.err.println("Invalid arguments");
|
||||
System.err.println(" Usage: <server|client> <id_path> <network> <addr> <port>");
|
||||
System.err.println(" example server ./ 0123456789abcdef 8080");
|
||||
System.err.println(" example client ./ 0123456789abcdef 192.168.22.1 8080\n");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
static void sleep(int ms) {
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
String storagePath = "";
|
||||
String remoteAddr = "";
|
||||
int port = 0;
|
||||
String mode = args[0];
|
||||
storagePath = args[1];
|
||||
BigInteger networkId = new BigInteger(args[2], 16);
|
||||
if (args.length == 4) {
|
||||
port = Integer.parseInt(args[3]);
|
||||
}
|
||||
if (args.length == 5) {
|
||||
remoteAddr = args[3];
|
||||
port = Integer.parseInt(args[4]);
|
||||
}
|
||||
System.out.println("mode = " + mode);
|
||||
System.out.println("networkId = " + Long.toHexString(networkId.longValue()));
|
||||
System.out.println("storagePath = " + storagePath);
|
||||
System.out.println("remoteAddr = " + remoteAddr);
|
||||
System.out.println("port = " + port);
|
||||
|
||||
public static void main(String[] args) {
|
||||
String keyPath="."; // Default to current directory
|
||||
BigInteger networkId;
|
||||
Integer servicePort=0;
|
||||
if (args.length != 3) {
|
||||
System.err.println(" Invalid arguments");
|
||||
System.err.println(" Usage: example <path_to_write_keys> <nwid> <ztServicePort>");
|
||||
System.exit(1);
|
||||
}
|
||||
keyPath = args[0];
|
||||
networkId = new BigInteger(args[1], 16);
|
||||
servicePort = Integer.parseInt(args[2]);
|
||||
System.out.println("networkId = " + Long.toHexString(networkId.longValue()));
|
||||
System.out.println("keyPath = " + keyPath);
|
||||
System.out.println("servicePort = " + servicePort);
|
||||
// ZeroTier setup
|
||||
|
||||
//
|
||||
// BEGIN SETUP
|
||||
//
|
||||
ZeroTierNode node = new ZeroTierNode();
|
||||
|
||||
// Set up event listener and start service
|
||||
MyZeroTierEventListener listener = new MyZeroTierEventListener();
|
||||
ZeroTier.start(keyPath, (ZeroTierEventListener)listener, servicePort);
|
||||
// Wait for EVENT_NODE_ONLINE
|
||||
System.out.println("waiting for node to come online...");
|
||||
while (listener.isOnline == false) { sleep(50); }
|
||||
System.out.println("joining network");
|
||||
ZeroTier.join(networkId.longValue());
|
||||
// Wait for EVENT_NETWORK_READY_IP4/6
|
||||
System.out.println("waiting for network config...");
|
||||
while (listener.isNetworkReady == false) { sleep(50); }
|
||||
System.out.println("joined");
|
||||
node.initFromStorage(storagePath);
|
||||
// node.initSetEventHandler(new MyZeroTierEventListener());
|
||||
// node.initSetPort(9994);
|
||||
node.start();
|
||||
|
||||
//
|
||||
// END SETUP
|
||||
//
|
||||
System.out.println("Waiting for node to come online...");
|
||||
while (! node.isOnline()) {
|
||||
ZeroTierNative.zts_util_delay(50);
|
||||
}
|
||||
System.out.println("Node ID: " + Long.toHexString(node.getId()));
|
||||
System.out.println("Joining network...");
|
||||
node.join(networkId.longValue());
|
||||
System.out.println("Waiting for network...");
|
||||
while (! node.isNetworkTransportReady(networkId.longValue())) {
|
||||
ZeroTierNative.zts_util_delay(50);
|
||||
}
|
||||
System.out.println("joined");
|
||||
|
||||
sleep(120000);
|
||||
// Socket logic
|
||||
|
||||
/**
|
||||
*
|
||||
* Begin using socket API after this point:
|
||||
*
|
||||
* ZeroTier.ZeroTierSocket, ZeroTier.ZeroTierSocketFactory, etc
|
||||
*
|
||||
* (or)
|
||||
*
|
||||
* ZeroTier.socket(), ZeroTier.connect(), etc
|
||||
*/
|
||||
}
|
||||
if (mode.equals("server")) {
|
||||
System.out.println("Starting server...");
|
||||
try {
|
||||
ZeroTierSocket socket =
|
||||
new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_STREAM, 0);
|
||||
socket.bind("0.0.0.0", port);
|
||||
socket.listen(100);
|
||||
ZeroTierSocket newConnection = socket.accept();
|
||||
ZeroTierInputStream inputStream = newConnection.getInputStream();
|
||||
DataInputStream dataInputStream = new DataInputStream(inputStream);
|
||||
String message = dataInputStream.readUTF();
|
||||
System.out.println("recv: " + message);
|
||||
socket.close();
|
||||
newConnection.close();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
|
||||
if (mode.equals("client")) {
|
||||
System.out.println("Starting client...");
|
||||
try {
|
||||
ZeroTierSocket socket =
|
||||
new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_STREAM, 0);
|
||||
socket.connect(remoteAddr, port);
|
||||
ZeroTierOutputStream outputStream = socket.getOutputStream();
|
||||
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
|
||||
dataOutputStream.writeUTF("Hello from java!");
|
||||
socket.close();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example event handling
|
||||
* (OPTIONAL) event handler
|
||||
*/
|
||||
class MyZeroTierEventListener implements ZeroTierEventListener {
|
||||
|
||||
public boolean isNetworkReady = false;
|
||||
public boolean isOnline = false;
|
||||
|
||||
public void onZeroTierEvent(long id, int eventCode) {
|
||||
if (eventCode == ZeroTier.EVENT_NODE_UP) {
|
||||
System.out.println("EVENT_NODE_UP: nodeId=" + Long.toHexString(id));
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NODE_ONLINE) {
|
||||
// The core service is running properly and can join networks now
|
||||
System.out.println("EVENT_NODE_ONLINE: nodeId=" + Long.toHexString(id));
|
||||
isOnline = true;
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NODE_OFFLINE) {
|
||||
// Network does not seem to be reachable by any available strategy
|
||||
System.out.println("EVENT_NODE_OFFLINE");
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NODE_DOWN) {
|
||||
// Called when the node is shutting down
|
||||
System.out.println("EVENT_NODE_DOWN");
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NODE_IDENTITY_COLLISION) {
|
||||
// Another node with this identity already exists
|
||||
System.out.println("EVENT_NODE_IDENTITY_COLLISION");
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NODE_UNRECOVERABLE_ERROR) {
|
||||
// Try again
|
||||
System.out.println("EVENT_NODE_UNRECOVERABLE_ERROR");
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NODE_NORMAL_TERMINATION) {
|
||||
// Normal closure
|
||||
System.out.println("EVENT_NODE_NORMAL_TERMINATION");
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NETWORK_READY_IP4) {
|
||||
// We have at least one assigned address and we've received a network configuration
|
||||
System.out.println("ZTS_EVENT_NETWORK_READY_IP4: nwid=" + Long.toHexString(id));
|
||||
isNetworkReady = true;
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NETWORK_READY_IP6) {
|
||||
// We have at least one assigned address and we've received a network configuration
|
||||
System.out.println("ZTS_EVENT_NETWORK_READY_IP6: nwid=" + Long.toHexString(id));
|
||||
//isNetworkReady = true;
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NETWORK_DOWN) {
|
||||
// Someone called leave(), we have no assigned addresses, or otherwise cannot use this interface
|
||||
System.out.println("EVENT_NETWORK_DOWN: nwid=" + Long.toHexString(id));
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NETWORK_REQ_CONFIG) {
|
||||
// Waiting for network configuration
|
||||
System.out.println("EVENT_NETWORK_REQ_CONFIG: nwid=" + Long.toHexString(id));
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NETWORK_OK) {
|
||||
// Config received and this node is authorized for this network
|
||||
System.out.println("EVENT_NETWORK_OK: nwid=" + Long.toHexString(id));
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NETWORK_ACCESS_DENIED) {
|
||||
// You are not authorized to join this network
|
||||
System.out.println("EVENT_NETWORK_ACCESS_DENIED: nwid=" + Long.toHexString(id));
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NETWORK_NOT_FOUND) {
|
||||
// The virtual network does not exist
|
||||
System.out.println("EVENT_NETWORK_NOT_FOUND: nwid=" + Long.toHexString(id));
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_NETWORK_CLIENT_TOO_OLD) {
|
||||
// The core version is too old
|
||||
System.out.println("EVENT_NETWORK_CLIENT_TOO_OLD: nwid=" + Long.toHexString(id));
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_PEER_DIRECT) {
|
||||
System.out.println("EVENT_PEER_DIRECT: id=" + Long.toHexString(id));
|
||||
/*
|
||||
ZeroTierPeerDetails details = new ZeroTierPeerDetails();
|
||||
ZeroTier.get_peer(id, details);
|
||||
System.out.println("address="+Long.toHexString(details.address));
|
||||
System.out.println("pathCount="+details.pathCount);
|
||||
System.out.println("version="+details.versionMajor+"."+details.versionMinor+"."+details.versionRev);
|
||||
System.out.println("latency="+details.latency); // Not relevant
|
||||
System.out.println("role="+details.role); // Not relevent
|
||||
// Print all known paths
|
||||
for (int i=0; i<details.pathCount; i++) {
|
||||
System.out.println("addr="+details.paths[i].toString());
|
||||
}
|
||||
*/
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_PEER_RELAY) {
|
||||
System.out.println("EVENT_PEER_RELAY: id=" + Long.toHexString(id));
|
||||
}
|
||||
}
|
||||
class MyZeroTierEventListener implements ZeroTierEventListener {
|
||||
public void onZeroTierEvent(long id, int event_code)
|
||||
{
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NODE_UP) {
|
||||
System.out.println("EVENT_NODE_UP");
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NODE_ONLINE) {
|
||||
System.out.println("EVENT_NODE_ONLINE: " + Long.toHexString(id));
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NODE_OFFLINE) {
|
||||
System.out.println("EVENT_NODE_OFFLINE");
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NODE_DOWN) {
|
||||
System.out.println("EVENT_NODE_DOWN");
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP4) {
|
||||
System.out.println("ZTS_EVENT_NETWORK_READY_IP4: " + Long.toHexString(id));
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP6) {
|
||||
System.out.println("ZTS_EVENT_NETWORK_READY_IP6: " + Long.toHexString(id));
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_DOWN) {
|
||||
System.out.println("EVENT_NETWORK_DOWN: " + Long.toHexString(id));
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_OK) {
|
||||
System.out.println("EVENT_NETWORK_OK: " + Long.toHexString(id));
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_ACCESS_DENIED) {
|
||||
System.out.println("EVENT_NETWORK_ACCESS_DENIED: " + Long.toHexString(id));
|
||||
}
|
||||
if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_NOT_FOUND) {
|
||||
System.out.println("EVENT_NETWORK_NOT_FOUND: " + Long.toHexString(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
# Java example
|
||||
|
||||
1). Build or download the `zt-1.3.3.jar`, copy it to this directory
|
||||
1). Build or download the `libzt-1.3.3.jar`, copy it to this directory
|
||||
|
||||
2). Run `make`
|
||||
|
||||
3). `java -cp ".:zt-1.3.3.jar" Example id_path 0123456789abcdef 9997`
|
||||
3). `java -cp ".:libzt-1.3.3.jar" Example id_path 0123456789abcdef 9997`
|
||||
|
||||
See [src/bindings/java](../../src/bindings/java) for wrapper implementation code.
|
||||
See [src/bindings/java](../../src/bindings/java) for wrapper implementation code.
|
||||
|
||||
Reference in New Issue
Block a user