Coverage Summary for Class: EthSubscriptionNotificationEmitter (co.rsk.rpc)
Class |
Class, %
|
Method, %
|
Line, %
|
EthSubscriptionNotificationEmitter |
0%
(0/1)
|
0%
(0/5)
|
0%
(0/14)
|
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;
19
20 import co.rsk.rpc.modules.eth.subscribe.*;
21 import io.netty.channel.Channel;
22
23 /**
24 * This manages subscriptions and emits events to interested clients.
25 * Can only be used with the WebSockets transport.
26 */
27 public class EthSubscriptionNotificationEmitter implements EthSubscribeParamsVisitor {
28 private final BlockHeaderNotificationEmitter blockHeader;
29 private final LogsNotificationEmitter logs;
30
31 public EthSubscriptionNotificationEmitter(
32 BlockHeaderNotificationEmitter blockHeader,
33 LogsNotificationEmitter logs) {
34 this.blockHeader = blockHeader;
35 this.logs = logs;
36 }
37
38 @Override
39 public SubscriptionId visit(EthSubscribeNewHeadsParams params, Channel channel) {
40 SubscriptionId subscriptionId = new SubscriptionId();
41 blockHeader.subscribe(subscriptionId, channel);
42 return subscriptionId;
43 }
44
45 @Override
46 public SubscriptionId visit(EthSubscribeLogsParams params, Channel channel) {
47 SubscriptionId subscriptionId = new SubscriptionId();
48 logs.subscribe(subscriptionId, channel, params);
49 return subscriptionId;
50 }
51
52 /**
53 * @return whether the unsubscription succeeded.
54 */
55 public boolean unsubscribe(SubscriptionId subscriptionId) {
56 // temporal variables avoid short-circuiting behavior
57 boolean unsubscribedBlockHeader = blockHeader.unsubscribe(subscriptionId);
58 boolean unsubscribedLogs = logs.unsubscribe(subscriptionId);
59 return unsubscribedBlockHeader || unsubscribedLogs;
60 }
61
62 /**
63 * Clear all subscriptions for channel.
64 */
65 public void unsubscribe(Channel channel) {
66 blockHeader.unsubscribe(channel);
67 logs.unsubscribe(channel);
68 }
69 }