📄 uriseedingmanager.java
字号:
* {@inheritDoc} */ public synchronized RouteAdvertisement[] getActiveSeedRoutes() { refreshActiveSeeds(); List<RouteAdvertisement> result = new ArrayList<RouteAdvertisement>(activeSeeds); // Add more primordial seeds. if(!allowOnlySeeds) { for(RouteAdvertisement eachRoute : Arrays.asList(super.getActiveSeedRoutes())) { if(!result.contains(eachRoute)) { result.add(eachRoute); } } } return result.toArray(new RouteAdvertisement[result.size()]); } /** * {@inheritDoc} */ @Override public synchronized boolean isAcceptablePeer(PeerAdvertisement peeradv) { RouteAdvertisement route = EndpointUtils.extractRouteAdv(peeradv); boolean acceptable = true; if (allowOnlySeeds) { acceptable = isSeedPeer(route); } if (!acceptable) { return false; } if (null != route) { return isAcceptablePeer(route); } else { return acl.getGrantAll(); } } /** * {@inheritDoc} */ @Override public synchronized boolean isAcceptablePeer(RouteAdvertisement radv) { boolean acceptable = true; if (allowOnlySeeds) { acceptable = isSeedPeer(radv); } return acceptable && super.isAcceptablePeer(radv); } private void refreshActiveSeeds() { if (TimeUtils.timeNow() < nextSeedingURIrefreshTime) { return; } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Regenerating active seeds list."); } activeSeeds.clear(); if (!seedingURIs.isEmpty()) { List<URI> allSeedingURIs = new ArrayList<URI>(seedingURIs); boolean allLoadsFailed = true; Collections.shuffle(allSeedingURIs); for (URI aSeedingURI : allSeedingURIs) { try { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Loading seeding list from : " + aSeedingURI); } RouteAdvertisement ras[] = loadSeeds(aSeedingURI); for (RouteAdvertisement aRA : Arrays.asList(ras)) { if (!activeSeeds.contains(aRA)) { // Only add non-duplicates. activeSeeds.add(aRA); allLoadsFailed = false; } } } catch (IOException failed) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Failed loading seeding list from : " + aSeedingURI); } } } if (allLoadsFailed) { // Allow for an early reload if we couldn't contact any of the // seeding URIS. failedSeedingLoads++; long nextAttemptInterval = Math.min(MINIMUM_SEEDING_REFRESH_INTERVAL * failedSeedingLoads, STANDARD_SEEDING_REFRESH_INTERVAL); nextSeedingURIrefreshTime = TimeUtils.toAbsoluteTimeMillis(nextAttemptInterval); } else { failedSeedingLoads = 0; nextSeedingURIrefreshTime = TimeUtils.toAbsoluteTimeMillis(STANDARD_SEEDING_REFRESH_INTERVAL); } } // Add the (shuffled) permanent seeds at the last. List<RouteAdvertisement> asList = new ArrayList<RouteAdvertisement>(permanentSeeds); Collections.shuffle(asList); activeSeeds.addAll(asList); } /** * Evaluates if the given route corresponds to one of our seeds. This is * to support the allowOnlySeeds flag. The test is not completely foolproof * since our list of seeds is just transport addresses. We could be given a * pve that exhibits an address that corresponds to one of our seeds but is * fake. And we might later succeed in connecting to that peer via one * the other, real addresses. As a result, allowOnlySeeds is *not* a * security feature, just a convenience for certain kind of deployments. * The remote peer's certificates should be examined in order to fully * establish that it an appropriate peer. */ private boolean isSeedPeer(RouteAdvertisement route) { List<?> addrList = route.getDestEndpointAddresses(); ListIterator eachAddr = addrList.listIterator(); // convert each EndpointAddress to a URI to compare with seedHosts while (eachAddr.hasNext()) { EndpointAddress anAddr = (EndpointAddress) eachAddr.next(); eachAddr.set(anAddr.toURI()); } addrList.retainAll(Arrays.asList(getActiveSeedURIs())); // What's left is the intersection of activeSeeds and the set of // endpoint addresses in the given APA. If it is non-empty, then we // accept the route as that of a seed host. return (!addrList.isEmpty()); } /** * Load a list of seed peer RouteAdvertisements from the specified URI. * <p/> * Two formats are supported: * <dl> * <dt>TEXT</dt> * <dd>A simple UTF-8 or US ASCII text file containing one seed * endpoint address per line. These entries are converted into very * simple {@code RouteAdvertisement}s.</dd> * <dt>XML</dt> * <dd>A simple XML file containing a sequence of seed * {@code RouteAdvertisement}s. The seed advertisements may be ordered * or unordered.</dd> * </dl> * * @param seedingURI The intended source of the {@code RouteAdvertisement}s. * @return The loaded {@code RouteAdvertisement}s. * @throws IOException Thrown for errors encountered loading the seed * RouteAdvertisements. */ static RouteAdvertisement[] loadSeeds(URI seedingURI) throws IOException { boolean isXML; URL seedingURL = seedingURI.toURL(); URLConnection connection = seedingURL.openConnection(); connection.setDoInput(true); InputStream is = connection.getInputStream(); // Determine if the input file is an XML document or a plain text list. // If it is not XML then we assume it is text. String content_type = connection.getContentType(); MimeMediaType type; if (null == content_type) { // If we couldn't get a content-type from the connection then let's // try to get it from the URI path. String name = seedingURI.getPath(); int extIdx = name.lastIndexOf('.'); int sepIdx = name.lastIndexOf('/'); if ((-1 != extIdx) && (extIdx > sepIdx)) { String ext = name.substring(extIdx + 1); type = StructuredDocumentFactory.getMimeTypeForFileExtension(ext); } else { // Type is unknown. :-( type = MimeMediaType.AOS; } } else { type = new MimeMediaType(content_type); } isXML = MimeMediaType.XML_DEFAULTENCODING.equalsIngoringParams(type) || MimeMediaType.APPLICATION_XML_DEFAULTENCODING.equalsIngoringParams(type); BufferedReader seeds = new BufferedReader(new InputStreamReader(is)); List<RouteAdvertisement> result = new ArrayList<RouteAdvertisement>(); if (isXML) { // Read in XML format seeds. (a list of Route Advertisements) XMLDocument xmldoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XML_DEFAULTENCODING, seeds); Enumeration<XMLElement> eachRA = xmldoc.getChildren(RouteAdvertisement.getAdvertisementType()); while (eachRA.hasMoreElements()) { XMLElement anRAElement = eachRA.nextElement(); RouteAdvertisement ra = (RouteAdvertisement) AdvertisementFactory.newAdvertisement(anRAElement); result.add(ra); } boolean randomize = true; Attribute ordered = xmldoc.getAttribute("ordered"); if (null != ordered) { randomize = !Boolean.valueOf(ordered.getValue()); } if (randomize) { Collections.shuffle(result); } } else { // Read in plain text format seeds. A list of Endpoint Addresses while (true) { String aSeed = seeds.readLine(); if (null == aSeed) { break; } aSeed = aSeed.trim(); if (0 == aSeed.length()) { continue; } try { URI validation = URI.create(aSeed); EndpointAddress ea = new EndpointAddress(validation.toString()); RouteAdvertisement ra = (RouteAdvertisement) AdvertisementFactory.newAdvertisement( RouteAdvertisement.getAdvertisementType()); ra.addDestEndpointAddress(ea); // Add the world's most pathetic RouteAdvertisement to the result. result.add(ra); } catch (IllegalArgumentException badURI) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "bad URI in seeding list : " + aSeed, badURI); } } } } is.close(); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine(MessageFormat.format("Loaded #{0} seeds from : {1}", result.size(), seedingURI)); } return result.toArray(new RouteAdvertisement[result.size()]); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -