/** Test open live and loop with breakloop. */ public void testOpenLiveAndLoopWithBreakloop() { Pcap pcap = Pcap.openLive(device, 10000, 1, 60 * 1000, errbuf); assertNotNull(errbuf.toString(), pcap); PcapHandler<String> handler = new PcapHandler<String>() { public void nextPacket( String user, long seconds, int useconds, int caplen, int len, ByteBuffer buffer) { // System.out.printf("%s, ts=%s caplen=%d len=%d capacity=%d\n", user // .toString(), new Date(seconds * 1000).toString(), caplen, len, // buffer.capacity()); } }; pcap.breakloop(); // Should cause it to exit immediately assertEquals( "Error code does not indicate breakloop interrupted the loop when it should have", -2, pcap.loop(10, handler, "Hello")); pcap.close(); }
/** Test pcap closed exception handling. */ public void testPcapClosedExceptionHandling() { Pcap pcap = Pcap.openOffline(fname, errbuf); pcap.close(); try { pcap.breakloop(); fail("Expected PcapClosedException"); } catch (PcapClosedException e) { // Success } }
/** * Algorithm for breaking the loop, whatever it is. It can be overriden and a different algorithm * supplied. */ protected void breakLoop() { pcap.breakloop(); }