Skip to content

Commit

Permalink
added braces to one-line ifs
Browse files Browse the repository at this point in the history
  • Loading branch information
aionick committed Jun 1, 2018
1 parent 9c65721 commit 8def32b
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 80 deletions.
35 changes: 23 additions & 12 deletions modP2pImpl/src/org/aion/p2p/impl/comm/NodeMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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("<p2p inbound -> 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(
"<p2p inbound -> active node-id=" + node.getIdShort() + " ip=" + node
.getIpStr() + ">");
}
}
}
}
Expand Down Expand Up @@ -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("<p2p outbound -> active node-id=" + _shortId + " ip=" + node.getIpStr() + ">");
if (_p2pMgr.isShowLog()) {
System.out.println(
"<p2p outbound -> active node-id=" + _shortId + " ip=" + node.getIpStr()
+ ">");
}
}
}
}
Expand Down Expand Up @@ -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);
}

Expand Down
12 changes: 7 additions & 5 deletions modP2pImpl/src/org/aion/p2p/impl/zero/msg/ResActiveNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ public final class ResActiveNodes extends Msg {
public ResActiveNodes(final List<INode> _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<>();
}
}

/**
Expand All @@ -77,18 +78,19 @@ public List<INode> 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{

ByteBuffer buf = ByteBuffer.wrap(_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<INode> activeNodes = new ArrayList<>();
for (int i = 0; i < count; i++) {
Expand Down
25 changes: 13 additions & 12 deletions modP2pImpl/src/org/aion/p2p/impl1/P2pMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -203,9 +204,9 @@ public void run() {
thrdConn.setPriority(Thread.NORM_PRIORITY);
thrdConn.start();
} catch (SocketException e) {
if (showLog) System.out.println("<p2p tcp-server-socket-exception> " + e.getMessage());
if (showLog) { System.out.println("<p2p tcp-server-socket-exception> " + e.getMessage()); }
} catch (IOException e) {
if (showLog) System.out.println("<p2p tcp-server-io-exception>");
if (showLog) { System.out.println("<p2p tcp-server-io-exception>"); }
}
}

Expand Down Expand Up @@ -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("<p2p close-socket reason=" + _reason + ">");
if (showLog) { System.out.println("<p2p close-socket reason=" + _reason + ">"); }

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("<p2p close-socket-io-exception>");
if (showLog) { System.out.println("<p2p close-socket-io-exception>"); }
}
}

Expand Down
10 changes: 7 additions & 3 deletions modP2pImpl/src/org/aion/p2p/impl1/tasks/ChannelBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("<p2p route-cooldown=" + _route + " node=" + this.displayId + " count=" + prev.count + ">");
if(!shouldRoute) {
System.out.println(
"<p2p route-cooldown=" + _route + " node=" + this.displayId + " count="
+ prev.count + ">");
}
// too many msgs
//else
// System.out.println("<p2p route-cooldown=" + _route + " node=" + this.displayId + " count=" + prev.count + ">");
Expand Down
4 changes: 2 additions & 2 deletions modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskClear.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 15 additions & 11 deletions modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskConnectPeers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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(),
Expand All @@ -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();
Expand Down
Loading

0 comments on commit 8def32b

Please sign in to comment.