Source Code Cross Referenced for yacyClient.java in  » Search-Engine » yacy » de » anomic » yacy » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Search Engine » yacy » de.anomic.yacy 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        // yacyClient.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-02-03 12:40:40 +0000 (So, 03 Feb 2008) $
0008:        // $LastChangedRevision: 4434 $
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 notice 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:        package de.anomic.yacy;
0045:
0046:        import java.io.File;
0047:        import java.io.IOException;
0048:        import java.io.UnsupportedEncodingException;
0049:        import java.util.ArrayList;
0050:        import java.util.HashMap;
0051:        import java.util.Iterator;
0052:        import java.util.Map;
0053:        import java.util.TreeMap;
0054:
0055:        import de.anomic.http.httpRemoteProxyConfig;
0056:        import de.anomic.http.httpc;
0057:        import de.anomic.index.indexContainer;
0058:        import de.anomic.index.indexRWIEntry;
0059:        import de.anomic.index.indexRWIRowEntry;
0060:        import de.anomic.index.indexURLEntry;
0061:        import de.anomic.kelondro.kelondroBase64Order;
0062:        import de.anomic.kelondro.kelondroBitfield;
0063:        import de.anomic.plasma.plasmaCondenser;
0064:        import de.anomic.plasma.plasmaSearchRankingProcess;
0065:        import de.anomic.plasma.plasmaSearchRankingProfile;
0066:        import de.anomic.plasma.plasmaSnippetCache;
0067:        import de.anomic.plasma.plasmaSwitchboard;
0068:        import de.anomic.plasma.plasmaWordIndex;
0069:        import de.anomic.plasma.urlPattern.plasmaURLPattern;
0070:        import de.anomic.server.serverByteBuffer;
0071:        import de.anomic.server.serverCodings;
0072:        import de.anomic.server.serverCore;
0073:        import de.anomic.server.serverDomains;
0074:        import de.anomic.server.serverObjects;
0075:        import de.anomic.tools.crypt;
0076:        import de.anomic.tools.nxTools;
0077:        import de.anomic.xml.rssReader;
0078:
0079:        public final class yacyClient {
0080:
0081:            public static int publishMySeed(String address, String otherHash) {
0082:                // this is called to enrich the seed information by
0083:                // - own address (if peer is behind a nat/router)
0084:                // - check peer type (virgin/junior/senior/principal)
0085:                // to do this, we send a 'Hello' to another peer
0086:                // this carries the following information:
0087:                // 'iam' - own hash
0088:                // 'youare' - remote hash, to verify that we are correct
0089:                // 'key' - a session key that the remote peer may use to answer
0090:                // and the own seed string
0091:                // we expect the following information to be send back:
0092:                // - 'yourip' the ip of the connection peer (we)
0093:                // - 'yourtype' the type of this peer that the other peer checked by asking for a specific word
0094:                // and the remote seed string
0095:                // the number of new seeds are returned
0096:                // one exceptional failure case is when we know the other's peers hash, the other peers responds correctly
0097:                // but they appear to be another peer by comparisment of the other peer's hash
0098:                // this works of course only if we know the other peer's hash.
0099:
0100:                HashMap<String, String> result = null;
0101:                final serverObjects post = yacyNetwork.basicRequestPost(
0102:                        plasmaSwitchboard.getSwitchboard(), null);
0103:                for (int retry = 0; retry < 3; retry++)
0104:                    try {
0105:                        // generate request
0106:                        post.put("count", "20");
0107:                        post.put("seed", yacyCore.seedDB.mySeed().genSeedStr(
0108:                                post.get("key", "")));
0109:                        yacyCore.log
0110:                                .logFine("yacyClient.publishMySeed thread '"
0111:                                        + Thread.currentThread().getName()
0112:                                        + "' contacting peer at " + address);
0113:                        // send request
0114:                        result = nxTools.table(httpc.wput(new yacyURL("http://"
0115:                                + address + "/yacy/hello.html", null), yacySeed
0116:                                .b64Hash2hexHash(otherHash)
0117:                                + ".yacyh", 12000, null, null, proxyConfig(),
0118:                                post, null), "UTF-8");
0119:                        break;
0120:                    } catch (Exception e) {
0121:                        if (Thread.currentThread().isInterrupted()) {
0122:                            yacyCore.log
0123:                                    .logFine("yacyClient.publishMySeed thread '"
0124:                                            + Thread.currentThread().getName()
0125:                                            + "' interrupted.");
0126:                            return -1;
0127:                        } else {
0128:                            yacyCore.log
0129:                                    .logFine("yacyClient.publishMySeed thread '"
0130:                                            + Thread.currentThread().getName()
0131:                                            + "' exception: "
0132:                                            + e.getMessage()
0133:                                            + "; retry = " + retry); // here VERY OFTEN a 'Connection reset' appears. What is the cause?
0134:                            // try again (go into loop)
0135:                        }
0136:                        result = null;
0137:                    }
0138:
0139:                if (result == null || result.size() < 3) {
0140:                    yacyCore.log
0141:                            .logFine("yacyClient.publishMySeed result error: "
0142:                                    + ((result == null) ? "result null"
0143:                                            : ("result=" + result.toString())));
0144:                    return -1;
0145:                }
0146:
0147:                // check consistency with expectation
0148:                yacySeed otherPeer = null;
0149:                float otherPeerVersion = 0;
0150:                String seed;
0151:                if ((otherHash != null) && (otherHash.length() > 0)
0152:                        && ((seed = (String) result.get("seed0")) != null)) {
0153:                    if (seed.length() > yacySeed.maxsize) {
0154:                        yacyCore.log
0155:                                .logInfo("hello/client 0: rejected contacting seed; too large ("
0156:                                        + seed.length()
0157:                                        + " > "
0158:                                        + yacySeed.maxsize + ")");
0159:                    } else {
0160:                        otherPeer = yacySeed.genRemoteSeed(seed, post.get(
0161:                                "key", ""), true);
0162:                        if (otherPeer == null
0163:                                || !otherPeer.hash.equals(otherHash)) {
0164:                            yacyCore.log
0165:                                    .logFine("yacyClient.publishMySeed: consistency error: other peer '"
0166:                                            + ((otherPeer == null) ? "unknown"
0167:                                                    : otherPeer.getName())
0168:                                            + "' wrong");
0169:                            return -1; // no success
0170:                        }
0171:                        otherPeerVersion = otherPeer.getVersion();
0172:                    }
0173:                }
0174:
0175:                // set my own seed according to new information
0176:                // we overwrite our own IP number only, if we do not portForwarding
0177:                if (serverCore.portForwardingEnabled || serverCore.useStaticIP) {
0178:                    yacyCore.seedDB.mySeed().put(yacySeed.IP,
0179:                            serverDomains.myPublicIP());
0180:                } else {
0181:                    yacyCore.seedDB.mySeed().put(yacySeed.IP,
0182:                            (String) result.get("yourip"));
0183:                }
0184:
0185:                /* If we have port forwarding enabled but the other peer uses a too old yacy version
0186:                 * we can ignore the seed-type that was reported by the peer.
0187:                 * 
0188:                 * Otherwise we have to change our seed-type  
0189:                 * 
0190:                 * @see serverCore#portForwardingEnabled 
0191:                 */
0192:                if (!serverCore.portForwardingEnabled
0193:                        || otherPeerVersion >= yacyVersion.YACY_SUPPORTS_PORT_FORWARDING) {
0194:                    String mytype = (String) result.get(yacySeed.YOURTYPE);
0195:                    if (mytype == null) {
0196:                        mytype = "";
0197:                    }
0198:                    yacyAccessible accessible = new yacyAccessible();
0199:                    if (mytype.equals(yacySeed.PEERTYPE_SENIOR)
0200:                            || mytype.equals(yacySeed.PEERTYPE_PRINCIPAL)) {
0201:                        accessible.IWasAccessed = true;
0202:                        if (yacyCore.seedDB.mySeed().isPrincipal()) {
0203:                            mytype = yacySeed.PEERTYPE_PRINCIPAL;
0204:                        }
0205:                    } else {
0206:                        accessible.IWasAccessed = false;
0207:                    }
0208:                    accessible.lastUpdated = System.currentTimeMillis();
0209:                    yacyCore.amIAccessibleDB.put(otherHash, accessible);
0210:
0211:                    /* 
0212:                     * If we were reported as junior we have to check if your port forwarding channel is broken
0213:                     * If this is true we try to reconnect the sch channel to the remote server now.
0214:                     */
0215:                    if (mytype.equalsIgnoreCase(yacySeed.PEERTYPE_JUNIOR)) {
0216:                        yacyCore.log.logInfo("yacyClient.publishMySeed: Peer '"
0217:                                + ((otherPeer == null) ? "unknown" : otherPeer
0218:                                        .getName())
0219:                                + "' reported us as junior.");
0220:                        if (serverCore.portForwardingEnabled) {
0221:                            if (!Thread.currentThread().isInterrupted()
0222:                                    && serverCore.portForwarding != null
0223:                                    && !serverCore.portForwarding.isConnected()) {
0224:                                yacyCore.log
0225:                                        .logWarning("yacyClient.publishMySeed: Broken portForwarding channel detected. Trying to reconnect ...");
0226:                                try {
0227:                                    serverCore.portForwarding.reconnect();
0228:                                } catch (IOException e) {
0229:                                    yacyCore.log
0230:                                            .logWarning("yacyClient.publishMySeed: Unable to reconnect to port forwarding host.");
0231:                                }
0232:                            }
0233:                        }
0234:                    } else if ((mytype
0235:                            .equalsIgnoreCase(yacySeed.PEERTYPE_SENIOR))
0236:                            || (mytype
0237:                                    .equalsIgnoreCase(yacySeed.PEERTYPE_PRINCIPAL))) {
0238:                        yacyCore.log.logFine("yacyClient.publishMySeed: Peer '"
0239:                                + ((otherPeer == null) ? "unknown" : otherPeer
0240:                                        .getName()) + "' reported us as "
0241:                                + mytype + ", accepted other peer.");
0242:                    } else {
0243:                        // wrong type report
0244:                        yacyCore.log.logFine("yacyClient.publishMySeed: Peer '"
0245:                                + ((otherPeer == null) ? "unknown" : otherPeer
0246:                                        .getName()) + "' reported us as "
0247:                                + mytype + ", rejecting other peer.");
0248:                        return -1;
0249:                    }
0250:                    if (yacyCore.seedDB.mySeed().orVirgin().equals(
0251:                            yacySeed.PEERTYPE_VIRGIN))
0252:                        yacyCore.seedDB.mySeed().put(yacySeed.PEERTYPE, mytype);
0253:                }
0254:
0255:                final String error = yacyCore.seedDB.mySeed().isProper();
0256:                if (error != null) {
0257:                    yacyCore.log
0258:                            .logSevere("yacyClient.publishMySeed mySeed error - not proper: "
0259:                                    + error);
0260:                    return -1;
0261:                }
0262:
0263:                //final Date remoteTime = yacyCore.parseUniversalDate((String) result.get(yacySeed.MYTIME)); // read remote time
0264:
0265:                // read the seeds that the peer returned and integrate them into own database
0266:                int i = 0;
0267:                int count = 0;
0268:                String seedStr;
0269:                while ((seedStr = (String) result.get("seed" + i++)) != null) {
0270:                    // integrate new seed into own database
0271:                    // the first seed, "seed0" is the seed of the responding peer
0272:                    if (seedStr.length() > yacySeed.maxsize) {
0273:                        yacyCore.log
0274:                                .logInfo("hello/client: rejected contacting seed; too large ("
0275:                                        + seedStr.length()
0276:                                        + " > "
0277:                                        + yacySeed.maxsize + ")");
0278:                    } else {
0279:                        if (yacyCore.peerActions.peerArrival(yacySeed
0280:                                .genRemoteSeed(seedStr, post.get("key", ""),
0281:                                        true), (i == 1)))
0282:                            count++;
0283:                    }
0284:                }
0285:                return count;
0286:            }
0287:
0288:            public static yacySeed querySeed(yacySeed target, String seedHash) {
0289:                // prepare request
0290:                final serverObjects post = yacyNetwork.basicRequestPost(
0291:                        plasmaSwitchboard.getSwitchboard(), target.hash);
0292:                post.put("object", "seed");
0293:                post.put("env", seedHash);
0294:
0295:                // send request
0296:                try {
0297:                    final HashMap<String, String> result = nxTools.table(httpc
0298:                            .wput(new yacyURL("http://"
0299:                                    + target.getClusterAddress()
0300:                                    + "/yacy/query.html", null), target
0301:                                    .getHexHash()
0302:                                    + ".yacyh", 8000, null, null,
0303:                                    proxyConfig(), post, null), "UTF-8");
0304:
0305:                    if (result == null || result.size() == 0) {
0306:                        return null;
0307:                    }
0308:                    //final Date remoteTime = yacyCore.parseUniversalDate((String) result.get(yacySeed.MYTIME)); // read remote time
0309:                    return yacySeed.genRemoteSeed((String) result
0310:                            .get("response"), post.get("key", ""), true);
0311:                } catch (Exception e) {
0312:                    yacyCore.log.logSevere("yacyClient.querySeed error:"
0313:                            + e.getMessage());
0314:                    return null;
0315:                }
0316:            }
0317:
0318:            public static int queryRWICount(yacySeed target, String wordHash) {
0319:                // prepare request
0320:                final serverObjects post = yacyNetwork.basicRequestPost(
0321:                        plasmaSwitchboard.getSwitchboard(), target.hash);
0322:                post.put("object", "rwicount");
0323:                post.put("ttl", "0");
0324:                post.put("env", wordHash);
0325:
0326:                // send request
0327:                try {
0328:                    final HashMap<String, String> result = nxTools.table(httpc
0329:                            .wput(new yacyURL("http://"
0330:                                    + target.getClusterAddress()
0331:                                    + "/yacy/query.html", null), target
0332:                                    .getHexHash()
0333:                                    + ".yacyh", 8000, null, null,
0334:                                    proxyConfig(), post, null), "UTF-8");
0335:
0336:                    if (result == null || result.size() == 0) {
0337:                        return -1;
0338:                    }
0339:                    return Integer.parseInt((String) result.get("response"));
0340:                } catch (Exception e) {
0341:                    yacyCore.log.logSevere("yacyClient.queryRWICount error:"
0342:                            + e.getMessage());
0343:                    return -1;
0344:                }
0345:            }
0346:
0347:            public static int queryUrlCount(yacySeed target) {
0348:                if (target == null) {
0349:                    return -1;
0350:                }
0351:                if (yacyCore.seedDB.mySeed() == null)
0352:                    return -1;
0353:
0354:                // prepare request
0355:                final serverObjects post = yacyNetwork.basicRequestPost(
0356:                        plasmaSwitchboard.getSwitchboard(), target.hash);
0357:                post.put("object", "lurlcount");
0358:                post.put("ttl", "0");
0359:                post.put("env", "");
0360:
0361:                // send request
0362:                try {
0363:                    final HashMap<String, String> result = nxTools.table(httpc
0364:                            .wput(new yacyURL("http://"
0365:                                    + target.getClusterAddress()
0366:                                    + "/yacy/query.html", null), target
0367:                                    .getHexHash()
0368:                                    + ".yacyh", 6000, null, null,
0369:                                    proxyConfig(), post, null), "UTF-8");
0370:
0371:                    if ((result == null) || (result.size() == 0))
0372:                        return -1;
0373:                    final String resp = (String) result.get("response");
0374:                    if (resp == null) {
0375:                        return -1;
0376:                    } else
0377:                        try {
0378:                            return Integer.parseInt(resp);
0379:                        } catch (NumberFormatException e) {
0380:                            return -1;
0381:                        }
0382:                } catch (IOException e) {
0383:                    yacyCore.log
0384:                            .logSevere("yacyClient.queryUrlCount error asking peer '"
0385:                                    + target.getName() + "':" + e.toString());
0386:                    return -1;
0387:                }
0388:            }
0389:
0390:            public static rssReader queryRemoteCrawlURLs(yacySeed target,
0391:                    int count) {
0392:                // returns a list of 
0393:                if (target == null) {
0394:                    return null;
0395:                }
0396:                if (yacyCore.seedDB.mySeed() == null)
0397:                    return null;
0398:
0399:                // prepare request
0400:                final serverObjects post = yacyNetwork.basicRequestPost(
0401:                        plasmaSwitchboard.getSwitchboard(), target.hash);
0402:                post.put("call", "remotecrawl");
0403:                post.put("count", count);
0404:
0405:                // send request
0406:                try {
0407:                    final byte[] result = httpc.wput(new yacyURL("http://"
0408:                            + target.getClusterAddress() + "/yacy/urls.xml",
0409:                            null), target.getHexHash() + ".yacyh", 60000, /* a long time-out is needed */
0410:                    null, null, proxyConfig(), post, null);
0411:
0412:                    rssReader reader = rssReader.parse(result);
0413:                    if (reader == null) {
0414:                        // case where the rss reader does not understand the content
0415:                        yacyCore.log
0416:                                .logWarning("yacyClient.queryRemoteCrawlURLs failed asking peer '"
0417:                                        + target.getName()
0418:                                        + "': probably bad response from remote peer");
0419:                        System.out.println("***DEBUG*** rss input = "
0420:                                + new String(result));
0421:                        target.put(yacySeed.RCOUNT, "0");
0422:                        yacyCore.seedDB.update(target.hash, target); // overwrite number of remote-available number to avoid that this peer is called again (until update is done by peer ping)
0423:                        //e.printStackTrace();
0424:                        return null;
0425:                    }
0426:                    return reader;
0427:                } catch (IOException e) {
0428:                    yacyCore.log
0429:                            .logSevere("yacyClient.queryRemoteCrawlURLs error asking peer '"
0430:                                    + target.getName() + "':" + e.toString());
0431:                    return null;
0432:                }
0433:            }
0434:
0435:            public static String[] search(String wordhashes,
0436:                    String excludehashes, String urlhashes, String prefer,
0437:                    String filter, int count, int maxDistance, boolean global,
0438:                    int partitions, yacySeed target, plasmaWordIndex wordIndex,
0439:                    plasmaSearchRankingProcess containerCache,
0440:                    Map<String, TreeMap<String, String>> abstractCache,
0441:                    plasmaURLPattern blacklist,
0442:                    plasmaSearchRankingProfile rankingProfile,
0443:                    kelondroBitfield constraint) {
0444:                // send a search request to peer with remote Hash
0445:                // this mainly converts the words into word hashes
0446:
0447:                // INPUT:
0448:                // iam        : complete seed of the requesting peer
0449:                // youare     : seed hash of the target peer, used for testing network stability
0450:                // key        : transmission key for response
0451:                // search     : a list of search words
0452:                // hsearch    : a string of word hashes
0453:                // fwdep      : forward depth. if "0" then peer may NOT ask another peer for more results
0454:                // fwden      : forward deny, a list of seed hashes. They may NOT be target of forward hopping
0455:                // count      : maximum number of wanted results
0456:                // global     : if "true", then result may consist of answers from other peers
0457:                // partitions : number of remote peers that are asked (for evaluation of QPM)
0458:                // duetime    : maximum time that a peer should spent to create a result
0459:
0460:                // prepare request
0461:                final serverObjects post = yacyNetwork.basicRequestPost(
0462:                        plasmaSwitchboard.getSwitchboard(), target.hash);
0463:                post.put("myseed", yacyCore.seedDB.mySeed().genSeedStr(
0464:                        post.get("key", "")));
0465:                post.put("count", Math.max(10, count));
0466:                post.put("resource", ((global) ? "global" : "local"));
0467:                post.put("partitions", partitions);
0468:                post.put("query", wordhashes);
0469:                post.put("exclude", excludehashes);
0470:                post.put("duetime", 1000);
0471:                post.put("urls", urlhashes);
0472:                post.put("prefer", prefer);
0473:                post.put("filter", filter);
0474:                post.put("ttl", "0");
0475:                post.put("maxdist", maxDistance);
0476:                post.put("profile", crypt.simpleEncode(rankingProfile
0477:                        .toExternalString()));
0478:                post.put("constraint", (constraint == null) ? "" : constraint
0479:                        .exportB64());
0480:                if (abstractCache != null)
0481:                    post.put("abstracts", "auto");
0482:                final long timestamp = System.currentTimeMillis();
0483:
0484:                // send request
0485:                HashMap<String, String> result = null;
0486:                try {
0487:                    result = nxTools.table(httpc.wput(new yacyURL("http://"
0488:                            + target.getClusterAddress() + "/yacy/search.html",
0489:                            null), target.getHexHash() + ".yacyh", 60000, null,
0490:                            null, proxyConfig(), post, null), "UTF-8");
0491:                } catch (IOException e) {
0492:                    yacyCore.log.logFine("SEARCH failed FROM "
0493:                            + target.hash
0494:                            + ":"
0495:                            + target.getName()
0496:                            + " ("
0497:                            + e.getMessage()
0498:                            + "), score="
0499:                            + target.selectscore
0500:                            + ", DHTdist="
0501:                            + yacyDHTAction.dhtDistance(target.hash, wordhashes
0502:                                    .substring(0, 12)));
0503:                    yacyCore.peerActions.peerDeparture(target,
0504:                            "search request to peer created io exception: "
0505:                                    + e.getMessage());
0506:                    return null;
0507:                }
0508:
0509:                if ((result == null) || (result.size() == 0)) {
0510:                    yacyCore.log.logFine("SEARCH failed FROM "
0511:                            + target.hash
0512:                            + ":"
0513:                            + target.getName()
0514:                            + " (zero response), score="
0515:                            + target.selectscore
0516:                            + ", DHTdist="
0517:                            + yacyDHTAction.dhtDistance(target.hash, wordhashes
0518:                                    .substring(0, 12)));
0519:                    return null;
0520:                }
0521:
0522:                // compute all computation times
0523:                final long totalrequesttime = System.currentTimeMillis()
0524:                        - timestamp;
0525:
0526:                // OUTPUT:
0527:                // version : application version of responder
0528:                // uptime : uptime in seconds of responder
0529:                // total : number of total available LURL's for this search
0530:                // count : number of returned LURL's for this search
0531:                // resource<n> : LURL of search
0532:                // fwhop : hops (depth) of forwards that had been performed to construct this result
0533:                // fwsrc : peers that helped to construct this result
0534:                // fwrec : peers that would have helped to construct this result (recommendations)
0535:                // searchtime : time that the peer actually spent to create the result
0536:                // references : references (search hints) that was calculated during search
0537:
0538:                // now create a plasmaIndex out of this result
0539:                // System.out.println("yacyClient: " + ((urlhashes.length() == 0) ? "primary" : "secondary")+ " search result = " + result.toString()); // debug
0540:
0541:                int results = 0, joincount = 0;
0542:                try {
0543:                    results = Integer.parseInt(result.get("count"));
0544:                    joincount = Integer.parseInt(result.get("joincount"));
0545:                } catch (NumberFormatException e) {
0546:                    yacyCore.log.logFine("SEARCH failed FROM " + target.hash
0547:                            + ":" + target.getName() + ", wrong output format");
0548:                    yacyCore.peerActions
0549:                            .peerDeparture(target,
0550:                                    "search request to peer created number format exception");
0551:                    return null;
0552:                }
0553:                // System.out.println("***result count " + results);
0554:
0555:                // create containers
0556:                final int words = wordhashes.length()
0557:                        / yacySeedDB.commonHashLength;
0558:                indexContainer[] container = new indexContainer[words];
0559:                for (int i = 0; i < words; i++) {
0560:                    container[i] = plasmaWordIndex.emptyContainer(wordhashes
0561:                            .substring(i * yacySeedDB.commonHashLength, (i + 1)
0562:                                    * yacySeedDB.commonHashLength), count);
0563:                }
0564:
0565:                // insert results to containers
0566:                indexURLEntry urlEntry;
0567:                String[] urls = new String[results];
0568:                for (int n = 0; n < results; n++) {
0569:                    // get one single search result
0570:                    urlEntry = wordIndex.loadedURL.newEntry((String) result
0571:                            .get("resource" + n));
0572:                    if (urlEntry == null)
0573:                        continue;
0574:                    assert (urlEntry.hash().length() == 12) : "urlEntry.hash() = "
0575:                            + urlEntry.hash();
0576:                    if (urlEntry.hash().length() != 12)
0577:                        continue; // bad url hash
0578:                    indexURLEntry.Components comp = urlEntry.comp();
0579:                    if (blacklist.isListed(plasmaURLPattern.BLACKLIST_SEARCH,
0580:                            comp.url())) {
0581:                        yacyCore.log
0582:                                .logInfo("remote search (client): filtered blacklisted url "
0583:                                        + comp.url()
0584:                                        + " from peer "
0585:                                        + target.getName());
0586:                        continue; // block with backlist
0587:                    }
0588:
0589:                    if (!plasmaSwitchboard.getSwitchboard().acceptURL(
0590:                            comp.url())) {
0591:                        yacyCore.log
0592:                                .logInfo("remote search (client): rejected url outside of our domain "
0593:                                        + comp.url()
0594:                                        + " from peer "
0595:                                        + target.getName());
0596:                        continue; // reject url outside of our domain
0597:                    }
0598:
0599:                    // save the url entry
0600:                    indexRWIEntry entry;
0601:                    if (urlEntry.word() == null) {
0602:                        yacyCore.log
0603:                                .logWarning("remote search (client): no word attached from peer "
0604:                                        + target.getName()
0605:                                        + ", version "
0606:                                        + target.getVersion());
0607:                        continue; // no word attached
0608:                    }
0609:
0610:                    // the search-result-url transports all the attributes of word indexes
0611:                    entry = urlEntry.word();
0612:                    if (!(entry.urlHash().equals(urlEntry.hash()))) {
0613:                        yacyCore.log
0614:                                .logInfo("remote search (client): url-hash "
0615:                                        + urlEntry.hash()
0616:                                        + " does not belong to word-attached-hash "
0617:                                        + entry.urlHash() + "; url = "
0618:                                        + comp.url() + " from peer "
0619:                                        + target.getName());
0620:                        continue; // spammed
0621:                    }
0622:
0623:                    // passed all checks, store url
0624:                    try {
0625:                        wordIndex.loadedURL.store(urlEntry);
0626:                        wordIndex.loadedURL.stack(urlEntry, yacyCore.seedDB
0627:                                .mySeed().hash, target.hash, 2);
0628:                    } catch (IOException e) {
0629:                        yacyCore.log.logSevere("could not store search result",
0630:                                e);
0631:                        continue; // db-error
0632:                    }
0633:
0634:                    if (urlEntry.snippet() != null) {
0635:                        // we don't store the snippets along the url entry,
0636:                        // because they are search-specific.
0637:                        // instead, they are placed in a snipped-search cache.
0638:                        // System.out.println("--- RECEIVED SNIPPET '" + link.snippet() + "'");
0639:                        plasmaSnippetCache.storeToCache(wordhashes, urlEntry
0640:                                .hash(), urlEntry.snippet());
0641:                    }
0642:
0643:                    // add the url entry to the word indexes
0644:                    for (int m = 0; m < words; m++) {
0645:                        container[m].add(entry, System.currentTimeMillis());
0646:                    }
0647:
0648:                    // store url hash for statistics
0649:                    urls[n] = urlEntry.hash();
0650:                }
0651:
0652:                // store remote result to local result container
0653:                synchronized (containerCache) {
0654:                    // insert one container into the search result buffer
0655:                    containerCache.insertRanked(container[0], false, joincount); // one is enough
0656:
0657:                    // integrate remote topwords
0658:                    String references = (String) result.get("references");
0659:                    yacyCore.log.logInfo("remote search (client): peer "
0660:                            + target.getName() + " sent references "
0661:                            + references);
0662:                    if (references != null) {
0663:                        // add references twice, so they can be countet (must have at least 2 entries)
0664:                        containerCache.addReferences(references.split(","));
0665:                        containerCache.addReferences(references.split(","));
0666:                    }
0667:                }
0668:
0669:                // read index abstract
0670:                if (abstractCache != null) {
0671:                    Iterator<Map.Entry<String, String>> i = result.entrySet()
0672:                            .iterator();
0673:                    Map.Entry<String, String> entry;
0674:                    TreeMap<String, String> singleAbstract;
0675:                    String wordhash;
0676:                    serverByteBuffer ci;
0677:                    while (i.hasNext()) {
0678:                        entry = i.next();
0679:                        if (entry.getKey().startsWith("indexabstract.")) {
0680:                            wordhash = entry.getKey().substring(14);
0681:                            synchronized (abstractCache) {
0682:                                singleAbstract = (TreeMap<String, String>) abstractCache
0683:                                        .get(wordhash); // a mapping from url-hashes to a string of peer-hashes
0684:                                if (singleAbstract == null)
0685:                                    singleAbstract = new TreeMap<String, String>();
0686:                                ci = new serverByteBuffer(entry.getValue()
0687:                                        .getBytes());
0688:                                //System.out.println("DEBUG-ABSTRACTFETCH: for word hash " + wordhash + " received " + ci.toString());
0689:                                indexContainer.decompressIndex(singleAbstract,
0690:                                        ci, target.hash);
0691:                                abstractCache.put(wordhash, singleAbstract);
0692:                            }
0693:                        }
0694:                    }
0695:                }
0696:
0697:                // insert the containers to the index
0698:                for (int m = 0; m < words; m++) {
0699:                    wordIndex.addEntries(container[m], true);
0700:                }
0701:
0702:                // generate statistics
0703:                long searchtime;
0704:                try {
0705:                    searchtime = Integer.parseInt((String) result
0706:                            .get("searchtime"));
0707:                } catch (NumberFormatException e) {
0708:                    searchtime = totalrequesttime;
0709:                }
0710:                yacyCore.log.logFine("SEARCH "
0711:                        + results
0712:                        + " URLS FROM "
0713:                        + target.hash
0714:                        + ":"
0715:                        + target.getName()
0716:                        + ", score="
0717:                        + target.selectscore
0718:                        + ", DHTdist="
0719:                        + ((wordhashes.length() < 12) ? "void" : Double
0720:                                .toString(yacyDHTAction.dhtDistance(
0721:                                        target.hash, wordhashes
0722:                                                .substring(0, 12))))
0723:                        + ", searchtime=" + searchtime + ", netdelay="
0724:                        + (totalrequesttime - searchtime) + ", references="
0725:                        + result.get("references"));
0726:                return urls;
0727:            }
0728:
0729:            public static HashMap<String, String> permissionMessage(
0730:                    String targetHash) {
0731:                // ask for allowed message size and attachement size
0732:                // if this replies null, the peer does not answer
0733:                if (yacyCore.seedDB == null || yacyCore.seedDB.mySeed() == null) {
0734:                    return null;
0735:                }
0736:
0737:                // prepare request
0738:                final serverObjects post = yacyNetwork.basicRequestPost(
0739:                        plasmaSwitchboard.getSwitchboard(), targetHash);
0740:                post.put("process", "permission");
0741:
0742:                // send request
0743:                try {
0744:                    final HashMap<String, String> result = nxTools.table(httpc
0745:                            .wput(new yacyURL("http://"
0746:                                    + targetAddress(targetHash)
0747:                                    + "/yacy/message.html", null), yacySeed
0748:                                    .b64Hash2hexHash(targetHash)
0749:                                    + ".yacyh", 8000, null, null,
0750:                                    proxyConfig(), post, null), "UTF-8");
0751:                    return result;
0752:                } catch (Exception e) {
0753:                    // most probably a network time-out exception
0754:                    yacyCore.log
0755:                            .logSevere("yacyClient.permissionMessage error:"
0756:                                    + e.getMessage());
0757:                    return null;
0758:                }
0759:            }
0760:
0761:            public static HashMap<String, String> postMessage(
0762:                    String targetHash, String subject, byte[] message) {
0763:                // this post a message to the remote message board
0764:
0765:                // prepare request
0766:                final serverObjects post = yacyNetwork.basicRequestPost(
0767:                        plasmaSwitchboard.getSwitchboard(), targetHash);
0768:                post.put("process", "post");
0769:                post.put("myseed", yacyCore.seedDB.mySeed().genSeedStr(
0770:                        post.get("key", "")));
0771:                post.put("subject", subject);
0772:                try {
0773:                    post.put("message", new String(message, "UTF-8"));
0774:                } catch (UnsupportedEncodingException e) {
0775:                    post.put("message", new String(message));
0776:                }
0777:
0778:                // send request
0779:                try {
0780:                    final HashMap<String, String> result = nxTools.table(httpc
0781:                            .wput(new yacyURL("http://"
0782:                                    + targetAddress(targetHash)
0783:                                    + "/yacy/message.html", null), yacySeed
0784:                                    .b64Hash2hexHash(targetHash)
0785:                                    + ".yacyh", 20000, null, null,
0786:                                    proxyConfig(), post, null), "UTF-8");
0787:                    return result;
0788:                } catch (Exception e) {
0789:                    yacyCore.log.logSevere("yacyClient.postMessage error:"
0790:                            + e.getMessage());
0791:                    return null;
0792:                }
0793:            }
0794:
0795:            public static String targetAddress(String targetHash) {
0796:                // find target address    
0797:                String address;
0798:                if (targetHash.equals(yacyCore.seedDB.mySeed().hash)) {
0799:                    address = yacyCore.seedDB.mySeed().getClusterAddress();
0800:                } else {
0801:                    final yacySeed targetSeed = yacyCore.seedDB
0802:                            .getConnected(targetHash);
0803:                    if (targetSeed == null) {
0804:                        return null;
0805:                    }
0806:                    address = targetSeed.getClusterAddress();
0807:                }
0808:                if (address == null)
0809:                    address = "localhost:8080";
0810:                return address;
0811:            }
0812:
0813:            public static HashMap<String, String> transferPermission(
0814:                    String targetAddress, long filesize, String filename) {
0815:
0816:                // prepare request
0817:                final serverObjects post = yacyNetwork.basicRequestPost(
0818:                        plasmaSwitchboard.getSwitchboard(), null);
0819:                post.put("process", "permission");
0820:                post.put("purpose", "crcon");
0821:                post.put("filename", filename);
0822:                post.put("filesize", Long.toString(filesize));
0823:                post.put("can-send-protocol", "http");
0824:
0825:                // send request
0826:                try {
0827:                    final yacyURL url = new yacyURL("http://" + targetAddress
0828:                            + "/yacy/transfer.html", null);
0829:                    final HashMap<String, String> result = nxTools.table(httpc
0830:                            .wput(url, url.getHost(), 6000, null, null,
0831:                                    proxyConfig(), post, null), "UTF-8");
0832:                    return result;
0833:                } catch (Exception e) {
0834:                    // most probably a network time-out exception
0835:                    yacyCore.log
0836:                            .logSevere("yacyClient.permissionTransfer error:"
0837:                                    + e.getMessage());
0838:                    return null;
0839:                }
0840:            }
0841:
0842:            public static HashMap<String, String> transferStore(
0843:                    String targetAddress, String access, String filename,
0844:                    byte[] file) {
0845:
0846:                // prepare request
0847:                final serverObjects post = yacyNetwork.basicRequestPost(
0848:                        plasmaSwitchboard.getSwitchboard(), null);
0849:                post.put("process", "store");
0850:                post.put("purpose", "crcon");
0851:                post.put("filename", filename);
0852:                post.put("filesize", Long.toString(file.length));
0853:                post.put("md5", serverCodings.encodeMD5Hex(file));
0854:                post.put("access", access);
0855:                HashMap<String, byte[]> files = new HashMap<String, byte[]>();
0856:                files.put("filename", file);
0857:
0858:                // send request
0859:                try {
0860:                    final yacyURL url = new yacyURL("http://" + targetAddress
0861:                            + "/yacy/transfer.html", null);
0862:                    final HashMap<String, String> result = nxTools.table(httpc
0863:                            .wput(url, url.getHost(), 20000, null, null,
0864:                                    proxyConfig(), post, files), "UTF-8");
0865:                    return result;
0866:                } catch (Exception e) {
0867:                    yacyCore.log.logSevere("yacyClient.postMessage error:"
0868:                            + e.getMessage());
0869:                    return null;
0870:                }
0871:            }
0872:
0873:            public static String transfer(String targetAddress,
0874:                    String filename, byte[] file) {
0875:                HashMap<String, String> phase1 = transferPermission(
0876:                        targetAddress, file.length, filename);
0877:                if (phase1 == null)
0878:                    return "no connection to remote address " + targetAddress
0879:                            + "; phase 1";
0880:                String access = (String) phase1.get("access");
0881:                String nextaddress = (String) phase1.get("address");
0882:                String protocol = (String) phase1.get("protocol");
0883:                //String path = (String) phase1.get("path");
0884:                //String maxsize = (String) phase1.get("maxsize");
0885:                String response = (String) phase1.get("response");
0886:                if ((response == null) || (protocol == null)
0887:                        || (access == null))
0888:                    return "wrong return values from other peer; phase 1";
0889:                if (!(response.equals("ok")))
0890:                    return "remote peer rejected transfer: " + response;
0891:                String accesscode = serverCodings
0892:                        .encodeMD5Hex(kelondroBase64Order.standardCoder
0893:                                .encodeString(access));
0894:                if (protocol.equals("http")) {
0895:                    HashMap<String, String> phase2 = transferStore(nextaddress,
0896:                            accesscode, filename, file);
0897:                    if (phase2 == null)
0898:                        return "no connection to remote address "
0899:                                + targetAddress + "; phase 2";
0900:                    response = (String) phase2.get("response");
0901:                    if (response == null)
0902:                        return "wrong return values from other peer; phase 2";
0903:                    if (!(response.equals("ok"))) {
0904:                        return "remote peer failed with transfer: " + response;
0905:                    }
0906:                    return null;
0907:                }
0908:                return "wrong protocol: " + protocol;
0909:            }
0910:
0911:            public static HashMap<String, String> crawlReceipt(yacySeed target,
0912:                    String process, String result, String reason,
0913:                    indexURLEntry entry, String wordhashes) {
0914:                assert (target != null);
0915:                assert (yacyCore.seedDB.mySeed() != null);
0916:                assert (yacyCore.seedDB.mySeed() != target);
0917:
0918:                /*
0919:                 the result can have one of the following values:
0920:                 negative cases, no retry
0921:                   unavailable - the resource is not avaiable (a broken link); not found or interrupted
0922:                   robot       - a robot-file has denied to crawl that resource
0923:                 
0924:                 negative cases, retry possible
0925:                   rejected    - the peer has rejected to load the resource
0926:                   dequeue     - peer too busy - rejected to crawl
0927:                 
0928:                 positive cases with crawling
0929:                   fill        - the resource was loaded and processed
0930:                   update      - the resource was already in database but re-loaded and processed
0931:                 
0932:                 positive cases without crawling
0933:                   known       - the resource is already in database, believed to be fresh and not reloaded
0934:                   stale       - the resource was reloaded but not processed because source had no changes
0935:                 
0936:                 */
0937:
0938:                // prepare request
0939:                final serverObjects post = yacyNetwork.basicRequestPost(
0940:                        plasmaSwitchboard.getSwitchboard(), target.hash);
0941:                post.put("process", process);
0942:                post.put("urlhash", ((entry == null) ? "" : entry.hash()));
0943:                post.put("result", result);
0944:                post.put("reason", reason);
0945:                post.put("wordh", wordhashes);
0946:                post.put("lurlEntry", ((entry == null) ? "" : crypt
0947:                        .simpleEncode(entry.toString(), post.get("key", ""))));
0948:
0949:                // determining target address
0950:                final String address = target.getClusterAddress();
0951:                if (address == null) {
0952:                    return null;
0953:                }
0954:
0955:                // send request
0956:                try {
0957:                    return nxTools.table(httpc.wput(new yacyURL("http://"
0958:                            + address + "/yacy/crawlReceipt.html", null),
0959:                            target.getHexHash() + ".yacyh", 60000, null, null,
0960:                            proxyConfig(), post, null), "UTF-8");
0961:                } catch (Exception e) {
0962:                    // most probably a network time-out exception
0963:                    yacyCore.log.logSevere("yacyClient.crawlReceipt error:"
0964:                            + e.getMessage());
0965:                    return null;
0966:                }
0967:            }
0968:
0969:            public static HashMap<String, Object> transferIndex(
0970:                    yacySeed targetSeed, indexContainer[] indexes,
0971:                    HashMap<String, indexURLEntry> urlCache, boolean gzipBody,
0972:                    int timeout) {
0973:
0974:                HashMap<String, Object> resultObj = new HashMap<String, Object>();
0975:                int payloadSize = 0;
0976:                try {
0977:
0978:                    // check if we got all necessary urls in the urlCache (only for debugging)
0979:                    Iterator<indexRWIRowEntry> eenum;
0980:                    indexRWIEntry entry;
0981:                    for (int i = 0; i < indexes.length; i++) {
0982:                        eenum = indexes[i].entries();
0983:                        while (eenum.hasNext()) {
0984:                            entry = (indexRWIEntry) eenum.next();
0985:                            if (urlCache.get(entry.urlHash()) == null) {
0986:                                yacyCore.log
0987:                                        .logFine("DEBUG transferIndex: to-send url hash '"
0988:                                                + entry.urlHash()
0989:                                                + "' is not contained in urlCache");
0990:                            }
0991:                        }
0992:                    }
0993:
0994:                    // transfer the RWI without the URLs
0995:                    HashMap<String, String> in = transferRWI(targetSeed,
0996:                            indexes, gzipBody, timeout);
0997:                    resultObj.put("resultTransferRWI", in);
0998:
0999:                    if (in == null) {
1000:                        resultObj.put("result", "no_connection_1");
1001:                        return resultObj;
1002:                    }
1003:                    if (in.containsKey("indexPayloadSize"))
1004:                        payloadSize += Integer.parseInt(in
1005:                                .get("indexPayloadSize"));
1006:
1007:                    String result = (String) in.get("result");
1008:                    if (result == null) {
1009:                        resultObj.put("result", "no_result_1");
1010:                        return resultObj;
1011:                    }
1012:                    if (!(result.equals("ok"))) {
1013:                        targetSeed.setFlagAcceptRemoteIndex(false);
1014:                        yacyCore.seedDB.update(targetSeed.hash, targetSeed);
1015:                        resultObj.put("result", result);
1016:                        return resultObj;
1017:                    }
1018:
1019:                    // in now contains a list of unknown hashes
1020:                    final String uhss = (String) in.get("unknownURL");
1021:                    if (uhss == null) {
1022:                        resultObj
1023:                                .put("result", "no_unknownURL_tag_in_response");
1024:                        return resultObj;
1025:                    }
1026:                    if (uhss.length() == 0) {
1027:                        return resultObj;
1028:                    } // all url's known, we are ready here
1029:
1030:                    final String[] uhs = uhss.split(",");
1031:                    if (uhs.length == 0) {
1032:                        return resultObj;
1033:                    } // all url's known
1034:
1035:                    // extract the urlCache from the result
1036:                    indexURLEntry[] urls = new indexURLEntry[uhs.length];
1037:                    for (int i = 0; i < uhs.length; i++) {
1038:                        urls[i] = (indexURLEntry) urlCache.get(uhs[i]);
1039:                        if (urls[i] == null) {
1040:                            yacyCore.log
1041:                                    .logFine("DEBUG transferIndex: requested url hash '"
1042:                                            + uhs[i]
1043:                                            + "', unknownURL='"
1044:                                            + uhss
1045:                                            + "'");
1046:                        }
1047:                    }
1048:
1049:                    in = transferURL(targetSeed, urls, gzipBody, timeout);
1050:                    resultObj.put("resultTransferURL", in);
1051:
1052:                    if (in == null) {
1053:                        resultObj.put("result", "no_connection_2");
1054:                        return resultObj;
1055:                    }
1056:                    if (in.containsKey("urlPayloadSize"))
1057:                        payloadSize += Integer.parseInt(in
1058:                                .get("urlPayloadSize"));
1059:
1060:                    result = (String) in.get("result");
1061:                    if (result == null) {
1062:                        resultObj.put("result", "no_result_2");
1063:                        return resultObj;
1064:                    }
1065:                    if (!(result.equals("ok"))) {
1066:                        targetSeed.setFlagAcceptRemoteIndex(false);
1067:                        yacyCore.seedDB.update(targetSeed.hash, targetSeed);
1068:                        resultObj.put("result", result);
1069:                        return resultObj;
1070:                    }
1071:                    //      int doubleentries = Integer.parseInt((String) in.get("double"));
1072:                    //      System.out.println("DEBUG tansferIndex: transferred " + uhs.length + " URL's, double=" + doubleentries);
1073:
1074:                    return resultObj;
1075:                } finally {
1076:                    resultObj.put("payloadSize", new Integer(payloadSize));
1077:                }
1078:            }
1079:
1080:            private static HashMap<String, String> transferRWI(
1081:                    yacySeed targetSeed, indexContainer[] indexes,
1082:                    boolean gzipBody, int timeout) {
1083:                final String address = targetSeed.getPublicAddress();
1084:                if (address == null) {
1085:                    return null;
1086:                }
1087:
1088:                // prepare post values
1089:                final serverObjects post = yacyNetwork.basicRequestPost(
1090:                        plasmaSwitchboard.getSwitchboard(), targetSeed.hash);
1091:
1092:                // enabling gzip compression for post request body
1093:                if ((gzipBody)
1094:                        && (targetSeed.getVersion() >= yacyVersion.YACY_SUPPORTS_GZIP_POST_REQUESTS)) {
1095:                    post.put(httpc.GZIP_POST_BODY, "true");
1096:                }
1097:                post.put("wordc", Integer.toString(indexes.length));
1098:
1099:                int indexcount = 0;
1100:                final StringBuffer entrypost = new StringBuffer(
1101:                        indexes.length * 73);
1102:                Iterator<indexRWIRowEntry> eenum;
1103:                indexRWIEntry entry;
1104:                for (int i = 0; i < indexes.length; i++) {
1105:                    eenum = indexes[i].entries();
1106:                    while (eenum.hasNext()) {
1107:                        entry = (indexRWIEntry) eenum.next();
1108:                        entrypost.append(indexes[i].getWordHash()).append(
1109:                                entry.toPropertyForm()).append(
1110:                                serverCore.CRLF_STRING);
1111:                        indexcount++;
1112:                    }
1113:                }
1114:
1115:                if (indexcount == 0) {
1116:                    // nothing to do but everything ok
1117:                    final HashMap<String, String> result = new HashMap<String, String>(
1118:                            2);
1119:                    result.put("result", "ok");
1120:                    result.put("unknownURL", "");
1121:                    return result;
1122:                }
1123:
1124:                post.put("entryc", indexcount);
1125:                post.put("indexes", entrypost.toString());
1126:                try {
1127:                    final ArrayList<String> v = nxTools.strings(httpc.wput(
1128:                            new yacyURL("http://" + address
1129:                                    + "/yacy/transferRWI.html", null),
1130:                            targetSeed.getHexHash() + ".yacyh", timeout, null,
1131:                            null, proxyConfig(), post, null), "UTF-8");
1132:                    // this should return a list of urlhashes that are unknwon
1133:                    if ((v != null) && (v.size() > 0)) {
1134:                        yacyCore.seedDB.mySeed().incSI(indexcount);
1135:                    }
1136:
1137:                    final HashMap<String, String> result = nxTools.table(v);
1138:                    // return the transfered index data in bytes (for debugging only)
1139:                    result.put("indexPayloadSize", Integer.toString(entrypost
1140:                            .length()));
1141:                    return result;
1142:                } catch (Exception e) {
1143:                    yacyCore.log.logSevere("yacyClient.transferRWI error:"
1144:                            + e.getMessage());
1145:                    return null;
1146:                }
1147:            }
1148:
1149:            private static HashMap<String, String> transferURL(
1150:                    yacySeed targetSeed, indexURLEntry[] urls,
1151:                    boolean gzipBody, int timeout) {
1152:                // this post a message to the remote message board
1153:                final String address = targetSeed.getPublicAddress();
1154:                if (address == null) {
1155:                    return null;
1156:                }
1157:
1158:                // prepare post values
1159:                final serverObjects post = yacyNetwork.basicRequestPost(
1160:                        plasmaSwitchboard.getSwitchboard(), targetSeed.hash);
1161:
1162:                // enabling gzip compression for post request body
1163:                if ((gzipBody)
1164:                        && (targetSeed.getVersion() >= yacyVersion.YACY_SUPPORTS_GZIP_POST_REQUESTS)) {
1165:                    post.put(httpc.GZIP_POST_BODY, "true");
1166:                }
1167:
1168:                String resource = "";
1169:                int urlc = 0;
1170:                int urlPayloadSize = 0;
1171:                for (int i = 0; i < urls.length; i++) {
1172:                    if (urls[i] != null) {
1173:                        resource = urls[i].toString();
1174:                        if (resource != null) {
1175:                            post.put("url" + urlc, resource);
1176:                            urlPayloadSize += resource.length();
1177:                            urlc++;
1178:                        }
1179:                    }
1180:                }
1181:                post.put("urlc", urlc);
1182:                try {
1183:                    final ArrayList<String> v = nxTools.strings(httpc.wput(
1184:                            new yacyURL("http://" + address
1185:                                    + "/yacy/transferURL.html", null),
1186:                            targetSeed.getHexHash() + ".yacyh", timeout, null,
1187:                            null, proxyConfig(), post, null), "UTF-8");
1188:
1189:                    if ((v != null) && (v.size() > 0)) {
1190:                        yacyCore.seedDB.mySeed().incSU(urlc);
1191:                    }
1192:
1193:                    HashMap<String, String> result = nxTools.table(v);
1194:                    // return the transfered url data in bytes (for debugging only)
1195:                    result.put("urlPayloadSize", Integer
1196:                            .toString(urlPayloadSize));
1197:                    return result;
1198:                } catch (Exception e) {
1199:                    yacyCore.log.logSevere("yacyClient.transferURL error:"
1200:                            + e.getMessage());
1201:                    return null;
1202:                }
1203:            }
1204:
1205:            public static HashMap<String, String> getProfile(yacySeed targetSeed) {
1206:
1207:                // this post a message to the remote message board
1208:                final serverObjects post = yacyNetwork.basicRequestPost(
1209:                        plasmaSwitchboard.getSwitchboard(), targetSeed.hash);
1210:
1211:                String address = targetSeed.getClusterAddress();
1212:                if (address == null) {
1213:                    address = "localhost:8080";
1214:                }
1215:                try {
1216:                    return nxTools.table(httpc.wput(new yacyURL("http://"
1217:                            + address + "/yacy/profile.html", null), targetSeed
1218:                            .getHexHash()
1219:                            + ".yacyh", 10000, null, null, proxyConfig(), post,
1220:                            null), "UTF-8");
1221:                } catch (Exception e) {
1222:                    yacyCore.log.logSevere("yacyClient.getProfile error:"
1223:                            + e.getMessage());
1224:                    return null;
1225:                }
1226:            }
1227:
1228:            private static final httpRemoteProxyConfig proxyConfig() {
1229:                httpRemoteProxyConfig p = plasmaSwitchboard.getSwitchboard().remoteProxyConfig;
1230:                return ((p != null) && (p.useProxy()) && (p.useProxy4Yacy())) ? p
1231:                        : null;
1232:            }
1233:
1234:            public static void main(String[] args) {
1235:                System.out.println("yacyClient Test");
1236:                try {
1237:                    final plasmaSwitchboard sb = new plasmaSwitchboard(
1238:                            new File(args[0]), "httpProxy.init",
1239:                            "DATA/SETTINGS/httpProxy.conf", false);
1240:                    /*final yacyCore core =*/new yacyCore(sb);
1241:                    yacyCore.peerActions.loadSeedLists();
1242:                    final yacySeed target = yacyCore.seedDB
1243:                            .getConnected(args[1]);
1244:                    final String wordhashe = plasmaCondenser.word2hash("test");
1245:                    //System.out.println("permission=" + permissionMessage(args[1]));
1246:
1247:                    // should we use the proxy?
1248:                    boolean useProxy = (sb.remoteProxyConfig != null)
1249:                            && (sb.remoteProxyConfig.useProxy())
1250:                            && (sb.remoteProxyConfig.useProxy4Yacy());
1251:
1252:                    final HashMap<String, String> result = nxTools
1253:                            .table(
1254:                                    httpc
1255:                                            .wget(
1256:                                                    new yacyURL(
1257:                                                            "http://"
1258:                                                                    + target
1259:                                                                            .getPublicAddress()
1260:                                                                    + "/yacy/search.html"
1261:                                                                    + "?myseed="
1262:                                                                    + yacyCore.seedDB
1263:                                                                            .mySeed()
1264:                                                                            .genSeedStr(
1265:                                                                                    null)
1266:                                                                    + "&youare="
1267:                                                                    + target.hash
1268:                                                                    + "&key="
1269:                                                                    + "&myseed="
1270:                                                                    + yacyCore.seedDB
1271:                                                                            .mySeed()
1272:                                                                            .genSeedStr(
1273:                                                                                    null)
1274:                                                                    + "&count=10"
1275:                                                                    + "&resource=global"
1276:                                                                    + "&query="
1277:                                                                    + wordhashe
1278:                                                                    + "&network.unit.name="
1279:                                                                    + plasmaSwitchboard
1280:                                                                            .getSwitchboard()
1281:                                                                            .getConfig(
1282:                                                                                    "network.unit.name",
1283:                                                                                    yacySeed.DFLT_NETWORK_UNIT),
1284:                                                            null),
1285:                                                    target.getHexHash()
1286:                                                            + ".yacyh",
1287:                                                    5000,
1288:                                                    null,
1289:                                                    null,
1290:                                                    (useProxy) ? sb.remoteProxyConfig
1291:                                                            : null, null, null),
1292:                                    "UTF-8");
1293:                    System.out.println("Result=" + result.toString());
1294:                } catch (Exception e) {
1295:                    e.printStackTrace();
1296:                }
1297:                System.exit(0);
1298:            }
1299:
1300:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.