Skip to content

Commit 24b7a31

Browse files
committed
restore emil's change of loadProfile
1 parent 4fdcb45 commit 24b7a31

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

src/main/java/com/cisco/trex/stateful/TRexAstfClient.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.cisco.trex.stateful;
22

3+
import java.nio.charset.StandardCharsets;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
36
import java.util.HashMap;
47
import java.util.Map;
58
import java.util.Map.Entry;
@@ -65,6 +68,20 @@ private Map<String, Object> createPayload() {
6568
return payload;
6669
}
6770

71+
private static String calculateMd5(String profile) {
72+
try {
73+
MessageDigest md = MessageDigest.getInstance("MD5");
74+
byte[] hashInBytes = md.digest(profile.getBytes(StandardCharsets.UTF_8));
75+
StringBuilder sb = new StringBuilder();
76+
for (byte b : hashInBytes) {
77+
sb.append(String.format("%02x", b));
78+
}
79+
return sb.toString();
80+
} catch (NoSuchAlgorithmException e) {
81+
throw new IllegalStateException("Could not generate MD5", e);
82+
}
83+
}
84+
6885
/**
6986
* start traffic on all ports on the last loaded profile
7087
*
@@ -179,22 +196,31 @@ public PortStatus acquirePort(int portIndex, Boolean force) {
179196
}
180197

181198
/**
182-
* @param fragFirst
183-
* @param fragLast
184-
* @param fragmentData
185-
* @param totalSize
199+
* Load profile object as string and upload in fragments
200+
*
201+
* @param profile
186202
*/
187-
public void loadProfile(boolean fragFirst, boolean fragLast, String fragmentData, long totalSize) {
188-
Map<String, Object> payload = createPayload();
189-
if (fragFirst) {
190-
payload.put("frag_first", true);
191-
payload.put("total_size", totalSize);
192-
}
193-
if (fragLast) {
194-
payload.put("frag_last", true);
203+
public void loadProfile(String profile) {
204+
int indexStart = 0;
205+
int fragmentLength = 1000; //shorter length the first time
206+
int totalLength = profile.length();
207+
while (totalLength > indexStart) {
208+
int indexEnd = indexStart + fragmentLength;
209+
Map<String, Object> payload = createPayload();
210+
if (indexStart == 0) { //is first fragment
211+
payload.put("frag_first", true);
212+
payload.put("total_size", totalLength);
213+
payload.put("md5", calculateMd5(profile));
214+
}
215+
if (indexEnd >= totalLength) {
216+
payload.put("frag_last", true);
217+
indexEnd = totalLength;
218+
}
219+
payload.put("fragment", profile.subSequence(indexStart, indexEnd));
220+
this.callMethod("profile_fragment", payload);
221+
indexStart = indexEnd;
222+
fragmentLength = 500000; //larger fragments after first fragment
195223
}
196-
payload.put("fragment", fragmentData);
197-
this.callMethod("profile_fragment", payload);
198224
}
199225

200226
/**

0 commit comments

Comments
 (0)