-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from btdex/feat/dynamic-remote-resources
feat: added remote resources, i.e. pools, nodes
- Loading branch information
Showing
6 changed files
with
365 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package btdex.core; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class RemoteSignumResources { | ||
static RemoteSignumResources instance; | ||
|
||
private final String baseURL = Constants.REMOTE_NODE_RESOURCES_URL; | ||
private final OkHttpClient httpClient = new OkHttpClient(); | ||
|
||
private Set<String> testnetNodes = new HashSet<>(); | ||
private Set<String> mainnetNodes = new HashSet<>(); | ||
private Set<String> testnetPools = new HashSet<>(); | ||
private Set<String> mainnetPools = new HashSet<>(); | ||
|
||
private Logger logger; | ||
|
||
public static RemoteSignumResources getInstance() { | ||
if (instance == null) | ||
instance = new RemoteSignumResources(); | ||
return instance; | ||
} | ||
|
||
private RemoteSignumResources() { | ||
this.logger = LogManager.getLogger(); | ||
} | ||
|
||
public RemoteSignumResources loadNodeLists() throws IOException { | ||
try { | ||
logger.debug("Fetching remote node list from {}...", this.baseURL); | ||
Request request = new Request | ||
.Builder() | ||
.url(this.baseURL + "/nodes.json") | ||
.addHeader("Accept", "application/json") | ||
.build(); | ||
Response response = this.httpClient.newCall(request).execute(); | ||
|
||
assert response.code() == 200; | ||
assert response.body() != null; | ||
|
||
String body = response.body().string(); | ||
JsonObject json = JsonParser.parseString(body).getAsJsonObject(); | ||
json.get("mainnet").getAsJsonArray().forEach(node -> { | ||
this.mainnetNodes.add(node.getAsJsonObject().get("url").getAsString()); | ||
}); | ||
json.get("testnet").getAsJsonArray().forEach(node -> { | ||
this.testnetNodes.add(node.getAsJsonObject().get("url").getAsString()); | ||
}); | ||
}catch(Exception e){ | ||
logger.error(e); | ||
logger.debug("Loading Remote node list failed. Using constant fallbacks"); | ||
this.mainnetNodes.addAll(Arrays.asList(Constants.NODE_LIST)); | ||
this.testnetNodes.addAll(Arrays.asList(Constants.NODE_LIST_TESTNET)); | ||
} | ||
return this; | ||
} | ||
public RemoteSignumResources loadPoolLists() throws IOException { | ||
try { | ||
logger.debug("Fetching remote pool list from {}...", this.baseURL); | ||
Request request = new Request | ||
.Builder() | ||
.url(this.baseURL + "/pools.json") | ||
.addHeader("Accept", "application/json") | ||
.build(); | ||
Response response = this.httpClient.newCall(request).execute(); | ||
|
||
assert response.code() == 200; | ||
assert response.body() != null; | ||
|
||
String body = response.body().string(); | ||
JsonObject json = JsonParser.parseString(body).getAsJsonObject(); | ||
json.get("mainnet").getAsJsonArray().forEach(node -> { | ||
this.mainnetPools.add(node.getAsJsonObject().get("url").getAsString()); | ||
}); | ||
json.get("testnet").getAsJsonArray().forEach(node -> { | ||
this.testnetPools.add(node.getAsJsonObject().get("url").getAsString()); | ||
}); | ||
}catch(Exception e){ | ||
logger.error(e); | ||
logger.debug("Loading Remote pool list failed. Using constant fallbacks"); | ||
this.mainnetPools.addAll(Arrays.asList(Constants.POOL_LIST)); | ||
this.testnetPools.addAll(Arrays.asList(Constants.POOL_LIST_TESTNET)); | ||
} | ||
return this; | ||
} | ||
|
||
public Set<String> testNetNodes() { | ||
return this.testnetNodes; | ||
} | ||
public Set<String> mainNetNodes() { | ||
return this.mainnetNodes; | ||
} | ||
|
||
public Set<String> testNetPools() { | ||
return this.testnetPools; | ||
} | ||
public Set<String> mainNetPools() { | ||
return this.mainnetPools; | ||
} | ||
} |
Oops, something went wrong.
d6d30bf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jjos2372 can we get a beta build release of this?