Class ServerConnectionHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.SimpleChannelInboundHandler<I>
-
- org.drasyl.peer.connection.handler.SimpleChannelDuplexHandler<Message,Message>
-
- org.drasyl.peer.connection.handler.ThreeWayHandshakeServerHandler<JoinMessage,WelcomeMessage>
-
- org.drasyl.peer.connection.server.ServerConnectionHandler
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler
,io.netty.channel.ChannelInboundHandler
,io.netty.channel.ChannelOutboundHandler
public class ServerConnectionHandler extends ThreeWayHandshakeServerHandler<JoinMessage,WelcomeMessage>
Acts as a guard for in- and outbound connections. A channel is only created, when aJoinMessage
was received. Outgoing messages are dropped unless aJoinMessage
was received. Every other incoming message is also dropped unless aJoinMessage
was received.If a
JoinMessage
was not received inDrasylConfig.getServerHandshakeTimeout()
the connection will be closed.This handler closes the channel if an exception occurs before a
JoinMessage
has been received.
-
-
Field Summary
Fields Modifier and Type Field Description protected CompletableFuture<Void>
handshakeFuture
protected Identity
identity
protected int
networkId
protected Pipeline
pipeline
static String
SERVER_CONNECTION_HANDLER
protected Duration
timeout
protected io.netty.util.concurrent.ScheduledFuture<?>
timeoutFuture
-
Constructor Summary
Constructors Constructor Description ServerConnectionHandler(ServerEnvironment environment)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelActive(io.netty.channel.ChannelHandlerContext ctx)
void
channelRead0(io.netty.channel.ChannelHandlerContext ctx, Message msg)
Is called for each message of typeSimpleChannelDuplexHandler
on inbound channel.protected void
channelWrite0(io.netty.channel.ChannelHandlerContext ctx, Message msg, io.netty.channel.ChannelPromise promise)
Is called for each message of typeSimpleChannelDuplexHandler
on the outbound channel.void
close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)
protected void
createConnection(io.netty.channel.ChannelHandlerContext ctx, JoinMessage requestMessage)
void
exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
protected org.slf4j.Logger
getLogger()
protected WelcomeMessage
offerSession(io.netty.channel.ChannelHandlerContext ctx, JoinMessage requestMessage)
protected void
processMessageAfterHandshake(io.netty.channel.ChannelHandlerContext ctx, Message message)
protected void
processUnexpectedMessageDuringHandshake(io.netty.channel.ChannelHandlerContext ctx, Message message)
protected void
rejectSession(io.netty.channel.ChannelHandlerContext ctx, String error)
protected void
startTimeoutGuard(io.netty.channel.ChannelHandlerContext ctx)
protected ErrorMessage.Error
validateSessionRequest(JoinMessage requestMessage)
-
Methods inherited from class org.drasyl.peer.connection.handler.ThreeWayHandshakeServerHandler
doHandshake
-
Methods inherited from class org.drasyl.peer.connection.handler.SimpleChannelDuplexHandler
acceptOutboundMessage, bind, connect, deregister, disconnect, flush, read, write
-
Methods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelRead
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
-
-
-
-
Field Detail
-
SERVER_CONNECTION_HANDLER
public static final String SERVER_CONNECTION_HANDLER
- See Also:
- Constant Field Values
-
timeout
protected final Duration timeout
-
handshakeFuture
protected final CompletableFuture<Void> handshakeFuture
-
pipeline
protected final Pipeline pipeline
-
networkId
protected final int networkId
-
identity
protected final Identity identity
-
timeoutFuture
protected io.netty.util.concurrent.ScheduledFuture<?> timeoutFuture
-
-
Constructor Detail
-
ServerConnectionHandler
public ServerConnectionHandler(ServerEnvironment environment)
-
-
Method Detail
-
validateSessionRequest
protected ErrorMessage.Error validateSessionRequest(JoinMessage requestMessage)
- Specified by:
validateSessionRequest
in classThreeWayHandshakeServerHandler<JoinMessage,WelcomeMessage>
-
offerSession
protected WelcomeMessage offerSession(io.netty.channel.ChannelHandlerContext ctx, JoinMessage requestMessage)
- Specified by:
offerSession
in classThreeWayHandshakeServerHandler<JoinMessage,WelcomeMessage>
-
createConnection
protected void createConnection(io.netty.channel.ChannelHandlerContext ctx, JoinMessage requestMessage)
- Specified by:
createConnection
in classThreeWayHandshakeServerHandler<JoinMessage,WelcomeMessage>
-
getLogger
protected org.slf4j.Logger getLogger()
-
processUnexpectedMessageDuringHandshake
protected void processUnexpectedMessageDuringHandshake(io.netty.channel.ChannelHandlerContext ctx, Message message)
-
channelRead0
public void channelRead0(io.netty.channel.ChannelHandlerContext ctx, Message msg)
Description copied from class:SimpleChannelDuplexHandler
Is called for each message of typeSimpleChannelDuplexHandler
on inbound channel.- Specified by:
channelRead0
in classSimpleChannelDuplexHandler<Message,Message>
- Parameters:
ctx
- theChannelHandlerContext
which thisSimpleChannelDuplexHandler
belongs tomsg
- the message to handle
-
close
public void close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception
- Specified by:
close
in interfaceio.netty.channel.ChannelOutboundHandler
- Overrides:
close
in classSimpleChannelDuplexHandler<Message,Message>
- Throws:
Exception
-
channelWrite0
protected void channelWrite0(io.netty.channel.ChannelHandlerContext ctx, Message msg, io.netty.channel.ChannelPromise promise)
Description copied from class:SimpleChannelDuplexHandler
Is called for each message of typeSimpleChannelDuplexHandler
on the outbound channel.- Specified by:
channelWrite0
in classSimpleChannelDuplexHandler<Message,Message>
- Parameters:
ctx
- theChannelHandlerContext
which thisSimpleChannelDuplexHandler
belongs tomsg
- the message to handlepromise
- the corresponding promise
-
processMessageAfterHandshake
protected void processMessageAfterHandshake(io.netty.channel.ChannelHandlerContext ctx, Message message)
-
channelActive
public void channelActive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelActive
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelActive
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
startTimeoutGuard
protected void startTimeoutGuard(io.netty.channel.ChannelHandlerContext ctx)
-
rejectSession
protected void rejectSession(io.netty.channel.ChannelHandlerContext ctx, String error)
-
exceptionCaught
public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
- Specified by:
exceptionCaught
in interfaceio.netty.channel.ChannelHandler
- Specified by:
exceptionCaught
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
exceptionCaught
in classio.netty.channel.ChannelInboundHandlerAdapter
-
-