use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.
the class SAMUtils method lookupHost.
/**
* Resolved the specified hostname.
*
* @param name Hostname to be resolved
*
* @return the Destination for the specified hostname, or null if not found
*/
private static Destination lookupHost(String name) {
NamingService ns = I2PAppContext.getGlobalContext().namingService();
Destination dest = ns.lookup(name);
return dest;
}
use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.
the class Daemon method getNamingService.
/**
* @return the configured NamingService, or the root NamingService
*/
private static NamingService getNamingService(String srch) {
NamingService root = I2PAppContext.getGlobalContext().namingService();
NamingService rv = searchNamingService(root, srch);
return rv != null ? rv : root;
}
use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.
the class Daemon method update.
/**
* Update the router and published address books using remote data from the
* subscribed address books listed in subscriptions.
* Merging of the "master" addressbook is NOT supported.
*
* @param router
* The NamingService to update, generally the root NamingService from the context.
* @param published
* The published AddressBook. This address book is published on
* the user's eepsite so that others may subscribe to it.
* May be null.
* If non-null, overwrite with the new addressbook.
* @param subscriptions
* A SubscriptionList listing the remote address books to update
* from.
* @param log
* The log to write changes and conflicts to.
* May be null.
* @since 0.8.7
*/
public static void update(NamingService router, File published, SubscriptionList subscriptions, Log log) {
// If the NamingService is a database, we look up as we go.
// If it is a text file, we do things differently, to avoid O(n**2) behavior
// when scanning large subscription results (i.e. those that return the whole file, not just the new entries) -
// we load all the known hostnames into a Set one time.
// This also has the advantage of not flushing the NamingService's LRU cache.
String nsClass = router.getClass().getSimpleName();
boolean isTextFile = nsClass.equals("HostsTxtNamingService") || nsClass.equals("SingleFileNamingService");
Set<String> knownNames;
if (isTextFile) {
// load the hostname set
Properties opts = new Properties();
opts.setProperty("file", "hosts.txt");
knownNames = router.getNames(opts);
} else {
knownNames = null;
}
NamingService publishedNS;
if (published != null) {
publishedNS = new SingleFileNamingService(I2PAppContext.getGlobalContext(), published.getAbsolutePath());
} else {
publishedNS = null;
}
Iterator<AddressBook> iter = subscriptions.iterator();
while (iter.hasNext()) {
// yes, the EepGet fetch() is done in next()
long start = System.currentTimeMillis();
AddressBook addressbook = iter.next();
// SubscriptionIterator puts in a dummy AddressBook with no location if no fetch is done
if (DEBUG && log != null && addressbook.getLocation() != null) {
long end = System.currentTimeMillis();
log.append("Fetch of " + addressbook.getLocation() + " took " + (end - start));
}
Iterator<Map.Entry<String, HostTxtEntry>> iter2 = addressbook.iterator();
try {
update(router, knownNames, publishedNS, addressbook, iter2, log);
} finally {
if (iter2 instanceof HostTxtIterator)
((HostTxtIterator) iter2).close();
addressbook.delete();
}
}
// subscriptions
subscriptions.write();
}
use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.
the class Daemon method searchNamingService.
/**
* depth-first search
*/
private static NamingService searchNamingService(NamingService ns, String srch) {
String name = ns.getName();
if (name.equals(srch) || name.endsWith('/' + srch) || name.endsWith('\\' + srch))
return ns;
List<NamingService> list = ns.getNamingServices();
if (list != null) {
for (NamingService nss : list) {
NamingService rv = searchNamingService(nss, srch);
if (rv != null)
return rv;
}
}
return null;
}
use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.
the class NamingServiceBean method getLoadBookMessages.
/**
* Load addressbook and apply filter, returning messages about this.
* To control memory, don't load the whole addressbook if we can help it...
* only load what is searched for.
*/
@Override
public String getLoadBookMessages() {
if (isDirect())
return super.getLoadBookMessages();
NamingService service = getNamingService();
debug("Searching within " + service + " with filename=" + getFileName() + " and with filter=" + filter + " and with search=" + search);
String message = "";
try {
LinkedList<AddressBean> list = new LinkedList<AddressBean>();
Map<String, Destination> results;
Properties searchProps = new Properties();
// only blockfile needs this
searchProps.setProperty("list", getFileName());
if (filter != null) {
String startsAt = filter.equals("0-9") ? "[0-9]" : filter;
searchProps.setProperty("startsWith", startsAt);
}
if (isPrefiltered()) {
// know the total number of results
if (beginIndex > 0)
searchProps.setProperty("skip", Integer.toString(beginIndex));
int limit = 1 + endIndex - beginIndex;
if (limit > 0)
searchProps.setProperty("limit", Integer.toString(limit));
}
if (search != null && search.length() > 0)
searchProps.setProperty("search", search.toLowerCase(Locale.US));
results = service.getEntries(searchProps);
debug("Result count: " + results.size());
for (Map.Entry<String, Destination> entry : results.entrySet()) {
String name = entry.getKey();
if (filter != null && filter.length() > 0) {
if (filter.equals("0-9")) {
char first = name.charAt(0);
if (first < '0' || first > '9')
continue;
} else if (!name.toLowerCase(Locale.US).startsWith(filter.toLowerCase(Locale.US))) {
continue;
}
}
if (search != null && search.length() > 0) {
if (name.indexOf(search) == -1) {
continue;
}
}
String destination = entry.getValue().toBase64();
if (destination != null) {
list.addLast(new AddressBean(name, destination));
} else {
// delete it too?
System.err.println("Bad entry " + name + " in database " + service.getName());
}
}
AddressBean[] array = list.toArray(new AddressBean[list.size()]);
if (!(results instanceof SortedMap))
Arrays.sort(array, sorter);
entries = array;
message = generateLoadMessage();
} catch (RuntimeException e) {
warn(e);
}
if (message.length() > 0)
message = "<p id=\"filtered\">" + message + "</p>";
return message;
}
Aggregations