Coverage Summary for Class: PeerClient (org.ethereum.net.client)

Class Method, % Line, %
PeerClient 0% (0/3) 0% (0/17)
PeerClient$1 0% (0/2) 0% (0/3)
Total 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.client; 21  22 import io.netty.bootstrap.Bootstrap; 23 import io.netty.channel.ChannelFuture; 24 import io.netty.channel.ChannelOption; 25 import io.netty.channel.DefaultMessageSizeEstimator; 26 import io.netty.channel.EventLoopGroup; 27 import io.netty.channel.nio.NioEventLoopGroup; 28 import io.netty.channel.socket.nio.NioSocketChannel; 29 import org.ethereum.config.SystemProperties; 30 import org.ethereum.listener.EthereumListener; 31 import org.ethereum.net.EthereumChannelInitializerFactory; 32 import org.ethereum.net.server.EthereumChannelInitializer; 33 import org.slf4j.Logger; 34 import org.slf4j.LoggerFactory; 35  36 import java.util.concurrent.ThreadFactory; 37 import java.util.concurrent.atomic.AtomicInteger; 38  39  40 /** 41  * This class creates the connection to an remote address using the Netty framework 42  * 43  * @see <a href="http://netty.io">http://netty.io</a> 44  */ 45 public class PeerClient { 46  47  private static final Logger logger = LoggerFactory.getLogger("net"); 48  49  private final SystemProperties config; 50  private final EthereumListener ethereumListener; 51  private final EthereumChannelInitializerFactory ethereumChannelInitializerFactory; 52  53  public PeerClient(SystemProperties config, EthereumListener ethereumListener, EthereumChannelInitializerFactory ethereumChannelInitializerFactory) { 54  this.config = config; 55  this.ethereumListener = ethereumListener; 56  this.ethereumChannelInitializerFactory = ethereumChannelInitializerFactory; 57  } 58  59  private static EventLoopGroup workerGroup = new NioEventLoopGroup(0, new ThreadFactory() { 60  AtomicInteger cnt = new AtomicInteger(0); 61  @Override 62  public Thread newThread(Runnable r) { 63  return new Thread(r, "EthJClientWorker-" + cnt.getAndIncrement()); 64  } 65  }); 66  67  public ChannelFuture connectAsync(String host, int port, String remoteId) { 68  ethereumListener.trace("Connecting to: " + host + ":" + port); 69  70  EthereumChannelInitializer ethereumChannelInitializer = ethereumChannelInitializerFactory.newInstance(remoteId); 71  72  Bootstrap b = new Bootstrap(); 73  b.group(workerGroup); 74  b.channel(NioSocketChannel.class); 75  76  b.option(ChannelOption.SO_KEEPALIVE, true); 77  b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); 78  b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout()); 79  b.remoteAddress(host, port); 80  81  b.handler(ethereumChannelInitializer); 82  83  // Start the client. 84  return b.connect(); 85  } 86  87 }