Coverage Summary for Class: Web3WebSocketServer (co.rsk.rpc.netty)

Class Method, % Line, %
Web3WebSocketServer 0% (0/5) 0% (0/27)
Web3WebSocketServer$1 0% (0/2) 0% (0/8)
Total 0% (0/7) 0% (0/35)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2018 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 package co.rsk.rpc.netty; 19  20 import co.rsk.config.InternalService; 21 import io.netty.bootstrap.ServerBootstrap; 22 import io.netty.channel.ChannelFuture; 23 import io.netty.channel.ChannelInitializer; 24 import io.netty.channel.ChannelPipeline; 25 import io.netty.channel.EventLoopGroup; 26 import io.netty.channel.nio.NioEventLoopGroup; 27 import io.netty.channel.socket.SocketChannel; 28 import io.netty.channel.socket.nio.NioServerSocketChannel; 29 import io.netty.handler.codec.http.HttpObjectAggregator; 30 import io.netty.handler.codec.http.HttpServerCodec; 31 import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34  35 import javax.annotation.Nullable; 36 import java.net.InetAddress; 37  38 public class Web3WebSocketServer implements InternalService { 39  private static final Logger logger = LoggerFactory.getLogger(Web3WebSocketServer.class); 40  41  private final InetAddress host; 42  private final int port; 43  private final RskJsonRpcHandler jsonRpcHandler; 44  private final JsonRpcWeb3ServerHandler web3ServerHandler; 45  private final EventLoopGroup bossGroup; 46  private final EventLoopGroup workerGroup; 47  private @Nullable ChannelFuture webSocketChannel; 48  49  public Web3WebSocketServer( 50  InetAddress host, 51  int port, 52  RskJsonRpcHandler jsonRpcHandler, 53  JsonRpcWeb3ServerHandler web3ServerHandler) { 54  this.host = host; 55  this.port = port; 56  this.jsonRpcHandler = jsonRpcHandler; 57  this.web3ServerHandler = web3ServerHandler; 58  this.bossGroup = new NioEventLoopGroup(); 59  this.workerGroup = new NioEventLoopGroup(); 60  } 61  62  @Override 63  public void start() { 64  logger.info("RPC WebSocket enabled"); 65  ServerBootstrap b = new ServerBootstrap(); 66  b.group(bossGroup, workerGroup) 67  .channel(NioServerSocketChannel.class) 68  .childHandler(new ChannelInitializer<SocketChannel>() { 69  @Override 70  protected void initChannel(SocketChannel ch) throws Exception { 71  ChannelPipeline p = ch.pipeline(); 72  p.addLast(new HttpServerCodec()); 73  p.addLast(new HttpObjectAggregator(1024 * 1024 * 5)); 74  p.addLast(new WebSocketServerProtocolHandler("/websocket")); 75  p.addLast(jsonRpcHandler); 76  p.addLast(web3ServerHandler); 77  p.addLast(new Web3ResultWebSocketResponseHandler()); 78  } 79  }); 80  webSocketChannel = b.bind(host, port); 81  try { 82  webSocketChannel.sync(); 83  } catch (InterruptedException e) { 84  logger.error("The RPC WebSocket server couldn't be started", e); 85  Thread.currentThread().interrupt(); 86  } 87  } 88  89  @Override 90  public void stop() { 91  try { 92  webSocketChannel.channel().close().sync(); 93  } catch (InterruptedException e) { 94  logger.error("Couldn't stop the RPC WebSocket server", e); 95  Thread.currentThread().interrupt(); 96  } 97  this.bossGroup.shutdownGracefully(); 98  this.workerGroup.shutdownGracefully(); 99  } 100 }