Skip to content

Commit 8558fcf

Browse files
authored
jni interfaces update (#433)
1 parent 496df21 commit 8558fcf

File tree

8 files changed

+150
-98
lines changed

8 files changed

+150
-98
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ allprojects {
5050
apply plugin: 'signing'
5151

5252
configurations.all {
53-
resolutionStrategy.cacheChangingModulesFor 30, 'seconds'
53+
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
5454
}
5555

5656
jacoco {

sdk-amop/src/main/java/org/fisco/bcos/sdk/amop/Amop.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,41 @@
1717

1818
import java.util.Set;
1919
import org.fisco.bcos.sdk.config.ConfigOption;
20+
import org.fisco.bcos.sdk.jni.BcosSDKJniObj;
2021
import org.fisco.bcos.sdk.jni.amop.AmopRequestCallback;
2122
import org.fisco.bcos.sdk.jni.amop.AmopResponseCallback;
2223
import org.fisco.bcos.sdk.jni.common.JniException;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2326

2427
/**
2528
* AMOP module interface.
2629
*
2730
* @author Maggie
2831
*/
2932
public interface Amop {
33+
static final Logger logger = LoggerFactory.getLogger(Amop.class);
34+
3035
/**
3136
* Create a Amop object.
3237
*
33-
* @param config the config object
38+
* @param configOption the config object
3439
* @return Amop instance
3540
*/
36-
static Amop build(ConfigOption config) throws JniException {
37-
return new AmopImp(config);
41+
static Amop build(ConfigOption configOption) throws JniException {
42+
long nativePointer = BcosSDKJniObj.create(configOption.getJniConfig());
43+
logger.info("build AMOP, configOption: {}", configOption);
44+
return new AmopImp(nativePointer);
45+
}
46+
47+
/**
48+
* Create a Amop object.
49+
*
50+
* @param nativePointer the
51+
* @return Amop instance
52+
*/
53+
static Amop build(long nativePointer) throws JniException {
54+
return new AmopImp(nativePointer);
3855
}
3956

4057
/**
@@ -95,4 +112,7 @@ static Amop build(ConfigOption config) throws JniException {
95112

96113
/** Stop. */
97114
void stop();
115+
116+
/** Destroy amop object */
117+
void destroy();
98118
}

sdk-amop/src/main/java/org/fisco/bcos/sdk/amop/AmopImp.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
package org.fisco.bcos.sdk.amop;
1717

18+
import java.util.HashSet;
1819
import java.util.Set;
19-
import org.fisco.bcos.sdk.config.ConfigOption;
20+
import org.fisco.bcos.sdk.jni.BcosSDKJniObj;
21+
import org.fisco.bcos.sdk.jni.amop.AmopJniObj;
2022
import org.fisco.bcos.sdk.jni.amop.AmopRequestCallback;
2123
import org.fisco.bcos.sdk.jni.amop.AmopResponseCallback;
2224
import org.fisco.bcos.sdk.jni.common.JniException;
@@ -27,11 +29,12 @@
2729
public class AmopImp implements Amop {
2830
private static final Logger logger = LoggerFactory.getLogger(AmopImp.class);
2931

30-
private org.fisco.bcos.sdk.jni.amop.Amop amopJni;
32+
private AmopJniObj amopJni;
3133

32-
public AmopImp(ConfigOption config) throws JniException {
33-
logger.info("newAmop, config: {}", config);
34-
this.amopJni = org.fisco.bcos.sdk.jni.amop.Amop.build(config.getJniConfig());
34+
public AmopImp(long nativePointer) throws JniException {
35+
this.amopJni = AmopJniObj.build(nativePointer);
36+
37+
logger.info("newAmop, nativePointer: {}", nativePointer);
3538
start();
3639
}
3740

@@ -42,7 +45,9 @@ public void subscribeTopic(String topicName, AmopRequestCallback callback) {
4245

4346
@Override
4447
public void unsubscribeTopic(String topicName) {
45-
amopJni.unsubscribeTopic(topicName);
48+
Set<String> topics = new HashSet<>();
49+
topics.add(topicName);
50+
amopJni.unsubscribeTopic(topics);
4651
}
4752

4853
@Override
@@ -84,4 +89,12 @@ public void stop() {
8489
amopJni.stop();
8590
}
8691
}
92+
93+
@Override
94+
public void destroy() {
95+
if (amopJni != null) {
96+
BcosSDKJniObj.destroy(amopJni.getNativePointer());
97+
amopJni = null;
98+
}
99+
}
87100
}

sdk-service/src/main/java/org/fisco/bcos/sdk/BcosSDK.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,20 @@
1919
import org.fisco.bcos.sdk.config.ConfigOption;
2020
import org.fisco.bcos.sdk.config.exceptions.ConfigException;
2121
import org.fisco.bcos.sdk.eventsub.EventSubscribe;
22+
import org.fisco.bcos.sdk.jni.BcosSDKJniObj;
2223
import org.slf4j.Logger;
2324
import org.slf4j.LoggerFactory;
2425

2526
public class BcosSDK {
2627
private static Logger logger = LoggerFactory.getLogger(BcosSDK.class);
2728

2829
private final ConfigOption config;
29-
// private org.fisco.bcos.sdk.jni.BcosSDK jniBcosSdk;
30-
private org.fisco.bcos.sdk.jni.rpc.Rpc jniRpcImpl;
30+
private BcosSDKJniObj bcosSDKJniObj;
3131

3232
public ConfigOption getConfig() {
3333
return config;
3434
}
3535

36-
// public org.fisco.bcos.sdk.jni.BcosSDK getJniBcosSdk() {
37-
// return jniBcosSdk;
38-
// }
39-
//
40-
// public void setJniBcosSdk(org.fisco.bcos.sdk.jni.BcosSDK jniBcosSdk) {
41-
// this.jniBcosSdk = jniBcosSdk;
42-
// }
43-
4436
/**
4537
* Build BcosSDK instance
4638
*
@@ -67,10 +59,9 @@ public static BcosSDK build(String tomlConfigFilePath) throws BcosSDKException {
6759
public BcosSDK(ConfigOption configOption) throws BcosSDKException {
6860
try {
6961
this.config = configOption;
70-
// this.jniBcosSdk = org.fisco.bcos.sdk.jni.BcosSDK.build(configOption.getJniConfig());
71-
this.jniRpcImpl = org.fisco.bcos.sdk.jni.rpc.Rpc.build(config.getJniConfig());
62+
this.bcosSDKJniObj = BcosSDKJniObj.build(this.config.getJniConfig());
7263
} catch (Exception e) {
73-
logger.warn("error: {}", e);
64+
logger.error("error: {}", e);
7465
throw new BcosSDKException("create BcosSDK failed, error: " + e.getMessage());
7566
}
7667
}
@@ -82,7 +73,7 @@ public BcosSDK(ConfigOption configOption) throws BcosSDKException {
8273
*/
8374
public Client getClient(String groupId) throws BcosSDKException {
8475
try {
85-
return Client.build(groupId, config, jniRpcImpl);
76+
return Client.build(groupId, config, bcosSDKJniObj.getNativePointer());
8677
} catch (Exception e) {
8778
logger.warn("create client for failed, error: {}", e);
8879
throw new BcosSDKException("get Client failed, e: " + e.getMessage());
@@ -96,8 +87,15 @@ public Client getClient(String groupId) throws BcosSDKException {
9687
*/
9788
public Client getClient() throws BcosSDKException {
9889
try {
99-
assert config.getNetworkConfig().getDefaultGroup() != null;
100-
return Client.build(config.getNetworkConfig().getDefaultGroup(), config);
90+
String groupId = config.getNetworkConfig().getDefaultGroup();
91+
if ((groupId == null) || groupId.isEmpty()) {
92+
throw new RuntimeException(
93+
"The default group is not set, please set it in config.toml: defaultGroup field");
94+
}
95+
return Client.build(
96+
config.getNetworkConfig().getDefaultGroup(),
97+
config,
98+
bcosSDKJniObj.getNativePointer());
10199
} catch (Exception e) {
102100
logger.warn("create client for failed, error: {}", e);
103101
throw new BcosSDKException("get Client failed, e: " + e.getMessage());
@@ -114,7 +112,7 @@ public Amop getAmop() throws BcosSDKException {
114112
Amop amop = Amop.build(config);
115113
return amop;
116114
} catch (Exception e) {
117-
logger.warn("create amop for failed, error: {}", e);
115+
logger.error("create amop for failed, error: {}", e);
118116
throw new BcosSDKException("get amop failed, e: " + e.getMessage());
119117
}
120118
}

sdk-service/src/main/java/org/fisco/bcos/sdk/client/Client.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.fisco.bcos.sdk.client.protocol.response.*;
2121
import org.fisco.bcos.sdk.config.ConfigOption;
2222
import org.fisco.bcos.sdk.crypto.CryptoSuite;
23+
import org.fisco.bcos.sdk.jni.BcosSDKJniObj;
2324
import org.fisco.bcos.sdk.jni.common.JniException;
2425
import org.fisco.bcos.sdk.model.callback.TransactionCallback;
2526
import org.slf4j.Logger;
@@ -31,44 +32,54 @@
3132
* @author Maggie
3233
*/
3334
public interface Client {
34-
static Logger logger = LoggerFactory.getLogger(Client.class);
35+
static final Logger logger = LoggerFactory.getLogger(Client.class);
3536

3637
/**
3738
* Build a client instance GroupId is identified, all interfaces are available
3839
*
39-
* @param groupID the group info
4040
* @param configOption the config
4141
* @return a client instance
4242
*/
43-
static Client build(String groupID, ConfigOption configOption) throws JniException {
44-
return new ClientImpl(groupID, configOption);
43+
static Client build(ConfigOption configOption) throws JniException {
44+
logger.info("build, configOption: {}", configOption);
45+
return build(null, configOption);
4546
}
4647

4748
/**
48-
* Build a client instance GroupId is identified, all interfaces are available, with specific
49-
* jniRPC
49+
* Build a client instance GroupId is identified, all interfaces are available
5050
*
51-
* @param groupID the group info
51+
* @param groupId the group info
5252
* @param configOption the config
53-
* @param jniRpcIml jni rpc impl
5453
* @return a client instance
5554
*/
56-
static Client build(
57-
String groupID, ConfigOption configOption, org.fisco.bcos.sdk.jni.rpc.Rpc jniRpcIml)
58-
throws JniException {
59-
return new ClientImpl(groupID, configOption, jniRpcIml);
55+
static Client build(String groupId, ConfigOption configOption) throws JniException {
56+
logger.info("build, groupID: {}, configOption: {}", groupId, configOption);
57+
long nativePointer = BcosSDKJniObj.create(configOption.getJniConfig());
58+
return build(groupId, configOption, nativePointer);
6059
}
6160

6261
/**
63-
* Build a client instance GroupId is identified, all interfaces are available
62+
* Build a client instance GroupId is identified, all interfaces are available, with specific
63+
* jniRPC
6464
*
65+
* @param groupId the group info
6566
* @param configOption the config
67+
* @param nativePointer jni impl native handler
6668
* @return a client instance
6769
*/
68-
static Client build(ConfigOption configOption) throws JniException {
69-
return new ClientImpl(configOption);
70+
static Client build(String groupId, ConfigOption configOption, long nativePointer)
71+
throws JniException {
72+
logger.info(
73+
"build, groupID: {}, configOption: {}, nativePointer: {}",
74+
groupId,
75+
configOption,
76+
nativePointer);
77+
return new ClientImpl(groupId, configOption, nativePointer);
7078
}
7179

80+
/** @return */
81+
long getNativePointer();
82+
7283
/**
7384
* Get CryptoSuite
7485
*
@@ -790,4 +801,6 @@ void getTransactionReceiptAsync(
790801
void start();
791802

792803
void stop();
804+
805+
void destroy();
793806
}

0 commit comments

Comments
 (0)