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

Class Method, % Line, %
UDPServer 0% (0/7) 0% (0/31)
UDPServer$1 0% (0/2) 0% (0/6)
UDPServer$2 0% (0/2) 0% (0/5)
Total 0% (0/11) 0% (0/42)


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 co.rsk.config.InternalService; 22 import io.netty.bootstrap.Bootstrap; 23 import io.netty.channel.Channel; 24 import io.netty.channel.ChannelInitializer; 25 import io.netty.channel.EventLoopGroup; 26 import io.netty.channel.nio.NioEventLoopGroup; 27 import io.netty.channel.socket.nio.NioDatagramChannel; 28 import org.slf4j.Logger; 29 import org.slf4j.LoggerFactory; 30  31 import java.util.concurrent.TimeUnit; 32  33 /** 34  * Created by mario on 10/02/17. 35  */ 36 public class UDPServer implements InternalService { 37  private static final Logger logger = LoggerFactory.getLogger(UDPServer.class); 38  39  private int port; 40  private String address; 41  42  private Channel channel; 43  private volatile boolean shutdown = false; 44  45  private PeerExplorer peerExplorer; 46  47  public UDPServer(String address, int port, PeerExplorer peerExplorer) { 48  this.address = address; 49  this.port = port; 50  this.peerExplorer = peerExplorer; 51  } 52  53  @Override 54  public void start() { 55  if (port == 0) { 56  logger.error("Discovery can't be started while listen port == 0"); 57  } else { 58  new Thread("UDPServer") { 59  @Override 60  public void run() { 61  try { 62  UDPServer.this.startUDPServer(); 63  } catch (Exception e) { 64  logger.error("Discovery can't be started. ", e); 65  throw new PeerDiscoveryException("Discovery can't be started. ", e); 66  } 67  } 68  }.start(); 69  } 70  } 71  72  public void startUDPServer() throws InterruptedException { 73  logger.info("Discovery UDPListener started"); 74  EventLoopGroup group = new NioEventLoopGroup(1); 75  76  while (!shutdown) { 77  Bootstrap bootstrap = this.createBootstrap(group); 78  79  channel = bootstrap.bind(address, port).sync().channel(); 80  channel.closeFuture().sync(); 81  82  logger.warn("UDP channel closed. Recreating after 5 sec pause..."); 83  TimeUnit.SECONDS.sleep(5); 84  } 85  86  group.shutdownGracefully().sync(); 87  } 88  89  @Override 90  public void stop() { 91  logger.info("Closing UDPListener..."); 92  shutdown = true; 93  94  if (channel != null) { 95  try { 96  channel.close().await(10, TimeUnit.SECONDS); 97  } catch (InterruptedException e) { 98  logger.error("Couldn't stop the UDP Server", e); 99  Thread.currentThread().interrupt(); 100  } 101  } 102  } 103  104  private Bootstrap createBootstrap(EventLoopGroup group) { 105  return new Bootstrap().group(group).channel(NioDatagramChannel.class) 106  .handler(new ChannelInitializer<NioDatagramChannel>() { 107  @Override 108  public void initChannel(NioDatagramChannel ch) 109  throws Exception { 110  ch.pipeline().addLast(new PacketDecoder()); 111  UDPChannel udpChannel = new UDPChannel(ch, peerExplorer); 112  peerExplorer.setUDPChannel(udpChannel); 113  ch.pipeline().addLast(udpChannel); 114  } 115  }); 116  } 117 } 118