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

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

View File

@ -9,7 +9,6 @@ import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
@ -101,7 +100,7 @@ public class NetworkServiceManager implements Application.ActivityLifecycleCallb
intent.putExtra(NetworkService.KEY_USERNAME, mUserName)
.putExtra(NetworkService.KEY_PASSWORD, mPassword)
.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_ENABLE_LATENCY_VERIFIER, mVerifyLatency);
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 java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import cy.agorise.graphenej.api.ApiAccess;
import cy.agorise.graphenej.api.android.NetworkServiceManager;
@ -18,11 +22,20 @@ public class SampleApplication extends Application {
// credentials and the requested API accesses
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()
.setUserName("username")
.setPassword("secret")
.setRequestedApis(requestedApis)
.setCustomNodeUrls("wss://eu.nodes.bitshares.ws")
.setCustomNodeUrls(nodes)
.setAutoConnect(true)
.setNodeLatencyVerification(true)
.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
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();
}
}