Skip to content

Commit 4404e63

Browse files
committed
Support new send(buf, size=0, fill=None) and send_mgs(buf, size=0,
fill=None) low_level APIs Signed-off-by: Leo Ma <[email protected]>
1 parent 4c5c5bb commit 4404e63

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

src/main/java/com/cisco/trex/stateful/api/lowlevel/ASTFCmdSend.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,30 @@ public class ASTFCmdSend extends ASTFCmd {
1515
* @param asciiBuf
1616
*/
1717
public ASTFCmdSend(byte[] asciiBuf) {
18+
this(asciiBuf, 0, null);
19+
}
20+
21+
/**
22+
* construct
23+
*
24+
* @param asciiBuf
25+
* @param size
26+
* @param fill
27+
*/
28+
public ASTFCmdSend(byte[] asciiBuf, int size, byte[] fill) {
1829
super();
19-
this.base64Buf = encodeBase64(asciiBuf);
30+
String bufStr = encodeBase64(asciiBuf);
31+
this.base64Buf = bufStr;
2032
fields.addProperty("name", NAME);
2133
fields.addProperty("buf_index", -1);
2234
this.bufLen = asciiBuf.length;
35+
if (size > asciiBuf.length) {
36+
this.base64Buf = "{ \"base\": \"" + bufStr + "\", \"size\": " + size + " }";
37+
if (fill != null) {
38+
this.base64Buf = "{ \"base\": \"" + bufStr + "\", \"fill\": \"" + encodeBase64(fill) + "\", \"size\": " + size + " }";
39+
}
40+
this.bufLen = size;
41+
}
2342
stream = true;
2443
buffer = true;
2544
}

src/main/java/com/cisco/trex/stateful/api/lowlevel/ASTFCmdTxPkt.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,30 @@ public class ASTFCmdTxPkt extends ASTFCmd {
1515
* @param asciiBuf
1616
*/
1717
public ASTFCmdTxPkt(byte[] asciiBuf) {
18+
this(asciiBuf, 0, null);
19+
}
20+
21+
/**
22+
* construct
23+
*
24+
* @param asciiBuf
25+
* @param size
26+
* @param fill
27+
*/
28+
public ASTFCmdTxPkt(byte[] asciiBuf, int size, byte[] fill) {
1829
super();
19-
this.base64Buf = encodeBase64(asciiBuf);
30+
String bufStr = encodeBase64(asciiBuf);
31+
this.base64Buf = bufStr;
2032
fields.addProperty("name", NAME);
2133
fields.addProperty("buf_index", -1);
2234
this.bufLen = asciiBuf.length;
35+
if (size > asciiBuf.length) {
36+
this.base64Buf = "{ \"base\": \"" + bufStr + "\", \"size\": " + size + " }";
37+
if (fill != null) {
38+
this.base64Buf = "{ \"base\": \"" + bufStr + "\", \"fill\": \"" + encodeBase64(fill) + "\", \"size\": " + size + " }";
39+
}
40+
this.bufLen = size;
41+
}
2342
stream = false;
2443
buffer = true;
2544
}

src/main/java/com/cisco/trex/stateful/api/lowlevel/ASTFProgram.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.cisco.trex.stateful.api.lowlevel;
22

3+
import com.google.gson.Gson;
34
import com.google.gson.JsonArray;
45
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonSyntaxException;
7+
58
import java.io.UnsupportedEncodingException;
69
import java.nio.charset.StandardCharsets;
710
import java.security.MessageDigest;
@@ -249,10 +252,32 @@ public void delay(int usec) {
249252
* @param buf l7 stream as string
250253
*/
251254
public void send(String buf) {
255+
send(buf, 0, null);
256+
}
257+
258+
/**
259+
* send (l7_buffer) over TCP and wait for the buffer to be acked by peer. Rx side could work in
260+
* parallel
261+
*
262+
* <p>example1 send (buffer1) send (buffer2)
263+
*
264+
* <p>Will behave differently than
265+
*
266+
* <p>example1 send (buffer1+ buffer2)
267+
*
268+
* <p>in the first example there would be PUSH in the last byte of the buffer and immediate ACK
269+
* from peer while in the last example the buffer will be sent together (might be one segment)
270+
*
271+
* @param buf l7 stream as string
272+
* @param size total size of l7 stream, effective only when size > buf.length
273+
* @param fill l7 stream filled by string, only if size is effective
274+
*/
275+
public void send(String buf, int size, String fill) {
252276
// we support bytes or ascii strings
277+
System.out.println("testing being by leo \n");
253278
ASTFCmdSend cmd = null;
254279
try {
255-
cmd = new ASTFCmdSend(buf.getBytes("ascii"));
280+
cmd = new ASTFCmdSend(buf.getBytes("ascii"), size, (fill != null) ? fill.getBytes("ascii") : null);
256281
} catch (UnsupportedEncodingException e) {
257282
throw new IllegalStateException("Unsupported Encoding Exception", e);
258283
}
@@ -268,9 +293,20 @@ public void send(String buf) {
268293
* @param buf l7 stream as string
269294
*/
270295
public void sendMsg(String buf) {
296+
sendMsg(buf, 0, null);
297+
}
298+
299+
/**
300+
* send UDP message
301+
*
302+
* @param buf l7 stream as string
303+
* @param size total size of l7 stream, effective only when size > buf.length
304+
* @param fill l7 stream filled by string, only if size is effective
305+
*/
306+
public void sendMsg(String buf, int size, String fill) {
271307
ASTFCmdTxPkt cmd = null;
272308
try {
273-
cmd = new ASTFCmdTxPkt(buf.getBytes("ascii"));
309+
cmd = new ASTFCmdTxPkt(buf.getBytes("ascii"), size, (fill != null) ? fill.getBytes("ascii") : null);
274310
} catch (UnsupportedEncodingException e) {
275311
throw new IllegalStateException("Unsupported Encoding Exception", e);
276312
}
@@ -662,7 +698,12 @@ public int add(String base64Buf) {
662698
public JsonArray toJson() {
663699
JsonArray jsonArray = new JsonArray();
664700
for (String buf : list) {
665-
jsonArray.add(buf);
701+
try {
702+
JsonObject jsonObj = new Gson().fromJson(buf, JsonObject.class);
703+
jsonArray.add(jsonObj);
704+
} catch (JsonSyntaxException e) {
705+
jsonArray.add(buf);
706+
}
666707
}
667708
return jsonArray;
668709
}

0 commit comments

Comments
 (0)