Moved the node list information out of the library, this must be provided by the application now

This commit is contained in:
Nelson R. Perez 2018-10-18 12:14:05 -05:00
parent d75957e5d1
commit 3a19808ac5
4 changed files with 53 additions and 45 deletions

View file

@ -3,6 +3,7 @@ package cy.agorise.graphenej.api.android;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.os.Binder; import android.os.Binder;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
@ -15,9 +16,9 @@ import com.google.gson.reflect.TypeToken;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.MissingResourceException;
import cy.agorise.graphenej.Asset; import cy.agorise.graphenej.Asset;
import cy.agorise.graphenej.AssetAmount; import cy.agorise.graphenej.AssetAmount;
@ -29,7 +30,6 @@ import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.api.ApiAccess; import cy.agorise.graphenej.api.ApiAccess;
import cy.agorise.graphenej.api.ConnectionStatusUpdate; import cy.agorise.graphenej.api.ConnectionStatusUpdate;
import cy.agorise.graphenej.api.bitshares.Nodes;
import cy.agorise.graphenej.api.calls.ApiCallable; import cy.agorise.graphenej.api.calls.ApiCallable;
import cy.agorise.graphenej.api.calls.GetAccounts; import cy.agorise.graphenej.api.calls.GetAccounts;
import cy.agorise.graphenej.api.calls.GetFullAccounts; import cy.agorise.graphenej.api.calls.GetFullAccounts;
@ -90,19 +90,20 @@ public class NetworkService extends Service {
public static final String KEY_ENABLE_LATENCY_VERIFIER = "key_enable_latency_verifier"; public static final String KEY_ENABLE_LATENCY_VERIFIER = "key_enable_latency_verifier";
/** /**
* Shared preference * Key used to pass via intent a boolean extra to specify whether the connection should
* be automatically established.
*/ */
public static final String KEY_AUTO_CONNECT = "key_auto_connect"; public static final String KEY_AUTO_CONNECT = "key_auto_connect";
/** /**
* Constant used to pass a custom list of node URLs. This should be a simple * Key used to pass via intent a list of node URLs. The value passed should be a String
* comma separated list of URLs. * containing a simple comma separated list of URLs.
* *
* For example: * For example:
* *
* wss://domain1.com/ws,wss://domain2.com/ws,wss://domain3.com/ws * wss://domain1.com/ws,wss://domain2.com/ws,wss://domain3.com/ws
*/ */
public static final String KEY_CUSTOM_NODE_URLS = "key_custom_node_urls"; public static final String KEY_NODE_URLS = "key_node_urls";
private final IBinder mBinder = new LocalBinder(); private final IBinder mBinder = new LocalBinder();
@ -231,33 +232,26 @@ public class NetworkService extends Service {
@Nullable @Nullable
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
Bundle extras = intent.getExtras();
// Retrieving credentials and requested API data from the shared preferences // Retrieving credentials and requested API data from the shared preferences
mUsername = intent.getStringExtra(NetworkService.KEY_USERNAME); mUsername = extras.getString(NetworkService.KEY_USERNAME, "");
mPassword = intent.getStringExtra(NetworkService.KEY_PASSWORD); mPassword = extras.getString(NetworkService.KEY_PASSWORD, "");
mRequestedApis = intent.getIntExtra(NetworkService.KEY_REQUESTED_APIS, 0); mRequestedApis = extras.getInt(NetworkService.KEY_REQUESTED_APIS, 0);
mAutoConnect = intent.getBooleanExtra(NetworkService.KEY_AUTO_CONNECT, true); mAutoConnect = extras.getBoolean(NetworkService.KEY_AUTO_CONNECT, true);
boolean verifyNodeLatency = intent.getBooleanExtra(NetworkService.KEY_ENABLE_LATENCY_VERIFIER, false); boolean verifyNodeLatency = extras.getBoolean(NetworkService.KEY_ENABLE_LATENCY_VERIFIER, false);
ArrayList<String> nodeUrls = new ArrayList<>();
// If the user of the library desires, a custom list of node URLs can // If the user of the library desires, a custom list of node URLs can
// be passed using the KEY_CUSTOM_NODE_URLS constant // be passed using the KEY_NODE_URLS constant
String customNodeUrls = intent.getStringExtra(NetworkService.KEY_CUSTOM_NODE_URLS); String nodeURLStr = extras.getString(NetworkService.KEY_NODE_URLS, "");
if(nodeURLStr.equals("")){
// Adding user-provided list of node URLs first throw new MissingResourceException("A comma-separated list of node URLs must be provided as an intent extra", String.class.getName(), NetworkService.KEY_NODE_URLS);
if(customNodeUrls != null){
String[] urls = customNodeUrls.split(",");
ArrayList<String> urlList = new ArrayList<>(Arrays.asList(urls));
nodeUrls.addAll(urlList);
} }
// Adding the library-provided list of nodes that are not repeated // Adding user-provided list of node URLs
for(String node : Nodes.NODE_URLS) { String[] urls = nodeURLStr.split(",");
if(!nodeUrls.contains(node))
nodeUrls.add(node);
}
// Feeding all node information to the NodeProvider instance // Feeding all node information to the NodeProvider instance
for(String nodeUrl : nodeUrls){ for(String nodeUrl : urls){
nodeProvider.addNode(new FullNode(nodeUrl)); nodeProvider.addNode(new FullNode(nodeUrl));
} }
@ -271,7 +265,7 @@ public class NetworkService extends Service {
// best node. // best node.
if(verifyNodeLatency){ if(verifyNodeLatency){
ArrayList<FullNode> fullNodes = new ArrayList<>(); ArrayList<FullNode> fullNodes = new ArrayList<>();
for(String url : nodeUrls){ for(String url : urls){
fullNodes.add(new FullNode(url)); fullNodes.add(new FullNode(url));
} }
nodeLatencyVerifier = new NodeLatencyVerifier(fullNodes); nodeLatencyVerifier = new NodeLatencyVerifier(fullNodes);

View file

@ -9,7 +9,6 @@ import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
@ -101,7 +100,7 @@ public class NetworkServiceManager implements Application.ActivityLifecycleCallb
intent.putExtra(NetworkService.KEY_USERNAME, mUserName) intent.putExtra(NetworkService.KEY_USERNAME, mUserName)
.putExtra(NetworkService.KEY_PASSWORD, mPassword) .putExtra(NetworkService.KEY_PASSWORD, mPassword)
.putExtra(NetworkService.KEY_REQUESTED_APIS, mRequestedApis) .putExtra(NetworkService.KEY_REQUESTED_APIS, mRequestedApis)
.putExtra(NetworkService.KEY_CUSTOM_NODE_URLS, customNodes) .putExtra(NetworkService.KEY_NODE_URLS, customNodes)
.putExtra(NetworkService.KEY_AUTO_CONNECT, mAutoConnect) .putExtra(NetworkService.KEY_AUTO_CONNECT, mAutoConnect)
.putExtra(NetworkService.KEY_ENABLE_LATENCY_VERIFIER, mVerifyLatency); .putExtra(NetworkService.KEY_ENABLE_LATENCY_VERIFIER, mVerifyLatency);
context.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); context.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

View file

@ -1,15 +0,0 @@
package cy.agorise.graphenej.api.bitshares;
/**
* Known public nodes
*/
public class Nodes {
public static final String[] NODE_URLS = {
//"wss://dexnode.net/ws", // Dallas, USA TODO not working properly, verify
"wss://bitshares.crypto.fans/ws", // Munich, Germany
"wss://bitshares.openledger.info/ws", // Openledger node
"wss://us.nodes.bitshares.ws",
"wss://eu.nodes.bitshares.ws"
};
}

View file

@ -2,6 +2,10 @@ package cy.agorise.labs.sample;
import android.app.Application; import android.app.Application;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import cy.agorise.graphenej.api.ApiAccess; import cy.agorise.graphenej.api.ApiAccess;
import cy.agorise.graphenej.api.android.NetworkServiceManager; import cy.agorise.graphenej.api.android.NetworkServiceManager;
@ -18,11 +22,20 @@ public class SampleApplication extends Application {
// credentials and the requested API accesses // credentials and the requested API accesses
int requestedApis = ApiAccess.API_DATABASE | ApiAccess.API_HISTORY | ApiAccess.API_NETWORK_BROADCAST; int requestedApis = ApiAccess.API_DATABASE | ApiAccess.API_HISTORY | ApiAccess.API_NETWORK_BROADCAST;
String[] nodeURLs = new String[]{
"wss://bitshares.crypto.fans/ws",
"wss://bitshares.openledger.info/ws",
"wss://us.nodes.bitshares.ws",
"wss://eu.nodes.bitshares.ws"
};
List<String> nodeList = Arrays.asList(nodeURLs);
String nodes = join(nodeList, ",");
NetworkServiceManager networkManager = new NetworkServiceManager.Builder() NetworkServiceManager networkManager = new NetworkServiceManager.Builder()
.setUserName("username") .setUserName("username")
.setPassword("secret") .setPassword("secret")
.setRequestedApis(requestedApis) .setRequestedApis(requestedApis)
.setCustomNodeUrls("wss://eu.nodes.bitshares.ws") .setCustomNodeUrls(nodes)
.setAutoConnect(true) .setAutoConnect(true)
.setNodeLatencyVerification(true) .setNodeLatencyVerification(true)
.build(this); .build(this);
@ -31,4 +44,21 @@ public class SampleApplication extends Application {
// better estimate when the user has left the app and it is safe to disconnect the websocket connection // better estimate when the user has left the app and it is safe to disconnect the websocket connection
registerActivityLifecycleCallbacks(networkManager); registerActivityLifecycleCallbacks(networkManager);
} }
/**
* Private method used to join a sequence of Strings given a iterable representation
* and a delimiter.
*
*
* @param s Any collection of CharSequence that implements the Iterable interface.
* @param delimiter The delimiter which will be used to join the different strings together.
* @return A single string combining all the iterable pieces with the delimiter.
*/
private String join(Iterable<? extends CharSequence> s, String delimiter) {
Iterator<? extends CharSequence> iter = s.iterator();
if (!iter.hasNext()) return "";
StringBuilder buffer = new StringBuilder(iter.next());
while (iter.hasNext()) buffer.append(delimiter).append(iter.next());
return buffer.toString();
}
} }