25
examples/java/README.md
Normal file
25
examples/java/README.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
### How to compile/use this example:
|
||||||
|
|
||||||
|
1. Follow "Building from source" section from README.md in the root of this git repository
|
||||||
|
(the linking step/example from that section does not apply to this example)
|
||||||
|
|
||||||
|
2. Create the java library .jar file:
|
||||||
|
|
||||||
|
```
|
||||||
|
make host_jar_release
|
||||||
|
```
|
||||||
|
|
||||||
|
for other `host_jar` variants see Makefile in the root of this git repository
|
||||||
|
|
||||||
|
2. Copy the output .jar to this directory:
|
||||||
|
|
||||||
|
```
|
||||||
|
cp lib/lib/release/linux-x86_64/zt.jar examples/java/simpleExample/
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Now you can compile this example:
|
||||||
|
|
||||||
|
```
|
||||||
|
cd src
|
||||||
|
javac -cp ../zt.jar ./com/zerotier/libzt/javasimpleexample/*.java
|
||||||
|
```
|
||||||
@@ -24,43 +24,41 @@
|
|||||||
* of your own application.
|
* of your own application.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
package com.zerotier.libzt.javasimpleexample;
|
||||||
|
|
||||||
import com.zerotier.libzt.ZeroTier;
|
import com.zerotier.libzt.ZeroTier;
|
||||||
import com.zerotier.libzt.ZeroTierEventListener;
|
|
||||||
|
|
||||||
public class ExampleApp
|
public class Main {
|
||||||
{
|
|
||||||
static void sleep(int ms)
|
|
||||||
{
|
|
||||||
try { Thread.sleep(ms); }
|
|
||||||
catch (InterruptedException e) { e.printStackTrace(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args)
|
static void sleep(int ms) {
|
||||||
{
|
try {
|
||||||
// Set up event listener and start service
|
Thread.sleep(ms);
|
||||||
MyZeroTierEventListener listener = new MyZeroTierEventListener();
|
} catch (InterruptedException e) {
|
||||||
int servicePort = 9994;
|
e.printStackTrace();
|
||||||
ZeroTier.start("test/path", listener, servicePort);
|
}
|
||||||
// Wait for EVENT_NODE_ONLINE
|
}
|
||||||
System.out.println("waiting for node to come online...");
|
|
||||||
while (listener.isOnline == false) { sleep(50); }
|
public static void main(String[] args) {
|
||||||
System.out.println("joining network");
|
// Set up event listener and start service
|
||||||
ZeroTier.join(0x0123456789abcdefL);
|
MyZeroTierEventListener listener = new MyZeroTierEventListener();
|
||||||
// Wait for EVENT_NETWORK_READY_IP4/6
|
int servicePort = 9994;
|
||||||
System.out.println("waiting for network config...");
|
ZeroTier.start("test/path", listener, servicePort);
|
||||||
while (listener.isNetworkReady == false) { sleep(50); }
|
// Wait for EVENT_NODE_ONLINE
|
||||||
System.out.println("joined");
|
System.out.println("waiting for node to come online...");
|
||||||
|
while (listener.isOnline == false) { sleep(50); }
|
||||||
|
System.out.println("joining network");
|
||||||
|
ZeroTier.join(0x0123456789abcdefL);
|
||||||
|
// Wait for EVENT_NETWORK_READY_IP4/6
|
||||||
|
System.out.println("waiting for network config...");
|
||||||
|
while (listener.isNetworkReady == false) { sleep(50); }
|
||||||
|
System.out.println("joined");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
Begin using socket API after this point
|
Begin using socket API after this point
|
||||||
|
|
||||||
Use ZeroTier.ZeroTierSocket, ZeroTier.ZeroTierSocketFactory, etc
|
Use ZeroTier.ZeroTierSocket, ZeroTier.ZeroTierSocketFactory, etc
|
||||||
|
|
||||||
(or)
|
(or)
|
||||||
|
|
||||||
ZeroTier.socket(), ZeroTier.connect(), etc
|
ZeroTier.socket(), ZeroTier.connect(), etc
|
||||||
|
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
|
package com.zerotier.libzt.javasimpleexample;
|
||||||
|
|
||||||
import com.zerotier.libzt.ZeroTier;
|
import com.zerotier.libzt.ZeroTier;
|
||||||
import com.zerotier.libzt.ZeroTierEventListener;
|
import com.zerotier.libzt.ZeroTierEventListener;
|
||||||
import com.zerotier.libzt.ZeroTierPeerDetails;
|
|
||||||
|
|
||||||
public class MyZeroTierEventListener implements ZeroTierEventListener {
|
public class MyZeroTierEventListener implements ZeroTierEventListener {
|
||||||
|
|
||||||
public boolean isNetworkReady = false;
|
public boolean isNetworkReady = false;
|
||||||
public boolean isOnline = false;
|
public boolean isOnline = false;
|
||||||
|
|
||||||
public void onZeroTierEvent(long id, int eventCode)
|
public void onZeroTierEvent(long id, int eventCode) {
|
||||||
{
|
|
||||||
if (eventCode == ZeroTier.EVENT_NODE_UP) {
|
if (eventCode == ZeroTier.EVENT_NODE_UP) {
|
||||||
System.out.println("EVENT_NODE_UP: nodeId=" + Long.toHexString(id));
|
System.out.println("EVENT_NODE_UP: nodeId=" + Long.toHexString(id));
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@ public class MyZeroTierEventListener implements ZeroTierEventListener {
|
|||||||
if (eventCode == ZeroTier.EVENT_NETWORK_READY_IP4) {
|
if (eventCode == ZeroTier.EVENT_NETWORK_READY_IP4) {
|
||||||
// We have at least one assigned address and we've received a network configuration
|
// 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));
|
System.out.println("ZTS_EVENT_NETWORK_READY_IP4: nwid=" + Long.toHexString(id));
|
||||||
if ( id == 0xa09acf0233e4b070L) {
|
if (id == 0xa09acf0233e4b070L) {
|
||||||
isNetworkReady = true;
|
isNetworkReady = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user