0001: // yacySeed.java
0002: // -------------------------------------
0003: // (C) by Michael Peter Christen; mc@anomic.de
0004: // first published on http://www.anomic.de
0005: // Frankfurt, Germany, 2004
0006: //
0007: // $LastChangedDate: 2008-01-29 10:12:48 +0000 (Di, 29 Jan 2008) $
0008: // $LastChangedRevision: 4414 $
0009: // $LastChangedBy: orbiter $
0010: //
0011: // This program is free software; you can redistribute it and/or modify
0012: // it under the terms of the GNU General Public License as published by
0013: // the Free Software Foundation; either version 2 of the License, or
0014: // (at your option) any later version.
0015: //
0016: // This program is distributed in the hope that it will be useful,
0017: // but WITHOUT ANY WARRANTY; without even the implied warranty of
0018: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0019: // GNU General Public License for more details.
0020: //
0021: // You should have received a copy of the GNU General Public License
0022: // along with this program; if not, write to the Free Software
0023: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0024: //
0025: // Using this software in any meaning (reading, learning, copying, compiling,
0026: // running) means that you agree that the Author(s) is (are) not responsible
0027: // for cost, loss of data or any harm that may be caused directly or indirectly
0028: // by usage of this softare or this documentation. The usage of this software
0029: // is on your own risk. The installation and usage (starting/running) of this
0030: // software may allow other people or application to access your computer and
0031: // any attached devices and is highly dependent on the configuration of the
0032: // software which must be done by the user of the software; the author(s) is
0033: // (are) also not responsible for proper configuration and usage of the
0034: // software, even if provoked by documentation provided together with
0035: // the software.
0036: //
0037: // Any changes to this file according to the GPL as documented in the file
0038: // gpl.txt aside this file in the shipment you received can be done to the
0039: // lines that follows this copyright notice here, but changes must not be
0040: // done inside the copyright notive above. A re-distribution must contain
0041: // the intact and unchanged copyright notice.
0042: // Contributions and changes to the program code must be marked as such.
0043: //
0044: //
0045: // YACY stands for Yet Another CYberspace
0046: //
0047: // the yacySeed Object is the object that bundles and carries all information about
0048: // a single peer in the yacy space.
0049: // The yacySeed object is carried along peers using a string representation, that can
0050: // be compressed and/or scrambled, depending on the purpose of the process.
0051: //
0052: // the yacy status
0053: // any value that is defined here will be overwritten each time the proxy is started
0054: // to prevent that the system gets confused, it should be set to "" which means
0055: // undefined. Other status' that can be reached at run-time are
0056: // junior - a peer that has no public socket, thus cannot be reached on demand
0057: // senior - a peer that has a public socked and serves search queries
0058: // principal - a peer like a senior socket and serves as gateway for network definition
0059:
0060: package de.anomic.yacy;
0061:
0062: import java.io.File;
0063: import java.io.FileReader;
0064: import java.io.FileWriter;
0065: import java.io.IOException;
0066: import java.net.InetAddress;
0067: import java.util.Date;
0068: import java.util.HashMap;
0069: import java.util.Iterator;
0070: import java.util.Random;
0071: import java.util.Set;
0072:
0073: import de.anomic.kelondro.kelondroBase64Order;
0074: import de.anomic.net.natLib;
0075: import de.anomic.plasma.plasmaCondenser;
0076: import de.anomic.plasma.plasmaSwitchboard;
0077: import de.anomic.server.serverCodings;
0078: import de.anomic.server.serverCore;
0079: import de.anomic.server.serverDate;
0080: import de.anomic.server.serverDomains;
0081: import de.anomic.server.serverSystem;
0082: import de.anomic.tools.bitfield;
0083: import de.anomic.tools.crypt;
0084:
0085: public class yacySeed {
0086:
0087: public static final int maxsize = 4096;
0088: /**
0089: * <b>substance</b> "sI" (send index/words)
0090: */
0091: public static final String INDEX_OUT = "sI";
0092: /**
0093: * <b>substance</b> "rI" (received index/words)
0094: */
0095: public static final String INDEX_IN = "rI";
0096: /**
0097: * <b>substance</b> "sU" (send URLs)
0098: */
0099: public static final String URL_OUT = "sU";
0100: /**
0101: * <b>substance</b> "rU" (received URLs)
0102: */
0103: public static final String URL_IN = "rU";
0104: /**
0105: * <b>substance</b> "virgin"
0106: */
0107: public static final String PEERTYPE_VIRGIN = "virgin";
0108: /**
0109: * <b>substance</b> "junior"
0110: */
0111: public static final String PEERTYPE_JUNIOR = "junior";
0112: /**
0113: * <b>substance</b> "senior"
0114: */
0115: public static final String PEERTYPE_SENIOR = "senior";
0116: /**
0117: * <b>substance</b> "principal"
0118: */
0119: public static final String PEERTYPE_PRINCIPAL = "principal";
0120: /**
0121: * <b>substance</b> "PeerType"
0122: */
0123: public static final String PEERTYPE = "PeerType";
0124:
0125: /** static/dynamic (if the IP changes often for any reason) */
0126: public static final String IPTYPE = "IPType";
0127: public static final String FLAGS = "Flags";
0128: public static final String FLAGSZERO = "____";
0129: /** the applications version */
0130: public static final String VERSION = "Version";
0131:
0132: public static final String YOURTYPE = "yourtype";
0133: public static final String LASTSEEN = "LastSeen";
0134: public static final String USPEED = "USpeed";
0135:
0136: /** the name of the peer (user-set) */
0137: public static final String NAME = "Name";
0138: public static final String HASH = "Hash";
0139: /** Birthday - first startup */
0140: public static final String BDATE = "BDate";
0141: /** UTC-Offset */
0142: public static final String UTC = "UTC";
0143: public static final String PEERTAGS = "Tags";
0144:
0145: /** the speed of indexing (pages/minute) of the peer */
0146: public static final String ISPEED = "ISpeed";
0147: /** the speed of retrieval (queries/minute) of the peer */
0148: public static final String RSPEED = "RSpeed";
0149: /** the number of minutes that the peer is up in minutes/day (moving average MA30) */
0150: public static final String UPTIME = "Uptime";
0151: /** the number of links that the peer has stored (LURL's) */
0152: public static final String LCOUNT = "LCount";
0153: /** the number of links that the peer has noticed, but not loaded (NURL's) */
0154: public static final String NCOUNT = "NCount";
0155: /** the number of links that the peer provides for remote crawls (ZURL's) */
0156: public static final String RCOUNT = "RCount";
0157: /** the number of words the peer has indexed (as it says) */
0158: public static final String ICOUNT = "ICount";
0159: /** the number of seeds that the peer has stored */
0160: public static final String SCOUNT = "SCount";
0161: /** the number of clients that the peer connects (connects/hour as double) */
0162: public static final String CCOUNT = "CCount";
0163: /** Citation Rank (Own) - Count */
0164: public static final String CRWCNT = "CRWCnt";
0165: /** Citation Rank (Other) - Count */
0166: public static final String CRTCNT = "CRTCnt";
0167: public static final String IP = "IP";
0168: public static final String PORT = "Port";
0169: /** zero-value */
0170: public static final String ZERO = "0";
0171:
0172: public static final String DFLT_NETWORK_UNIT = "freeworld";
0173: public static final String DFLT_NETWORK_GROUP = "";
0174:
0175: private static final Random random = new Random(System
0176: .currentTimeMillis());
0177:
0178: // class variables
0179: /** the peer-hash */
0180: public String hash;
0181: /** a set of identity founding values, eg. IP, name of the peer, YaCy-version, ...*/
0182: private final HashMap<String, String> dna;
0183: public int available;
0184: public int selectscore = -1; // only for debugging
0185: public String alternativeIP = null;
0186:
0187: public yacySeed(String theHash, HashMap<String, String> theDna) {
0188: // create a seed with a pre-defined hash map
0189: this .hash = theHash;
0190: this .dna = theDna;
0191: final String flags = (String) this .dna.get(yacySeed.FLAGS);
0192: if ((flags == null) || (flags.length() != 4)) {
0193: this .dna.put(yacySeed.FLAGS, yacySeed.FLAGSZERO);
0194: }
0195: this .available = 0;
0196: }
0197:
0198: public yacySeed(String theHash) {
0199: this .dna = new HashMap<String, String>();
0200:
0201: // settings that can only be computed by originating peer:
0202: // at first startup -
0203: this .hash = theHash; // the hash key of the peer - very important. should be static somehow, even after restart
0204: this .dna.put(yacySeed.NAME, "∅");
0205: this .dna.put(yacySeed.BDATE, "∅");
0206: this .dna.put(yacySeed.UTC, "+0000");
0207: // later during operation -
0208: this .dna.put(yacySeed.ISPEED, yacySeed.ZERO);
0209: this .dna.put(yacySeed.RSPEED, yacySeed.ZERO);
0210: this .dna.put(yacySeed.UPTIME, yacySeed.ZERO);
0211: this .dna.put(yacySeed.LCOUNT, yacySeed.ZERO);
0212: this .dna.put(yacySeed.NCOUNT, yacySeed.ZERO);
0213: this .dna.put(yacySeed.RCOUNT, yacySeed.ZERO);
0214: this .dna.put(yacySeed.ICOUNT, yacySeed.ZERO);
0215: this .dna.put(yacySeed.SCOUNT, yacySeed.ZERO);
0216: this .dna.put(yacySeed.CCOUNT, yacySeed.ZERO);
0217: this .dna.put(yacySeed.VERSION, yacySeed.ZERO);
0218:
0219: // settings that is created during the 'hello' phase - in first contact
0220: this .dna.put(yacySeed.IP, ""); // 123.234.345.456
0221: this .dna.put(yacySeed.PORT, "∅");
0222: this .dna.put(yacySeed.PEERTYPE, yacySeed.PEERTYPE_VIRGIN); // virgin/junior/senior/principal
0223: this .dna.put(yacySeed.IPTYPE, "∅");
0224:
0225: // settings that can only be computed by visiting peer
0226: this .dna.put(yacySeed.LASTSEEN, serverDate
0227: .formatShortSecond(new Date(System.currentTimeMillis()
0228: - serverDate.UTCDiff()))); // for last-seen date
0229: this .dna.put(yacySeed.USPEED, yacySeed.ZERO); // the computated uplink speed of the peer
0230:
0231: this .dna.put(yacySeed.CRWCNT, yacySeed.ZERO);
0232: this .dna.put(yacySeed.CRTCNT, yacySeed.ZERO);
0233:
0234: // settings that are needed to organize the seed round-trip
0235: this .dna.put(yacySeed.FLAGS, yacySeed.FLAGSZERO);
0236: setFlagDirectConnect(false);
0237: setFlagAcceptRemoteCrawl(true);
0238: setFlagAcceptRemoteIndex(true);
0239: setFlagAcceptCitationReference(true);
0240: setUnusedFlags();
0241:
0242: // index transfer
0243: this .dna.put(yacySeed.INDEX_OUT, yacySeed.ZERO); // send index
0244: this .dna.put(yacySeed.INDEX_IN, yacySeed.ZERO); // received index
0245: this .dna.put(yacySeed.URL_OUT, yacySeed.ZERO); // send URLs
0246: this .dna.put(yacySeed.URL_IN, yacySeed.ZERO); // received URLs
0247:
0248: this .available = 0;
0249: }
0250:
0251: /**
0252: * Generate a default peer name assembled of the following fragments in order:
0253: * <ul>
0254: * <li>the public IP (may be an IPv4- or IPv6-IP) obtained by {@link serverCore#myPublicIP()} followed by a minus sign (<code>-</code>)</li>
0255: * <li>a pseudo-random value, the {@link yacyCore#speedKey}</li>
0256: * <li>the string '<code>dpn</code>' which assumingly stands for Default Peer Name</li>
0257: * <li>shortened OS information, the {@link serverSystem#infoKey()}</li>
0258: * <li>another pseudo-random value derived from the {@link System#currentTimeMillis()}-method modulo 99</li>
0259: * </ul>
0260: * @return a default peer name following the above pattern whereas dots, underscores and colons are replaced by minus signs
0261: */
0262: public static String makeDefaultPeerName() {
0263: String name = serverDomains.myPublicIP() + "-"
0264: + yacyCore.speedKey + "dpn" + serverSystem.infoKey()
0265: + (System.currentTimeMillis() & 99);
0266: name = name.replace('.', '-');
0267: name = name.replace('_', '-');
0268: name = name.replace(':', '-');
0269: return name;
0270: }
0271:
0272: /**
0273: * Checks for the static fragments of a generated default peer name, such as the string 'dpn'
0274: * @see #makeDefaultPeerName()
0275: * @param name the peer name to check for default peer name compliance
0276: * @return whether the given peer name may be a default generated peer name
0277: */
0278: public static boolean isDefaultPeerName(String name) {
0279: return (name != null && name.length() > 10
0280: && name.charAt(0) <= '9'
0281: && name.charAt(name.length() - 1) <= '9' && name
0282: .indexOf("dpn") > 0);
0283: }
0284:
0285: /**
0286: * used when doing routing within a cluster; this can assign a ip and a port
0287: * that is used instead the address stored in the seed DNA
0288: */
0289: public void setAlternativeAddress(String ipport) {
0290: if (ipport == null)
0291: return;
0292: int p = ipport.indexOf(':');
0293: if (p < 0)
0294: this .alternativeIP = ipport;
0295: else
0296: this .alternativeIP = ipport.substring(0, p);
0297: }
0298:
0299: /**
0300: * try to get the IP<br>
0301: * @return the IP or null
0302: */
0303: public final String getIP() {
0304: return get(yacySeed.IP, "");
0305: }
0306:
0307: /**
0308: * try to get the peertype<br>
0309: * @return the peertype or null
0310: */
0311: public final String getPeerType() {
0312: return get(yacySeed.PEERTYPE, "");
0313: }
0314:
0315: /**
0316: * try to get the peertype<br>
0317: * @return the peertype or "Virgin"
0318: */
0319: public final String orVirgin() {
0320: return get(yacySeed.PEERTYPE, yacySeed.PEERTYPE_VIRGIN);
0321: }
0322:
0323: /**
0324: * try to get the peertype<br>
0325: * @return the peertype or "junior"
0326: */
0327: public final String orJunior() {
0328: return get(yacySeed.PEERTYPE, yacySeed.PEERTYPE_JUNIOR);
0329: }
0330:
0331: /**
0332: * try to get the peertype<br>
0333: * @return the peertype or "senior"
0334: */
0335: public final String orSenior() {
0336: return get(yacySeed.PEERTYPE, yacySeed.PEERTYPE_SENIOR);
0337: }
0338:
0339: /**
0340: * try to get the peertype<br>
0341: * @return the peertype or "principal"
0342: */
0343: public final String orPrincipal() {
0344: return get(yacySeed.PEERTYPE, yacySeed.PEERTYPE_PRINCIPAL);
0345: }
0346:
0347: /**
0348: * Get a value from the peer's DNA (its set of peer defining values, e.g. IP, name, version, ...)
0349: * @param key the key for the value to fetch
0350: * @param dflt the default value
0351: */
0352: public final String get(String key, String dflt) {
0353: final Object o = this .dna.get(key);
0354: if (o == null) {
0355: return dflt;
0356: }
0357: return (String) o;
0358: }
0359:
0360: public final long getLong(String key, long dflt) {
0361: final Object o = this .dna.get(key);
0362: if (o == null) {
0363: return dflt;
0364: }
0365: if (o instanceof String)
0366: try {
0367: return Long.parseLong((String) o);
0368: } catch (NumberFormatException e) {
0369: return dflt;
0370: }
0371: else if (o instanceof Long) {
0372: return ((Long) o).longValue();
0373: } else if (o instanceof Integer) {
0374: return (long) ((Integer) o).intValue();
0375: } else
0376: return dflt;
0377: }
0378:
0379: public final void setIP() {
0380: dna.put(yacySeed.IP, "");
0381: }
0382:
0383: public final void setIP(final String ip) {
0384: dna.put(yacySeed.IP, ip);
0385: }
0386:
0387: public final void setPort(final String port) {
0388: dna.put(yacySeed.PORT, port);
0389: }
0390:
0391: public final void setJunior() {
0392: dna.put(yacySeed.PEERTYPE, yacySeed.PEERTYPE_JUNIOR);
0393: }
0394:
0395: public final void setSenior() {
0396: dna.put(yacySeed.PEERTYPE, yacySeed.PEERTYPE_SENIOR);
0397: }
0398:
0399: public final void setPrincipal() {
0400: dna.put(yacySeed.PEERTYPE, yacySeed.PEERTYPE_PRINCIPAL);
0401: }
0402:
0403: public final void put(String key, String value) {
0404: synchronized (this .dna) {
0405: this .dna.put(key, value);
0406: }
0407: }
0408:
0409: /** @return the DNA-map of this peer */
0410: public final HashMap<String, String> getMap() {
0411: return this .dna;
0412: }
0413:
0414: public final String getName() {
0415: return get(yacySeed.NAME, "∅");
0416: }
0417:
0418: public final String getHexHash() {
0419: return b64Hash2hexHash(this .hash);
0420: }
0421:
0422: public final void incSI(int count) {
0423: String v = (String) this .dna.get(yacySeed.INDEX_OUT);
0424: if (v == null) {
0425: v = yacySeed.ZERO;
0426: }
0427: dna.put(yacySeed.INDEX_OUT, Integer.toString(Integer
0428: .parseInt(v)
0429: + count));
0430: }
0431:
0432: public final void incRI(int count) {
0433: String v = (String) this .dna.get(yacySeed.INDEX_IN);
0434: if (v == null) {
0435: v = yacySeed.ZERO;
0436: }
0437: dna.put(yacySeed.INDEX_IN, Integer.toString(Integer.parseInt(v)
0438: + count));
0439: }
0440:
0441: public final void incSU(int count) {
0442: String v = (String) this .dna.get(yacySeed.URL_OUT);
0443: if (v == null) {
0444: v = yacySeed.ZERO;
0445: }
0446: dna.put(yacySeed.URL_OUT, Integer.toString(Integer.parseInt(v)
0447: + count));
0448: }
0449:
0450: public final void incRU(int count) {
0451: String v = (String) this .dna.get(yacySeed.URL_IN);
0452: if (v == null) {
0453: v = yacySeed.ZERO;
0454: }
0455: dna.put(yacySeed.URL_IN, Integer.toString(Integer.parseInt(v)
0456: + count));
0457: }
0458:
0459: /**
0460: * <code>12 * 6 bit = 72 bit = 24</code> characters octal-hash
0461: * <p>Octal hashes are used for cache-dumps that are DHT-ready</p>
0462: * <p>
0463: * Cause: the natural order of octal hashes are the same as the b64-order of b64Hashes.
0464: * a hexhash cannot be used in such cases, and b64Hashes are not appropriate for file names
0465: * </p>
0466: * @param b64Hash a base64 hash
0467: * @return the octal representation of the given base64 hash
0468: */
0469: public static String b64Hash2octalHash(String b64Hash) {
0470: return serverCodings
0471: .encodeOctal(kelondroBase64Order.enhancedCoder.decode(
0472: b64Hash,
0473: "de.anomic.yacy.yacySeed.b64Hash2octalHash()"));
0474: }
0475:
0476: /**
0477: * <code>12 * 6 bit = 72 bit = 18</code> characters hex-hash
0478: * @param b64Hash a base64 hash
0479: * @return the hexadecimal representation of the given base64 hash
0480: */
0481: public static String b64Hash2hexHash(String b64Hash) {
0482: // the hash string represents 12 * 6 bit = 72 bits. This is too much for a long integer.
0483: return serverCodings
0484: .encodeHex(kelondroBase64Order.enhancedCoder.decode(
0485: b64Hash,
0486: "de.anomic.yacy.yacySeed.b64Hash2hexHash()"));
0487: }
0488:
0489: /**
0490: * @param hexHash a hexadecimal hash
0491: * @return the base64 representation of the given hex hash
0492: */
0493: public static String hexHash2b64Hash(String hexHash) {
0494: return kelondroBase64Order.enhancedCoder.encode(serverCodings
0495: .decodeHex(hexHash));
0496: }
0497:
0498: /**
0499: * <code>12 * 6 bit = 72 bit = 9 byte</code>
0500: * @param b64Hash a base64 hash
0501: * @return returns a base256 - a byte - representation of the given base64 hash
0502: */
0503: public static byte[] b64Hash2b256Hash(String b64Hash) {
0504: assert b64Hash.length() == 12;
0505: return kelondroBase64Order.enhancedCoder.decode(b64Hash,
0506: "de.anomic.yacy.yacySeed.b64Hash2b256Hash()");
0507: }
0508:
0509: /**
0510: * @param b256Hash a base256 hash - normal byte number system
0511: * @return the base64 representation of the given base256 hash
0512: */
0513: public static String b256Hash2b64Hash(byte[] b256Hash) {
0514: assert b256Hash.length == 9;
0515: return kelondroBase64Order.enhancedCoder.encode(b256Hash);
0516: }
0517:
0518: /**
0519: * The returned version follows this pattern: <code>MAJORVERSION . MINORVERSION 0 SVN REVISION</code>
0520: * @return the YaCy version of this peer as a float or <code>0</code> if no valid value could be retrieved
0521: * from this yacySeed object
0522: */
0523: public final float getVersion() {
0524: try {
0525: return Float
0526: .parseFloat(get(yacySeed.VERSION, yacySeed.ZERO));
0527: } catch (NumberFormatException e) {
0528: return 0;
0529: }
0530: }
0531:
0532: /**
0533: * @return the public address of the peer as IP:port string or <code>null</code> if no valid values for
0534: * either the IP or the port could be retrieved from this yacySeed object
0535: */
0536: public final String getPublicAddress() {
0537: String ip = (String) this .dna.get(yacySeed.IP);
0538: if (ip == null) {
0539: return null;
0540: }
0541: if (ip.length() < 8) {
0542: return null;
0543: } // 10.0.0.0
0544: // if (ip.equals(yacyCore.seedDB.mySeed.dna.get(yacySeed.IP))) ip = "127.0.0.1";
0545: // if (this.hash.equals("xxxxxxxxxxxx")) return "192.168.100.1:3300";
0546:
0547: final String port = (String) this .dna.get(yacySeed.PORT);
0548: if ((port == null) || (port.length() < 2))
0549: return null;
0550:
0551: return ip + ":" + port;
0552: }
0553:
0554: /**
0555: * If this seed is part of a cluster, the peer has probably the {@linkplain #alternativeIP} object set to
0556: * a local IP. If this is present and the public IP of this peer is identical to the public IP of the own seed,
0557: * construct an address using this IP; otherwise return the public address
0558: * @see #getPublicAddress()
0559: * @return the alternative IP:port if present, else the public address
0560: */
0561: public final String getClusterAddress() {
0562: if (this .alternativeIP == null)
0563: return getPublicAddress();
0564:
0565: final String port = (String) this .dna.get(yacySeed.PORT);
0566: if ((port == null) || (port.length() < 2))
0567: return null;
0568:
0569: return this .alternativeIP + ":" + port;
0570: }
0571:
0572: /**
0573: * @return the IP address of the peer represented by this yacySeed object as {@link InetAddress}
0574: */
0575: public final InetAddress getInetAddress() {
0576: return natLib.getInetAddress(this .dna.get(yacySeed.IP));
0577: }
0578:
0579: /** @return the portnumber of this seed or <code>-1</code> if not present */
0580: public final int getPort() {
0581: final String port = (String) this .dna.get(yacySeed.PORT);
0582: if (port == null)
0583: return -1;
0584: /*if (port.length() < 2) return -1; It is possible to use port 0-9*/
0585: return Integer.parseInt(port);
0586: }
0587:
0588: /**
0589: * To synchronize peer pings the local time differential must be included in calculations.
0590: * @return the difference to UTC (universal time coordinated) in milliseconds of this yacySeed,
0591: * the difference to <code>+0130</code> if not present or <code>0</code> if an error occured during conversion
0592: */
0593: public final long getUTCDiff() {
0594: String utc = (String) this .dna.get(yacySeed.UTC);
0595: if (utc == null) {
0596: utc = "+0130";
0597: }
0598: try {
0599: return serverDate.UTCDiff(utc);
0600: } catch (IllegalArgumentException e) {
0601: return 0;
0602: }
0603: }
0604:
0605: /** puts the current time into the lastseen field and cares about the time differential to UTC */
0606: public final void setLastSeenUTC() {
0607: // because java thinks it must apply the UTC offset to the current time,
0608: // to create a string that looks like our current time, it adds the local UTC offset to the
0609: // time. To create a corrected UTC Date string, we first subtract the local UTC offset.
0610: dna.put(yacySeed.LASTSEEN, serverDate
0611: .formatShortSecond(new Date(System.currentTimeMillis()
0612: - serverDate.UTCDiff())));
0613: }
0614:
0615: /**
0616: * @return the last seen time converted to UTC in milliseconds
0617: */
0618: public final long getLastSeenUTC() {
0619: try {
0620: final long t = serverDate.parseShortSecond(
0621: get(yacySeed.LASTSEEN, "20040101000000")).getTime();
0622: // getTime creates a UTC time number. But in this case java thinks, that the given
0623: // time string is a local time, which has a local UTC offset applied.
0624: // Therefore java subtracts the local UTC offset, to get a UTC number.
0625: // But the given time string is already in UTC time, so the subtraction
0626: // of the local UTC offset is wrong. We correct this here by adding the local UTC
0627: // offset again.
0628: return t + serverDate.UTCDiff();
0629: } catch (java.text.ParseException e) {
0630: return System.currentTimeMillis();
0631: } catch (java.lang.NumberFormatException e) {
0632: return System.currentTimeMillis();
0633: }
0634: }
0635:
0636: /**
0637: * @see #getLastSeenUTC()
0638: * @return the last seen value as string representation in the following format: YearMonthDayHoursMinutesSeconds
0639: * or <code>20040101000000</code> if not present
0640: */
0641: public final String getLastSeenString() {
0642: return get(yacySeed.LASTSEEN, "20040101000000");
0643: }
0644:
0645: /** @return the age of the seed in number of days */
0646: public final int getAge() {
0647: try {
0648: final long t = serverDate.parseShortSecond(
0649: get(yacySeed.BDATE, "20040101000000")).getTime();
0650: return (int) ((System.currentTimeMillis() - (t
0651: - getUTCDiff() + serverDate.UTCDiff())) / 1000 / 60 / 60 / 24);
0652: } catch (java.text.ParseException e) {
0653: return -1;
0654: } catch (java.lang.NumberFormatException e) {
0655: return -1;
0656: }
0657: }
0658:
0659: public void setPeerTags(Set<String> keys) {
0660: dna.put(PEERTAGS, serverCodings.set2string(keys, "|", false));
0661: }
0662:
0663: public Set<String> getPeerTags() {
0664: return serverCodings.string2set(get(PEERTAGS, ""), "|");
0665: }
0666:
0667: public boolean matchPeerTags(Set<String> searchHashes) {
0668: Set<String> tags = serverCodings.string2set(get(PEERTAGS, ""),
0669: "|");
0670: Iterator<String> i = tags.iterator();
0671: while (i.hasNext()) {
0672: if (searchHashes.contains(plasmaCondenser
0673: .word2hash((String) i.next())))
0674: return true;
0675: }
0676: return false;
0677: }
0678:
0679: public int getPPM() {
0680: try {
0681: return Integer
0682: .parseInt(get(yacySeed.ISPEED, yacySeed.ZERO));
0683: } catch (NumberFormatException e) {
0684: return 0;
0685: }
0686: }
0687:
0688: public double getQPM() {
0689: try {
0690: return Double.parseDouble(get(yacySeed.RSPEED,
0691: yacySeed.ZERO));
0692: } catch (NumberFormatException e) {
0693: return 0d;
0694: }
0695: }
0696:
0697: public final long getLinkCount() {
0698: try {
0699: return getLong(yacySeed.LCOUNT, 0);
0700: } catch (NumberFormatException e) {
0701: return 0;
0702: }
0703: }
0704:
0705: private boolean getFlag(int flag) {
0706: final String flags = get(yacySeed.FLAGS, yacySeed.FLAGSZERO);
0707: return (new bitfield(flags.getBytes())).get(flag);
0708: }
0709:
0710: private void setFlag(int flag, boolean value) {
0711: String flags = get(yacySeed.FLAGS, yacySeed.FLAGSZERO);
0712: if (flags.length() != 4) {
0713: flags = yacySeed.FLAGSZERO;
0714: }
0715: final bitfield f = new bitfield(flags.getBytes());
0716: f.set(flag, value);
0717: dna.put(yacySeed.FLAGS, new String(f.getBytes()));
0718: }
0719:
0720: public final void setFlagDirectConnect(final boolean value) {
0721: setFlag(0, value);
0722: }
0723:
0724: public final void setFlagAcceptRemoteCrawl(final boolean value) {
0725: setFlag(1, value);
0726: }
0727:
0728: public final void setFlagAcceptRemoteIndex(final boolean value) {
0729: setFlag(2, value);
0730: }
0731:
0732: public final void setFlagAcceptCitationReference(final boolean value) {
0733: setFlag(3, value);
0734: }
0735:
0736: public final boolean getFlagDirectConnect() {
0737: return getFlag(0);
0738: }
0739:
0740: public final boolean getFlagAcceptRemoteCrawl() {
0741: //if (getVersion() < 0.300) return false;
0742: //if (getVersion() < 0.334) return true;
0743: return getFlag(1);
0744: }
0745:
0746: public final boolean getFlagAcceptRemoteIndex() {
0747: //if (getVersion() < 0.335) return false;
0748: return getFlag(2);
0749: }
0750:
0751: public final boolean getFlagAcceptCitationReference() {
0752: return getFlag(3);
0753: }
0754:
0755: public final void setUnusedFlags() {
0756: for (int i = 4; i < 24; i++) {
0757: setFlag(i, true);
0758: }
0759: }
0760:
0761: public final boolean isPotential() {
0762: return isVirgin() || isJunior();
0763: }
0764:
0765: public final boolean isVirgin() {
0766: return get(yacySeed.PEERTYPE, "").equals(
0767: yacySeed.PEERTYPE_VIRGIN);
0768: }
0769:
0770: public final boolean isJunior() {
0771: return get(yacySeed.PEERTYPE, "").equals(
0772: yacySeed.PEERTYPE_JUNIOR);
0773: }
0774:
0775: public final boolean isActive() {
0776: return isSenior() || isPrincipal();
0777: }
0778:
0779: public final boolean isSenior() {
0780: return get(yacySeed.PEERTYPE, "").equals(
0781: yacySeed.PEERTYPE_SENIOR);
0782: }
0783:
0784: public final boolean isPrincipal() {
0785: return get(yacySeed.PEERTYPE, "").equals(
0786: yacySeed.PEERTYPE_PRINCIPAL);
0787: }
0788:
0789: public final boolean isOnline() {
0790: return isSenior() || isPrincipal();
0791: }
0792:
0793: public final boolean isOnline(final String type) {
0794: return type.equals(yacySeed.PEERTYPE_SENIOR)
0795: || type.equals(yacySeed.PEERTYPE_PRINCIPAL);
0796: }
0797:
0798: public static final long minDHTNumber = kelondroBase64Order.enhancedCoder
0799: .cardinal(kelondroBase64Order.zero(12));
0800: public static final long maxDHTDistance = Long.MAX_VALUE;
0801:
0802: public double dhtPosition() {
0803: // normalized to 1.0
0804: return dhtPosition(this .hash);
0805: }
0806:
0807: public static double dhtPosition(String ahash) {
0808: // normalized to 1.0
0809: return ((double) kelondroBase64Order.enhancedCoder
0810: .cardinal(ahash.getBytes()))
0811: / ((double) maxDHTDistance);
0812: }
0813:
0814: public final static double dhtDistance(String from, String to) {
0815: // computes a virtual distance, the result must be set in relation to maxDHTDistace
0816: // if the distance is small, this peer is more responsible for that word hash
0817: // if the distance is big, this peer is less responsible for that word hash
0818: if (from == null)
0819: return dhtPosition(to);
0820: final double fromPos = dhtPosition(from);
0821: final double toPos = dhtPosition(to);
0822: return (fromPos < toPos) ? (toPos - fromPos)
0823: : (1.0 - fromPos + toPos);
0824: }
0825:
0826: private static String bestNewHash(yacySeedDB seedDB) {
0827: if ((seedDB == null) || (seedDB.sizeConnected() <= 8)) {
0828: // use random hash
0829: return randomHash();
0830: }
0831:
0832: int tries = Math.max(1, Math
0833: .min(32, seedDB.sizeConnected() / 2));
0834: String hash;
0835: String bestHash = null;
0836: double c, bestC = Double.MAX_VALUE;
0837: double segmentSize = Math
0838: .min(0.9, 4.0 / seedDB.sizeConnected());
0839: Iterator<yacySeed> i;
0840: double d;
0841: yacySeed s;
0842: for (int t = 0; t < tries; t++) {
0843: hash = randomHash();
0844: i = seedDB.seedsConnected(true, true, hash, (float) 0.0);
0845: c = 0.0;
0846: while (i.hasNext()) {
0847: s = i.next();
0848: d = dhtDistance(hash, s.hash);
0849: if (d > segmentSize)
0850: break;
0851: c = c + 1.0 / d;
0852: }
0853: System.out.println("BESTHASH " + hash + " = " + c);
0854: if (c < bestC) {
0855: bestC = c;
0856: bestHash = hash;
0857: System.out
0858: .println("BESTHASH " + hash + " is best now");
0859: }
0860: }
0861: if (bestHash == null)
0862: return randomHash();
0863:
0864: // at this point we know only the position of a peer sequence
0865: double bestPosition = dhtPosition(bestHash) + (segmentSize / 2);
0866: if (bestPosition > 1.0)
0867: bestPosition = bestPosition - 1.0;
0868: bestHash = positionToHash(bestPosition);
0869: System.out.println("BESTHASH finally is " + bestHash);
0870: return bestHash;
0871: }
0872:
0873: private static String positionToHash(double t) {
0874: // transform the position of a peer position into a close peer hash
0875: assert t >= 0.0 : "t = " + t;
0876: assert t < 1.0 : "t = " + t;
0877:
0878: // now calculate a hash that is closest to the best position
0879: double d, bestD = Double.MAX_VALUE;
0880: int tries = 128;
0881: String hash, bestHash = null;
0882: for (int v = 0; v < tries; v++) {
0883: hash = randomHash();
0884: d = dhtPosition(hash);
0885: if (Math.abs(d - t) < bestD) {
0886: bestD = Math.abs(d - t);
0887: bestHash = hash;
0888: }
0889: }
0890: return bestHash;
0891: }
0892:
0893: public static yacySeed genLocalSeed(plasmaSwitchboard sb) {
0894: // genera a seed for the local peer
0895: // this is the birthplace of a seed, that then will start to travel to other peers
0896:
0897: final String hash = bestNewHash(yacyCore.seedDB);
0898: yacyCore.log.logInfo("init: OWN SEED = " + hash);
0899:
0900: final yacySeed newSeed = new yacySeed(hash);
0901:
0902: // now calculate other information about the host
0903: newSeed.dna.put(yacySeed.NAME, sb.getConfig("peerName",
0904: "unnamed"));
0905: if ((serverCore.portForwardingEnabled)
0906: && (serverCore.portForwarding != null)) {
0907: newSeed.dna.put(yacySeed.PORT, Integer
0908: .toString(serverCore.portForwarding.getPort()));
0909: } else {
0910: newSeed.dna.put(yacySeed.PORT, Integer.toString(serverCore
0911: .getPortNr(sb.getConfig("port", "8080"))));
0912: }
0913: newSeed.dna.put(yacySeed.BDATE, serverDate
0914: .formatShortSecond(new Date(System.currentTimeMillis()
0915: - serverDate.UTCDiff())));
0916: newSeed.dna.put(yacySeed.LASTSEEN, newSeed.dna
0917: .get(yacySeed.BDATE)); // just as initial setting
0918: newSeed.dna.put(yacySeed.UTC, serverDate.UTCDiffString());
0919: newSeed.dna.put(yacySeed.PEERTYPE, yacySeed.PEERTYPE_VIRGIN);
0920:
0921: return newSeed;
0922: }
0923:
0924: //public static String randomHash() { return "zLXFf5lTteUv"; } // only for debugging
0925:
0926: public static String randomHash() {
0927: final String hash = kelondroBase64Order.enhancedCoder.encode(
0928: serverCodings.encodeMD5Raw(Long.toString(random
0929: .nextLong()))).substring(0, 6)
0930: + kelondroBase64Order.enhancedCoder.encode(
0931: serverCodings.encodeMD5Raw(Long.toString(random
0932: .nextLong()))).substring(0, 6);
0933: return hash;
0934: }
0935:
0936: public static yacySeed genRemoteSeed(String seedStr, String key,
0937: boolean properTest) {
0938: // this method is used to convert the external representation of a seed into a seed object
0939: // yacyCore.log.logFinest("genRemoteSeed: seedStr=" + seedStr + " key=" + key);
0940: if (seedStr == null) {
0941: return null;
0942: }
0943: final String seed = crypt.simpleDecode(seedStr, key);
0944: if (seed == null) {
0945: return null;
0946: }
0947: final HashMap<String, String> dna = serverCodings.string2map(
0948: seed, ",");
0949: final String hash = (String) dna.remove(yacySeed.HASH);
0950: final yacySeed resultSeed = new yacySeed(hash, dna);
0951: if (properTest) {
0952: final String testResult = resultSeed.isProper();
0953: if (testResult != null) {
0954: yacyCore.log.logFinest("seed is not proper ("
0955: + testResult + "): " + resultSeed);
0956: return null;
0957: }
0958: }
0959: return resultSeed;
0960: }
0961:
0962: public final String toString() {
0963: synchronized (this .dna) {
0964: this .dna.put(yacySeed.HASH, this .hash); // set hash into seed code structure
0965: final String s = serverCodings.map2string(this .dna, ",",
0966: true); // generate string representation
0967: this .dna.remove(yacySeed.HASH); // reconstruct original: hash is stored external
0968: return s;
0969: }
0970: }
0971:
0972: public final String genSeedStr(String key) {
0973: // use a default encoding
0974: return this .genSeedStr('b', key);
0975: }
0976:
0977: public final synchronized String genSeedStr(char method, String key) {
0978: return crypt.simpleEncode(this .toString(), key, method);
0979: }
0980:
0981: public final boolean isPeerOK() {
0982: return this .hash != null && this .isProper() == null;
0983: }
0984:
0985: public final String isProper() {
0986: // checks if everything is ok with that seed
0987: if (this .hash == null) {
0988: return "hash is null";
0989: }
0990: if (this .hash.length() != yacySeedDB.commonHashLength) {
0991: return "wrong hash length (" + this .hash.length() + ")";
0992: }
0993: final String ip = (String) this .dna.get(yacySeed.IP);
0994: if (ip == null) {
0995: return "IP is null";
0996: }
0997: if (ip.length() < 8) {
0998: return "IP is too short: " + ip;
0999: }
1000: if (!natLib.isProper(ip)) {
1001: return "IP is not proper: " + ip;
1002: } //this does not work with staticIP
1003: return null;
1004: }
1005:
1006: public final void save(File f) throws IOException {
1007: final String out = this .genSeedStr('p', null);
1008: final FileWriter fw = new FileWriter(f);
1009: fw.write(out, 0, out.length());
1010: fw.close();
1011: }
1012:
1013: public static yacySeed load(File f) throws IOException {
1014: final FileReader fr = new FileReader(f);
1015: final char[] b = new char[(int) f.length()];
1016: fr.read(b, 0, b.length);
1017: fr.close();
1018: return genRemoteSeed(new String(b), null, false);
1019: }
1020:
1021: @SuppressWarnings("unchecked")
1022: public final Object clone() {
1023: synchronized (this .dna) {
1024: return new yacySeed(
1025: this .hash,
1026: (HashMap<String, String>) (new HashMap<String, String>(
1027: this.dna)).clone());
1028: }
1029: }
1030:
1031: }
|