Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
ardikars committed Aug 31, 2018
2 parents 7bbd458 + 1e4345c commit 0d4390e
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ a.out
/jxnet-spring-boot-starter/build/
/jxnet-spring-boot-starter/target
/jxnet-spring-boot-starter/out
/jxnet-spring-boot-starter-example/.gradle
/jxnet-spring-boot-starter-example/build/
/jxnet-spring-boot-starter-example/target
/jxnet-spring-boot-starter-example/out
/jxnet-android/.gradle
/jxnet-android/build/
/jxnet-android/target
Expand Down
2 changes: 1 addition & 1 deletion gradle/configure.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ext {

NAME = 'Jxnet'
GROUP = 'com.ardikars.jxnet'
VERSION = '1.4.0.RC2'
VERSION = '1.4.0.RC3'

NDK_HOME = "${System.env.NDK_HOME}"
TOOLS_DIR = "${rootDir}/tools"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ private Application() {

/**
* Bootstraping application.
* @param aplicationName application name.
* @param applicationDisplayName application display name.
* @param applicationVersion application version.
* @param builder pcap builder.
*/
public static void run(Builder<Pcap, Void> builder) {
public static void run(String aplicationName, String applicationDisplayName, String applicationVersion, Builder<Pcap, Void> builder) {
Validate.notIllegalArgument(builder != null,
new IllegalArgumentException("Pcap builder should be not null."));
instance.context = new ApplicationContext(builder);
instance.context = new ApplicationContext(aplicationName, applicationDisplayName, applicationVersion, builder);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,30 @@
import com.ardikars.jxnet.exception.PcapDumperCloseException;
import com.ardikars.jxnet.exception.PlatformNotSupportedException;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
import java.util.concurrent.Executor;

/**
* @author Ardika Rommy Sanjaya
* @since 1.1.5
*/
public final class ApplicationContext implements Context {

private static final Logger LOGGER = Logger.getLogger(ApplicationContext.class.getSimpleName());
private final String applicationName;

private String applicationName;
private final String applicationDisplayName;

private String applicationVersion;
private final String applicationVersion;

private final Pcap pcap;

private BpfProgram bpfProgram;

private PcapDumper pcapDumper;

protected ApplicationContext(Builder<Pcap, Void> builder) {
protected ApplicationContext(String applicationName, String applicationDisplayName, String applicationVersion, Builder<Pcap, Void> builder) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Expand All @@ -65,21 +62,11 @@ public void run() {
if (pcapDumper != null && !pcapDumper.isClosed()) {
Jxnet.PcapDumpClose(pcapDumper);
}
LOGGER.info("Application closed gracefully.");
}
});
final Properties properties = new Properties();
try (InputStream stream = ClassLoader.class.getResourceAsStream("application.properties")) {
if (stream != null) {
properties.load(stream);
}
this.applicationName = properties.getProperty("jxnet.application.name", "");
this.applicationVersion = properties.getProperty("jxnet.application.version", "");
} catch (IOException e) {
this.applicationName = "";
this.applicationVersion = "";
LOGGER.warning(e.getMessage());
}
this.applicationName = applicationName;
this.applicationDisplayName = applicationDisplayName;
this.applicationVersion = applicationVersion;
Validate.notIllegalArgument(builder != null, new IllegalArgumentException("Pcap builder should be not null."));
this.pcap = builder.build();
}
Expand All @@ -89,14 +76,19 @@ public String getApplicationName() {
return applicationName;
}

@Override
@Override
public String getApplicationDisplayName() {
return applicationDisplayName;
}

@Override
public String getApplicationVersion() {
return applicationVersion;
}

@Override
public Context newInstance(Builder<Pcap, Void> builder) {
return new ApplicationContext(builder);
return new ApplicationContext(this.applicationName, this.applicationDisplayName, this.applicationVersion, builder);
}

@Override
Expand All @@ -108,6 +100,27 @@ public <T> PcapCode pcapLoop(int cnt, PcapHandler<T> callback, T user) throws Pc
return PcapCode.PCAP_ERROR;
}

@Override
public <T> PcapCode pcapLoop(final int cnt, final PcapHandler<T> callback, final T user, final Executor executor) throws PcapCloseException {
Validate.notIllegalArgument(executor != null,
new IllegalArgumentException("Executor should be not null."));
int result = Jxnet.PcapLoop(pcap, cnt, new PcapHandler<T>() {
@Override
public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) {
executor.execute(new Runnable() {
@Override
public void run() {
callback.nextPacket(user, h, bytes);
}
});
}
}, user);
if (result == 0) {
return PcapCode.PCAP_OK;
}
return PcapCode.PCAP_ERROR;
}

@Override
public <T> PcapCode pcapDispatch(int cnt, PcapHandler<T> callback, T user) throws PcapCloseException {
int result = Jxnet.PcapDispatch(pcap, cnt, callback, user);
Expand All @@ -117,6 +130,28 @@ public <T> PcapCode pcapDispatch(int cnt, PcapHandler<T> callback, T user) throw
return PcapCode.PCAP_ERROR;
}

@Override
public <T> PcapCode pcapDispatch(final int cnt, final PcapHandler<T> callback, final T user, final Executor executor)
throws PcapCloseException {
Validate.notIllegalArgument(executor != null,
new IllegalArgumentException("Executor should be not null."));
int result = Jxnet.PcapDispatch(pcap, cnt, new PcapHandler<T>() {
@Override
public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) {
executor.execute(new Runnable() {
@Override
public void run() {
callback.nextPacket(user, h, bytes);
}
});
}
}, user);
if (result == 0) {
return PcapCode.PCAP_OK;
}
return PcapCode.PCAP_ERROR;
}

