|
28 | 28 | import java.text.MessageFormat; |
29 | 29 | import java.util.AbstractMap; |
30 | 30 | import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
31 | 32 | import java.util.Base64; |
32 | 33 | import java.util.Collections; |
33 | 34 | import java.util.HashMap; |
| 35 | +import java.util.Iterator; |
34 | 36 | import java.util.LinkedList; |
35 | 37 | import java.util.List; |
36 | 38 | import java.util.Map; |
@@ -379,6 +381,72 @@ public void removeRxFilters(int portIndex, int profileId) { |
379 | 381 | callMethod("remove_rx_filters", payload); |
380 | 382 | } |
381 | 383 |
|
| 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 | + |
382 | 450 | /** Set promiscuous mode, Enable interface to receive packets from all mac addresses */ |
383 | 451 | public void setPromiscuousMode(int portIndex, boolean enabled) { |
384 | 452 | Map<String, Object> payload = createPayload(portIndex); |
|
0 commit comments