Minor updates to Java ExampleApp and added select() to JNI layer

This commit is contained in:
Joseph Henry
2018-09-06 11:08:06 -07:00
parent 6e06cb36ff
commit 84fcfd3baf
5 changed files with 109 additions and 24 deletions

View File

@@ -39,7 +39,6 @@
#include "libzt.h"
// function pointers which will have values assigned once the dynamic library is loaded
int (*_zts_set_service_port)(int portno);
@@ -96,8 +95,8 @@ void load_library_symbols(char *library_path)
{
void *libHandle = dlopen(library_path, RTLD_LAZY);
if (libHandle == NULL) {
DEBUG_ERROR("unable to load dynamic lib");
exit(0);
DEBUG_ERROR("unable to load dynamic libs: %s", dlerror());
exit(-1);
}
// Load symbols from library (call these directly)
@@ -160,6 +159,15 @@ int main(int argc, char **argv)
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
exit(0);
}
#ifdef __linux__
char *library_path = (char*)"./libzt.so";
#endif
#ifdef __APPLE__
char *library_path = (char*)"./libzt.dylib";
#endif
load_library_symbols(library_path);
std::string path = argv[1];
std::string nwidstr = argv[2];
std::string remote_addr = argv[3];
@@ -173,7 +181,7 @@ int main(int argc, char **argv)
in4.sin_addr.s_addr = inet_addr(remote_addr.c_str());
in4.sin_family = AF_INET;
// --- BEGIN EXAMPLE CODE
//
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);

View File

@@ -95,8 +95,8 @@ void load_library_symbols(char *library_path)
{
void *libHandle = dlopen(library_path, RTLD_LAZY);
if (libHandle == NULL) {
DEBUG_ERROR("unable to load dynamic lib");
exit(0);
DEBUG_ERROR("unable to load dynamic libs: %s", dlerror());
exit(-1);
}
// Load symbols from library (call these directly)
@@ -150,7 +150,6 @@ void load_library_symbols(char *library_path)
_zts_add_dns_nameserver = (int(*)(struct sockaddr *addr))dlsym(libHandle, "zts_add_dns_nameserver");
}
int main(int argc, char **argv)
{
if (argc != 4) {
@@ -158,6 +157,15 @@ int main(int argc, char **argv)
printf("server [config_file_path] [nwid] [bind_port]\n");
exit(0);
}
#ifdef __linux__
char *library_path = (char*)"./libzt.so";
#endif
#ifdef __APPLE__
char *library_path = (char*)"./libzt.dylib";
#endif
load_library_symbols(library_path);
std::string path = argv[1];
std::string nwidstr = argv[2];
int bind_port = atoi(argv[3]);
@@ -170,10 +178,7 @@ int main(int argc, char **argv)
in4.sin_addr.s_addr = INADDR_ANY;
in4.sin_family = AF_INET;
// --- BEGIN EXAMPLE CODE
char *library_path = (char*)"bin/lib/libzt.dylib";
load_library_symbols(library_path);
//
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);

View File

@@ -30,13 +30,9 @@ 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 {
public native int loadsymbols();
public native void startOneService();
static { System.loadLibrary("zt"); } // load libzt.dylib or libzt.so
@@ -143,11 +139,11 @@ public class ExampleApp {
return;
}
String echo_msg = "echo!";
w = zt.write(fd, echo_msg.getBytes(), echo_msg.length());
w = zt.write(fd, echo_msg.getBytes());
rxBuffer = new byte[100];
lengthToRead = 100;
System.out.println("reading bytes...");
r = zt.read(fd, rxBuffer, lengthToRead);
r = zt.read(fd, rxBuffer);
System.out.println("r="+r);
System.out.println("string="+new String(rxBuffer));
}
@@ -174,12 +170,12 @@ public class ExampleApp {
rxBuffer = new byte[100];
lengthToRead = 100;
System.out.println("reading bytes...");
r = zt.read(client_fd, rxBuffer, lengthToRead);
r = zt.read(client_fd, rxBuffer);
System.out.println("r="+r);
System.out.println("string="+new String(rxBuffer));
System.out.println("writing bytes...");
String echo_msg = "echo!";
w = zt.write(client_fd, echo_msg.getBytes(), echo_msg.length());
w = zt.write(client_fd, echo_msg.getBytes());
System.out.println("wrote (" + w + ") bytes");
}
}
@@ -204,14 +200,14 @@ public class ExampleApp {
if (loop) {
while (true) {
sleep(500);
if ((w = zt.sendto(fd, txBuffer, txBuffer.length, flags, remoteAddr)) < 0) {
if ((w = zt.sendto(fd, txBuffer, flags, remoteAddr)) < 0) {
System.out.println("error sending bytes");
} else {
System.out.println("sendto()=" + w);
}
}
} else {
if ((w = zt.sendto(fd, txBuffer, txBuffer.length, flags, remoteAddr)) < 0) {
if ((w = zt.sendto(fd, txBuffer, flags, remoteAddr)) < 0) {
System.out.println("error sending bytes");
} else {
System.out.println("sendto()=" + w);
@@ -252,7 +248,7 @@ public class ExampleApp {
if (master_set.ISSET(i))
{
System.out.println("attempting to read from: " + i);
r = zt.recvfrom(fd, rxBuffer, rxBuffer.length, flags, remoteAddr);
r = zt.recvfrom(fd, rxBuffer, flags, remoteAddr);
System.out.println("read (" + r + ") bytes from " + remoteAddr.toString() + ", buffer = " + new String(rxBuffer));
}
}
@@ -263,7 +259,7 @@ public class ExampleApp {
{
while(true) {
addr = new ZTSocketAddress();
r = zt.recvfrom(fd, rxBuffer, rxBuffer.length, flags, remoteAddr);
r = zt.recvfrom(fd, rxBuffer, flags, remoteAddr);
System.out.println("read (" + r + ") bytes from " + remoteAddr.toString() + ", buffer = " + new String(rxBuffer));
}
}