@Override
public PcapCode pcapDumpOpen(String fname) throws PcapCloseException {
pcapDumper = Jxnet.PcapDumpOpen(pcap, fname);
Expand Down
48 changes: 46 additions & 2 deletions jxnet-context/src/main/java/com/ardikars/jxnet/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@

import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.Executor;

public interface Context extends Factory<Context, Builder<Pcap, Void>> {

String getApplicationName();

String getApplicationDisplayName();

String getApplicationVersion();

/**
Expand All @@ -46,23 +49,39 @@ public interface Context extends Factory<Context, Builder<Pcap, Void>> {
* @param user args
* @param <T> args type.
* @return PcapLoop() returns 0 if cnt is exhausted or if, when reading from a
* @throws PcapCloseException pcap close exception.
* savefile, no more packets are available. It returns -1 if an error
* occurs or -2 if the loop terminated due to a call to PcapBreakLoop()
* before any packets were processed. It does not return when live packet
* buffer timeouts occur; instead, it attempts to read more packets.
* @throws PcapCloseException pcap close exception.
* @since 1.1.4
*/
<T> PcapCode pcapLoop(int cnt, PcapHandler<T> callback, T user) throws PcapCloseException;

/**
* Collect a group of packets.
* @param cnt maximum iteration, -1 is infinite iteration.
* @param callback callback funtion.
* @param user args
* @param executor executor service.
* @param <T> args type.
* @return PcapLoop() returns 0 if cnt is exhausted or if, when reading from a
* savefile, no more packets are available. It returns -1 if an error
* occurs or -2 if the loop terminated due to a call to PcapBreakLoop()
* before any packets were processed. It does not return when live packet
* buffer timeouts occur; instead, it attempts to read more packets.
* @throws PcapCloseException pcap close exception.
* @since 1.1.4
*/
<T> PcapCode pcapLoop(int cnt, PcapHandler<T> callback, T user, Executor executor) throws PcapCloseException;

/**
* Collect a group of packets.
* @param cnt maximum iteration, -1 to infinite.
* @param callback callback function.
* @param user arg.
* @param <T> args type.
* @return PcapDispatch() returns the number of packets processed on success;
* @throws PcapCloseException pcap close exception.
* this can be 0 if no packets were read from a live capture (if, for
* example, they were discarded because they didn't pass the packet filter,
* or if, on platforms that support a packet buffer timeout that
Expand All @@ -74,10 +93,35 @@ public interface Context extends Factory<Context, Builder<Pcap, Void>> {
* before any packets were processed. If your application uses
* PcapBreakLoop(), make sure that you explicitly check for -1 and -2,
* rather than just checking for a return value less then 0.
* @throws PcapCloseException pcap close exception.
* @since 1.1.4
*/
<T> PcapCode pcapDispatch(int cnt, PcapHandler<T> callback, T user) throws PcapCloseException;

/**
* Collect a group of packets.
* @param cnt maximum iteration, -1 to infinite.
* @param callback callback function.
* @param user arg.
* @param executor executor.
* @param <T> args type.
* @return PcapDispatch() returns the number of packets processed on success;
* this can be 0 if no packets were read from a live capture (if, for
* example, they were discarded because they didn't pass the packet filter,
* or if, on platforms that support a packet buffer timeout that
* starts before any packets arrive, the timeout expires before any packets
* arrive, or if the file descriptor for the capture device is in non-blocking
* mode and no packets were available to be read) or if no more
* packets are available in a savefile. It returns -1 if an error
* occurs or -2 if the loop terminated due to a call to PcapBreakLoop()
* before any packets were processed. If your application uses
* PcapBreakLoop(), make sure that you explicitly check for -1 and -2,
* rather than just checking for a return value less then 0.
* @throws PcapCloseException pcap close exception.
* @since 1.1.4
*/
<T> PcapCode pcapDispatch(int cnt, PcapHandler<T> callback, T user, Executor executor) throws PcapCloseException;

/**
* Open a file to write packets.
* @param fname fname specifies the name of the file to open. The file will have the same format
Expand Down
14 changes: 14 additions & 0 deletions jxnet-example/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

dependencies {
compile project(':jxnet-context')
}

jar {
manifest {
attributes "Main-Class": "com.ardikars.jxnet.example.Application"
}

from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Loading

0 comments on commit 0d4390e

Please sign in to comment.