diff --git a/modP2pImpl/src/org/aion/p2p/impl/comm/NodeMgr.java b/modP2pImpl/src/org/aion/p2p/impl/comm/NodeMgr.java index 99ccca3303..3dd908915d 100644 --- a/modP2pImpl/src/org/aion/p2p/impl/comm/NodeMgr.java +++ b/modP2pImpl/src/org/aion/p2p/impl/comm/NodeMgr.java @@ -143,8 +143,9 @@ public boolean isSeedIp(String _ip) { */ @Override public synchronized void addTempNode(final INode _n) { - if(tempNodes.size() < maxTempNodes) + if(tempNodes.size() < maxTempNodes) { tempNodes.add(_n); + } } @Override @@ -195,8 +196,9 @@ public INode getOutboundNode(int k) { @Override public INode allocNode(String ip, int p0) { INode n = new Node(ip, p0); - if (seedIps.contains(ip)) + if (seedIps.contains(ip)) { n.setFromBootList(true); + } return n; } @@ -273,13 +275,18 @@ public synchronized void moveInboundToActive(int _channelHashCode, final IP2pMgr node.setConnection("inbound"); node.setFromBootList(seedIps.contains(node.getIpStr())); INode previous = activeNodes.putIfAbsent(node.getIdHash(), node); - if (previous != null) - _p2pMgr.closeSocket(node.getChannel(), "inbound -> active, node " + previous.getIdShort() + " exits"); - else if(!activeIpAllow(node.getIpStr())) - _p2pMgr.closeSocket(node.getChannel(), "inbound -> active, ip " + node.getIpStr() + " exits"); - else { - if (_p2pMgr.isShowLog()) - System.out.println(" active node-id=" + node.getIdShort() + " ip=" + node.getIpStr() + ">"); + if (previous != null) { + _p2pMgr.closeSocket(node.getChannel(), + "inbound -> active, node " + previous.getIdShort() + " exits"); + } else if (!activeIpAllow(node.getIpStr())) { + _p2pMgr.closeSocket(node.getChannel(), + "inbound -> active, ip " + node.getIpStr() + " exits"); + } else { + if (_p2pMgr.isShowLog()) { + System.out.println( + " active node-id=" + node.getIdShort() + " ip=" + node + .getIpStr() + ">"); + } } } } @@ -310,8 +317,11 @@ public synchronized void moveOutboundToActive(int _nodeIdHash, String _shortId, if (previous != null) _p2pMgr.closeSocket(node.getChannel(), "outbound -> active, node " + previous.getIdShort() + " exits"); else { - if (_p2pMgr.isShowLog()) - System.out.println(" active node-id=" + _shortId + " ip=" + node.getIpStr() + ">"); + if (_p2pMgr.isShowLog()) { + System.out.println( + " active node-id=" + _shortId + " ip=" + node.getIpStr() + + ">"); + } } } } @@ -357,8 +367,9 @@ public void timeoutActive(IP2pMgr _p2pMgr) { public void dropActive(int nodeIdHash, final IP2pMgr _p2pMgr, String _reason) { INode node = activeNodes.remove(nodeIdHash); - if (node == null) + if (node == null) { return; + } _p2pMgr.closeSocket(node.getChannel(), _reason); } diff --git a/modP2pImpl/src/org/aion/p2p/impl/zero/msg/ResActiveNodes.java b/modP2pImpl/src/org/aion/p2p/impl/zero/msg/ResActiveNodes.java index 75b739fb5b..e6e8534c44 100644 --- a/modP2pImpl/src/org/aion/p2p/impl/zero/msg/ResActiveNodes.java +++ b/modP2pImpl/src/org/aion/p2p/impl/zero/msg/ResActiveNodes.java @@ -58,10 +58,11 @@ public final class ResActiveNodes extends Msg { public ResActiveNodes(final List _nodes) { super(Ver.V0, Ctrl.NET, Act.RES_ACTIVE_NODES); this.count = Math.min(MAX_NODES, _nodes.size()); - if (this.count > 0) + if (this.count > 0) { this.nodes = _nodes.subList(0, this.count); - else + } else { this.nodes = new ArrayList<>(); + } } /** @@ -77,9 +78,9 @@ public List getNodes() { * @return ResActiveNodes */ public static ResActiveNodes decode(final byte[] _bytes) { - if (_bytes == null || _bytes.length == 0 || (_bytes.length - 1) % NODE_BYTES_LENGTH != 0) + if (_bytes == null || _bytes.length == 0 || (_bytes.length - 1) % NODE_BYTES_LENGTH != 0) { return null; - else { + } else { try{ @@ -87,8 +88,9 @@ public static ResActiveNodes decode(final byte[] _bytes) { int count = buf.get(); // fix bug: https://github.com/aionnetwork/aion/issues/390 - if (_bytes.length != count * NODE_BYTES_LENGTH + 1) + if (_bytes.length != count * NODE_BYTES_LENGTH + 1) { return null; + } ArrayList activeNodes = new ArrayList<>(); for (int i = 0; i < count; i++) { diff --git a/modP2pImpl/src/org/aion/p2p/impl1/P2pMgr.java b/modP2pImpl/src/org/aion/p2p/impl1/P2pMgr.java index 8dc13b0aa1..1bbd677803 100644 --- a/modP2pImpl/src/org/aion/p2p/impl1/P2pMgr.java +++ b/modP2pImpl/src/org/aion/p2p/impl1/P2pMgr.java @@ -152,7 +152,7 @@ public void run() { thrdIn.setPriority(Thread.NORM_PRIORITY); thrdIn.start(); - if (showLog) + if (showLog) { this.handlers.forEach( (route, callbacks) -> { Handler handler = callbacks.get(0); @@ -161,6 +161,7 @@ public void run() { getRouteMsg(route, h.getVer(), h.getCtrl(), h.getAction(), handler.getClass().getSimpleName())); }); + } for (int i = 0; i < TaskSend.TOTAL_LANE; i++) { Thread thrdOut = new Thread(getSendInstance(i), "p2p-out-" + i); @@ -174,27 +175,27 @@ public void run() { t.start(); } - if (upnpEnable) + if (upnpEnable) { scheduledWorkers.scheduleWithFixedDelay( new TaskUPnPManager(selfPort), 1, PERIOD_UPNP_PORT_MAPPING, TimeUnit.MILLISECONDS); - - if (showStatus) + } + if (showStatus) { scheduledWorkers.scheduleWithFixedDelay( getStatusInstance(), 2, PERIOD_SHOW_STATUS, TimeUnit.MILLISECONDS); - - if (!syncSeedsOnly) + } + if (!syncSeedsOnly) { scheduledWorkers.scheduleWithFixedDelay( new TaskRequestActiveNodes(this), 5000, PERIOD_REQUEST_ACTIVE_NODES, TimeUnit.MILLISECONDS); - + } Thread thrdClear = new Thread(getClearInstance(), "p2p-clear"); thrdClear.setPriority(Thread.NORM_PRIORITY); thrdClear.start(); @@ -203,9 +204,9 @@ public void run() { thrdConn.setPriority(Thread.NORM_PRIORITY); thrdConn.start(); } catch (SocketException e) { - if (showLog) System.out.println(" " + e.getMessage()); + if (showLog) { System.out.println(" " + e.getMessage()); } } catch (IOException e) { - if (showLog) System.out.println(""); + if (showLog) { System.out.println(""); } } } @@ -272,14 +273,14 @@ public void errCheck(int _nodeIdHash, String _displayId) { /** @param _sc SocketChannel */ public void closeSocket(final SocketChannel _sc, String _reason) { - if (showLog) System.out.println(""); + if (showLog) { System.out.println(""); } try { SelectionKey sk = _sc.keyFor(selector); _sc.close(); - if (sk != null) sk.cancel(); + if (sk != null) { sk.cancel(); } } catch (IOException e) { - if (showLog) System.out.println(""); + if (showLog) { System.out.println(""); } } } diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/ChannelBuffer.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/ChannelBuffer.java index d5544fab5a..3a16e08e76 100644 --- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/ChannelBuffer.java +++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/ChannelBuffer.java @@ -67,12 +67,16 @@ synchronized boolean shouldRoute(int _route, int _maxReqsPerSec) { return true; } boolean shouldRoute = prev.count < _maxReqsPerSec; - if(shouldRoute) + if(shouldRoute) { prev.count++; + } if(showLog) { - if(!shouldRoute) - System.out.println(""); + if(!shouldRoute) { + System.out.println( + ""); + } // too many msgs //else // System.out.println(""); diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskClear.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskClear.java index 29e1720581..b1ed935769 100644 --- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskClear.java +++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskClear.java @@ -55,12 +55,12 @@ public void run() { Object obj = outboundIt.next(); - if (obj == null) continue; + if (obj == null) { continue; } int nodeIdHash = (int) obj; INode node = nodeMgr.getOutboundNodes().get(nodeIdHash); - if (node == null) continue; + if (node == null) { continue; } if (System.currentTimeMillis() - node.getTimestamp() > TIMEOUT_OUTBOUND_NODES) { this.mgr.closeSocket( diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskConnectPeers.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskConnectPeers.java index 2dde338202..fea763fd0d 100644 --- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskConnectPeers.java +++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskConnectPeers.java @@ -72,28 +72,29 @@ public void run() { try { Thread.sleep(PERIOD_CONNECT_OUTBOUND); } catch (InterruptedException e) { - if (this.mgr.isShowLog()) System.out.println(getTcpInterruptedMsg()); + if (this.mgr.isShowLog()) { System.out.println(getTcpInterruptedMsg()); } } if (this.nodeMgr.activeNodesSize() >= this.maxActiveNodes) { - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(getTcpPassMaxNodesMsg()); + } continue; } INode node; try { node = this.nodeMgr.tempNodesTake(); - if (this.nodeMgr.isSeedIp(node.getIpStr())) node.setFromBootList(true); - if (node.getIfFromBootList()) this.nodeMgr.addTempNode(node); + if (this.nodeMgr.isSeedIp(node.getIpStr())) { node.setFromBootList(true); } + if (node.getIfFromBootList()) { this.nodeMgr.addTempNode(node); } // if (node.peerMetric.shouldNotConn()) { // continue; // } } catch (InterruptedException e) { - if (this.mgr.isShowLog()) System.out.println(getTcpInterruptedMsg()); + if (this.mgr.isShowLog()) { System.out.println(getTcpInterruptedMsg()); } return; } catch (Exception e) { - if (this.mgr.isShowLog()) e.printStackTrace(); + if (this.mgr.isShowLog()) { e.printStackTrace(); } continue; } int nodeIdHash = node.getIdHash(); @@ -111,9 +112,9 @@ public void run() { if (channel.finishConnect() && channel.isConnected()) { - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(getSucesCnctMsg(node.getIdShort(), node.getIpStr())); - + } SelectionKey sk = channel.register(this.selector, SelectionKey.OP_READ); ChannelBuffer rb = new ChannelBuffer(this.mgr.isShowLog()); rb.displayId = node.getIdShort(); @@ -124,8 +125,9 @@ public void run() { node.setChannel(channel); this.nodeMgr.addOutboundNode(node); - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(getPrepRqstMsg(node.getIdShort(), node.getIpStr())); + } this.sendMsgQue.offer( new MsgOut( node.getIdHash(), @@ -135,14 +137,16 @@ public void run() { // node.peerMetric.decFailedCount(); } else { - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(getFailCnctMsg(node.getIdShort(), node.getIpStr())); + } channel.close(); // node.peerMetric.incFailedCount(); } } catch (IOException e) { - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(getOutboundConnectMsg(node.getIpStr(), _port)); + } // node.peerMetric.incFailedCount(); } catch (Exception e) { if (this.mgr.isShowLog()) e.printStackTrace(); diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskInbound.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskInbound.java index 5b046c6cf6..ddcfe03876 100644 --- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskInbound.java +++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskInbound.java @@ -296,7 +296,7 @@ private int readBody(final ChannelBuffer _cb, ByteBuffer readBuffer, int cnt) { return cnt; } - if (cnt < bodyLen) return cnt; + if (cnt < bodyLen) { return cnt; } int origPos = readBuffer.position(); int startP = origPos - cnt; @@ -331,7 +331,7 @@ private int read(final SelectionKey _sk, ByteBuffer _readBuffer, int _cnt) throw currCnt = readBody(rb, _readBuffer, currCnt); } - if (!rb.isBodyCompleted()) return currCnt; + if (!rb.isBodyCompleted()) { return currCnt; } Header h = rb.header; @@ -352,9 +352,10 @@ private int read(final SelectionKey _sk, ByteBuffer _readBuffer, int _cnt) throw : P2pConstant.READ_MAX_RATE)); if (!underRC) { - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println( getRouteMsg(ver, ctrl, act, rb.getRouteCount(route).count, rb.displayId)); + } return currCnt; } @@ -365,29 +366,33 @@ private int read(final SelectionKey _sk, ByteBuffer _readBuffer, int _cnt) throw try { handleP2pMsg(_sk, act, bodyBytes); } catch (Exception ex) { - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println( - ""); + ""); + } } break; case Ctrl.SYNC: if (!this.handlers.containsKey(route)) { - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(getUnregRouteMsg(ver, ctrl, act, rb.displayId)); + } return currCnt; } this.handleKernelMsg(rb.nodeIdHash, route, bodyBytes); break; default: - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(getInvalRouteMsg(ver, ctrl, act, rb.displayId)); + } break; } break; default: - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(""); + } break; } @@ -420,30 +425,32 @@ private void handleP2pMsg(final SelectionKey _sk, byte _act, final byte[] _msgBy break; case Act.RES_HANDSHAKE: - if (rb.nodeIdHash == 0) return; + if (rb.nodeIdHash == 0) { return; } if (_msgBytes.length > ResHandshake.LEN) { ResHandshake1 resHandshake1 = ResHandshake1.decode(_msgBytes); - if (resHandshake1 != null && resHandshake1.getSuccess()) + if (resHandshake1 != null && resHandshake1.getSuccess()) { handleResHandshake(rb.nodeIdHash, resHandshake1.getBinaryVersion()); + } } break; case Act.REQ_ACTIVE_NODES: if (rb.nodeIdHash != 0) { INode node = nodeMgr.getActiveNode(rb.nodeIdHash); - if (node != null) + if (node != null) { this.sendMsgQue.offer( - new MsgOut( - node.getIdHash(), - node.getIdShort(), - new ResActiveNodes(nodeMgr.getActiveNodesList()), - Dest.ACTIVE)); + new MsgOut( + node.getIdHash(), + node.getIdShort(), + new ResActiveNodes(nodeMgr.getActiveNodesList()), + Dest.ACTIVE)); + } } break; case Act.RES_ACTIVE_NODES: - if (this.mgr.isSyncSeedsOnly()) break; + if (this.mgr.isSyncSeedsOnly()) { break; } if (rb.nodeIdHash != 0) { INode node = nodeMgr.getActiveNode(rb.nodeIdHash); @@ -453,17 +460,19 @@ private void handleP2pMsg(final SelectionKey _sk, byte _act, final byte[] _msgBy if (resActiveNodes != null) { List incomingNodes = resActiveNodes.getNodes(); for (INode incomingNode : incomingNodes) { - if (nodeMgr.tempNodesSize() >= this.mgr.getMaxTempNodes()) return; - if (this.mgr.validateNode(incomingNode)) + if (nodeMgr.tempNodesSize() >= this.mgr.getMaxTempNodes()) { return; } + if (this.mgr.validateNode(incomingNode)) { nodeMgr.addTempNode(incomingNode); + } } } } } break; default: - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(""); + } break; } } @@ -511,7 +520,7 @@ private void handleReqHandshake( } } else { - if (this.mgr.isShowLog()) System.out.println(""); + if (this.mgr.isShowLog()) { System.out.println(""); } } } } @@ -543,7 +552,7 @@ private void handleKernelMsg(int _nodeIdHash, int _route, final byte[] _msgBytes /** @return boolean TODO: implementation */ private boolean handshakeRuleCheck(int netId) { // check net id - if (netId != this.mgr.getSelfNetId()) return false; + if (netId != this.mgr.getSelfNetId()) { return false; } // check supported protocol versions return true; } diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskReceive.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskReceive.java index 3ee80bb64a..b5af5e5e0d 100644 --- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskReceive.java +++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskReceive.java @@ -28,8 +28,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.aion.p2p.Handler; -public class -TaskReceive implements Runnable { +public class TaskReceive implements Runnable { private final AtomicBoolean start; private final BlockingQueue receiveMsgQue; private final Map> handlers; @@ -53,21 +52,21 @@ public void run() { MsgIn mi = this.receiveMsgQue.take(); List hs = this.handlers.get(mi.getRoute()); - if (hs == null) continue; + if (hs == null) { continue; } for (Handler hlr : hs) { - if (hlr == null) continue; + if (hlr == null) { continue; } try { hlr.receive(mi.getNodeId(), mi.getDisplayId(), mi.getMsg()); } catch (Exception e) { - if (this.showLog) e.printStackTrace(); + if (this.showLog) { e.printStackTrace(); } } } } catch (InterruptedException e) { - if (this.showLog) System.out.println(""); + if (this.showLog) { System.out.println(""); } return; } catch (Exception e) { - if (this.showLog) e.printStackTrace(); + if (this.showLog) { e.printStackTrace(); } } } } diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskSend.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskSend.java index 595ada5af0..904467b305 100644 --- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskSend.java +++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskSend.java @@ -65,8 +65,9 @@ public void run() { // if timeout , throw away this msg. long now = System.currentTimeMillis(); if (now - mo.getTimestamp() > P2pConstant.WRITE_MSG_TIMEOUT) { - if (this.mgr.isShowLog()) + if (this.mgr.isShowLog()) { System.out.println(getTimeoutMsg(mo.getDisplayId(), now)); + } continue; } @@ -107,14 +108,16 @@ public void run() { } } } else { - if (this.mgr.isShowLog()) - System.out.println(getNodeNotExitMsg(mo.getDest().name(), mo.getDisplayId())); + if (this.mgr.isShowLog()) { + System.out + .println(getNodeNotExitMsg(mo.getDest().name(), mo.getDisplayId())); + } } } catch (InterruptedException e) { - if (this.mgr.isShowLog()) System.out.println(""); + if (this.mgr.isShowLog()) { System.out.println(""); } return; } catch (Exception e) { - if (this.mgr.isShowLog()) e.printStackTrace(); + if (this.mgr.isShowLog()) { e.printStackTrace(); } } } } diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskWrite.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskWrite.java index 26b75ee0c8..38e3dacbd4 100644 --- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskWrite.java +++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskWrite.java @@ -81,7 +81,7 @@ public void run() { // System.out.println("write " + h.getVer() + "-" + h.getCtrl() + "-" + h.getAction()); ByteBuffer buf = ByteBuffer.allocate(headerBytes.length + bodyLen); buf.put(headerBytes); - if (bodyBytes != null) buf.put(bodyBytes); + if (bodyBytes != null) { buf.put(bodyBytes); } buf.flip(); try {