Skip to content

Commit

Permalink
Improved networking code
Browse files Browse the repository at this point in the history
Mostly tidying of code that excessively re-calls itself.

Normalized duplicate code in Peer that was used for connecting to
new peer or accepting a connection from another peer.

Added MessageException so different, specific situations in Peer's main
run() thread can be treated differently instead of one, broad catch-all Exception.

Added documentation and reformatted code to "Eclipse" format
but with extra indent on switch "case:" statements.

Network.java still needs work in terms of calling both peer.close() and peer.interrupt().
(Has comments added to this effect).
  • Loading branch information
catbref committed Nov 6, 2017
1 parent 940f973 commit 02443f4
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 131 deletions.
13 changes: 11 additions & 2 deletions Qora/src/api/PeersResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ public String addPeer(String address) {
throw ApiErrorFactory.getInstance().createError(
ApiErrorFactory.ERROR_INVALID_NETWORK_ADDRESS);
}
peer.addPingCounter();
/*
* XXX what is the purpose of this call?
* PeerMap.addPeer() tests for peer.pingCounter > 1 but after this call
* peer.pingCounter will only be 1 so there's no difference code-path-wise
* between pingCounter being 0 or 1.
* Also creating a new Peer starts a Pinger thread which will increment pingCounter
* and call PeerMap.addPeer()
*/
// was: peer.addPingCounter();
// now: peer.onPingSuccess(); which also performs next line:
DBSet.getInstance().getPeerMap().addPeer(peer);

return "OK";
Expand Down Expand Up @@ -173,7 +182,7 @@ else if(DBSet.getInstance().getPeerMap().contains(peer.getAddress().getAddress()
o.put("version", Controller.getInstance().getVersionOfPeer(peer).getA());
o.put("buildTime", DateTimeFormat.timestamptoString(Controller.getInstance().getVersionOfPeer(peer).getB(), "yyyy-MM-dd HH:mm:ss z", "UTC"));
}
if(peer.isPinger()) {
if(peer.hasPinger()) {
o.put("ping", peer.getPing());
}
if(peer.getConnectionTime()>0) {
Expand Down
2 changes: 1 addition & 1 deletion Qora/src/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void onDisconnect(Peer peer) {

//CLOSE CONNECTION IF STILL ACTIVE
peer.close();
peer.interrupt();
peer.interrupt(); // might be handled inside peer.close()

//NOTIFY OBSERVERS
this.setChanged();
Expand Down
Loading

0 comments on commit 02443f4

Please sign in to comment.