|
35 | 35 | import java.util.LinkedList; |
36 | 36 | import java.util.List; |
37 | 37 | import java.util.Map; |
| 38 | +import java.util.Map.Entry; |
38 | 39 | import java.util.Queue; |
39 | 40 | import java.util.Set; |
40 | 41 | import java.util.function.Predicate; |
@@ -380,6 +381,68 @@ public void removeRxFilters(int portIndex, int profileId) { |
380 | 381 | callMethod("remove_rx_filters", payload); |
381 | 382 | } |
382 | 383 |
|
| 384 | + /** |
| 385 | + * Block until traffic on specified port(s) has ended |
| 386 | + * |
| 387 | + * @param timeoutInSecounds 0 means blocking |
| 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 timeoutInSecounds, int rxDelayMs, Port... ports) { |
| 396 | + Map<Port, Boolean> portTrafficMap = new HashMap<>(); |
| 397 | + long endTime = System.currentTimeMillis() + timeoutInSecounds * 1000; |
| 398 | + |
| 399 | + for (Port port : ports) { |
| 400 | + portTrafficMap.put(port, true); |
| 401 | + } |
| 402 | + while (portTrafficMap.containsValue(true)) { |
| 403 | + for (Entry<Port, Boolean> entry : portTrafficMap.entrySet()) { |
| 404 | + if (entry.getValue()) { // port is still true, meaning still running traffic |
| 405 | + // set port to false if traffic is stopped(IDLE) |
| 406 | + entry.setValue(!getPortStatus(entry.getKey().getIndex()).get().getState().equals("IDLE")); |
| 407 | + } |
| 408 | + } |
| 409 | + if (System.currentTimeMillis() > endTime) { |
| 410 | + break; |
| 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 | + try { |
| 436 | + Thread.sleep(rxDelayToUse); |
| 437 | + } catch (InterruptedException e) { |
| 438 | + // Do nothing |
| 439 | + } |
| 440 | + |
| 441 | + for (Port port : ports) { |
| 442 | + removeRxFilters(port.getIndex(), 0); |
| 443 | + } |
| 444 | + } |
| 445 | + |
383 | 446 | /** Set promiscuous mode, Enable interface to receive packets from all mac addresses */ |
384 | 447 | public void setPromiscuousMode(int portIndex, boolean enabled) { |
385 | 448 | Map<String, Object> payload = createPayload(portIndex); |
|
0 commit comments