|
26 | 26 | @Component |
27 | 27 | public class PeerClient { |
28 | 28 |
|
29 | | - private static final Logger logger = LoggerFactory.getLogger("PeerClient"); |
30 | | - |
31 | | - @Autowired |
32 | | - private ApplicationContext ctx; |
33 | | - |
34 | | - @Autowired |
35 | | - @Lazy |
36 | | - private NodeImpl node; |
37 | | - |
38 | | - private EventLoopGroup workerGroup; |
39 | | - |
40 | | - public PeerClient() { |
41 | | - workerGroup = new NioEventLoopGroup(0, new ThreadFactory() { |
42 | | - AtomicInteger cnt = new AtomicInteger(0); |
43 | | - @Override |
44 | | - public Thread newThread(Runnable r) { |
45 | | - return new Thread(r, "TronJClientWorker-" + cnt.getAndIncrement()); |
46 | | - } |
47 | | - }); |
48 | | - } |
49 | | - |
50 | | - public void connect(String host, int port, String remoteId) { |
51 | | - try { |
52 | | - ChannelFuture f = connectAsync(host, port, remoteId, false); |
53 | | - f.sync().channel().closeFuture().sync(); |
54 | | - } catch (Exception e) { |
55 | | - logger.info("PeerClient: Can't connect to " + host + ":" + port + " (" + e.getMessage() + ")"); |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - public ChannelFuture connectAsync(NodeHandler nodeHandler, boolean discoveryMode) { |
60 | | - Node node = nodeHandler.getNode(); |
61 | | - return connectAsync(node.getHost(), node.getPort(), node.getHexId(), discoveryMode) |
62 | | - .addListener((ChannelFutureListener) future -> { |
63 | | - if (!future.isSuccess()) { |
64 | | - logger.error("connect to {}:{} fail,cause:{}", node.getHost(), node.getPort(), |
65 | | - future.cause().getMessage()); |
66 | | - nodeHandler.getNodeStatistics().nodeDisconnectedLocal(ReasonCode.CONNECT_FAIL); |
67 | | - nodeHandler.getNodeStatistics().notifyDisconnect(); |
68 | | - future.channel().close(); |
69 | | - } |
70 | | - }); |
| 29 | + private static final Logger logger = LoggerFactory.getLogger("PeerClient"); |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private ApplicationContext ctx; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + @Lazy |
| 36 | + private NodeImpl node; |
| 37 | + |
| 38 | + private EventLoopGroup workerGroup; |
| 39 | + |
| 40 | + public PeerClient() { |
| 41 | + workerGroup = new NioEventLoopGroup(0, new ThreadFactory() { |
| 42 | + AtomicInteger cnt = new AtomicInteger(0); |
| 43 | + |
| 44 | + @Override |
| 45 | + public Thread newThread(Runnable r) { |
| 46 | + return new Thread(r, "TronJClientWorker-" + cnt.getAndIncrement()); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + public void connect(String host, int port, String remoteId) { |
| 52 | + try { |
| 53 | + ChannelFuture f = connectAsync(host, port, remoteId, false); |
| 54 | + f.sync().channel().closeFuture().sync(); |
| 55 | + } catch (Exception e) { |
| 56 | + logger |
| 57 | + .info("PeerClient: Can't connect to " + host + ":" + port + " (" + e.getMessage() + ")"); |
71 | 58 | } |
| 59 | + } |
| 60 | + |
| 61 | + public ChannelFuture connectAsync(NodeHandler nodeHandler, boolean discoveryMode) { |
| 62 | + Node node = nodeHandler.getNode(); |
| 63 | + return connectAsync(node.getHost(), node.getPort(), node.getHexId(), discoveryMode) |
| 64 | + .addListener((ChannelFutureListener) future -> { |
| 65 | + if (!future.isSuccess()) { |
| 66 | + logger.error("connect to {}:{} fail,cause:{}", node.getHost(), node.getPort(), |
| 67 | + future.cause().getMessage()); |
| 68 | + nodeHandler.getNodeStatistics().nodeDisconnectedLocal(ReasonCode.CONNECT_FAIL); |
| 69 | + nodeHandler.getNodeStatistics().notifyDisconnect(); |
| 70 | + future.channel().close(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
72 | 74 |
|
73 | | - public ChannelFuture connectAsync(String host, int port, String remoteId, boolean discoveryMode) { |
| 75 | + public ChannelFuture connectAsync(String host, int port, String remoteId, boolean discoveryMode) { |
74 | 76 |
|
75 | | - logger.info("connect peer {} {} {}", host, port, remoteId); |
| 77 | + logger.info("connect peer {} {} {}", host, port, remoteId); |
76 | 78 |
|
77 | | - TronChannelInitializer tronChannelInitializer = ctx.getBean(TronChannelInitializer.class, remoteId); |
78 | | - tronChannelInitializer.setPeerDiscoveryMode(discoveryMode); |
79 | | - tronChannelInitializer.setNodeImpl(node); |
| 79 | + TronChannelInitializer tronChannelInitializer = ctx |
| 80 | + .getBean(TronChannelInitializer.class, remoteId); |
| 81 | + tronChannelInitializer.setPeerDiscoveryMode(discoveryMode); |
| 82 | + tronChannelInitializer.setNodeImpl(node); |
80 | 83 |
|
81 | | - Bootstrap b = new Bootstrap(); |
82 | | - b.group(workerGroup); |
83 | | - b.channel(NioSocketChannel.class); |
| 84 | + Bootstrap b = new Bootstrap(); |
| 85 | + b.group(workerGroup); |
| 86 | + b.channel(NioSocketChannel.class); |
84 | 87 |
|
85 | | - b.option(ChannelOption.SO_KEEPALIVE, true); |
86 | | - b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); |
87 | | - b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Args.getInstance().getNodeConnectionTimeout()); |
88 | | - b.remoteAddress(host, port); |
| 88 | + b.option(ChannelOption.SO_KEEPALIVE, true); |
| 89 | + b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); |
| 90 | + b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Args.getInstance().getNodeConnectionTimeout()); |
| 91 | + b.remoteAddress(host, port); |
89 | 92 |
|
90 | | - b.handler(tronChannelInitializer); |
| 93 | + b.handler(tronChannelInitializer); |
91 | 94 |
|
92 | | - // Start the client. |
93 | | - return b.connect(); |
94 | | - } |
| 95 | + // Start the client. |
| 96 | + return b.connect(); |
| 97 | + } |
95 | 98 |
|
96 | | - public void close() { |
97 | | - workerGroup.shutdownGracefully(); |
98 | | - workerGroup.terminationFuture().syncUninterruptibly(); |
99 | | - } |
| 99 | + public void close() { |
| 100 | + workerGroup.shutdownGracefully(); |
| 101 | + workerGroup.terminationFuture().syncUninterruptibly(); |
| 102 | + } |
100 | 103 | } |
0 commit comments