Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 26, 2020. It is now read-only.
/JxnetPublic archive

Commit0577d7a

Browse files
committed
Add async handler for netty and nio buffer
1 parent5ed06ec commit0577d7a

File tree

19 files changed

+304
-204
lines changed

19 files changed

+304
-204
lines changed

‎gradle/configure.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ext {
77

88
NAME='Jxnet'
99
GROUP='com.ardikars.jxnet'
10-
VERSION='1.5.3.RC6'
10+
VERSION='1.5.3.RC7'
1111
DESCRIPTION='Jxnet is a java library for capturing and sending network packet.'
1212

1313
NDK_HOME="${System.env.NDK_HOME}"
@@ -19,7 +19,7 @@ ext {
1919
MAVEN_LOCAL_REPOSITORY="${rootDir}/build/repository"
2020
//MAVEN_LOCAL_REPOSITORY = "${System.env.HOME}/.m2/repository"
2121

22-
JAVA_VERSION='1.8'
22+
JAVA_VERSION='1.7'
2323
JUNIT_VERSION='4.12'
2424
MOCKITO_VERSION='2.13.0'
2525
GRADLE_VERSION='5.0'

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/AbstractJxnetApplicationRunner.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
importorg.springframework.boot.CommandLineRunner;
3434
importorg.springframework.core.annotation.Order;
3535

36+
/**
37+
* Abstract jxnet spring boot runner.
38+
* @param <T> type.
39+
* @since 1.5.3
40+
*/
3641
@Order(Integer.MIN_VALUE)
3742
publicabstractclassAbstractJxnetApplicationRunner<T>implementsCommandLineRunner {
3843

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (C) 2015-2018 Jxnet
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
packagecom.ardikars.jxnet.spring.boot.autoconfigure;
19+
20+
importcom.ardikars.common.annotation.Incubating;
21+
importcom.ardikars.jxnet.PcapPktHdr;
22+
importcom.ardikars.jxpacket.common.Packet;
23+
24+
importjava.util.concurrent.ExecutionException;
25+
26+
/**
27+
* Handler
28+
* @param <T> arg type.
29+
* @param <V> packet type.
30+
* @author <a href="mailto:contact@ardikars.com">Ardika Rommy Sanjaya</a>
31+
* @since 1.5.3
32+
*/
33+
@Incubating
34+
publicinterfaceHandler<T,V> {
35+
36+
/**
37+
* Next available packet.
38+
* @param argument user argument.
39+
* @param packet a tuple of {@link PcapPktHdr} and {@link Packet}.
40+
* @throws ExecutionException execution exception.
41+
* @throws InterruptedException interrupted exception.
42+
*/
43+
voidnext(Targument,Vpacket)throwsExecutionException,InterruptedException;
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright (C) 2015-2018 Jxnet
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
packagecom.ardikars.jxnet.spring.boot.autoconfigure;
19+
20+
importcom.ardikars.common.annotation.Incubating;
21+
importcom.ardikars.jxnet.DataLinkType;
22+
importcom.ardikars.jxnet.context.Context;
23+
importcom.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName;
24+
importcom.ardikars.jxpacket.common.Packet;
25+
importcom.ardikars.jxpacket.common.UnknownPacket;
26+
importcom.ardikars.jxpacket.core.ethernet.Ethernet;
27+
importio.netty.buffer.ByteBuf;
28+
importio.netty.buffer.Unpooled;
29+
importjava.nio.ByteBuffer;
30+
importjava.util.concurrent.ExecutorService;
31+
importorg.springframework.beans.factory.annotation.Autowired;
32+
importorg.springframework.beans.factory.annotation.Qualifier;
33+
34+
/**
35+
*
36+
* @param <T> type.
37+
* @param <V> packet type.
38+
* @author <a href="mailto:contact@ardikars.com">Ardika Rommy Sanjaya</a>
39+
*/
40+
@Incubating
41+
publicclassHandlerConfigurer<T,V> {
42+
43+
@Autowired
44+
@Qualifier(JxnetObjectName.EXECUTOR_SERVICE_BEAN_NAME)
45+
protectedExecutorServiceexecutorService;
46+
47+
@Autowired
48+
@Qualifier(JxnetObjectName.CONTEXT_BEAN_NAME)
49+
protectedContextcontext;
50+
51+
@Autowired
52+
@Qualifier(JxnetObjectName.DATALINK_TYPE_BEAN_NAME)
53+
protectedDataLinkTypedataLinkType;
54+
55+
@Autowired
56+
privateHandler<T,V>handler;
57+
58+
/**
59+
* Decode buffer.
60+
* @param bytes direct byte buffer.
61+
* @return returns {@link Packet}.
62+
*/
63+
publicPacketdecode(ByteBufferbytes) {
64+
ByteBufbuffer =Unpooled.wrappedBuffer(bytes);
65+
Packetpacket;
66+
if (dataLinkType.getValue() ==1) {
67+
packet =Ethernet.newPacket(buffer);
68+
}else {
69+
packet =UnknownPacket.newPacket(buffer);
70+
}
71+
returnpacket;
72+
}
73+
74+
/**
75+
* Get handler.
76+
* @return returns {@link Handler} implementation.
77+
*/
78+
publicHandler<T,V>getHandler() {
79+
returnhandler;
80+
}
81+
82+
}

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxnetAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
importjava.util.List;
5757
importjava.util.concurrent.ExecutorService;
5858
importjava.util.concurrent.Executors;
59+
5960
importorg.springframework.beans.factory.annotation.Qualifier;
6061
importorg.springframework.beans.factory.annotation.Value;
6162
importorg.springframework.boot.autoconfigure.AutoConfigureOrder;

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketAsyncHandler.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,13 @@
2121
importcom.ardikars.jxnet.PcapPktHdr;
2222
importcom.ardikars.jxpacket.common.Packet;
2323

24-
importjava.util.concurrent.CompletableFuture;
25-
importjava.util.concurrent.ExecutionException;
2624

2725
/**
2826
* Callback function used for capturing packets.
2927
*
3028
* @author <a href="mailto:contact@ardikars.com">Ardika Rommy Sanjaya</a>
3129
* @since 1.4.9
3230
*/
33-
publicinterfaceJxpacketAsyncHandler<T> {
34-
35-
/**
36-
* Next available packet.
37-
* @param argument user argument.
38-
* @param packet a tuple of {@link PcapPktHdr} and {@link Packet}.
39-
* @throws ExecutionException execution exception.
40-
* @throws InterruptedException interrupted exception.
41-
*/
42-
voidnext(Targument,CompletableFuture<Pair<PcapPktHdr,Packet>>packet)throwsExecutionException,InterruptedException;
31+
publicinterfaceJxpacketAsyncHandler<T>extendsHandler<T,Pair<PcapPktHdr,Packet>> {
4332

4433
}

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/JxpacketHandler.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
importcom.ardikars.jxnet.PcapPktHdr;
2222
importcom.ardikars.jxpacket.common.Packet;
2323

24-
importjava.util.concurrent.ExecutionException;
2524
importjava.util.concurrent.Future;
2625

2726
/**
@@ -30,15 +29,6 @@
3029
* @author <a href="mailto:contact@ardikars.com">Ardika Rommy Sanjaya</a>
3130
* @since 1.4.9
3231
*/
33-
publicinterfaceJxpacketHandler<T> {
34-
35-
/**
36-
* Next available packet.
37-
* @param argument user argument.
38-
* @param packet a tuple of {@link PcapPktHdr} and {@link Packet}.
39-
* @throws ExecutionException execution exception.
40-
* @throws InterruptedException interrupted exception.
41-
*/
42-
voidnext(Targument,Future<Pair<PcapPktHdr,Packet>>packet)throwsExecutionException,InterruptedException;
32+
publicinterfaceJxpacketHandler<T>extendsHandler<T,Future<Pair<PcapPktHdr,Packet>>> {
4333

4434
}

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NettyBufferHandler.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
importcom.ardikars.common.tuple.Pair;
2121
importcom.ardikars.jxnet.PcapPktHdr;
2222
importio.netty.buffer.ByteBuf;
23-
importjava.util.concurrent.ExecutionException;
23+
2424
importjava.util.concurrent.Future;
2525

2626
/**
@@ -29,15 +29,6 @@
2929
* @author <a href="mailto:contact@ardikars.com">Ardika Rommy Sanjaya</a>
3030
* @since 1.4.9
3131
*/
32-
publicinterfaceNettyBufferHandler<T> {
33-
34-
/**
35-
* Next available packet.
36-
* @param argument user argument.
37-
* @param packet a taple of {@link PcapPktHdr} and {@link ByteBuf}.
38-
* @throws ExecutionException execution exception.
39-
* @throws InterruptedException interrupted exception.
40-
*/
41-
voidnext(Targument,Future<Pair<PcapPktHdr,ByteBuf>>packet)throwsExecutionException,InterruptedException;
32+
publicinterfaceNettyBufferHandler<T>extendsHandler<T,Future<Pair<PcapPktHdr,ByteBuf>>> {
4233

4334
}

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/NioBufferHandler.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
importcom.ardikars.common.tuple.Pair;
2121
importcom.ardikars.jxnet.PcapPktHdr;
22+
2223
importjava.nio.ByteBuffer;
23-
importjava.util.concurrent.ExecutionException;
2424
importjava.util.concurrent.Future;
2525

2626
/**
@@ -29,15 +29,6 @@
2929
* @author <a href="mailto:contact@ardikars.com">Ardika Rommy Sanjaya</a>
3030
* @since 1.4.9
3131
*/
32-
publicinterfaceNioBufferHandler<T> {
33-
34-
/**
35-
* Next available packet.
36-
* @param argument user argument.
37-
* @param packet a taple of {@link PcapPktHdr} and {@link ByteBuffer}.
38-
* @throws ExecutionException execution exception.
39-
* @throws InterruptedException interrupted exception.
40-
*/
41-
voidnext(Targument,Future<Pair<PcapPktHdr,ByteBuffer>>packet)throwsExecutionException,InterruptedException;
32+
publicinterfaceNioBufferHandler<T>extendsHandler<T,Future<Pair<PcapPktHdr,ByteBuffer>>> {
4233

4334
}

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/JxnetObjectName.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public final class JxnetObjectName {
4040
publicstaticfinalStringJXPACKET_HANDLER_CONFIGURATION_BEAN_NAME ="com.ardikras.jxnet.jxpacketHandlerConfiguration";
4141
publicstaticfinalStringJXPACKET_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME ="com.ardikras.jxnet.jxpacketAsyncHandlerConfiguration";
4242
publicstaticfinalStringNETTY_BUFFER_HANDLER_CONFIGURATION_BEAN_NAME ="com.ardikras.jxnet.nettyBufferHandlerConfiguration";
43+
publicstaticfinalStringNETTY_BUFFER_ASYCN_HANDLER_CONFIGURATION_BEAN_NAME ="com.ardikras.jxnet.nettyBufferAsycHandlerConfiguration";
4344
publicstaticfinalStringNIO_BUFFER_HANDLER_CONFIGURATION_BEAN_NAME ="com.ardikras.jxnet.nioBufferHandlerConfiguration";
45+
publicstaticfinalStringNIO_BUFFER_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME ="com.ardikras.jxnet.nioBufferAsyncHandlerConfiguration";
4446
publicstaticfinalStringPCAP_BUILDER_BEAN_NAME ="com.ardikras.jxnet.pcapBuilder";
4547
publicstaticfinalStringJVM_BEAN_NAME ="com.ardikras.jxnet.jvm";
4648

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/constant/PacketHandlerType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
*/
2626
publicenumPacketHandlerType {
2727

28-
JXPACKET,JXPACKET_ASYNC,NETTY_BUFFER,NIO_BUFFER
28+
JXPACKET,JXPACKET_ASYNC,NETTY_BUFFER,NETTY_BUFFER_ASYNC,NIO_BUFFER,NIO_BUFFER_ASYNC
2929

3030
}

‎jxnet-spring-boot-autoconfigure/src/main/java/com/ardikars/jxnet/spring/boot/autoconfigure/jxpacket/JxpacketAsyncHandlerConfiguration.java

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,78 +17,41 @@
1717

1818
packagecom.ardikars.jxnet.spring.boot.autoconfigure.jxpacket;
1919

20-
importstaticcom.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.DATALINK_TYPE_BEAN_NAME;
21-
importstaticcom.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.EXECUTOR_SERVICE_BEAN_NAME;
2220
importstaticcom.ardikars.jxnet.spring.boot.autoconfigure.constant.JxnetObjectName.JXPACKET_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME;
2321

2422
importcom.ardikars.common.logging.Logger;
2523
importcom.ardikars.common.logging.LoggerFactory;
2624
importcom.ardikars.common.tuple.Pair;
2725
importcom.ardikars.common.tuple.Tuple;
28-
importcom.ardikars.jxnet.DataLinkType;
2926
importcom.ardikars.jxnet.PcapHandler;
3027
importcom.ardikars.jxnet.PcapPktHdr;
31-
importcom.ardikars.jxnet.spring.boot.autoconfigure.JxpacketAsyncHandler;
28+
importcom.ardikars.jxnet.spring.boot.autoconfigure.HandlerConfigurer;
3229
importcom.ardikars.jxpacket.common.Packet;
33-
importcom.ardikars.jxpacket.common.UnknownPacket;
34-
importcom.ardikars.jxpacket.core.ethernet.Ethernet;
3530
importio.netty.buffer.ByteBuf;
36-
importio.netty.buffer.Unpooled;
3731
importjava.nio.ByteBuffer;
38-
importjava.util.concurrent.CompletableFuture;
3932
importjava.util.concurrent.ExecutionException;
40-
importjava.util.concurrent.ExecutorService;
41-
importjava.util.function.Supplier;
42-
importorg.springframework.beans.factory.annotation.Qualifier;
4333
importorg.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4434
importorg.springframework.context.annotation.Configuration;
4535

4636
@ConditionalOnClass({Packet.class,ByteBuf.class})
4737
@Configuration(JXPACKET_ASYNC_HANDLER_CONFIGURATION_BEAN_NAME)
48-
publicclassJxpacketAsyncHandlerConfiguration<T>implementsPcapHandler<T> {
38+
publicclassJxpacketAsyncHandlerConfiguration<T>extendsHandlerConfigurer<T,Pair<PcapPktHdr,Packet>>implementsPcapHandler<T> {
4939

5040
privatestaticfinalLoggerLOGGER =LoggerFactory.getLogger(JxpacketHandlerConfiguration.class);
5141

52-
privatefinalintrawDataLinkType;
53-
privatefinalJxpacketAsyncHandler<T>packetHandler;
54-
privatefinalExecutorServiceexecutorService;
55-
56-
/**
57-
* Jxpacket async handler configuration (completable future).
58-
* @param executorService thread pool.
59-
* @param dataLinkType datalink type.
60-
* @param packetHandler packet handler.
61-
*/
62-
publicJxpacketAsyncHandlerConfiguration(@Qualifier(EXECUTOR_SERVICE_BEAN_NAME)ExecutorServiceexecutorService,
63-
@Qualifier(DATALINK_TYPE_BEAN_NAME)DataLinkTypedataLinkType,
64-
JxpacketAsyncHandler<T>packetHandler) {
65-
this.rawDataLinkType =dataLinkType !=null ?dataLinkType.getValue() :1;
66-
this.packetHandler =packetHandler;
67-
this.executorService =executorService;
68-
}
69-
7042
@Override
71-
publicvoidnextPacket(Tuser,PcapPktHdrh,ByteBufferbytes) {
72-
CompletableFuture<Pair<PcapPktHdr,Packet>>packet =CompletableFuture.supplyAsync(newSupplier<Pair<PcapPktHdr,Packet>>() {
43+
publicvoidnextPacket(finalTuser,finalPcapPktHdrh,finalByteBufferbytes) {
44+
executorService.execute(newRunnable() {
7345
@Override
74-
publicPair<PcapPktHdr,Packet>get() {
75-
ByteBufbuffer =Unpooled.wrappedBuffer(bytes);
76-
Packetpacket;
77-
if (rawDataLinkType ==1) {
78-
packet =Ethernet.newPacket(buffer);
79-
}else {
80-
packet =UnknownPacket.newPacket(buffer);
46+
publicvoidrun() {
47+
try {
48+
getHandler().next(user,Tuple.of(h,decode(bytes)));
49+
}catch (ExecutionException |InterruptedExceptione) {
50+
LOGGER.warn(e);
8151
}
82-
returnTuple.of(h,packet);
8352
}
84-
},executorService);
85-
try {
86-
packetHandler.next(user,packet);
87-
}catch (ExecutionException |InterruptedExceptione) {
88-
if (LOGGER.isWarnEnabled()) {
89-
LOGGER.warn(e.getMessage());
90-
}
91-
}
53+
});
9254
}
9355

56+
9457
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp