Coverage Summary for Class: FrameCodecHandler (org.ethereum.net.rlpx)
Class |
Class, %
|
Method, %
|
Line, %
|
FrameCodecHandler |
0%
(0/1)
|
0%
(0/5)
|
0%
(0/20)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.ethereum.net.rlpx;
21
22 import io.netty.buffer.ByteBuf;
23 import io.netty.channel.ChannelHandlerContext;
24 import io.netty.handler.codec.ByteToMessageCodec;
25 import org.ethereum.net.server.Channel;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.io.IOException;
30 import java.util.List;
31
32 /**
33 * The Netty handler responsible for decrypting/encrypting RLPx frames
34 * with the FrameCodec crated during HandshakeHandler initial work
35 *
36 * Created by Anton Nashatyrev on 15.10.2015.
37 */
38 public class FrameCodecHandler extends ByteToMessageCodec<FrameCodec.Frame> {
39 private static final Logger loggerWire = LoggerFactory.getLogger("wire");
40 private static final Logger loggerNet = LoggerFactory.getLogger("net");
41
42 protected final FrameCodec frameCodec;
43 protected final Channel channel;
44
45 public FrameCodecHandler(FrameCodec frameCodec, Channel channel) {
46 this.frameCodec = frameCodec;
47 this.channel = channel;
48 }
49
50 protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IOException {
51 if (in.readableBytes() == 0) {
52 loggerWire.trace("in.readableBytes() == 0");
53 return;
54 }
55
56 loggerWire.trace("Decoding frame ({} bytes)", in.readableBytes());
57 List<FrameCodec.Frame> frames = frameCodec.readFrames(in);
58
59
60 // Check if a full frame was available. If not, we'll try later when more bytes come in.
61 if (frames == null || frames.isEmpty()) {
62 return;
63 }
64
65 for (int i = 0; i < frames.size(); i++) {
66 frames.get(i);
67 channel.getNodeStatistics().rlpxInMessages.add();
68 }
69
70 out.addAll(frames);
71 }
72
73 @Override
74 protected void encode(ChannelHandlerContext ctx, FrameCodec.Frame frame, ByteBuf out) throws Exception {
75
76 frameCodec.writeFrame(frame, out);
77
78 channel.getNodeStatistics().rlpxOutMessages.add();
79 }
80
81 @Override
82 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
83 if (cause instanceof IOException) {
84 loggerNet.info("FrameCodec failed: address {}", ctx.channel().remoteAddress(), cause);
85 } else {
86 loggerNet.warn("FrameCodec failed: ", cause);
87 }
88 ctx.close();
89 }
90 }