From 0577d7a24c50efb882f0753ded74b91a3c383ab5 Mon Sep 17 00:00:00 2001 From: Ardika Rommy Sanjaya Date: Thu, 3 Jan 2019 10:45:11 +0700 Subject: [PATCH] Add async handler for netty and nio buffer --- gradle/configure.gradle | 4 +- .../AbstractJxnetApplicationRunner.java | 5 ++ .../spring/boot/autoconfigure/Handler.java | 45 ++++++++++ .../boot/autoconfigure/HandlerConfigurer.java | 82 +++++++++++++++++++ .../autoconfigure/JxnetAutoConfiguration.java | 1 + .../autoconfigure/JxpacketAsyncHandler.java | 13 +-- .../boot/autoconfigure/JxpacketHandler.java | 12 +-- .../autoconfigure/NettyBufferHandler.java | 13 +-- .../boot/autoconfigure/NioBufferHandler.java | 13 +-- .../constant/JxnetObjectName.java | 2 + .../constant/PacketHandlerType.java | 2 +- .../JxpacketAsyncHandlerConfiguration.java | 59 +++---------- .../JxpacketHandlerConfiguration.java | 47 ++--------- .../NettyBufferAsyncHandlerConfiguration.java | 67 +++++++++++++++ .../NettyBufferHandlerConfiguration.java | 27 +----- .../NioBufferAsyncHandlerConfiguration.java | 56 +++++++++++++ .../nio/NioBufferHandlerConfiguration.java | 27 +----- .../JxpacketConfigurationSelector.java | 22 +++-- .../DefaultJxpacketAsyncHandler.java | 11 +-- 19 files changed, 304 insertions(+), 204 deletions(-) create mode 100644 jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/Handler.java create mode 100644 jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/HandlerConfigurer.java create mode 100644 jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/netty/NettyBufferAsyncHandlerConfiguration.java create mode 100644 jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/nio/NioBufferAsyncHandlerConfiguration.java diff --git a/gradle/configure.gradle b/gradle/configure.gradle index 5f0d8e5a..042a91dd 100644 --- a/gradle/configure.gradle +++ b/gradle/configure.gradle @@ -7,7 +7,7 @@ ext { NAME = 'Jxnet' GROUP = 'com.ardikars.jxnet' - VERSION = '1.5.3.RC6' + VERSION = '1.5.3.RC7' DESCRIPTION = 'Jxnet is a java library for capturing and sending network packet.' NDK_HOME = "${System.env.NDK_HOME}" @@ -19,7 +19,7 @@ ext { MAVEN_LOCAL_REPOSITORY = "${rootDir}/build/repository" // MAVEN_LOCAL_REPOSITORY = "${System.env.HOME}/.m2/repository" - JAVA_VERSION = '1.8' + JAVA_VERSION = '1.7' JUNIT_VERSION = '4.12' MOCKITO_VERSION = '2.13.0' GRADLE_VERSION = '5.0' diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/AbstractJxnetApplicationRunner.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/AbstractJxnetApplicationRunner.java index 6853c8b1..bdbfa028 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/AbstractJxnetApplicationRunner.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/AbstractJxnetApplicationRunner.java @@ -33,6 +33,11 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; +/** + * Abstract jxnet spring boot runner. + * @param type. + * @since 1.5.3 + */ @Order(Integer.MIN_VALUE) public abstract class AbstractJxnetApplicationRunner implements CommandLineRunner { diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/Handler.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/Handler.java new file mode 100644 index 00000000..539a2d95 --- /dev/null +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/Handler.java @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2015-2018 Jxnet + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.ardikars.jxnet.spring.boot.autoconfigure; + +import com.ardikars.common.annotation.Incubating; +import com.ardikars.jxnet.PcapPktHdr; +import com.ardikars.jxpacket.common.Packet; + +import java.util.concurrent.ExecutionException; + +/** + * Handler + * @param arg type. + * @param packet type. + * @author Ardika Rommy Sanjaya + * @since 1.5.3 + */ +@Incubating +public interface Handler { + + /** + * Next available packet. + * @param argument user argument. + * @param packet a tuple of {@link PcapPktHdr} and {@link Packet}. + * @throws ExecutionException execution exception. + * @throws InterruptedException interrupted exception. + */ + void next(T argument, V packet) throws ExecutionException, InterruptedException; + +} diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/HandlerConfigurer.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/HandlerConfigurer.java new file mode 100644 index 00000000..99329930 --- /dev/null +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/HandlerConfigurer.java @@ -0,0 +1,82 @@ +/** + * Copyright (C) 2015-2018 Jxnet + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.ardikars.jxnet.spring.boot.autoconfigure; + +import com.ardikars.common.annotation.Incubating; +import com.ardikars.jxnet.DataLinkType; +import com.ardikars.jxnet.context.Context; +import com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName; +import com.ardikars.jxpacket.common.Packet; +import com.ardikars.jxpacket.common.UnknownPacket; +import com.ardikars.jxpacket.core.ethernet.Ethernet; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.nio.ByteBuffer; +import java.util.concurrent.ExecutorService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +/** + * + * @param type. + * @param packet type. + * @author Ardika Rommy Sanjaya + */ +@Incubating +public class HandlerConfigurer { + + @Autowired + @Qualifier(JxnetObjectName.EXECUTOR_SERVICE_BEAN_NAME) + protected ExecutorService executorService; + + @Autowired + @Qualifier(JxnetObjectName.CONTEXT_BEAN_NAME) + protected Context context; + + @Autowired + @Qualifier(JxnetObjectName.DATALINK_TYPE_BEAN_NAME) + protected DataLinkType dataLinkType; + + @Autowired + private Handler handler; + + /** + * Decode buffer. + * @param bytes direct byte buffer. + * @return returns {@link Packet}. + */ + public Packet decode(ByteBuffer bytes) { + ByteBuf buffer = Unpooled.wrappedBuffer(bytes); + Packet packet; + if (dataLinkType.getValue() == 1) { + packet = Ethernet.newPacket(buffer); + } else { + packet = UnknownPacket.newPacket(buffer); + } + return packet; + } + + /** + * Get handler. + * @return returns {@link Handler} implementation. + */ + public Handler getHandler() { + return handler; + } + +} diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxnetAutoConfiguration.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxnetAutoConfiguration.java index 42f0d014..f0751837 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxnetAutoConfiguration.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxnetAutoConfiguration.java @@ -56,6 +56,7 @@ import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; + import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfigureOrder; diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketAsyncHandler.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketAsyncHandler.java index f6c1c7f9..d0e240b7 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketAsyncHandler.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketAsyncHandler.java @@ -21,8 +21,6 @@ import com.ardikars.jxnet.PcapPktHdr; import com.ardikars.jxpacket.common.Packet; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; /** * Callback function used for capturing packets. @@ -30,15 +28,6 @@ * @author Ardika Rommy Sanjaya * @since 1.4.9 */ -public interface JxpacketAsyncHandler { - - /** - * Next available packet. - * @param argument user argument. - * @param packet a tuple of {@link PcapPktHdr} and {@link Packet}. - * @throws ExecutionException execution exception. - * @throws InterruptedException interrupted exception. - */ - void next(T argument, CompletableFuture> packet) throws ExecutionException, InterruptedException; +public interface JxpacketAsyncHandler extends Handler> { } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketHandler.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketHandler.java index fa37caad..38c50000 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketHandler.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketHandler.java @@ -21,7 +21,6 @@ import com.ardikars.jxnet.PcapPktHdr; import com.ardikars.jxpacket.common.Packet; -import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; /** @@ -30,15 +29,6 @@ * @author Ardika Rommy Sanjaya * @since 1.4.9 */ -public interface JxpacketHandler { - - /** - * Next available packet. - * @param argument user argument. - * @param packet a tuple of {@link PcapPktHdr} and {@link Packet}. - * @throws ExecutionException execution exception. - * @throws InterruptedException interrupted exception. - */ - void next(T argument, Future> packet) throws ExecutionException, InterruptedException; +public interface JxpacketHandler extends Handler>> { } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NettyBufferHandler.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NettyBufferHandler.java index ff918f47..1f01fdcc 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NettyBufferHandler.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NettyBufferHandler.java @@ -20,7 +20,7 @@ import com.ardikars.common.tuple.Pair; import com.ardikars.jxnet.PcapPktHdr; import io.netty.buffer.ByteBuf; -import java.util.concurrent.ExecutionException; + import java.util.concurrent.Future; /** @@ -29,15 +29,6 @@ * @author Ardika Rommy Sanjaya * @since 1.4.9 */ -public interface NettyBufferHandler { - - /** - * Next available packet. - * @param argument user argument. - * @param packet a taple of {@link PcapPktHdr} and {@link ByteBuf}. - * @throws ExecutionException execution exception. - * @throws InterruptedException interrupted exception. - */ - void next(T argument, Future> packet) throws ExecutionException, InterruptedException; +public interface NettyBufferHandler extends Handler>> { } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NioBufferHandler.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NioBufferHandler.java index a2cc2397..31efbf08 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NioBufferHandler.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NioBufferHandler.java @@ -19,8 +19,8 @@ import com.ardikars.common.tuple.Pair; import com.ardikars.jxnet.PcapPktHdr; + import java.nio.ByteBuffer; -import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; /** @@ -29,15 +29,6 @@ * @author Ardika Rommy Sanjaya * @since 1.4.9 */ -public interface NioBufferHandler { - - /** - * Next available packet. - * @param argument user argument. - * @param packet a taple of {@link PcapPktHdr} and {@link ByteBuffer}. - * @throws ExecutionException execution exception. - * @throws InterruptedException interrupted exception. - */ - void next(T argument, Future> packet) throws ExecutionException, InterruptedException; +public interface NioBufferHandler extends Handler>> { } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/JxnetObjectName.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/JxnetObjectName.java index 38f5385d..e80f0fb4 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/JxnetObjectName.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/JxnetObjectName.java @@ -40,7 +40,9 @@ public final class JxnetObjectName { public static final String JXPACKET_HANDLER_CONFIGURATION_BEAN_NAME = "com.ardikras.jxnet.jxpacketHandlerConfiguration"; public static final String JXPACKET_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME = "com.ardikras.jxnet.jxpacketAsyncHandlerConfiguration"; public static final String NETTY_BUFFER_HANDLER_CONFIGURATION_BEAN_NAME = "com.ardikras.jxnet.nettyBufferHandlerConfiguration"; + public static final String NETTY_BUFFER_ASYCN_HANDLER_CONFIGURATION_BEAN_NAME = "com.ardikras.jxnet.nettyBufferAsycHandlerConfiguration"; public static final String NIO_BUFFER_HANDLER_CONFIGURATION_BEAN_NAME = "com.ardikras.jxnet.nioBufferHandlerConfiguration"; + public static final String NIO_BUFFER_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME = "com.ardikras.jxnet.nioBufferAsyncHandlerConfiguration"; public static final String PCAP_BUILDER_BEAN_NAME = "com.ardikras.jxnet.pcapBuilder"; public static final String JVM_BEAN_NAME = "com.ardikras.jxnet.jvm"; diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/PacketHandlerType.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/PacketHandlerType.java index de4f3229..175902cc 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/PacketHandlerType.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/PacketHandlerType.java @@ -25,6 +25,6 @@ */ public enum PacketHandlerType { - JXPACKET, JXPACKET_ASYNC, NETTY_BUFFER, NIO_BUFFER + JXPACKET, JXPACKET_ASYNC, NETTY_BUFFER, NETTY_BUFFER_ASYNC, NIO_BUFFER, NIO_BUFFER_ASYNC } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketAsyncHandlerConfiguration.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketAsyncHandlerConfiguration.java index e996dad3..922f76bc 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketAsyncHandlerConfiguration.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketAsyncHandlerConfiguration.java @@ -17,78 +17,41 @@ package com.ardikars.jxnet.spring.boot.autoconfigure.jxpacket; -import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.DATALINK_TYPE_BEAN_NAME; -import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.EXECUTOR_SERVICE_BEAN_NAME; import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.JXPACKET_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME; import com.ardikars.common.logging.Logger; import com.ardikars.common.logging.LoggerFactory; 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.JxpacketAsyncHandler; +import com.ardikars.jxnet.spring.boot.autoconfigure.HandlerConfigurer; import com.ardikars.jxpacket.common.Packet; -import com.ardikars.jxpacket.common.UnknownPacket; -import com.ardikars.jxpacket.core.ethernet.Ethernet; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import java.nio.ByteBuffer; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.function.Supplier; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; @ConditionalOnClass({Packet.class, ByteBuf.class}) @Configuration(JXPACKET_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME) -public class JxpacketAsyncHandlerConfiguration implements PcapHandler { +public class JxpacketAsyncHandlerConfiguration extends HandlerConfigurer> implements PcapHandler { private static final Logger LOGGER = LoggerFactory.getLogger(JxpacketHandlerConfiguration.class); - private final int rawDataLinkType; - private final JxpacketAsyncHandler packetHandler; - private final ExecutorService executorService; - - /** - * Jxpacket async handler configuration (completable future). - * @param executorService thread pool. - * @param dataLinkType datalink type. - * @param packetHandler packet handler. - */ - public JxpacketAsyncHandlerConfiguration(@Qualifier(EXECUTOR_SERVICE_BEAN_NAME) ExecutorService executorService, - @Qualifier(DATALINK_TYPE_BEAN_NAME) DataLinkType dataLinkType, - JxpacketAsyncHandler packetHandler) { - this.rawDataLinkType = dataLinkType != null ? dataLinkType.getValue() : 1; - this.packetHandler = packetHandler; - this.executorService = executorService; - } - @Override - public void nextPacket(T user, PcapPktHdr h, ByteBuffer bytes) { - CompletableFuture> packet = CompletableFuture.supplyAsync(new Supplier>() { + public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) { + executorService.execute(new Runnable() { @Override - public Pair get() { - ByteBuf buffer = Unpooled.wrappedBuffer(bytes); - Packet packet; - if (rawDataLinkType == 1) { - packet = Ethernet.newPacket(buffer); - } else { - packet = UnknownPacket.newPacket(buffer); + public void run() { + try { + getHandler().next(user, Tuple.of(h, decode(bytes))); + } catch (ExecutionException | InterruptedException e) { + LOGGER.warn(e); } - return Tuple.of(h, packet); } - }, executorService); - try { - packetHandler.next(user, packet); - } catch (ExecutionException | InterruptedException e) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn(e.getMessage()); - } - } + }); } + } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketHandlerConfiguration.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketHandlerConfiguration.java index 8767100a..df9ff98d 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketHandlerConfiguration.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketHandlerConfiguration.java @@ -17,29 +17,21 @@ package com.ardikars.jxnet.spring.boot.autoconfigure.jxpacket; -import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.DATALINK_TYPE_BEAN_NAME; -import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.EXECUTOR_SERVICE_BEAN_NAME; import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.JXPACKET_HANDLER_CONFIGURATION_BEAN_NAME; import com.ardikars.common.logging.Logger; import com.ardikars.common.logging.LoggerFactory; 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.JxpacketHandler; +import com.ardikars.jxnet.spring.boot.autoconfigure.HandlerConfigurer; import com.ardikars.jxpacket.common.Packet; -import com.ardikars.jxpacket.common.UnknownPacket; -import com.ardikars.jxpacket.core.ethernet.Ethernet; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import java.nio.ByteBuffer; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; @@ -51,49 +43,22 @@ */ @ConditionalOnClass({Packet.class, ByteBuf.class}) @Configuration(JXPACKET_HANDLER_CONFIGURATION_BEAN_NAME) -public class JxpacketHandlerConfiguration implements PcapHandler { +public class JxpacketHandlerConfiguration extends HandlerConfigurer>> implements PcapHandler { private static final Logger LOGGER = LoggerFactory.getLogger(JxpacketHandlerConfiguration.class); - private final int rawDataLinkType; - private final JxpacketHandler packetHandler; - private final ExecutorService executorService; - - /** - * - * @param executorService thread pool. - * @param dataLinkType datalink type. - * @param packetHandler callback function. - */ - public JxpacketHandlerConfiguration(@Qualifier(EXECUTOR_SERVICE_BEAN_NAME) ExecutorService executorService, - @Qualifier(DATALINK_TYPE_BEAN_NAME) DataLinkType dataLinkType, - JxpacketHandler 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 = executorService.submit(new Callable>() { + final Future> packet = executorService.submit(new Callable>() { @Override public Pair call() throws Exception { - ByteBuf buffer = Unpooled.wrappedBuffer(bytes); - Packet packet; - if (rawDataLinkType == 1) { - packet = Ethernet.newPacket(buffer); - } else { - packet = UnknownPacket.newPacket(buffer); - } - return Tuple.of(h, packet); + return Tuple.of(h, decode(bytes)); } }); try { - packetHandler.next(user, packet); + getHandler().next(user, packet); } catch (ExecutionException | InterruptedException e) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn(e.getMessage()); - } + LOGGER.warn(e.getMessage()); } } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/netty/NettyBufferAsyncHandlerConfiguration.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/netty/NettyBufferAsyncHandlerConfiguration.java new file mode 100644 index 00000000..a3e96984 --- /dev/null +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/netty/NettyBufferAsyncHandlerConfiguration.java @@ -0,0 +1,67 @@ +/** + * Copyright (C) 2015-2018 Jxnet + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.ardikars.jxnet.spring.boot.autoconfigure.netty; + +import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.NETTY_BUFFER_ASYCN_HANDLER_CONFIGURATION_BEAN_NAME; + +import com.ardikars.common.logging.Logger; +import com.ardikars.common.logging.LoggerFactory; +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.HandlerConfigurer; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.nio.ByteBuffer; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.annotation.Configuration; + +/** + * Async netty buffer. + * + * @param type. + * @author Ardika Rommy Sanjaya + * @since 1.5.3 + */ +@ConditionalOnClass({ByteBuf.class}) +@Configuration(NETTY_BUFFER_ASYCN_HANDLER_CONFIGURATION_BEAN_NAME) +public class NettyBufferAsyncHandlerConfiguration extends HandlerConfigurer>> implements PcapHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(NettyBufferAsyncHandlerConfiguration.class); + + @Override + public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) { + Future> packet = executorService.submit(new Callable>() { + @Override + public Pair call() throws Exception { + ByteBuf buf = Unpooled.wrappedBuffer(bytes); + return Tuple.of(h, buf); + } + }); + try { + getHandler().next(user, packet); + } catch (ExecutionException | InterruptedException e) { + LOGGER.warn(e); + } + } + +} diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/netty/NettyBufferHandlerConfiguration.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/netty/NettyBufferHandlerConfiguration.java index 1cbc36dd..60ce0f96 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/netty/NettyBufferHandlerConfiguration.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/netty/NettyBufferHandlerConfiguration.java @@ -17,7 +17,6 @@ package com.ardikars.jxnet.spring.boot.autoconfigure.netty; -import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.EXECUTOR_SERVICE_BEAN_NAME; import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.NETTY_BUFFER_HANDLER_CONFIGURATION_BEAN_NAME; import com.ardikars.common.logging.Logger; @@ -26,15 +25,13 @@ import com.ardikars.common.tuple.Tuple; import com.ardikars.jxnet.PcapHandler; import com.ardikars.jxnet.PcapPktHdr; -import com.ardikars.jxnet.spring.boot.autoconfigure.NettyBufferHandler; +import com.ardikars.jxnet.spring.boot.autoconfigure.HandlerConfigurer; import com.ardikars.jxpacket.common.Packet; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.nio.ByteBuffer; import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; @@ -46,24 +43,10 @@ */ @ConditionalOnClass({Packet.class, ByteBuf.class}) @Configuration(NETTY_BUFFER_HANDLER_CONFIGURATION_BEAN_NAME) -public class NettyBufferHandlerConfiguration implements PcapHandler { +public class NettyBufferHandlerConfiguration extends HandlerConfigurer>> implements PcapHandler { private static final Logger LOGGER = LoggerFactory.getLogger(NettyBufferHandlerConfiguration.class); - private final NettyBufferHandler packetHandler; - private final ExecutorService executorService; - - /** - * - * @param executorService thread pool. - * @param packetHandler callback function. - */ - public NettyBufferHandlerConfiguration(@Qualifier(EXECUTOR_SERVICE_BEAN_NAME) ExecutorService executorService, - NettyBufferHandler packetHandler) { - this.packetHandler = packetHandler; - this.executorService = executorService; - } - @Override public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) { Future> packet = executorService.submit(new Callable>() { @@ -74,11 +57,9 @@ public Pair call() throws Exception { } }); try { - packetHandler.next(user, packet); + getHandler().next(user, packet); } catch (Exception e) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn(e.getMessage()); - } + LOGGER.warn(e.getMessage()); } } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/nio/NioBufferAsyncHandlerConfiguration.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/nio/NioBufferAsyncHandlerConfiguration.java new file mode 100644 index 00000000..9fbe38d6 --- /dev/null +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/nio/NioBufferAsyncHandlerConfiguration.java @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2015-2018 Jxnet + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.ardikars.jxnet.spring.boot.autoconfigure.nio; + +import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.NIO_BUFFER_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME; + +import com.ardikars.common.logging.Logger; +import com.ardikars.common.logging.LoggerFactory; +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.HandlerConfigurer; +import java.nio.ByteBuffer; +import java.util.concurrent.ExecutionException; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.annotation.Configuration; + +/** + * Nio buffer async handler. + * @param type. + * @author Ardika Rommy Sanjaya + * @since 1.5.3 + */ +@ConditionalOnClass({ByteBuffer.class}) +@Configuration(NIO_BUFFER_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME) +public class NioBufferAsyncHandlerConfiguration extends HandlerConfigurer> implements PcapHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(NioBufferAsyncHandlerConfiguration.class); + + @Override + public void nextPacket(T user, PcapPktHdr h, ByteBuffer bytes) { + try { + getHandler().next(user, Tuple.of(h, bytes)); + } catch (ExecutionException | InterruptedException e) { + LOGGER.debug(e); + } + } + +} diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/nio/NioBufferHandlerConfiguration.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/nio/NioBufferHandlerConfiguration.java index 66d1c7f0..985381c4 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/nio/NioBufferHandlerConfiguration.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/nio/NioBufferHandlerConfiguration.java @@ -17,7 +17,6 @@ package com.ardikars.jxnet.spring.boot.autoconfigure.nio; -import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.EXECUTOR_SERVICE_BEAN_NAME; import static com.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.NIO_BUFFER_HANDLER_CONFIGURATION_BEAN_NAME; import com.ardikars.common.logging.Logger; @@ -26,13 +25,11 @@ import com.ardikars.common.tuple.Tuple; import com.ardikars.jxnet.PcapHandler; import com.ardikars.jxnet.PcapPktHdr; -import com.ardikars.jxnet.spring.boot.autoconfigure.NioBufferHandler; +import com.ardikars.jxnet.spring.boot.autoconfigure.HandlerConfigurer; import com.ardikars.jxpacket.common.Packet; import java.nio.ByteBuffer; import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; @@ -44,24 +41,10 @@ */ @ConditionalOnClass(Packet.class) @Configuration(NIO_BUFFER_HANDLER_CONFIGURATION_BEAN_NAME) -public class NioBufferHandlerConfiguration implements PcapHandler { +public class NioBufferHandlerConfiguration extends HandlerConfigurer>> implements PcapHandler { private static final Logger LOGGER = LoggerFactory.getLogger(NioBufferHandlerConfiguration.class); - private final NioBufferHandler packetHandler; - private final ExecutorService executorService; - - /** - * - * @param executorService thread pool. - * @param packetHandler callback function. - */ - public NioBufferHandlerConfiguration(@Qualifier(EXECUTOR_SERVICE_BEAN_NAME) ExecutorService executorService, - NioBufferHandler packetHandler) { - this.packetHandler = packetHandler; - this.executorService = executorService; - } - @Override public void nextPacket(final T user, final PcapPktHdr h, final ByteBuffer bytes) { Future> packet = executorService.submit(new Callable>() { @@ -71,11 +54,9 @@ public Pair call() throws Exception { } }); try { - packetHandler.next(user, packet); + getHandler().next(user, packet); } catch (Exception e) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn(e.getMessage()); - } + LOGGER.warn(e.getMessage()); } } diff --git a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/selector/JxpacketConfigurationSelector.java b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/selector/JxpacketConfigurationSelector.java index 288b600e..692e47e9 100644 --- a/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/selector/JxpacketConfigurationSelector.java +++ b/jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/selector/JxpacketConfigurationSelector.java @@ -45,24 +45,22 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) { PacketHandlerType type = attributes.getEnum("packetHandlerType"); switch (type) { case NETTY_BUFFER: - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Applying netty buffer handler configuration."); - } + LOGGER.debug("Applying netty buffer handler configuration."); return new String[] {"com.ardikars.jxnet.spring.boot.autoconfigure.netty.NettyBufferHandlerConfiguration"}; + case NETTY_BUFFER_ASYNC: + LOGGER.debug("Applying netty buffer async handler configuration."); + return new String[] {"com.ardikars.jxnet.spring.boot.autoconfigure.netty.NettyBufferAsyncHandlerConfiguration"}; case NIO_BUFFER: - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Applying nio buffer handler configuration."); - } + LOGGER.debug("Applying nio buffer handler configuration."); return new String[] {"com.ardikars.jxnet.spring.boot.autoconfigure.nio.NioBufferHandlerConfiguration"}; + case NIO_BUFFER_ASYNC: + LOGGER.debug("Applying nio buffer async handler configuration."); + return new String[] {"com.ardikars.jxnet.spring.boot.autoconfigure.nio.NioBufferAsyncHandlerConfiguration"}; case JXPACKET_ASYNC: - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Applying jxpacket async handler configuration."); - } + LOGGER.debug("Applying jxpacket async handler configuration."); return new String[] {"com.ardikars.jxnet.spring.boot.autoconfigure.jxpacket.JxpacketAsyncHandlerConfiguration"}; default: - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Applying jxpacket handler configuration."); - } + LOGGER.debug("Applying jxpacket handler configuration."); return new String[] {"com.ardikars.jxnet.spring.boot.autoconfigure.jxpacket.JxpacketHandlerConfiguration"}; } } diff --git a/jxnet-spring-boot-starter-example/src/main/java/com/ardikars/jxnet/spring/boot/starter/example/configuration/DefaultJxpacketAsyncHandler.java b/jxnet-spring-boot-starter-example/src/main/java/com/ardikars/jxnet/spring/boot/starter/example/configuration/DefaultJxpacketAsyncHandler.java index e3d1b096..65ebd41a 100644 --- a/jxnet-spring-boot-starter-example/src/main/java/com/ardikars/jxnet/spring/boot/starter/example/configuration/DefaultJxpacketAsyncHandler.java +++ b/jxnet-spring-boot-starter-example/src/main/java/com/ardikars/jxnet/spring/boot/starter/example/configuration/DefaultJxpacketAsyncHandler.java @@ -24,9 +24,7 @@ //import com.ardikars.jxnet.spring.boot.autoconfigure.constant.PacketHandlerType; import com.ardikars.jxpacket.common.Packet; import java.util.Iterator; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import java.util.function.Consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,14 +56,9 @@ private void print(Pair packet) { } @Override - public void next(String argument, CompletableFuture> packet) throws ExecutionException, InterruptedException { + public void next(String argument, Pair packet) throws ExecutionException, InterruptedException { LOGGER.info("User argument : {}", argument); - packet.thenAccept(new Consumer>() { - @Override - public void accept(Pair pcapPktHdrPacketPair) { - print(pcapPktHdrPacketPair); - } - }); + print(packet); } }