Coverage Summary for Class: UDPChannel (co.rsk.net.discovery)

Class Class, % Method, % Line, %
UDPChannel 0% (0/1) 0% (0/8) 0% (0/13)


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 import io.netty.buffer.Unpooled; 22 import io.netty.channel.Channel; 23 import io.netty.channel.ChannelHandlerContext; 24 import io.netty.channel.SimpleChannelInboundHandler; 25 import io.netty.channel.socket.DatagramPacket; 26 import org.slf4j.Logger; 27 import org.slf4j.LoggerFactory; 28  29 import java.net.InetSocketAddress; 30  31 public class UDPChannel extends SimpleChannelInboundHandler<DiscoveryEvent> { 32  static final Logger logger = LoggerFactory.getLogger(UDPChannel.class); 33  34  private Channel channel; 35  36  private PeerExplorer peerExplorer; 37  38  public UDPChannel(Channel ch, PeerExplorer peerExplorer) { 39  this.channel = ch; 40  this.peerExplorer = peerExplorer; 41  } 42  43  @Override 44  public void channelRead0(ChannelHandlerContext ctx, DiscoveryEvent event) throws Exception { 45  this.peerExplorer.handleMessage(event); 46  } 47  48  public void write(DiscoveryEvent discoveryEvent) { 49  InetSocketAddress address = discoveryEvent.getAddress(); 50  sendPacket(discoveryEvent.getMessage().getPacket(), address); 51  } 52  53  void sendPacket(byte[] wire, InetSocketAddress address) { 54  DatagramPacket packet = new DatagramPacket(Unpooled.copiedBuffer(wire), address); 55  channel.write(packet); 56  channel.flush(); 57  } 58  59  @Override 60  public void channelReadComplete(ChannelHandlerContext ctx) { 61  ctx.flush(); 62  } 63  64  @Override 65  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 66  logger.debug("Discover channel error", cause); 67  // We don't close the channel because we can keep serving requests. 68  } 69  70  @Override 71  public void channelActive(ChannelHandlerContext ctx) throws Exception { 72  peerExplorer.start(); 73  } 74  75 }