use of bftsmart.tom.ServiceProxy in project aware by bergerch.
the class CounterClient method main.
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.out.println("Usage: java ... CounterClient <process id> <increment> [<number of operations>]");
System.out.println(" if <increment> equals 0 the request will be read-only");
System.out.println(" default <number of operations> equals 1000");
System.exit(-1);
}
ServiceProxy counterProxy = new ServiceProxy(Integer.parseInt(args[0]));
try {
int inc = Integer.parseInt(args[1]);
int numberOfOps = (args.length > 2) ? Integer.parseInt(args[2]) : 1000;
for (int i = 0; i < numberOfOps; i++) {
ByteArrayOutputStream out = new ByteArrayOutputStream(4);
new DataOutputStream(out).writeInt(inc);
System.out.print("Invocation " + i);
byte[] reply = (inc == 0) ? counterProxy.invokeUnordered(out.toByteArray()) : // magic happens here
counterProxy.invokeOrdered(out.toByteArray());
if (reply != null) {
int newValue = new DataInputStream(new ByteArrayInputStream(reply)).readInt();
System.out.println(", returned value: " + newValue);
} else {
System.out.println(", ERROR! Exiting.");
break;
}
}
} catch (IOException | NumberFormatException e) {
counterProxy.close();
}
}
use of bftsmart.tom.ServiceProxy in project jdchain-core by blockchain-jd-com.
the class ParticipantManagerService4Bft method submitNodeStateChangeTx.
@Override
public TransactionResponse submitNodeStateChangeTx(ParticipantContext context, int activeID, TransactionRequest txRequest, List<NodeSettings> origConsensusNodes) {
Properties systemConfig = getCustomProperties(context);
int viewId = ((BftsmartConsensusViewSettings) getConsensusSetting(context)).getViewId();
TransactionResponse transactionResponse = new TxResponseMessage();
ServiceProxy peerProxy = createPeerProxy(systemConfig, viewId, origConsensusNodes, context.sslSecurity());
byte[] result = peerProxy.invokeOrdered(BinaryProtocol.encode(txRequest, TransactionRequest.class));
if (result == null) {
((TxResponseMessage) transactionResponse).setExecutionState(TransactionState.CONSENSUS_NO_REPLY_ERROR);
return transactionResponse;
}
peerProxy.close();
return txResponseWrapper(BinaryProtocol.decode(result));
}
use of bftsmart.tom.ServiceProxy in project jdchain-core by blockchain-jd-com.
the class ParticipantManagerService4Bft method createPeerProxy.
private ServiceProxy createPeerProxy(Properties systemConfig, int viewId, List<NodeSettings> origConsensusNodes, SSLSecurity security) {
HostsConfig hostsConfig;
List<HostsConfig.Config> configList = new ArrayList<>();
List<NodeNetwork> nodeAddresses = new ArrayList<>();
try {
int[] origConsensusProcesses = new int[origConsensusNodes.size()];
for (int i = 0; i < origConsensusNodes.size(); i++) {
BftsmartNodeSettings node = (BftsmartNodeSettings) origConsensusNodes.get(i);
origConsensusProcesses[i] = node.getId();
configList.add(new HostsConfig.Config(node.getId(), node.getNetworkAddress().getHost(), node.getNetworkAddress().getPort(), -1, node.getNetworkAddress().isSecure(), false));
nodeAddresses.add(new NodeNetwork(node.getNetworkAddress().getHost(), node.getNetworkAddress().getPort(), -1, node.getNetworkAddress().isSecure(), false));
}
// 构建共识的代理客户端需要的主机配置和系统参数配置结构
hostsConfig = new HostsConfig(configList.toArray(new HostsConfig.Config[configList.size()]));
Properties tempSystemConfig = (Properties) systemConfig.clone();
// 构建tom 配置
TOMConfiguration tomConfig = new TOMConfiguration(-(new Random().nextInt(Integer.MAX_VALUE - 2) - 1), tempSystemConfig, hostsConfig);
View view = new View(viewId, origConsensusProcesses, tomConfig.getF(), nodeAddresses.toArray(new NodeNetwork[nodeAddresses.size()]));
LOGGER.info("ManagementController start updateView operation!, current view : {}", view.toString());
// 构建共识的代理客户端,连接目标共识节点,并递交交易进行共识过程
return new ServiceProxy(tomConfig, new MemoryBasedViewStorage(view), null, null, security);
} catch (Exception e) {
throw new CreateProxyClientException("create proxy client exception!");
}
}
use of bftsmart.tom.ServiceProxy in project jdchain-core by blockchain-jd-com.
the class ParticipantManagerService4Bft method updateView.
// 通知原有的共识网络更新共识的视图ID
private View updateView(ParticipantNode node, NetworkAddress networkAddress, SSLSecurity security, ManagementController.ParticipantUpdateType participantUpdateType, Properties systemConfig, int viewId, List<NodeSettings> origConsensusNodes) {
LOGGER.info("ManagementController start updateView operation!");
TransactionRequest reconfigRequest = null;
try {
ServiceProxy peerProxy = createPeerProxy(systemConfig, viewId, origConsensusNodes, security);
Reconfiguration reconfiguration = new Reconfiguration(peerProxy.getProcessId(), peerProxy);
if (participantUpdateType == ManagementController.ParticipantUpdateType.ACTIVE) {
// addServer的第一个参数指待加入共识的新参与方的编号
reconfiguration.addServer(node.getId(), networkAddress);
reconfigRequest = prepareReconfigTx("active");
} else if (participantUpdateType == ManagementController.ParticipantUpdateType.DEACTIVE) {
// 参数为待移除共识节点的id
reconfiguration.removeServer(node.getId());
reconfigRequest = prepareReconfigTx("deactive");
} else if (participantUpdateType == ManagementController.ParticipantUpdateType.UPDATE) {
// 共识参数修改,先移除后添加
reconfiguration.removeServer(node.getId());
reconfiguration.addServer(node.getId(), networkAddress);
reconfigRequest = prepareReconfigTx("update");
} else {
throw new IllegalArgumentException("op type error!");
}
// 把交易作为reconfig操作的扩展信息携带,目的是为了让该操作上链,便于后续跟踪;
reconfiguration.addExtendInfo(BinaryProtocol.encode(reconfigRequest, TransactionRequest.class));
// 执行更新目标共识网络的视图ID
ReconfigureReply reconfigureReply = reconfiguration.execute();
peerProxy.close();
// 返回新视图
return reconfigureReply.getView();
} catch (Exception e) {
throw new ViewUpdateException("view update fail exception!", e);
}
}
use of bftsmart.tom.ServiceProxy in project aware by bergerch.
the class YCSBClient method init.
@Override
public void init() {
Properties props = getProperties();
int initId = Integer.valueOf((String) props.get("smart-initkey"));
myId = initId + counter.addAndGet(1);
proxy = new ServiceProxy(myId);
System.out.println("YCSBKVClient. Initiated client id: " + myId);
}
Aggregations