Re-work of thread model
This commit is contained in:
17
examples/android/ExampleAndroidApp/.project
Normal file
17
examples/android/ExampleAndroidApp/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>ExampleAndroidApp</name>
|
||||
<comment>Project ExampleAndroidApp created by Buildship.</comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
connection.project.dir=
|
||||
eclipse.preferences.version=1
|
||||
6
examples/android/ExampleAndroidApp/app/.classpath
Normal file
6
examples/android/ExampleAndroidApp/app/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-9/"/>
|
||||
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
|
||||
<classpathentry kind="output" path="bin/default"/>
|
||||
</classpath>
|
||||
23
examples/android/ExampleAndroidApp/app/.project
Normal file
23
examples/android/ExampleAndroidApp/app/.project
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>app</name>
|
||||
<comment>Project app created by Buildship.</comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
connection.project.dir=..
|
||||
eclipse.preferences.version=1
|
||||
@@ -20,15 +20,25 @@ android {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
release {
|
||||
debuggable true
|
||||
jniDebuggable true
|
||||
minifyEnabled false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.squareup.picasso:picasso:2.71828'
|
||||
implementation files('libs/libzt-android-debug.aar')
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
|
||||
implementation("com.squareup.okhttp3:okhttp:3.12.0")
|
||||
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||
|
||||
implementation 'com.github.bumptech.glide:glide:4.6.1'
|
||||
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,79 @@
|
||||
package com.example.exampleandroidapp;
|
||||
|
||||
import com.zerotier.libzt.ZeroTierSocketFactory;
|
||||
import com.zerotier.libzt.ZeroTier;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class HTTPWorker extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long tid = Thread.currentThread().getId();
|
||||
// Test: Perform randomly-delayed HTTP GET requests
|
||||
if (true) {
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
builder.socketFactory(new ZeroTierSocketFactory());
|
||||
OkHttpClient client = builder.build();
|
||||
Request request1 = new Request.Builder()
|
||||
.url("http://11.7.7.223:80/warandpeace.txt")
|
||||
.build();
|
||||
Request request2 = new Request.Builder()
|
||||
.url("http://11.7.7.223:8082/pumpkin.jpg")
|
||||
.build();
|
||||
RequestBody formBody = new FormBody.Builder()
|
||||
.add("message", "Your message")
|
||||
.build();
|
||||
Request request3 = new Request.Builder()
|
||||
.url("http://11.7.7.223:8082/")
|
||||
.post(formBody)
|
||||
.build();
|
||||
|
||||
long i = 0;
|
||||
for (;;) {
|
||||
try {
|
||||
System.out.println("PEER STATUS IS = " + ZeroTier.get_peer_status(0x7caea82fc4L));
|
||||
int randomNum = ThreadLocalRandom.current().nextInt(0, 2 + 1);
|
||||
i++;
|
||||
Response response = null;
|
||||
if (randomNum == 0) {
|
||||
response = client.newCall(request1).execute();
|
||||
}
|
||||
if (randomNum == 1) {
|
||||
//response = client.newCall(request2).execute();
|
||||
response = client.newCall(request1).execute();
|
||||
}
|
||||
if (randomNum == 2) {
|
||||
//response = client.newCall(request3).execute();
|
||||
response = client.newCall(request1).execute();
|
||||
//System.out.println(tid+"::POST");
|
||||
//continue;
|
||||
}
|
||||
InputStream in = response.body().byteStream();
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
int nRead;
|
||||
byte[] data = new byte[16384];
|
||||
while ((nRead = in.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, nRead);
|
||||
}
|
||||
System.out.println(tid+"::GET: i="+i+", len="+buffer.toByteArray().length);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
//System.exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,103 +1,442 @@
|
||||
package com.example.exampleandroidapp;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.*;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.FormBody;
|
||||
|
||||
import com.zerotier.libzt.ZeroTier;
|
||||
import com.zerotier.libzt.ZTSocketAddress;
|
||||
import com.zerotier.libzt.ZTFDSet;
|
||||
import com.zerotier.libzt.ZeroTierSocket;
|
||||
import com.zerotier.libzt.ZeroTierSocketFactory;
|
||||
import com.zerotier.libzt.ZeroTierSSLSocketFactory;
|
||||
//import com.zerotier.libzt.ZeroTierEventListener;
|
||||
import com.zerotier.libzt.ZeroTierSocketImplFactory;
|
||||
import com.example.exampleandroidapp.MyZeroTierEventListener;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
//import java.net.HttpURLConnection;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.net.URL;
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import com.squareup.picasso.Picasso;
|
||||
import com.squareup.picasso.Callback;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.RequestBuilder;
|
||||
import com.bumptech.glide.RequestManager;
|
||||
import android.widget.ImageView;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
String requestURL = "http://11.7.7.223:80/";
|
||||
|
||||
// Used to load the 'native-lib' library on application startup.
|
||||
static {
|
||||
System.loadLibrary("zt-shared");
|
||||
private static String createDataSize(int msgSize) {
|
||||
StringBuilder sb = new StringBuilder(msgSize);
|
||||
for (int i=0; i<msgSize; i++) {
|
||||
sb.append('a');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/*
|
||||
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
|
||||
String HTTP_POST(String url, String json) throws IOException {
|
||||
RequestBody body = RequestBody.create(JSON, json);
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
builder.socketFactory(new ZeroTierSocketFactory());
|
||||
OkHttpClient client = builder.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
.build();
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
return response.body().string();
|
||||
}
|
||||
}
|
||||
|
||||
void HTTP_GET() throws IOException {
|
||||
}
|
||||
|
||||
String sampleJson(String player1, String player2) {
|
||||
return "{'winCondition':'HIGH_SCORE',"
|
||||
+ "'name':'Bowling',"
|
||||
+ "'round':4,"
|
||||
+ "'lastSaved':1367702411696,"
|
||||
+ "'dateStarted':1367702378785,"
|
||||
+ "'players':["
|
||||
+ "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39},"
|
||||
+ "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}"
|
||||
+ "]}";
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
final ZeroTier zt = new ZeroTier();
|
||||
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
final String path = getApplicationContext().getFilesDir() + "/zerotier";
|
||||
long nwid = 0xe42a7f55c2b9be61L;
|
||||
// WD TEST
|
||||
if (true) {
|
||||
System.out.println("Starting ZeroTier...");
|
||||
MyZeroTierEventListener listener = new MyZeroTierEventListener();
|
||||
ZeroTier.start(getApplicationContext().getFilesDir() + "/zerotier3", listener, 9994);
|
||||
|
||||
// Test modes
|
||||
boolean blocking_start_call = true;
|
||||
boolean client_mode = true;
|
||||
boolean tcp = false;
|
||||
boolean loop = true;
|
||||
|
||||
int fd = -1, client_fd = -1, err, r, w, length = 0, flags = 0;
|
||||
byte[] rxBuffer;
|
||||
byte[] txBuffer = "welcome to the machine".getBytes();
|
||||
String remoteAddrStr = "172.28.221.116";
|
||||
String localAddrStr = "1.2.3.4";
|
||||
int portNo = 4040;
|
||||
|
||||
ZTSocketAddress remoteAddr, localAddr;
|
||||
ZTSocketAddress sockname = new ZTSocketAddress();
|
||||
ZTSocketAddress addr = new ZTSocketAddress();
|
||||
|
||||
// METHOD 1 (easy)
|
||||
// Blocking call that waits for all components of the service to start
|
||||
System.out.println("Starting ZT service...");
|
||||
if (blocking_start_call) {
|
||||
zt.startjoin(path, nwid);
|
||||
}
|
||||
System.out.println("ZT service ready.");
|
||||
|
||||
// Device/Node address info
|
||||
System.out.println("path=" + zt.get_path());
|
||||
long nodeId = zt.get_node_id();
|
||||
System.out.println("nodeId=" + Long.toHexString(nodeId));
|
||||
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<numAddresses; i++) {
|
||||
zt.get_address_at_index(nwid, i, sockname);
|
||||
//System.out.println("address[" + i + "] = " + sockname.toString()); // ip:port
|
||||
System.out.println("address[" + i + "] = " + sockname.toCIDR());
|
||||
}
|
||||
|
||||
zt.get_6plane_addr(nwid, nodeId, sockname);
|
||||
System.out.println("6PLANE address = " + sockname.toCIDR());
|
||||
|
||||
System.out.println("mode:tcp");
|
||||
if ((fd = zt.socket(zt.AF_INET, zt.SOCK_STREAM, 0)) < 0) {
|
||||
System.out.println("error creating socket");
|
||||
return;
|
||||
}
|
||||
// CLIENT
|
||||
int lengthToRead;
|
||||
if (client_mode) {
|
||||
remoteAddr = new ZTSocketAddress(remoteAddrStr, portNo);
|
||||
if ((err = zt.connect(fd, remoteAddr)) < 0) {
|
||||
System.out.println("error connecting (err=" + err + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
int n = 20000;
|
||||
String in = "echo!";
|
||||
String echo_msg = "";
|
||||
for (int i = 0; i < n; i += 1) {
|
||||
echo_msg += in;
|
||||
}
|
||||
|
||||
w = zt.write(fd, echo_msg.getBytes());
|
||||
//rxBuffer = new byte[100];
|
||||
//lengthToRead = 100;
|
||||
System.out.println("w="+w);
|
||||
//System.out.println("reading bytes...");
|
||||
//r = zt.read(fd, rxBuffer, lengthToRead);
|
||||
//System.out.println("r="+r);
|
||||
//System.out.println("string="+new String(rxBuffer));
|
||||
while (listener.isOnline == false) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("joining network...");
|
||||
ZeroTier.join(0x0);
|
||||
System.out.println("waiting for callback");
|
||||
|
||||
while (listener.isNetworkReady == false) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
|
||||
System.out.println("Starting ZeroTier...");
|
||||
MyZeroTierEventListener listener = new MyZeroTierEventListener();
|
||||
ZeroTier.start(getApplicationContext().getFilesDir() + "/zerotier3", listener, 9994);
|
||||
//System.out.println("ZTSDK nodeId=" + Long.toHexString(ZeroTier.get_node_id()));
|
||||
|
||||
while (listener.isOnline == false) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("joining network...");
|
||||
ZeroTier.join(0x0);
|
||||
System.out.println("waiting for callback");
|
||||
|
||||
while (listener.isNetworkReady == false) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
// Start threads
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
for(int i = 0;i < 5;i++){
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (Exception e) {}
|
||||
HTTPWorker thread = new HTTPWorker();
|
||||
thread.start();
|
||||
threads.add(thread);
|
||||
}
|
||||
|
||||
System.out.println("sleeping...");
|
||||
try {
|
||||
Thread.sleep(60000000);
|
||||
} catch (Exception e) {}
|
||||
|
||||
System.exit(0);
|
||||
|
||||
// Test: setsockopt() and getsockopt() [non-exhaustive]
|
||||
/*
|
||||
if (true) {
|
||||
try {
|
||||
ZeroTierSocket zts = new ZeroTierSocket();
|
||||
zts.connect(new InetSocketAddress("11.7.7.223", 8082));
|
||||
zts.setSoTimeout(1337);
|
||||
int to = zts.getSoTimeout();
|
||||
assert to == 1337;
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Test: Perform randomly-delayed HTTP GET requests
|
||||
if (false) {
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
builder.socketFactory(new ZeroTierSocketFactory());
|
||||
OkHttpClient client = builder.build();
|
||||
Request request1 = new Request.Builder()
|
||||
.url("http://11.7.7.223:8082/")
|
||||
.build();
|
||||
|
||||
long i = 0;
|
||||
for (;;) {
|
||||
try {
|
||||
Thread.sleep((long)(Math.random() * 250));
|
||||
Response response = client.newCall(request1).execute();
|
||||
i++;
|
||||
System.out.println("GET: i="+i+", len="+response.toString().length());
|
||||
//response.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
System.exit(0);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Test: Perform randomly-delayed HTTP GET requests
|
||||
if (true) {
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
builder.socketFactory(new ZeroTierSocketFactory());
|
||||
OkHttpClient client = builder.build();
|
||||
Request request1 = new Request.Builder()
|
||||
.url("http://11.7.7.223:80/warandpeace.txt")
|
||||
.header("Transfer-Encoding", "chunked")
|
||||
//.header("Connection","close")
|
||||
.build();
|
||||
|
||||
Request request2 = new Request.Builder()
|
||||
.url("http://11.7.7.223:8082/pumpkin.jpg")
|
||||
.header("Transfer-Encoding", "chunked")
|
||||
.header("Connection","close")
|
||||
.build();
|
||||
|
||||
String postMessage = "what?";
|
||||
try {
|
||||
postMessage = new String(createDataSize(1024).getBytes(), "UTF8");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
okhttp3.RequestBody body = RequestBody.create(JSON, postMessage);
|
||||
|
||||
//RequestBody formBody = new FormBody.Builder()
|
||||
// .add("message", postMessage)
|
||||
// .build();
|
||||
Request request3 = new Request.Builder()
|
||||
.url("http://11.7.7.223:8082/")
|
||||
//.retryOnConnectionFailure(true)
|
||||
.header("Transfer-Encoding", "chunked")
|
||||
.header("Connection","close")
|
||||
//.header("Connection","keep-alive")
|
||||
.post(body)
|
||||
.build();
|
||||
|
||||
long i = 0;
|
||||
for (;;) {
|
||||
try {
|
||||
System.out.println("iteration="+i);
|
||||
int randomNum = ThreadLocalRandom.current().nextInt(0, 2 + 1);
|
||||
Thread.sleep(10);
|
||||
i++;
|
||||
Response response = null;
|
||||
if (randomNum == 0) {
|
||||
System.out.println("(0) GET: war");
|
||||
response = client.newCall(request1).execute();
|
||||
|
||||
}
|
||||
if (randomNum == 1) {
|
||||
System.out.println("(1) GET: pumpkin");
|
||||
response = client.newCall(request2).execute();
|
||||
|
||||
}
|
||||
if (randomNum == 2) {
|
||||
System.out.println("(2) POST: data");
|
||||
response = client.newCall(request3).execute();
|
||||
response.close();
|
||||
continue;
|
||||
}
|
||||
InputStream in = response.body().byteStream();
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
int nRead;
|
||||
byte[] data = new byte[16384];
|
||||
while ((nRead = in.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, nRead);
|
||||
}
|
||||
System.out.println("GET: i="+i+", len="+buffer.toByteArray().length);
|
||||
response.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Test: Perform randomly-delayed HTTP GET requests
|
||||
if (true) {
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
builder.socketFactory(new ZeroTierSocketFactory());
|
||||
OkHttpClient client = builder.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(requestURL+"warandpeace.txt")
|
||||
.build();
|
||||
|
||||
long i = 0;
|
||||
for (;;) {
|
||||
try {
|
||||
//Thread.sleep((long)(Math.random()) );
|
||||
//System.out.println("iteration="+i);
|
||||
i++;
|
||||
Response response = client.newCall(request).execute();
|
||||
//System.out.println(response.body().string());
|
||||
|
||||
InputStream in = response.body().byteStream();
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
int nRead;
|
||||
byte[] data = new byte[16384];
|
||||
while ((nRead = in.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, nRead);
|
||||
}
|
||||
|
||||
System.out.println("\tCOMPLETE, i="+i+", len="+buffer.toByteArray().length);
|
||||
|
||||
|
||||
/*
|
||||
//Bitmap bitmap = BitmapFactory.decodeByteArray(buffer.toByteArray(), 0, buffer.toByteArray().length);
|
||||
//ImageView imageView = (ImageView) findViewById(R.id.imageView);
|
||||
//imageView.setImageBitmap(bitmap);
|
||||
*/
|
||||
/*
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
|
||||
Bitmap bitmap=BitmapFactory.decodeStream(bufferedInputStream);
|
||||
ImageView imageView = (ImageView) findViewById(R.id.imageView);
|
||||
imageView.setImageBitmap(bitmap);
|
||||
*/
|
||||
|
||||
//
|
||||
// Thread.sleep(5000);
|
||||
//response.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println("EXCEPTION:"+e);
|
||||
//System.exit(0);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ...
|
||||
|
||||
// Test #2: HttpURLConnection
|
||||
if (false)
|
||||
{
|
||||
|
||||
|
||||
// Create a trust manager that does not validate certificate chains
|
||||
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
||||
}
|
||||
public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
SSLContext context = null;
|
||||
|
||||
// Set runtime default socket implementation factory
|
||||
try {
|
||||
//Socket.setSocketImplFactory(new ZeroTierSocketImplFactory());
|
||||
//SSLSocket.setSocketImplFactory(new ZeroTierSocketImplFactory());
|
||||
|
||||
context = SSLContext.getInstance("SSL");
|
||||
//context.init(null, null, null);
|
||||
context.init(null, trustAllCerts, new java.security.SecureRandom());
|
||||
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Create all-trusting host name verifier
|
||||
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
// Install the all-trusting host verifier
|
||||
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
|
||||
|
||||
|
||||
ZeroTierSSLSocketFactory customSocketFactory = new ZeroTierSSLSocketFactory(context.getSocketFactory());
|
||||
HttpsURLConnection.setDefaultSSLSocketFactory(customSocketFactory);
|
||||
|
||||
|
||||
|
||||
|
||||
// Test HTTP client
|
||||
String GET_URL = "https://10.6.6.152";
|
||||
String USER_AGENT = "Mozilla/5.0";
|
||||
try {
|
||||
URL obj = new URL(GET_URL);
|
||||
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
|
||||
|
||||
con.setSSLSocketFactory(customSocketFactory);
|
||||
|
||||
con.setRequestMethod("GET");
|
||||
con.setRequestProperty("User-Agent", USER_AGENT);
|
||||
System.out.println("neat?");
|
||||
int responseCode = con.getResponseCode();
|
||||
System.out.println("GET Response Code :: " + responseCode);
|
||||
if (responseCode == HttpsURLConnection.HTTP_OK)
|
||||
{
|
||||
// success
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer response = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
response.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
System.out.println(response.toString());
|
||||
} else {
|
||||
System.out.println("GET request failed");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
).start();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.example.exampleandroidapp;
|
||||
|
||||
import com.zerotier.libzt.ZeroTier;
|
||||
import com.zerotier.libzt.ZeroTierEventListener;
|
||||
|
||||
public 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(ZeroTier.get_node_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_REQUESTING_CONFIG) {
|
||||
// Waiting for network configuration
|
||||
System.out.println("EVENT_NETWORK_REQUESTING_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_P2P) {
|
||||
System.out.println("EVENT_PEER_P2P: id=" + Long.toHexString(id));
|
||||
}
|
||||
if (eventCode == ZeroTier.EVENT_PEER_RELAY) {
|
||||
System.out.println("EVENT_PEER_RELAY: id=" + Long.toHexString(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,4 +15,12 @@
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView"
|
||||
android:layout_width="136dp"
|
||||
android:layout_height="213dp"
|
||||
app:srcCompat="@android:color/transparent"
|
||||
tools:layout_editor_absoluteX="25dp"
|
||||
tools:layout_editor_absoluteY="16dp" />
|
||||
|
||||
</android.support.constraint.ConstraintLayout>
|
||||
Reference in New Issue
Block a user