Skip to content

Commit

Permalink
add multibroadcast and binary send test function (#391)
Browse files Browse the repository at this point in the history
* update config file

* update config file fro rerun

* add multibroadcast and binary send test function
  • Loading branch information
qwdarrenyin authored Aug 28, 2019
1 parent cfccba1 commit 891e3f3
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/org/fisco/bcos/channel/client/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,12 @@ public void run(Timeout timeout) throws Exception {
}
}

public void asyncMulticastChannelMessageForVerifyTopic(ChannelRequest request) {
String toTopic = request.getToTopic();
request.setToTopic(getNeedVerifyTopics(toTopic));
asyncMulticastChannelMessage2(request);
}

public void asyncMulticastChannelMessage2(ChannelRequest request) {
try {
logger.debug("ChannelRequest:{} ", request.getMessageID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public static String getPeerHost(ChannelHandlerContext ctx) {
public static boolean isChannelAvailable(ChannelHandlerContext ctx) {

// return ctx.channel().isActive();

return (null != ctx) && ctx.channel().isActive() && (null != getProtocolVersion(ctx));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.fisco.bcos.channel.test.amop;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import org.fisco.bcos.channel.client.Service;
import org.fisco.bcos.channel.dto.ChannelRequest;
import org.fisco.bcos.channel.dto.ChannelResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Channel2ClientBinNeedVerify {
private static Logger logger = LoggerFactory.getLogger(Channel2ClientBinNeedVerify.class);

public static byte[] toByteArrFromFile(String path) throws Exception {
File inFile = new File(path);
FileInputStream fileInputStream = new FileInputStream(inFile);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
while ((i = fileInputStream.read()) != -1) {
byteArrayOutputStream.write(i);
}
fileInputStream.close();
byte[] bytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return bytes;
}

public static byte[] intToByteArray(int i) {
byte[] result = new byte[4];
result[0] = (byte) ((i >> 24) & 0xFF);
result[1] = (byte) ((i >> 16) & 0xFF);
result[2] = (byte) ((i >> 8) & 0xFF);
result[3] = (byte) (i & 0xFF);
return result;
}

public static byte[] byteCat(byte[] data1, byte[] data2) {
byte[] data3 = new byte[data1.length + data2.length];
System.arraycopy(data1, 0, data3, 0, data1.length);
System.arraycopy(data2, 0, data3, data1.length, data2.length);
return data3;
}

public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("param: target topic filename of request");
return;
}
String topic = args[0];
String filename = args[1];
Integer count = 10;

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

logger.debug("init client");

ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

Service service = context.getBean(Service.class);
service.run();

System.out.println("3s ...");
Thread.sleep(1000);
System.out.println("2s ...");
Thread.sleep(1000);
System.out.println("1s ...");
Thread.sleep(1000);

System.out.println("start test");
System.out.println("===================================================================");

for (Integer i = 0; i < count; ++i) {
Thread.sleep(2000);
ChannelRequest request = new ChannelRequest();
request.setToTopic(topic);
request.setMessageID(service.newSeq());
request.setTimeout(5000);

/*设置为-128表示为传输二进制*/
int flag = -128;
byte[] byteflag = intToByteArray(flag);
int filelength = filename.length();
byte[] bytelength = intToByteArray(filelength);
byte[] bytefilename = filename.getBytes();
byte[] contentfile = toByteArrFromFile(filename);
byte[] content =
byteCat(byteCat(byteCat(byteflag, bytelength), bytefilename), contentfile);
request.setContent(content);

logger.info("msg:" + Arrays.toString(content));

System.out.println(
df.format(LocalDateTime.now())
+ " request seq:"
+ String.valueOf(request.getMessageID())
+ " content length:"
+ content.length);

ChannelResponse response = service.sendChannelMessageForVerifyTopic(request);

System.out.println(
df.format(LocalDateTime.now())
+ "response seq:"
+ String.valueOf(response.getMessageID())
+ ", ErrorCode:"
+ response.getErrorCode()
+ ", Content:"
+ response.getContent());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.fisco.bcos.channel.test.amop;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.fisco.bcos.channel.client.Service;
import org.fisco.bcos.channel.dto.ChannelRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Channel2ClientMultiBinNeedVerify {
private static Logger logger = LoggerFactory.getLogger(Channel2ClientMulti.class);
private static final int parameterNum = 2;

public static void main(String[] args) throws Exception {
if (args.length < parameterNum) {
System.out.println("param: target topic total number of request");
return;
}
String topic = args[0];
Integer count = 10;
String filename = args[1];

int flag = -128;
byte[] byteflag = Channel2ClientBin.intToByteArray(flag);
int filelength = filename.length();
byte[] bytelength = Channel2ClientBin.intToByteArray(filelength);
byte[] bytefilename = filename.getBytes();
byte[] contentfile = Channel2ClientBin.toByteArrFromFile(filename);
byte[] content =
Channel2ClientBin.byteCat(
Channel2ClientBin.byteCat(
Channel2ClientBin.byteCat(byteflag, bytelength), bytefilename),
contentfile);

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

logger.debug("init client");

ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

Service service = context.getBean(Service.class);
service.run();

System.out.println("3s ...");
Thread.sleep(1000);
System.out.println("2s ...");
Thread.sleep(1000);
System.out.println("1s ...");
Thread.sleep(1000);

System.out.println("start test");
System.out.println("===================================================================");

for (Integer i = 0; i < count; ++i) {
Thread.sleep(2000);
ChannelRequest request = new ChannelRequest();
request.setToTopic(topic);
request.setMessageID(service.newSeq());
request.setTimeout(5000);
request.setContent(content);

System.out.println(
df.format(LocalDateTime.now())
+ " multicast request seq:"
+ String.valueOf(request.getMessageID())
+ ", filename:"
+ filename);
service.asyncMulticastChannelMessageForVerifyTopic(request);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.fisco.bcos.channel.test.amop;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.fisco.bcos.channel.client.Service;
import org.fisco.bcos.channel.dto.ChannelRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Channel2ClientMultiNeedVerify {
private static Logger logger = LoggerFactory.getLogger(Channel2ClientMultiNeedVerify.class);
private static final int parameterNum = 2;

public static void main(String[] args) throws Exception {
if (args.length < parameterNum) {
System.out.println("param: target topic total number of request");
return;
}
String topic = args[0];
Integer count = Integer.parseInt(args[1]);

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

logger.debug("init client");

ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

Service service = context.getBean(Service.class);
service.run();

System.out.println("3s ...");
Thread.sleep(1000);
System.out.println("2s ...");
Thread.sleep(1000);
System.out.println("1s ...");
Thread.sleep(1000);

System.out.println("start test");
System.out.println("===================================================================");

for (Integer i = 0; i < count; ++i) {
Thread.sleep(2000);
ChannelRequest request = new ChannelRequest();
request.setToTopic(topic);
request.setMessageID(service.newSeq());
request.setTimeout(5000);

request.setContent("request seq:" + request.getMessageID());

System.out.println(
df.format(LocalDateTime.now())
+ " multicast request seq:"
+ String.valueOf(request.getMessageID())
+ ", Content:"
+ request.getContent());

service.asyncMulticastChannelMessageForVerifyTopic(request);
}
}
}

0 comments on commit 891e3f3

Please sign in to comment.