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

Commit

Permalink
Add netty buffer and nio buffer spring configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ardikars committed Dec 2, 2018
1 parent b16a177 commit 6ab9260
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 193 deletions.
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,31 @@ public class Application implements CommandLineRunner {
- ##### Pcap Packet Handler Configuration

```java
@EnablePacket
@Configuration
public class DefaultPacketHandler implements PacketHandler<String> {
public class DefaultJxpacketHandler implements JxpacketHandler<String> {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPacketHandler.class.getName());
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJxpacketHandler.class.getName());

private static final String PRETTY_FOOTER = ""
+ "+-----------------------------------------------------------------------------------------------------+";
private static final String PRETTY_FOOTER = "+---------------------------------------------------"
+ "--------------------------------------------------+";

@Override
public void next(String argument, PcapPktHdr header, Future<Packet> packet) throws ExecutionException, InterruptedException {
Iterator<Packet> iterator = packet.get().iterator();
private void print(Pair<PcapPktHdr, Packet> packet) {
Iterator<Packet> iterator = packet.getRight().iterator();
LOGGER.info("Pcap packet header : {}", packet.getLeft());
LOGGER.info("Packet header : ");
while (iterator.hasNext()) {
LOGGER.info(iterator.next().toString());
LOGGER.info("{}", iterator.next());
}
LOGGER.info(PRETTY_FOOTER);
}

@Override
public void next(String argument, Future<Pair<PcapPktHdr, Packet>> packet) throws ExecutionException, InterruptedException {
LOGGER.info("User argument : {}", argument);
print(packet.get());
}

}
```

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.9.RC1'
VERSION = '1.4.9.RC2'
DESCRIPTION = 'Jxnet is a java library for capturing and sending network packet.'

NDK_HOME = "${System.env.NDK_HOME}"
Expand Down
5 changes: 3 additions & 2 deletions jxnet-spring-boot-autoconfigure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ dependencyManagement {
mavenBom("org.springframework.boot:spring-boot-dependencies:${SPRING_BOOT_VERSION}")
mavenBom("com.ardikars.jxpacket:jxpacket:${JXPACKET_VERSION}")
mavenBom("io.netty:netty-bom:${NETTY_VERSION}")
mavenBom("io.projectreactor:reactor-bom:${REACTOR_VERSION}")
}
}

