Skip to content

Commit

Permalink
refactor: split parsing peers into multiple steps
Browse files Browse the repository at this point in the history
The previous code nested too much error handling even though many
parts are independent.
  • Loading branch information
ArneBab committed Nov 12, 2024
1 parent b892b9b commit 13c71cb
Showing 1 changed file with 48 additions and 46 deletions.
94 changes: 48 additions & 46 deletions src/freenet/node/PeerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,58 +238,60 @@ private boolean readPeers(File peersFile, NodeCrypto crypto, OpennetManager open
BufferedReader br = new BufferedReader(ris);
File brokenPeersFile = new File(peersFile.getPath() + ".broken");
DroppedOldPeersUserAlert droppedOldPeers = new DroppedOldPeersUserAlert(brokenPeersFile);
try { // FIXME: no better way?
while(true) {
// Read a single NodePeer
SimpleFieldSet fs;
fs = new SimpleFieldSet(br, false, true);
try {
PeerNode pn = PeerNode.create(fs, node, crypto, opennet, this);
if(oldOpennetPeers) {
if(!(pn instanceof OpennetPeerNode))
Logger.error(this, "Darknet node in old opennet peers?!: "+pn);
else
opennet.addOldOpennetNode((OpennetPeerNode)pn);
} else
addPeer(pn, true, false);
} catch(FSParseException e2) {
Logger.error(this, "Could not parse peer: " + e2 + '\n' + fs.toString(), e2);
System.err.println("Cannot parse a friend from the peers file: "+e2);
someBroken = true;
continue;
} catch(PeerParseException e2) {
Logger.error(this, "Could not parse peer: " + e2 + '\n' + fs.toString(), e2);
System.err.println("Cannot parse a friend from the peers file: "+e2);
someBroken = true;
continue;
} catch(ReferenceSignatureVerificationException e2) {
Logger.error(this, "Could not parse peer: " + e2 + '\n' + fs.toString(), e2);
System.err.println("Cannot parse a friend from the peers file: "+e2);
someBroken = true;
continue;
} catch (RuntimeException e2) {
Logger.error(this, "Could not parse peer: " + e2 + '\n' + fs.toString(), e2);
System.err.println("Cannot parse a friend from the peers file: "+e2);
someBroken = true;
continue;
// FIXME tell the user???
} catch (PeerTooOldException e) {
if(crypto.isOpennet()) {
// Ignore.
Logger.error(this, "Dropping too-old opennet peer");
} else {
// A lot more noisy!
droppedOldPeers.add(e, fs.get("myName"));
}
someBroken = true;
continue;
}
List<SimpleFieldSet> peerEntries = new ArrayList<>();
// read the peers file
try {
while (true) {
peerEntries.add(new SimpleFieldSet(br, false, true));
}
} catch(EOFException e) {
// End of file, fine
} catch(IOException e1) {
Logger.error(this, "Could not read peers file: " + e1, e1);
}

List<PeerNode> createdNodes = new ArrayList<>();
for (SimpleFieldSet fs : peerEntries) {
try {
createdNodes.add(PeerNode.create(fs, node, crypto, opennet, this));
} catch(FSParseException e2) {
Logger.error(this, "Could not parse peer due to broken fieldset syntax: " + e2 + '\n' + fs.toString(), e2);
System.err.println("Cannot parse a friend from the peers file due to broken fieldset syntax: "+e2);
someBroken = true;
} catch(PeerParseException e2) {
Logger.error(this, "Could not parse peer: " + e2 + '\n' + fs.toString(), e2);
System.err.println("Cannot parse a friend from the peers file: "+e2);
someBroken = true;
} catch(ReferenceSignatureVerificationException e2) {
Logger.error(this, "Could not verify signature of peer: " + e2 + '\n' + fs.toString(), e2);
System.err.println("Cannot verify signature of a friend from the peers file: "+e2);
someBroken = true;
} catch (RuntimeException e2) {
Logger.error(this, "Could not parse peer: " + e2 + '\n' + fs.toString(), e2);
System.err.println("Cannot parse a friend from the peers file: " + e2);
someBroken = true;
// FIXME tell the user???
} catch (PeerTooOldException e) {
someBroken = true;
if(crypto.isOpennet()) {
// Ignore.
Logger.error(this, "Dropping too-old opennet peer");
} else {
// A lot more noisy
droppedOldPeers.add(e, fs.get("myName"));
}
}
}

for (PeerNode pn : createdNodes) {
if(oldOpennetPeers) {
if(!(pn instanceof OpennetPeerNode))
Logger.error(this, "Darknet node in old opennet peers?!: "+pn);
else
opennet.addOldOpennetNode((OpennetPeerNode)pn);
} else
addPeer(pn, true, false);
}
try {
br.close();
} catch(IOException e3) {
Expand Down

0 comments on commit 13c71cb

Please sign in to comment.