* Moved PetName and PetNameDB to core.
* Implement PetNameNamingService.
This commit is contained in:
172
core/java/src/net/i2p/client/naming/PetName.java
Normal file
172
core/java/src/net/i2p/client/naming/PetName.java
Normal file
@ -0,0 +1,172 @@
|
||||
package net.i2p.client.naming;
|
||||
|
||||
import java.util.*;
|
||||
import net.i2p.data.DataHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class PetName {
|
||||
private String _name;
|
||||
private String _network;
|
||||
private String _protocol;
|
||||
private List _groups;
|
||||
private boolean _isPublic;
|
||||
private String _location;
|
||||
|
||||
public PetName() {
|
||||
this(null, null, null, null);
|
||||
}
|
||||
public PetName(String name, String network, String protocol, String location) {
|
||||
_name = name;
|
||||
_network = network;
|
||||
_protocol = protocol;
|
||||
_location = location;
|
||||
_groups = new ArrayList();
|
||||
_isPublic = false;
|
||||
}
|
||||
/**
|
||||
* @param dbLine name:network:protocol:isPublic:group1,group2,group3:location
|
||||
*/
|
||||
public PetName(String dbLine) {
|
||||
_groups = new ArrayList();
|
||||
StringTokenizer tok = new StringTokenizer(dbLine, ":\n", true);
|
||||
int tokens = tok.countTokens();
|
||||
System.out.println("Tokens: " + tokens);
|
||||
if (tokens < 7) {
|
||||
return;
|
||||
}
|
||||
String s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
_name = null;
|
||||
} else {
|
||||
_name = s;
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
_network = null;
|
||||
} else {
|
||||
_network = s;
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
_protocol = null;
|
||||
} else {
|
||||
_protocol = s;
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
_isPublic = false;
|
||||
} else {
|
||||
if ("true".equals(s))
|
||||
_isPublic = true;
|
||||
else
|
||||
_isPublic = false;
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
// noop
|
||||
} else {
|
||||
StringTokenizer gtok = new StringTokenizer(s, ",");
|
||||
while (gtok.hasMoreTokens())
|
||||
_groups.add(gtok.nextToken().trim());
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
while (tok.hasMoreTokens()) {
|
||||
if (_location == null)
|
||||
_location = tok.nextToken();
|
||||
else
|
||||
_location = _location + tok.nextToken();
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() { return _name; }
|
||||
public String getNetwork() { return _network; }
|
||||
public String getProtocol() { return _protocol; }
|
||||
public String getLocation() { return _location; }
|
||||
public boolean getIsPublic() { return _isPublic; }
|
||||
public int getGroupCount() { return _groups.size(); }
|
||||
public String getGroup(int i) { return (String)_groups.get(i); }
|
||||
|
||||
public void setName(String name) { _name = name; }
|
||||
public void setNetwork(String network) { _network = network; }
|
||||
public void setProtocol(String protocol) { _protocol = protocol; }
|
||||
public void setLocation(String location) { _location = location; }
|
||||
public void setIsPublic(boolean pub) { _isPublic = pub; }
|
||||
public void addGroup(String name) {
|
||||
if ( (name != null) && (name.length() > 0) && (!_groups.contains(name)) )
|
||||
_groups.add(name);
|
||||
}
|
||||
public void removeGroup(String name) { _groups.remove(name); }
|
||||
public void setGroups(String groups) {
|
||||
if (groups != null) {
|
||||
_groups.clear();
|
||||
StringTokenizer tok = new StringTokenizer(groups, ", \t");
|
||||
while (tok.hasMoreTokens())
|
||||
addGroup(tok.nextToken().trim());
|
||||
} else {
|
||||
_groups.clear();
|
||||
}
|
||||
}
|
||||
public boolean isMember(String group) {
|
||||
for (int i = 0; i < getGroupCount(); i++)
|
||||
if (getGroup(i).equals(group))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buf = new StringBuffer(256);
|
||||
if (_name != null) buf.append(_name.trim());
|
||||
buf.append(':');
|
||||
if (_network != null) buf.append(_network.trim());
|
||||
buf.append(':');
|
||||
if (_protocol != null) buf.append(_protocol.trim());
|
||||
buf.append(':').append(_isPublic).append(':');
|
||||
if (_groups != null) {
|
||||
for (int i = 0; i < _groups.size(); i++) {
|
||||
buf.append(((String)_groups.get(i)).trim());
|
||||
if (i + 1 < _groups.size())
|
||||
buf.append(',');
|
||||
}
|
||||
}
|
||||
buf.append(':');
|
||||
if (_location != null) buf.append(_location.trim());
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if ( (obj == null) || !(obj instanceof PetName) ) return false;
|
||||
PetName pn = (PetName)obj;
|
||||
return DataHelper.eq(_name, pn._name) &&
|
||||
DataHelper.eq(_location, pn._location) &&
|
||||
DataHelper.eq(_network, pn._network) &&
|
||||
DataHelper.eq(_protocol, pn._protocol);
|
||||
}
|
||||
public int hashCode() {
|
||||
int rv = 0;
|
||||
rv += DataHelper.hashCode(_name);
|
||||
rv += DataHelper.hashCode(_location);
|
||||
rv += DataHelper.hashCode(_network);
|
||||
rv += DataHelper.hashCode(_protocol);
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
test("a:b:c:true:e:f");
|
||||
test("a:::true::d");
|
||||
test("a:::true::");
|
||||
test("a:b::true::");
|
||||
test(":::trye::");
|
||||
test("a:b:c:true:e:http://foo.bar");
|
||||
}
|
||||
private static void test(String line) {
|
||||
PetName pn = new PetName(line);
|
||||
String val = pn.toString();
|
||||
System.out.println("OK? " + val.equals(line) + ": " + line + " [" + val + "]");
|
||||
}
|
||||
}
|
75
core/java/src/net/i2p/client/naming/PetNameDB.java
Normal file
75
core/java/src/net/i2p/client/naming/PetNameDB.java
Normal file
@ -0,0 +1,75 @@
|
||||
package net.i2p.client.naming;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class PetNameDB {
|
||||
/** name (String) to PetName mapping */
|
||||
private Map _names;
|
||||
|
||||
public PetNameDB() {
|
||||
_names = Collections.synchronizedMap(new HashMap());
|
||||
}
|
||||
|
||||
public PetName get(String name) { return (PetName)_names.get(name); }
|
||||
public boolean exists(String name) { return _names.containsKey(name); }
|
||||
public void set(String name, PetName pn) { _names.put(name, pn); }
|
||||
public void remove(String name) { _names.remove(name); }
|
||||
public Set getNames() { return new HashSet(_names.keySet()); }
|
||||
public List getGroups() {
|
||||
List rv = new ArrayList();
|
||||
for (Iterator iter = new HashSet(_names.values()).iterator(); iter.hasNext(); ) {
|
||||
PetName name = (PetName)iter.next();
|
||||
for (int i = 0; i < name.getGroupCount(); i++)
|
||||
if (!rv.contains(name.getGroup(i)))
|
||||
rv.add(name.getGroup(i));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
public String getNameByLocation(String location) {
|
||||
if (location == null) return null;
|
||||
synchronized (_names) {
|
||||
for (Iterator iter = _names.values().iterator(); iter.hasNext(); ) {
|
||||
PetName name = (PetName)iter.next();
|
||||
if ( (name.getLocation() != null) && (name.getLocation().trim().equals(location.trim())) )
|
||||
return name.getName();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void load(String location) throws IOException {
|
||||
File f = new File(location);
|
||||
if (!f.exists()) return;
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
in = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
|
||||
String line = null;
|
||||
while ( (line = in.readLine()) != null) {
|
||||
PetName name = new PetName(line);
|
||||
if (name.getName() != null)
|
||||
_names.put(name.getName(), name);
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
public void store(String location) throws IOException {
|
||||
Writer out = null;
|
||||
try {
|
||||
out = new OutputStreamWriter(new FileOutputStream(location), "UTF-8");
|
||||
for (Iterator names = getNames().iterator(); names.hasNext(); ) {
|
||||
PetName name = get((String)names.next());
|
||||
if (name != null)
|
||||
out.write(name.toString() + "\n");
|
||||
}
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package net.i2p.client.naming;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.data.Destination;
|
||||
|
||||
public class PetNameNamingService extends NamingService {
|
||||
|
||||
private PetNameDB _petnameDb;
|
||||
public final static String PROP_PETNAME_FILE = "i2p.petnamefile";
|
||||
public final static String DEFAULT_PETNAME_FILE = "petnames.txt";
|
||||
|
||||
public PetNameNamingService(I2PAppContext context) {
|
||||
super(context);
|
||||
_petnameDb = new PetNameDB();
|
||||
String file = _context.getProperty(PROP_PETNAME_FILE, DEFAULT_PETNAME_FILE);
|
||||
|
||||
//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) {
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
_petnameDb.load(file);
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
|
||||
public Destination lookup(String hostname) {
|
||||
PetName name = _petnameDb.get(hostname);
|
||||
if (name != null && name.getNetwork().equals("i2p")) {
|
||||
return lookupBase64(name.getLocation());
|
||||
} else {
|
||||
return lookupBase64(hostname);
|
||||
}
|
||||
}
|
||||
|
||||
public String reverseLookup(Destination dest) {
|
||||
return _petnameDb.getNameByLocation(dest.toBase64());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user