I/O Multiplexing

Capturing on multiple interfaces with single thread.

Minimum version: 1.1.x or above.

public static void main(String[] _args) throws Exception {
  var service = Service.Creator.create("PcapService");
  var dev1 = service.interfaces();
  var dev2 = dev1.next();
  var pcap1 = service.live(dev1, new DefaultLiveOptions());
  var pcap2 = service.live(dev2, new DefaultLiveOptions());
  PacketHandler<Pcap> handler =
      (args, header, buffer) -> {
        System.out.println("Args     : " + args);
        System.out.println("Header   : " + header);
        System.out.println("Packet   : " + buffer);
      };
  var selector = service.selector();
  selector.register(pcap1);
  selector.register(pcap2);
  var timeout = new DefaultTimeout(1000000L, Timeout.Precision.MICRO);
  try {
    var selectables = selector.select(timeout);
    var iterator = selectables.iterator();
    while (iterator.hasNext()) {
      var next = iterator.next();
      var pcap = (Pcap) next;
      try {
        pcap.dispatch(1, handler, pcap);
      } catch (BreakException e) {
        System.err.println(e);
      }
    }
  } catch (TimeoutException e) {
    System.err.println(e);
  }
  selector.close();
  pcap1.close();
  pcap2.close();
}

Since 1.4.0

public static void main(String[] args) throws Exception {
  final Service service = Service.Creator.create("PcapService");
  final Interface interfaces = service.interfaces();
  final Selector selector = service.selector();
  final Iterator<Interface> iterator = interfaces.iterator();
  while (iterator.hasNext()) {
    final Interface source = iterator.next();
    if ((source.flags() & Interface.PCAP_IF_UP) != 0) {
      final Pcap live = service.live(source, new DefaultLiveOptions());
      final Selection selection = live.register(selector, Selection.OPERATION_READ, null);
      assert selection.interestOperations() == Selection.OPERATION_READ;
    }
  }
  final Timeout timeout = new DefaultTimeout(1000000L, Timeout.Precision.MICRO);
  for (int i = 0; i < 100; i++) {
    try {
      final int nSelected =
          selector.select(
              new Consumer<Selection>() {
                @Override
                public void accept(Selection selection) {
                  if (selection.isReadable()) {
                    final Pcap pcap = (Pcap) selection.selectable();
                    try {
                      pcap.dispatch(
                          1,
                          new PacketHandler<String>() {
                            @Override
                            public void gotPacket(
                                String s, PacketHeader packetHeader, PacketBuffer packetBuffer) {
                              System.out.println("Header : " + packetHeader);
                            }
                          },
                          "");
                      selection.interestOperations(Selection.OPERATION_WRITE);
                    } catch (BreakException e) {
                      System.err.println(e);
                    } catch (ErrorException e) {
                      System.err.println(e);
                    } catch (TimeoutException e) {
                      System.err.println(e);
                    }
                  } else if (selection.isWritable()) {
                    selection.interestOperations(Selection.OPERATION_READ);
                  }
                }
              },
              timeout);
      System.out.println("N selected : " + nSelected);
    } catch (TimeoutException e) {
      //
    }
  }
}

Last updated