Skip to content

Commit 1eb15b5

Browse files
Merge pull request #119 from cisco-system-traffic-generator/waitOnTraffic
add method waitOnTrafficToFinish
2 parents 2a62510 + 151a274 commit 1eb15b5

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/main/java/com/cisco/trex/stateless/TRexClient.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import java.text.MessageFormat;
2929
import java.util.AbstractMap;
3030
import java.util.ArrayList;
31+
import java.util.Arrays;
3132
import java.util.Base64;
3233
import java.util.Collections;
3334
import java.util.HashMap;
35+
import java.util.Iterator;
3436
import java.util.LinkedList;
3537
import java.util.List;
3638
import java.util.Map;
@@ -379,6 +381,72 @@ public void removeRxFilters(int portIndex, int profileId) {
379381
callMethod("remove_rx_filters", payload);
380382
}
381383

384+
/**
385+
* Wait until traffic on specified port(s) has ended
386+
*
387+
* @param timeoutInSeconds
388+
* @param rxDelayMs Time to wait (in milliseconds) after last packet was sent, until RX filters
389+
* used for measuring flow statistics and latency are removed. This value should reflect the
390+
* time it takes packets which were transmitted to arrive to the destination. After this time,
391+
* RX filters will be removed, and packets arriving for per flow statistics feature and
392+
* latency flows will be counted as errors.
393+
* @param ports Ports on which to execute the command
394+
*/
395+
public void waitOnTrafficToFinish(int timeoutInSeconds, int rxDelayMs, Port... ports) {
396+
long endTime = System.currentTimeMillis() + timeoutInSeconds * 1000;
397+
List<Port> portsStillSendingTraffic = new ArrayList<>(Arrays.asList(ports));
398+
399+
while (!portsStillSendingTraffic.isEmpty()) {
400+
Iterator<Port> iter = portsStillSendingTraffic.iterator();
401+
while (iter.hasNext()) {
402+
if (getPortStatus(iter.next().getIndex()).get().getState() != "TX") {
403+
iter.remove();
404+
}
405+
}
406+
if (System.currentTimeMillis() > endTime) {
407+
break;
408+
}
409+
if (!portsStillSendingTraffic.isEmpty()) {
410+
sleepMilliSeconds(10);
411+
}
412+
}
413+
414+
removeRxFiltersWithDelay(rxDelayMs, ports);
415+
}
416+
417+
/**
418+
* Delay some time to let packets arrive at destination port before removing filters
419+
*
420+
* @param rxDelayMs
421+
* @param ports
422+
*/
423+
protected void removeRxFiltersWithDelay(int rxDelayMs, Port... ports) {
424+
int rxDelayToUse;
425+
if (rxDelayMs <= 0) {
426+
if (ports[0].is_virtual) {
427+
rxDelayToUse = 100;
428+
} else {
429+
rxDelayToUse = 10;
430+
}
431+
} else {
432+
rxDelayToUse = rxDelayMs;
433+
}
434+
435+
sleepMilliSeconds(rxDelayToUse);
436+
437+
for (Port port : ports) {
438+
removeRxFilters(port.getIndex(), 0);
439+
}
440+
}
441+
442+
protected void sleepMilliSeconds(int milliSeconds) {
443+
try {
444+
Thread.sleep(milliSeconds);
445+
} catch (InterruptedException e) {
446+
// Do nothing
447+
}
448+
}
449+
382450
/** Set promiscuous mode, Enable interface to receive packets from all mac addresses */
383451
public void setPromiscuousMode(int portIndex, boolean enabled) {
384452
Map<String, Object> payload = createPayload(portIndex);

0 commit comments

Comments
 (0)