Expand All @@ -19,11 +18,13 @@ dependencies {
implementation project (":jxnet-context")
implementation ("org.springframework.boot:spring-boot-configuration-processor")
implementation ("org.springframework.boot:spring-boot-autoconfigure")
implementation ("com.ardikars.common:common-annotation")
implementation ("com.ardikars.common:common-net")
implementation ("com.ardikars.common:common-util")
implementation ("com.ardikars.common:common-tuple")
implementation ("io.netty:netty-buffer")
implementation ("com.ardikars.jxpacket:jxpacket-common")
implementation ("com.ardikars.jxpacket:jxpacket-core")
implementation ("io.projectreactor:reactor-core")
implementation ("org.slf4j:slf4j-api:${SLF4J_VERSION}")
testImplementation ("junit:junit:${JUNIT_VERSION}")
testImplementation ("org.mockito:mockito-core:${MOCKITO_VERSION}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.ardikars.jxnet.spring.boot.autoconfigure;

import com.ardikars.common.tuple.Pair;
import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxpacket.common.Packet;

Expand All @@ -27,18 +28,17 @@
* Callback function used for capturing packets.
*
* @author <a href="mailto:[email protected]">Ardika Rommy Sanjaya</a>
* @since 1.4.8
* @since 1.4.9
*/
public interface PacketHandler<T> {
public interface JxpacketHandler<T> {

/**
* Next available packet.
* @param argument user argument.
* @param header pcap header.
* @param packet {@link Packet} object.
* @param packet a tuple of {@link PcapPktHdr} and {@link Packet}.
* @throws ExecutionException execution exception.
* @throws InterruptedException interrupted exception.
*/
void next(T argument, PcapPktHdr header, Future<Packet> packet) throws ExecutionException, InterruptedException;
void next(T argument, Future<Pair<PcapPktHdr, Packet>> packet) throws ExecutionException, InterruptedException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package com.ardikars.jxnet.spring.boot.autoconfigure;

import com.ardikars.common.tuple.Pair;
import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxpacket.common.Packet;
import io.netty.buffer.ByteBuf;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand All @@ -34,11 +34,10 @@ public interface NettyBufferHandler<T> {
/**
* Next available packet.
* @param argument user argument.
* @param header pcap header.
* @param packet {@link Packet} object.
* @param packet a taple of {@link PcapPktHdr} and {@link ByteBuf}.
* @throws ExecutionException execution exception.
* @throws InterruptedException interrupted exception.
*/
void next(T argument, PcapPktHdr header, Future<ByteBuf> packet) throws ExecutionException, InterruptedException;
void next(T argument, Future<Pair<PcapPktHdr, ByteBuf>> packet) throws ExecutionException, InterruptedException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package com.ardikars.jxnet.spring.boot.autoconfigure;

import com.ardikars.common.tuple.Pair;
import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxpacket.common.Packet;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand All @@ -34,11 +34,10 @@ public interface NioBufferHandler<T> {
/**
* Next available packet.
* @param argument user argument.
* @param header pcap header.
* @param packet {@link Packet} object.
* @param packet a taple of {@link PcapPktHdr} and {@link ByteBuffer}.
* @throws ExecutionException execution exception.
* @throws InterruptedException interrupted exception.
*/
void next(T argument, PcapPktHdr header, Future<ByteBuffer> packet) throws ExecutionException, InterruptedException;
void next(T argument, Future<Pair<PcapPktHdr, ByteBuffer>> packet) throws ExecutionException, InterruptedException;

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
*/
public enum PacketHandlerType {

JXPACKET, NETTY_BUFFER, NIO_BUFFER, REACTOR, RXJAVA
JXPACKET, NETTY_BUFFER, NIO_BUFFER

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package com.ardikars.jxnet.spring.boot.autoconfigure.jxpacket;

import com.ardikars.common.tuple.Pair;
import com.ardikars.common.tuple.Tuple;
import com.ardikars.jxnet.DataLinkType;
import com.ardikars.jxnet.PcapHandler;
import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxnet.spring.boot.autoconfigure.PacketHandler;
import com.ardikars.jxnet.spring.boot.autoconfigure.JxpacketHandler;
import com.ardikars.jxpacket.common.Packet;
import com.ardikars.jxpacket.common.UnknownPacket;
import com.ardikars.jxpacket.core.ethernet.Ethernet;
Expand All @@ -42,7 +44,7 @@
* Jxpacket handler.
*
* @author <a href="mailto:[email protected]">Ardika Rommy Sanjaya</a>
* @since 1.4.8
* @since 1.4.9
*/
@ConditionalOnClass({Packet.class, ByteBuf.class})
@Configuration("com.ardikars.jxnet.spring.boot.autoconfigure.jxpacket.jxpacketHandler")
Expand All @@ -51,7 +53,7 @@ public class JxpacketHandlerConfiguration<T> implements PcapHandler<T> {
private static final Log LOG = LogFactory.getLog(JxpacketHandlerConfiguration.class.getName());

private final int rawDataLinkType;
private final PacketHandler<T> packetHandler;
private final JxpacketHandler<T> packetHandler;
private final ExecutorService executorService;

/**
Expand All @@ -62,17 +64,17 @@ public class JxpacketHandlerConfiguration<T> implements PcapHandler<T> {
*/
public JxpacketHandlerConfiguration(@Qualifier("com.ardikars.jxnet.spring.boot.autoconfigure.executorService") ExecutorService executorService,
DataLinkType dataLinkType,
PacketHandler<T> packetHandler) {
JxpacketHandler<T> packetHandler) {
this.rawDataLinkType = dataLinkType != null ? dataLinkType.getValue() : 1;
this.packetHandler = packetHandler;
this.executorService = executorService;
}

@Override
public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) {
Future<Packet> packet = executorService.submit(new Callable<Packet>() {
Future<Pair<PcapPktHdr, Packet>> packet = executorService.submit(new Callable<Pair<PcapPktHdr, Packet>>() {
@Override
public Packet call() throws Exception {
public Pair<PcapPktHdr, Packet> call() throws Exception {
ByteBuf buffer = ByteBufAllocator.DEFAULT.directBuffer(bytes.capacity());
buffer.setBytes(0, bytes);
Packet packet;
Expand All @@ -81,11 +83,11 @@ public Packet call() throws Exception {
} else {
packet = UnknownPacket.newPacket(buffer);
}
return packet;
return Tuple.of(h, packet);
}
});
try {
packetHandler.next(user, h, packet);
packetHandler.next(user, packet);
} catch (ExecutionException | InterruptedException e) {
LOG.warn(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.ardikars.jxnet.spring.boot.autoconfigure.netty;

import com.ardikars.common.tuple.Pair;
import com.ardikars.common.tuple.Tuple;
import com.ardikars.jxnet.PcapHandler;
import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxnet.spring.boot.autoconfigure.NettyBufferHandler;
Expand Down Expand Up @@ -61,16 +63,16 @@ public NettyBufferHandlerConfiguration(@Qualifier("com.ardikars.jxnet.spring.boo

@Override
public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) {
Future<ByteBuf> packet = executorService.submit(new Callable<ByteBuf>() {
Future<Pair<PcapPktHdr, ByteBuf>> packet = executorService.submit(new Callable<Pair<PcapPktHdr, ByteBuf>>() {
@Override
public ByteBuf call() throws Exception {
public Pair<PcapPktHdr, ByteBuf> call() throws Exception {
ByteBuf buffer = ByteBufAllocator.DEFAULT.directBuffer(bytes.capacity());
buffer.setBytes(0, bytes);
return buffer;
return Tuple.of(h.copy(), buffer);
}
});
try {
packetHandler.next(user, h, packet);
packetHandler.next(user, packet);
} catch (Exception e) {
LOG.warn(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.ardikars.jxnet.spring.boot.autoconfigure.nio;

import com.ardikars.common.tuple.Pair;
import com.ardikars.common.tuple.Tuple;
import com.ardikars.jxnet.PcapHandler;
import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxnet.spring.boot.autoconfigure.NioBufferHandler;
Expand Down Expand Up @@ -59,14 +61,14 @@ public NioBufferHandlerConfiguration(@Qualifier("com.ardikars.jxnet.spring.boot.

@Override
public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) {
Future<ByteBuffer> packet = executorService.submit(new Callable<ByteBuffer>() {
Future<Pair<PcapPktHdr, ByteBuffer>> packet = executorService.submit(new Callable<Pair<PcapPktHdr, ByteBuffer>>() {
@Override
public ByteBuffer call() throws Exception {
return bytes;
public Pair<PcapPktHdr, ByteBuffer> call() throws Exception {
return Tuple.of(h, bytes);
}
});
try {
packetHandler.next(user, h, packet);
packetHandler.next(user, packet);
} catch (Exception e) {
LOG.warn(e.getMessage());
}
Expand Down
Loading

0 comments on commit 6ab9260

Please sign in to comment.