Coverage Summary for Class: BlockHeaderNotificationEmitter (co.rsk.rpc.modules.eth.subscribe)

Class Method, % Line, %
BlockHeaderNotificationEmitter 0% (0/8) 0% (0/18)
BlockHeaderNotificationEmitter$1 0% (0/2) 0% (0/2)
Total 0% (0/10) 0% (0/20)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 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.modules.eth.subscribe; 19  20 import co.rsk.rpc.JsonRpcSerializer; 21 import io.netty.channel.Channel; 22 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; 23 import org.ethereum.core.Block; 24 import org.ethereum.core.TransactionReceipt; 25 import org.ethereum.facade.Ethereum; 26 import org.ethereum.listener.EthereumListenerAdapter; 27 import org.slf4j.Logger; 28 import org.slf4j.LoggerFactory; 29  30 import java.io.IOException; 31 import java.util.List; 32 import java.util.Map; 33 import java.util.concurrent.ConcurrentHashMap; 34  35 public class BlockHeaderNotificationEmitter { 36  private static final Logger logger = LoggerFactory.getLogger(BlockHeaderNotificationEmitter.class); 37  38  private final JsonRpcSerializer jsonRpcSerializer; 39  40  private final Map<SubscriptionId, Channel> subscriptions = new ConcurrentHashMap<>(); 41  42  public BlockHeaderNotificationEmitter(Ethereum ethereum, JsonRpcSerializer jsonRpcSerializer) { 43  this.jsonRpcSerializer = jsonRpcSerializer; 44  ethereum.addListener(new EthereumListenerAdapter() { 45  @Override 46  public void onBlock(Block block, List<TransactionReceipt> receipts) { 47  emitBlockHeader(block); 48  } 49  }); 50  } 51  52  public void subscribe(SubscriptionId subscriptionId, Channel channel) { 53  subscriptions.put(subscriptionId, channel); 54  } 55  56  public boolean unsubscribe(SubscriptionId subscriptionId) { 57  return subscriptions.remove(subscriptionId) != null; 58  } 59  60  public void unsubscribe(Channel channel) { 61  subscriptions.values().removeIf(channel::equals); 62  } 63  64  private void emitBlockHeader(Block block) { 65  if (subscriptions.isEmpty()) { 66  return; 67  } 68  69  BlockHeaderNotification header = new BlockHeaderNotification(block); 70  71  subscriptions.forEach((SubscriptionId id, Channel channel) -> { 72  EthSubscriptionNotification request = new EthSubscriptionNotification( 73  new EthSubscriptionParams(id, header) 74  ); 75  76  try { 77  String msg = jsonRpcSerializer.serializeMessage(request); 78  channel.writeAndFlush(new TextWebSocketFrame(msg)); 79  } catch (IOException e) { 80  logger.error("Couldn't serialize block header result for notification", e); 81  } 82  }); 83  } 84 }