getMap() {
return this.dna;
}
public final String getName() {
return get(yacySeed.NAME, "∅");
}
public final String getHexHash() {
return b64Hash2hexHash(this.hash);
}
public final void incSI(int count) {
String v = (String) this.dna.get(yacySeed.INDEX_OUT);
if (v == null) { v = yacySeed.ZERO; }
dna.put(yacySeed.INDEX_OUT, Integer.toString(Integer.parseInt(v) + count));
}
public final void incRI(int count) {
String v = (String) this.dna.get(yacySeed.INDEX_IN);
if (v == null) { v = yacySeed.ZERO; }
dna.put(yacySeed.INDEX_IN, Integer.toString(Integer.parseInt(v) + count));
}
public final void incSU(int count) {
String v = (String) this.dna.get(yacySeed.URL_OUT);
if (v == null) { v = yacySeed.ZERO; }
dna.put(yacySeed.URL_OUT, Integer.toString(Integer.parseInt(v) + count));
}
public final void incRU(int count) {
String v = (String) this.dna.get(yacySeed.URL_IN);
if (v == null) { v = yacySeed.ZERO; }
dna.put(yacySeed.URL_IN, Integer.toString(Integer.parseInt(v) + count));
}
/**
* 12 * 6 bit = 72 bit = 24
characters octal-hash
* Octal hashes are used for cache-dumps that are DHT-ready
*
* Cause: the natural order of octal hashes are the same as the b64-order of b64Hashes.
* a hexhash cannot be used in such cases, and b64Hashes are not appropriate for file names
*
* @param b64Hash a base64 hash
* @return the octal representation of the given base64 hash
*/
public static String b64Hash2octalHash(String b64Hash) {
return serverCodings.encodeOctal(kelondroBase64Order.enhancedCoder.decode(b64Hash, "de.anomic.yacy.yacySeed.b64Hash2octalHash()"));
}
/**
* 12 * 6 bit = 72 bit = 18
characters hex-hash
* @param b64Hash a base64 hash
* @return the hexadecimal representation of the given base64 hash
*/
public static String b64Hash2hexHash(String b64Hash) {
// the hash string represents 12 * 6 bit = 72 bits. This is too much for a long integer.
return serverCodings.encodeHex(kelondroBase64Order.enhancedCoder.decode(b64Hash, "de.anomic.yacy.yacySeed.b64Hash2hexHash()"));
}
/**
* @param hexHash a hexadecimal hash
* @return the base64 representation of the given hex hash
*/
public static String hexHash2b64Hash(String hexHash) {
return kelondroBase64Order.enhancedCoder.encode(serverCodings.decodeHex(hexHash));
}
/**
* 12 * 6 bit = 72 bit = 9 byte
* @param b64Hash a base64 hash
* @return returns a base256 - a byte - representation of the given base64 hash
*/
public static byte[] b64Hash2b256Hash(String b64Hash) {
assert b64Hash.length() == 12;
return kelondroBase64Order.enhancedCoder.decode(b64Hash, "de.anomic.yacy.yacySeed.b64Hash2b256Hash()");
}
/**
* @param b256Hash a base256 hash - normal byte number system
* @return the base64 representation of the given base256 hash
*/
public static String b256Hash2b64Hash(byte[] b256Hash) {
assert b256Hash.length == 9;
return kelondroBase64Order.enhancedCoder.encode(b256Hash);
}
/**
* The returned version follows this pattern: MAJORVERSION . MINORVERSION 0 SVN REVISION
* @return the YaCy version of this peer as a float or 0
if no valid value could be retrieved
* from this yacySeed object
*/
public final float getVersion() {
try {
return Float.parseFloat(get(yacySeed.VERSION, yacySeed.ZERO));
} catch (NumberFormatException e) {
return 0;
}
}
/**
* @return the public address of the peer as IP:port string or null
if no valid values for
* either the IP or the port could be retrieved from this yacySeed object
*/
public final String getPublicAddress() {
String ip = (String) this.dna.get(yacySeed.IP);
if (ip == null) { return null; }
if (ip.length() < 8) { return null; } // 10.0.0.0
// if (ip.equals(yacyCore.seedDB.mySeed.dna.get(yacySeed.IP))) ip = "127.0.0.1";
// if (this.hash.equals("xxxxxxxxxxxx")) return "192.168.100.1:3300";
final String port = (String) this.dna.get(yacySeed.PORT);
if ((port == null) || (port.length() < 2)) return null;
return ip + ":" + port;
}
/**
* If this seed is part of a cluster, the peer has probably the {@linkplain #alternativeIP} object set to
* a local IP. If this is present and the public IP of this peer is identical to the public IP of the own seed,
* construct an address using this IP; otherwise return the public address
* @see #getPublicAddress()
* @return the alternative IP:port if present, else the public address
*/
public final String getClusterAddress() {
if (this.alternativeIP == null) return getPublicAddress();
final String port = (String) this.dna.get(yacySeed.PORT);
if ((port == null) || (port.length() < 2)) return null;
return this.alternativeIP + ":" + port;
}
/**
* @return the IP address of the peer represented by this yacySeed object as {@link InetAddress}
*/
public final InetAddress getInetAddress() {
return natLib.getInetAddress(this.dna.get(yacySeed.IP));
}
/** @return the portnumber of this seed or -1
if not present */
public final int getPort() {
final String port = (String) this.dna.get(yacySeed.PORT);
if (port == null) return -1;
/*if (port.length() < 2) return -1; It is possible to use port 0-9*/
return Integer.parseInt(port);
}
/**
* To synchronize peer pings the local time differential must be included in calculations.
* @return the difference to UTC (universal time coordinated) in milliseconds of this yacySeed,
* the difference to +0130
if not present or 0
if an error occured during conversion
*/
public final long getUTCDiff() {
String utc = (String) this.dna.get(yacySeed.UTC);
if (utc == null) { utc = "+0130"; }
try {
return serverDate.UTCDiff(utc);
} catch (IllegalArgumentException e) {
return 0;
}
}
/** puts the current time into the lastseen field and cares about the time differential to UTC */
public final void setLastSeenUTC() {
// because java thinks it must apply the UTC offset to the current time,
// to create a string that looks like our current time, it adds the local UTC offset to the
// time. To create a corrected UTC Date string, we first subtract the local UTC offset.
dna.put(yacySeed.LASTSEEN, serverDate.formatShortSecond(new Date(System.currentTimeMillis() - serverDate.UTCDiff())) );
}
/**
* @return the last seen time converted to UTC in milliseconds
*/
public final long getLastSeenUTC() {
try {
final long t = serverDate.parseShortSecond(get(yacySeed.LASTSEEN, "20040101000000")).getTime();
// getTime creates a UTC time number. But in this case java thinks, that the given
// time string is a local time, which has a local UTC offset applied.
// Therefore java subtracts the local UTC offset, to get a UTC number.
// But the given time string is already in UTC time, so the subtraction
// of the local UTC offset is wrong. We correct this here by adding the local UTC
// offset again.
return t + serverDate.UTCDiff();
} catch (java.text.ParseException e) {
return System.currentTimeMillis();
} catch (java.lang.NumberFormatException e) {
return System.currentTimeMillis();
}
}
/**
* @see #getLastSeenUTC()
* @return the last seen value as string representation in the following format: YearMonthDayHoursMinutesSeconds
* or 20040101000000
if not present
*/
public final String getLastSeenString() {
return get(yacySeed.LASTSEEN, "20040101000000");
}
/** @return the age of the seed in number of days */
public final int getAge() {
try {
final long t = serverDate.parseShortSecond(get(yacySeed.BDATE, "20040101000000")).getTime();
return (int) ((System.currentTimeMillis() - (t - getUTCDiff() + serverDate.UTCDiff())) / 1000 / 60 / 60 / 24);
} catch (java.text.ParseException e) {
return -1;
} catch (java.lang.NumberFormatException e) {
return -1;
}
}
public void setPeerTags(Set keys) {
dna.put(PEERTAGS, serverCodings.set2string(keys, "|", false));
}
public Set getPeerTags() {
return serverCodings.string2set(get(PEERTAGS, ""), "|");
}
public boolean matchPeerTags(Set searchHashes) {
Set tags = serverCodings.string2set(get(PEERTAGS, ""), "|");
Iterator i = tags.iterator();
while (i.hasNext()) {
if (searchHashes.contains(plasmaCondenser.word2hash((String) i.next()))) return true;
}
return false;
}
public int getPPM() {
try {
return Integer.parseInt(get(yacySeed.ISPEED, yacySeed.ZERO));
} catch (NumberFormatException e) {
return 0;
}
}
public double getQPM() {
try {
return Double.parseDouble(get(yacySeed.RSPEED, yacySeed.ZERO));
} catch (NumberFormatException e) {
return 0d;
}
}
public final long getLinkCount() {
try {
return getLong(yacySeed.LCOUNT, 0);
} catch (NumberFormatException e) {
return 0;
}
}
private boolean getFlag(int flag) {
final String flags = get(yacySeed.FLAGS, yacySeed.FLAGSZERO);
return (new bitfield(flags.getBytes())).get(flag);
}
private void setFlag(int flag, boolean value) {
String flags = get(yacySeed.FLAGS, yacySeed.FLAGSZERO);
if (flags.length() != 4) { flags = yacySeed.FLAGSZERO; }
final bitfield f = new bitfield(flags.getBytes());
f.set(flag, value);
dna.put(yacySeed.FLAGS, new String(f.getBytes()));
}
public final void setFlagDirectConnect(final boolean value) { setFlag(0, value); }
public final void setFlagAcceptRemoteCrawl(final boolean value) { setFlag(1, value); }
public final void setFlagAcceptRemoteIndex(final boolean value) { setFlag(2, value); }
public final void setFlagAcceptCitationReference(final boolean value) { setFlag(3, value); }
public final boolean getFlagDirectConnect() { return getFlag(0); }
public final boolean getFlagAcceptRemoteCrawl() {
//if (getVersion() < 0.300) return false;
//if (getVersion() < 0.334) return true;
return getFlag(1);
}
public final boolean getFlagAcceptRemoteIndex() {
//if (getVersion() < 0.335) return false;
return getFlag(2);
}
public final boolean getFlagAcceptCitationReference() {
return getFlag(3);
}
public final void setUnusedFlags() {
for (int i = 4; i < 24; i++) { setFlag(i, true); }
}
public final boolean isPotential() {
return isVirgin() || isJunior();
}
public final boolean isVirgin() {
return get(yacySeed.PEERTYPE, "").equals(yacySeed.PEERTYPE_VIRGIN);
}
public final boolean isJunior() {
return get(yacySeed.PEERTYPE, "").equals(yacySeed.PEERTYPE_JUNIOR);
}
public final boolean isActive() {
return isSenior() || isPrincipal();
}
public final boolean isSenior() {
return get(yacySeed.PEERTYPE, "").equals(yacySeed.PEERTYPE_SENIOR);
}
public final boolean isPrincipal() {
return get(yacySeed.PEERTYPE, "").equals(yacySeed.PEERTYPE_PRINCIPAL);
}
public final boolean isOnline() {
return isSenior() || isPrincipal();
}
public final boolean isOnline(final String type) {
return type.equals(yacySeed.PEERTYPE_SENIOR) || type.equals(yacySeed.PEERTYPE_PRINCIPAL);
}
public static final long minDHTNumber = kelondroBase64Order.enhancedCoder.cardinal(kelondroBase64Order.zero(12));
public static final long maxDHTDistance = Long.MAX_VALUE;
public double dhtPosition() {
// normalized to 1.0
return dhtPosition(this.hash);
}
public static double dhtPosition(String ahash) {
// normalized to 1.0
return ((double) kelondroBase64Order.enhancedCoder.cardinal(ahash.getBytes())) / ((double) maxDHTDistance);
}
public final static double dhtDistance(String from, String to) {
// computes a virtual distance, the result must be set in relation to maxDHTDistace
// if the distance is small, this peer is more responsible for that word hash
// if the distance is big, this peer is less responsible for that word hash
if (from == null) return dhtPosition(to);
final double fromPos = dhtPosition(from);
final double toPos = dhtPosition(to);
return (fromPos < toPos) ? (toPos - fromPos) : (1.0 - fromPos + toPos);
}
private static String bestNewHash(yacySeedDB seedDB) {
if ((seedDB == null) || (seedDB.sizeConnected() <= 8)) {
// use random hash
return randomHash();
}
int tries = Math.max(1, Math.min(32, seedDB.sizeConnected() / 2));
String hash;
String bestHash = null;
double c, bestC = Double.MAX_VALUE;
double segmentSize = Math.min(0.9, 4.0 / seedDB.sizeConnected());
Iterator i;
double d;
yacySeed s;
for (int t = 0; t < tries; t++) {
hash = randomHash();
i = seedDB.seedsConnected(true, true, hash, (float) 0.0);
c = 0.0;
while (i.hasNext()) {
s = i.next();
d = dhtDistance(hash, s.hash);
if (d > segmentSize) break;
c = c + 1.0/d;
}
System.out.println("BESTHASH " + hash + " = " + c);
if (c < bestC) {
bestC = c;
bestHash = hash;
System.out.println("BESTHASH " + hash + " is best now");
}
}
if (bestHash == null) return randomHash();
// at this point we know only the position of a peer sequence
double bestPosition = dhtPosition(bestHash) + (segmentSize / 2);
if (bestPosition > 1.0) bestPosition = bestPosition - 1.0;
bestHash = positionToHash(bestPosition);
System.out.println("BESTHASH finally is " + bestHash);
return bestHash;
}
private static String positionToHash(double t) {
// transform the position of a peer position into a close peer hash
assert t >= 0.0 : "t = " + t;
assert t < 1.0 : "t = " + t;
// now calculate a hash that is closest to the best position
double d, bestD = Double.MAX_VALUE;
int tries = 128;
String hash, bestHash = null;
for (int v = 0; v < tries; v++) {
hash = randomHash();
d = dhtPosition(hash);
if (Math.abs(d - t) < bestD) {
bestD = Math.abs(d - t);
bestHash = hash;
}
}
return bestHash;
}
public static yacySeed genLocalSeed(plasmaSwitchboard sb) {
// genera a seed for the local peer
// this is the birthplace of a seed, that then will start to travel to other peers
final String hash = bestNewHash(yacyCore.seedDB);
yacyCore.log.logInfo("init: OWN SEED = " + hash);
final yacySeed newSeed = new yacySeed(hash);
// now calculate other information about the host
newSeed.dna.put(yacySeed.NAME, sb.getConfig("peerName", "unnamed"));
if ((serverCore.portForwardingEnabled) && (serverCore.portForwarding != null)) {
newSeed.dna.put(yacySeed.PORT, Integer.toString(serverCore.portForwarding.getPort()));
} else {
newSeed.dna.put(yacySeed.PORT, Integer.toString(serverCore.getPortNr(sb.getConfig("port", "8080"))));
}
newSeed.dna.put(yacySeed.BDATE, serverDate.formatShortSecond(new Date(System.currentTimeMillis() - serverDate.UTCDiff())) );
newSeed.dna.put(yacySeed.LASTSEEN, newSeed.dna.get(yacySeed.BDATE)); // just as initial setting
newSeed.dna.put(yacySeed.UTC, serverDate.UTCDiffString());
newSeed.dna.put(yacySeed.PEERTYPE, yacySeed.PEERTYPE_VIRGIN);
return newSeed;
}
//public static String randomHash() { return "zLXFf5lTteUv"; } // only for debugging
public static String randomHash() {
final String hash =
kelondroBase64Order.enhancedCoder.encode(serverCodings.encodeMD5Raw(Long.toString(random.nextLong()))).substring(0, 6) +
kelondroBase64Order.enhancedCoder.encode(serverCodings.encodeMD5Raw(Long.toString(random.nextLong()))).substring(0, 6);
return hash;
}
public static yacySeed genRemoteSeed(String seedStr, String key, boolean properTest) {
// this method is used to convert the external representation of a seed into a seed object
// yacyCore.log.logFinest("genRemoteSeed: seedStr=" + seedStr + " key=" + key);
if (seedStr == null) { return null; }
final String seed = crypt.simpleDecode(seedStr, key);
if (seed == null) { return null; }
final Map dna = serverCodings.string2map(seed, ",");
final String hash = (String) dna.remove(yacySeed.HASH);
final yacySeed resultSeed = new yacySeed(hash, dna);
if (properTest) {
final String testResult = resultSeed.isProper();
if (testResult != null) {
yacyCore.log.logFinest("seed is not proper (" + testResult + "): " + resultSeed);
return null;
}
}
return resultSeed;
}
public final String toString() {
synchronized (this.dna) {
this.dna.put(yacySeed.HASH, this.hash); // set hash into seed code structure
final String s = serverCodings.map2string(this.dna, ",", true); // generate string representation
this.dna.remove(yacySeed.HASH); // reconstruct original: hash is stored external
return s;
}
}
public final String genSeedStr(String key) {
// use a default encoding
return this.genSeedStr('b', key);
}
public final synchronized String genSeedStr(char method, String key) {
return crypt.simpleEncode(this.toString(), key, method);
}
public final boolean isPeerOK() {
return this.hash != null && this.isProper() == null;
}
public final String isProper() {
// checks if everything is ok with that seed
if (this.hash == null) { return "hash is null"; }
if (this.hash.length() != yacySeedDB.commonHashLength) { return "wrong hash length (" + this.hash.length() + ")"; }
final String ip = (String) this.dna.get(yacySeed.IP);
if (ip == null) { return "IP is null"; }
if (ip.length() < 8) { return "IP is too short: " + ip; }
if (!natLib.isProper(ip)) { return "IP is not proper: " + ip; } //this does not work with staticIP
return null;
}
public final void save(File f) throws IOException {
final String out = this.genSeedStr('p', null);
final FileWriter fw = new FileWriter(f);
fw.write(out, 0, out.length());
fw.close();
}
public static yacySeed load(File f) throws IOException {
final FileReader fr = new FileReader(f);
final char[] b = new char[(int) f.length()];
fr.read(b, 0, b.length);
fr.close();
return genRemoteSeed(new String(b), null, false);
}
@SuppressWarnings("unchecked")
public final Object clone() {
synchronized (this.dna) {
return new yacySeed(this.hash, (HashMap) (new HashMap(this.dna)).clone());
}
}
}