Skip to content

Commit

Permalink
<feat>(client): add get support configs interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay committed Jun 11, 2024
1 parent 6a7b902 commit 9cb825c
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ext {
// integrationTest.mustRunAfter test
allprojects {
group = 'org.fisco-bcos.java-sdk'
version = '3.7.0'
version = '3.8.0-SNAPSHOT'

apply plugin: 'maven-publish'
apply plugin: 'idea'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -92,6 +94,17 @@ public void testClient() {
System.out.println(
"New block, group: " + groupId + ", blockNumber: " + blockNumber));

Map<String, Optional<SystemConfig>> systemConfigList = client.getSystemConfigList();
Assert.assertFalse(systemConfigList.isEmpty());
systemConfigList.forEach(
(key, value) ->
System.out.println(
key
+ " : "
+ (value.isPresent()
? value.get().getSystemConfig()
: "null")));

// test getBlockNumber
BlockNumber blockNumber = client.getBlockNumber();
Assert.assertTrue(blockNumber.getBlockNumber().compareTo(BigInteger.ZERO) >= 0);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package org.fisco.bcos.sdk.v3.client;

import java.math.BigInteger;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.fisco.bcos.sdk.jni.BcosSDKJniObj;
import org.fisco.bcos.sdk.v3.client.protocol.request.Transaction;
import org.fisco.bcos.sdk.v3.client.protocol.response.Abi;
Expand Down Expand Up @@ -840,6 +843,13 @@ void getTransactionReceiptAsync(
*/
SystemConfig getSystemConfigByKey(String node, String key);

/**
* Peer operation: get system config list, witch will fetch all config
*
* @return all config value
*/
Map<String, Optional<SystemConfig>> getSystemConfigList();

/**
* Peer operation: async get system config
*
Expand All @@ -857,6 +867,20 @@ void getTransactionReceiptAsync(
*/
void getSystemConfigByKeyAsync(String node, String key, RespCallback<SystemConfig> callback);

/**
* async get all connect nodes support keys
*
* @param callback the callback instance
*/
void getSupportSysConfigKeysAsync(RespCallback<Set<String>> callback);

/**
* Peer operation: async get system config list, witch will fetch all config
*
* @param callback the callback instance
*/
void getSystemConfigListAsync(RespCallback<Map<String, Optional<SystemConfig>>> callback);

/**
* Peer operation: get sync status
*
Expand Down
116 changes: 115 additions & 1 deletion src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.fisco.bcos.sdk.jni.BcosSDKJniObj;
import org.fisco.bcos.sdk.jni.rpc.RpcJniObj;
import org.fisco.bcos.sdk.v3.client.exceptions.ClientException;
Expand Down Expand Up @@ -1008,6 +1015,32 @@ public SystemConfig getSystemConfigByKey(String node, String key) {
SystemConfig.class);
}

@Override
public Map<String, Optional<SystemConfig>> getSystemConfigList() {
CompletableFuture<Map<String, Optional<SystemConfig>>> future = new CompletableFuture<>();
this.getSystemConfigListAsync(
new RespCallback<Map<String, Optional<SystemConfig>>>() {
@Override
public void onResponse(Map<String, Optional<SystemConfig>> configMap) {
future.complete(configMap);
}

@Override
public void onError(Response errorResponse) {
future.completeExceptionally(
new ClientException(
"getSystemConfigList failed, error: "
+ errorResponse.getErrorMessage()));
}
});
try {
return future.get(configOption.getNetworkConfig().getTimeout(), TimeUnit.MILLISECONDS);
} catch (Exception e) {
logger.warn("getSystemConfigList failed, error: {}", e.getMessage(), e);
throw new ClientException("getSystemConfigList failed, error: " + e.getMessage(), e);
}
}

@Override
public void getSystemConfigByKeyAsync(String key, RespCallback<SystemConfig> callback) {
this.getSystemConfigByKeyAsync(nodeToSendRequest, key, callback);
Expand All @@ -1026,6 +1059,80 @@ public void getSystemConfigByKeyAsync(
callback);
}

@Override
public void getSupportSysConfigKeysAsync(RespCallback<Set<String>> callback) {
this.getGroupInfoListAsync(
new RespCallback<BcosGroupInfoList>() {
@Override
public void onResponse(BcosGroupInfoList bcosGroupInfoList) {
Optional<BcosGroupInfo.GroupInfo> group =
bcosGroupInfoList.getResult().stream()
.filter(gInfo -> gInfo.getGroupID().equals(getGroup()))
.findFirst();
Set<String> keys = new TreeSet<>();
if (group.isPresent() && !group.get().getNodeList().isEmpty()) {
group.get()
.getNodeList()
.forEach(
groupNodeInfo -> {
keys.addAll(groupNodeInfo.getFeatureKeys());
keys.addAll(groupNodeInfo.getSupportConfigs());
});
}
callback.onResponse(keys);
}

@Override
public void onError(Response errorResponse) {
callback.onError(errorResponse);
}
});
}

@Override
public void getSystemConfigListAsync(
RespCallback<Map<String, Optional<SystemConfig>>> callback) {

this.getSupportSysConfigKeysAsync(
new RespCallback<Set<String>>() {
@Override
public void onResponse(Set<String> keys) {
Map<String, Optional<SystemConfig>> configMap =
new ConcurrentSkipListMap<>();
keys.forEach(
key ->
getSystemConfigByKeyAsync(
key,
new RespCallback<SystemConfig>() {
@Override
public void onResponse(
SystemConfig systemConfig) {
configMap.put(
key,
Optional.ofNullable(systemConfig));
if (configMap.size() == keys.size()) {
callback.onResponse(configMap);
}
}

@Override
public void onError(Response errorResponse) {
// maybe not exist
configMap.put(key, Optional.empty());
if (configMap.size() == keys.size()) {
callback.onResponse(configMap);
}
}
}));
}

@Override
public void onError(Response errorResponse) {
callback.onError(errorResponse);
}
});
}

@Override
public SyncStatus getSyncStatus(String node) {
node = Objects.isNull(node) ? "" : node;
Expand Down Expand Up @@ -1130,7 +1237,8 @@ public BcosGroupInfo getGroupInfo() {

future.complete(response);
});
Response response = future.get();
Response response =
future.get(configOption.getNetworkConfig().getTimeout(), TimeUnit.MILLISECONDS);
return ClientImpl.parseResponseIntoJsonRpcResponse(
JsonRpcMethods.GET_GROUP_INFO, response, BcosGroupInfo.class);
} catch (ClientException e) {
Expand All @@ -1146,6 +1254,12 @@ public BcosGroupInfo getGroupInfo() {
"getGroupInfo failed for decode the message exception, error message:"
+ e.getMessage(),
e);
} catch (TimeoutException e) {
logger.error("e: ", e);
throw new ClientException(
"getGroupInfo failed for get group info timeout, error message:"
+ e.getMessage(),
e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.fisco.bcos.sdk.v3.client.protocol.model.GroupNodeIniInfo;
import org.fisco.bcos.sdk.v3.model.JsonRpcResponse;
Expand Down Expand Up @@ -80,7 +81,8 @@ public static class GroupNodeInfo {
private String name;
private List<ServiceInfo> serviceInfoList;
private Protocol protocol;
private List<String> featureKeys;
private List<String> featureKeys = new ArrayList<>();
private List<String> supportConfigs = new ArrayList<>();

@Override
public String toString() {
Expand Down Expand Up @@ -150,6 +152,14 @@ public void setFeatureKeys(List<String> featureKeys) {
this.featureKeys = featureKeys;
}

public List<String> getSupportConfigs() {
return supportConfigs;
}

public void setSupportConfigs(List<String> supportConfigs) {
this.supportConfigs = supportConfigs;
}

static class ServiceInfo {
private String serviceName;
private int type;
Expand Down

0 comments on commit 9cb825c

Please sign in to comment.