Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gass-device-report
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
耿迪迪
gass-device-report
Commits
68e0f79f
Commit
68e0f79f
authored
Apr 06, 2023
by
耿迪迪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
原型模式修改udp服务
parent
01d8b83c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
130 additions
and
0 deletions
+130
-0
UdpServerStart.java
...ava/com/zehong/gassdevicereport/netty/UdpServerStart.java
+59
-0
UdpStartProperty.java
...ong/gassdevicereport/netty/property/UdpStartProperty.java
+71
-0
No files found.
src/main/java/com/zehong/gassdevicereport/netty/UdpServerStart.java
0 → 100644
View file @
68e0f79f
package
com
.
zehong
.
gassdevicereport
.
netty
;
import
com.zehong.gassdevicereport.netty.handler.FactoryDetectorUdpServerHandler
;
import
com.zehong.gassdevicereport.netty.handler.UDPServerHandler
;
import
com.zehong.gassdevicereport.netty.property.UdpStartProperty
;
import
io.netty.channel.SimpleChannelInboundHandler
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PostConstruct
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author geng
* upd服务启动类
*/
@Component
public
class
UdpServerStart
implements
ApplicationContextAware
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
UdpServerStart
.
class
);
private
ApplicationContext
applicationContext
;
private
Map
<
Integer
,
Class
>
handlerMap
=
new
HashMap
<
Integer
,
Class
>(){{
put
(
65011
,
UDPServerHandler
.
class
);
//工业报警器
put
(
65012
,
FactoryDetectorUdpServerHandler
.
class
);
}};
@Value
(
"${UDPNetty.ports}"
)
private
int
[]
ports
;
@PostConstruct
public
void
udpStart
(){
UdpStartProperty
property
=
new
UdpStartProperty
();
for
(
int
port
:
ports
){
try
{
property
.
setPort
(
port
);
SimpleChannelInboundHandler
handler
=
(
SimpleChannelInboundHandler
)
applicationContext
.
getBean
(
handlerMap
.
get
(
port
));
property
.
setHandler
(
handler
);
UdpStartProperty
udpServer
=
(
UdpStartProperty
)
property
.
clone
();
udpServer
.
start
();
}
catch
(
CloneNotSupportedException
e
)
{
logger
.
error
(
"udp 原型创建失败!"
);
}
}
}
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
this
.
applicationContext
=
applicationContext
;
}
}
src/main/java/com/zehong/gassdevicereport/netty/property/UdpStartProperty.java
0 → 100644
View file @
68e0f79f
package
com
.
zehong
.
gassdevicereport
.
netty
.
property
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.SimpleChannelInboundHandler
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioDatagramChannel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* @author geng
* upd服务原型类
*/
public
class
UdpStartProperty
implements
Cloneable
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
UdpStartProperty
.
class
);
private
SimpleChannelInboundHandler
handler
;
private
int
port
;
/**
* 启动udp服务
*/
public
void
start
(){
NioEventLoopGroup
group
=
new
NioEventLoopGroup
();
//2.启动器
Bootstrap
bootstrap
=
new
Bootstrap
();
//3.配置启动器
//3.1指定group
bootstrap
.
group
(
group
)
//3.2指定channel
.
channel
(
NioDatagramChannel
.
class
)
//3.3指定为广播模式
.
option
(
ChannelOption
.
SO_BACKLOG
,
128
)
.
handler
(
handler
);
try
{
//4.bind到指定端口,并返回一个channel,该端口就是监听UDP报文的端口
ChannelFuture
channel
=
bootstrap
.
bind
(
this
.
port
).
sync
();
if
(
channel
.
isSuccess
()){
logger
.
info
(
"upd服务启动"
+
this
.
getPort
()
+
"成功"
);
}
}
catch
(
InterruptedException
e
)
{
logger
.
error
(
"upd 服务端口启动失败:"
+
e
);
}
}
public
SimpleChannelInboundHandler
getHandler
()
{
return
handler
;
}
public
void
setHandler
(
SimpleChannelInboundHandler
handler
)
{
this
.
handler
=
handler
;
}
public
int
getPort
()
{
return
port
;
}
public
void
setPort
(
int
port
)
{
this
.
port
=
port
;
}
@Override
public
Object
clone
()
throws
CloneNotSupportedException
{
Object
obj
=
super
.
clone
();
return
obj
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment