Coverage Summary for Class: PacketDecoder (co.rsk.net.discovery)
Class |
Class, %
|
Method, %
|
Line, %
|
PacketDecoder |
0%
(0/1)
|
0%
(0/4)
|
0%
(0/11)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.net.discovery;
20
21
22 import co.rsk.net.discovery.message.MessageDecoder;
23 import co.rsk.net.discovery.message.PeerDiscoveryMessage;
24 import io.netty.buffer.ByteBuf;
25 import io.netty.channel.ChannelHandlerContext;
26 import io.netty.channel.socket.DatagramPacket;
27 import io.netty.handler.codec.MessageToMessageDecoder;
28 import org.ethereum.util.ByteUtil;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.net.InetSocketAddress;
33 import java.util.List;
34
35 public class PacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
36 private static final Logger logger = LoggerFactory.getLogger(PacketDecoder.class);
37
38 @Override
39 public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
40 ByteBuf buf = packet.content();
41 byte[] encoded = new byte[buf.readableBytes()];
42 buf.readBytes(encoded);
43 out.add(this.decodeMessage(ctx, encoded, packet.sender()));
44 }
45
46 public DiscoveryEvent decodeMessage(ChannelHandlerContext ctx, byte[] encoded, InetSocketAddress sender) {
47 try {
48 PeerDiscoveryMessage msg = MessageDecoder.decode(encoded);
49 return new DiscoveryEvent(msg, sender);
50 } catch (Exception e) {
51 logger.error("Exception processing inbound message from {} : {}", ctx.channel().remoteAddress(), ByteUtil.toHexString(encoded), e);
52 throw e;
53 }
54 }
55 }