Merge branch 'develop' of github.com:Agorise/graphenej into develop
This commit is contained in:
commit
a5f0ba1345
2 changed files with 38 additions and 29 deletions
|
@ -2,6 +2,7 @@ package cy.agorise.graphenej.api.android;
|
||||||
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
@ -210,7 +211,10 @@ public class NetworkService extends Service {
|
||||||
* Actually establishes a connection from this Service to one of the full nodes.
|
* Actually establishes a connection from this Service to one of the full nodes.
|
||||||
*/
|
*/
|
||||||
public void connect(){
|
public void connect(){
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient
|
||||||
|
.Builder()
|
||||||
|
.connectTimeout(2, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
mSelectedNode = nodeProvider.getBestNode();
|
mSelectedNode = nodeProvider.getBestNode();
|
||||||
Log.v(TAG,"connect.url: "+ mSelectedNode.getUrl());
|
Log.v(TAG,"connect.url: "+ mSelectedNode.getUrl());
|
||||||
Request request = new Request.Builder().url(mSelectedNode.getUrl()).build();
|
Request request = new Request.Builder().url(mSelectedNode.getUrl()).build();
|
||||||
|
|
|
@ -102,11 +102,10 @@ public class NodeLatencyVerifier {
|
||||||
}
|
}
|
||||||
|
|
||||||
String normalURL = fullNode.getUrl().replace("wss://", "https://");
|
String normalURL = fullNode.getUrl().replace("wss://", "https://");
|
||||||
if(!nodeURLMap.containsKey(fullNode.getUrl().replace("wss://", "https://"))){
|
HttpUrl key = HttpUrl.parse(normalURL);
|
||||||
HttpUrl key = HttpUrl.parse(normalURL);
|
if(!nodeURLMap.containsKey(key)){
|
||||||
nodeURLMap.put(key, fullNode);
|
nodeURLMap.put(key, fullNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
client.newWebSocket(request, mWebSocketListener);
|
client.newWebSocket(request, mWebSocketListener);
|
||||||
}
|
}
|
||||||
mHandler.postDelayed(this, verificationPeriod);
|
mHandler.postDelayed(this, verificationPeriod);
|
||||||
|
@ -133,37 +132,43 @@ public class NodeLatencyVerifier {
|
||||||
* Method used to handle the node's first response. The idea here is to obtain
|
* Method used to handle the node's first response. The idea here is to obtain
|
||||||
* the RTT (Round Trip Time) measurement and publish it using the PublishSubject.
|
* the RTT (Round Trip Time) measurement and publish it using the PublishSubject.
|
||||||
*
|
*
|
||||||
* @param webSocket Websocket instance
|
* @param webSocket WebSocket instance
|
||||||
* @param response Response instance
|
* @param response Response instance
|
||||||
*/
|
*/
|
||||||
private void handleResponse(WebSocket webSocket, Response response){
|
private void handleResponse(WebSocket webSocket, Response response){
|
||||||
// Obtaining the HttpUrl instance that was previously used as a key
|
synchronized (this){
|
||||||
HttpUrl url = webSocket.request().url();
|
// Obtaining the HttpUrl instance that was previously used as a key
|
||||||
if(nodeURLMap.containsKey(url)){
|
HttpUrl url = webSocket.request().url();
|
||||||
FullNode fullNode = nodeURLMap.get(url);
|
if(nodeURLMap.containsKey(url)){
|
||||||
long delay;
|
FullNode fullNode = nodeURLMap.get(url);
|
||||||
|
long delay;
|
||||||
|
|
||||||
if(response == null) {
|
if(response == null) {
|
||||||
// There is no internet connection, or the node is unreachable. We are just
|
// There is no internet connection, or the node is unreachable. We are just
|
||||||
// putting an artificial delay.
|
// putting an artificial delay.
|
||||||
delay = Long.MAX_VALUE;
|
delay = Long.MAX_VALUE;
|
||||||
} else {
|
} else {
|
||||||
long after = System.currentTimeMillis();
|
long after = System.currentTimeMillis();
|
||||||
long before = timestamps.get(fullNode);
|
long before = timestamps.get(fullNode);
|
||||||
delay = after - before;
|
delay = after - before;
|
||||||
}
|
}
|
||||||
fullNode.addLatencyValue(delay);
|
if(fullNode != null){
|
||||||
subject.onNext(fullNode);
|
fullNode.addLatencyValue(delay);
|
||||||
}else{
|
subject.onNext(fullNode);
|
||||||
// We cannot properly handle a response to a request whose
|
}else{
|
||||||
// URL was not registered at the nodeURLMap. This is because without this,
|
Log.w(TAG,"Could not extract FullNode instance from the map");
|
||||||
// we cannot know to which node this response corresponds. This should not happen.
|
}
|
||||||
Log.e(TAG,"nodeURLMap does not contain url: "+url);
|
}else{
|
||||||
for(HttpUrl key : nodeURLMap.keySet()){
|
// We cannot properly handle a response to a request whose
|
||||||
Log.e(TAG,"> "+key);
|
// URL was not registered at the nodeURLMap. This is because without this,
|
||||||
|
// we cannot know to which node this response corresponds. This should not happen.
|
||||||
|
Log.e(TAG,"nodeURLMap does not contain url: "+url);
|
||||||
|
for(HttpUrl key : nodeURLMap.keySet()){
|
||||||
|
Log.e(TAG,"> "+key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
webSocket.close(NetworkService.NORMAL_CLOSURE_STATUS, null);
|
||||||
}
|
}
|
||||||
webSocket.close(NetworkService.NORMAL_CLOSURE_STATUS, null);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue