Search in sources :

Example 1 with NamingService

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;
}
Also used : Destination(net.i2p.data.Destination) NamingService(net.i2p.client.naming.NamingService)

Example 2 with NamingService

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;
}
Also used : NamingService(net.i2p.client.naming.NamingService) SingleFileNamingService(net.i2p.client.naming.SingleFileNamingService)

Example 3 with NamingService

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();
}
Also used : HostTxtEntry(net.i2p.client.naming.HostTxtEntry) NamingService(net.i2p.client.naming.NamingService) SingleFileNamingService(net.i2p.client.naming.SingleFileNamingService) Properties(java.util.Properties) OrderedProperties(net.i2p.util.OrderedProperties) SingleFileNamingService(net.i2p.client.naming.SingleFileNamingService)

Example 4 with NamingService

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;
}
Also used : NamingService(net.i2p.client.naming.NamingService) SingleFileNamingService(net.i2p.client.naming.SingleFileNamingService)

Example 5 with NamingService

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;
}
Also used : Destination(net.i2p.data.Destination) Properties(java.util.Properties) LinkedList(java.util.LinkedList) NamingService(net.i2p.client.naming.NamingService) SortedMap(java.util.SortedMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Aggregations

NamingService (net.i2p.client.naming.NamingService)10 Properties (java.util.Properties)5 SingleFileNamingService (net.i2p.client.naming.SingleFileNamingService)4 Destination (net.i2p.data.Destination)4 OrderedProperties (net.i2p.util.OrderedProperties)3 File (java.io.File)2 I2PAppContext (net.i2p.I2PAppContext)2 DataFormatException (net.i2p.data.DataFormatException)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 StringTokenizer (java.util.StringTokenizer)1 I2PClient (net.i2p.client.I2PClient)1 I2PSession (net.i2p.client.I2PSession)1 I2PSessionException (net.i2p.client.I2PSessionException)1 I2PSimpleClient (net.i2p.client.I2PSimpleClient)1 HostTxtEntry (net.i2p.client.naming.HostTxtEntry)1