diff --git a/src/freenet/node/PeerManager.java b/src/freenet/node/PeerManager.java index 1d6a1a4c2e..4e8074f078 100644 --- a/src/freenet/node/PeerManager.java +++ b/src/freenet/node/PeerManager.java @@ -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. } @@ -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 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 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) { Logger.error(this, "Ignoring " + e3 + " caught reading " + peersFile, e3); } - if(someBroken) { + if (someBroken) { try { brokenPeersFile.delete(); FileOutputStream fos = new FileOutputStream(brokenPeersFile); @@ -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; }