Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split parsing peers into multiple steps #994

Open
wants to merge 8 commits into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 65 additions & 59 deletions src/freenet/node/PeerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,21 @@ void tryReadPeers(String filename, NodeCrypto crypto, OpennetManager opennet, bo
if(peersFile.exists())
if(readPeers(peersFile, crypto, opennet, oldOpennetPeers)) {
String msg;
if(oldOpennetPeers)
if (oldOpennetPeers) {
msg = "Read " + opennet.countOldOpennetPeers() + " old-opennet-peers from " + peersFile;
else if(isOpennet)
} else if(isOpennet) {
msg = "Read " + getOpennetPeers().length + " opennet peers from " + peersFile;
else
} else {
msg = "Read " + getDarknetPeers().length + " darknet peers from " + peersFile;
}
Logger.normal(this, msg);
System.out.println(msg);
return;
}
}
if(!isOpennet)
if (!isOpennet) {
System.out.println("No darknet peers file found.");
}
// The other cases are less important.
}

Expand All @@ -238,64 +240,68 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if this whole block is only copied, please add braces for all parts of if statements. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces and spaces added (and some tabify) ☺

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) {
Logger.error(this, "Ignoring " + e3 + " caught reading " + peersFile, e3);
}
if(someBroken) {
if (someBroken) {
try {
brokenPeersFile.delete();
FileOutputStream fos = new FileOutputStream(brokenPeersFile);
Expand All @@ -308,14 +314,14 @@ private boolean readPeers(File peersFile, NodeCrypto crypto, OpennetManager open
System.err.println("Unable to copy broken peers file.");
}
}
if(!droppedOldPeers.isEmpty()) {
try {
node.getClientCore().getAlerts().register(droppedOldPeers);
Logger.error(this, droppedOldPeers.getText());
} catch (Throwable t) {
// Startup MUST complete, don't let client layer problems kill it.
Logger.error(this, "Caught error telling user about dropped peers", t);
}
if (!droppedOldPeers.isEmpty()) {
try {
node.getClientCore().getAlerts().register(droppedOldPeers);
Logger.error(this, droppedOldPeers.getText());
} catch (Throwable t) {
// Startup MUST complete, don't let client layer problems kill it.
Logger.error(this, "Caught error telling user about dropped peers", t);
}
}
return !someBroken;
}
Expand Down