From edf04f07c9a4afe89b14ffb5dd0c411d36b9dbdc Mon Sep 17 00:00:00 2001 From: ragnarok Date: Sun, 18 Sep 2005 08:50:56 +0000 Subject: [PATCH] * Implemented a MetaNamingService. --- .../i2p/client/naming/MetaNamingService.java | 60 +++++++++++++++++++ .../client/naming/PetNameNamingService.java | 42 ++++++------- history.txt | 10 +++- 3 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 core/java/src/net/i2p/client/naming/MetaNamingService.java diff --git a/core/java/src/net/i2p/client/naming/MetaNamingService.java b/core/java/src/net/i2p/client/naming/MetaNamingService.java new file mode 100644 index 0000000000..00cd382acb --- /dev/null +++ b/core/java/src/net/i2p/client/naming/MetaNamingService.java @@ -0,0 +1,60 @@ +package net.i2p.client.naming; + +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.List; +import java.util.Iterator; +import java.util.StringTokenizer; + +import net.i2p.I2PAppContext; +import net.i2p.data.Destination; + +public class MetaNamingService extends NamingService { + + private final static String PROP_NAME_SERVICES = "i2p.nameservicelist"; + private final static String DEFAULT_NAME_SERVICES = + "net.i2p.client.naming.PetNameNamingService,net.i2p.client.naming.HostsTxtNamingService"; + private List _services; + + public MetaNamingService(I2PAppContext context) { + super(context); + + String list = _context.getProperty(PROP_NAME_SERVICES, DEFAULT_NAME_SERVICES); + StringTokenizer tok = new StringTokenizer(list, ","); + _services = new ArrayList(tok.countTokens()); + while (tok.hasMoreTokens()) { + try { + Class cls = Class.forName(tok.nextToken()); + Constructor con = cls.getConstructor(new Class[] { I2PAppContext.class }); + _services.add(con.newInstance(new Object[] { context })); + } catch (Exception ex) { + _services.add(new DummyNamingService(context)); // fallback + } + } + } + + public Destination lookup(String hostname) { + Iterator iter = _services.iterator(); + while (iter.hasNext()) { + NamingService ns = (NamingService)iter.next(); + Destination dest = ns.lookup(hostname); + if (dest != null) { + return dest; + } + } + return lookupBase64(hostname); + } + + public String reverseLookup(Destination dest) { + Iterator iter = _services.iterator(); + while (iter.hasNext()) { + NamingService ns = (NamingService)iter.next(); + String hostname = ns.reverseLookup(dest); + if (hostname != null) { + return hostname; + } + } + return null; + } + +} diff --git a/core/java/src/net/i2p/client/naming/PetNameNamingService.java b/core/java/src/net/i2p/client/naming/PetNameNamingService.java index 7c197cb63a..164498d4a7 100644 --- a/core/java/src/net/i2p/client/naming/PetNameNamingService.java +++ b/core/java/src/net/i2p/client/naming/PetNameNamingService.java @@ -22,27 +22,27 @@ public class PetNameNamingService extends NamingService { //If the petnamedb file doesn't exist, create it, using the //contents of hosts.txt. - File nameFile = new File(file); - if (!nameFile.exists()) { - Properties hosts = new Properties(); - File hostsFile = new File("hosts.txt"); - if (hostsFile.exists() && hostsFile.canRead()) { - try { - DataHelper.loadProps(hosts, hostsFile); - } catch (IOException ioe) { - } - } - Iterator iter = hosts.keySet().iterator(); - while (iter.hasNext()) { - String hostname = (String)iter.next(); - PetName pn = new PetName(hostname, "i2p", "http", hosts.getProperty(hostname)); - _petnameDb.set(hostname, pn); - } - try { - _petnameDb.store(file); - } catch (IOException ioe) { - } - } +// File nameFile = new File(file); +// if (!nameFile.exists()) { +// Properties hosts = new Properties(); +// File hostsFile = new File("hosts.txt"); +// if (hostsFile.exists() && hostsFile.canRead()) { +// try { +// DataHelper.loadProps(hosts, hostsFile); +// } catch (IOException ioe) { +// } +// } +// Iterator iter = hosts.keySet().iterator(); +// while (iter.hasNext()) { +// String hostname = (String)iter.next(); +// PetName pn = new PetName(hostname, "i2p", "http", hosts.getProperty(hostname)); +// _petnameDb.set(hostname, pn); +// } +// try { +// _petnameDb.store(file); +// } catch (IOException ioe) { +// } +// } try { _petnameDb.load(file); diff --git a/history.txt b/history.txt index 825e2ed1fe..ebf15720df 100644 --- a/history.txt +++ b/history.txt @@ -1,11 +1,17 @@ -$Id: history.txt,v 1.257 2005/09/17 20:29:59 jrandom Exp $ +$Id: history.txt,v 1.258 2005/09/18 00:41:46 ragnarok Exp $ 2005-09-17 Ragnarok * Implemented a naming service using Syndie's petname db. It's not enabled by default, but you can try it out by setting i2p.naming.impl=net.i2p.client.naming.PetNameNamingService in router.config. - + * Implemented a meta naming service that will first lookup names in the + PetNameNamingService then fallback on the HostTxtNamingService. Which + naming services are checked and in which order is specified by + i2p.nameservicelist. This will probably become the default naming service + so please help test it out by setting + i2p.naming.impl=net.i2p.client.naming.MetaNamingService in router.config. + * 2005-09-17 0.6.0.6 released 2005-09-17 jrandom