001: // plasmaRankingDistribution.java
002: // -------------------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2005
006: // created 9.11.2005
007: //
008: // $LastChangedDate: 2005-11-04 14:41:51 +0100 (Fri, 04 Nov 2005) $
009: // $LastChangedRevision: 1026 $
010: // $LastChangedBy: borg-0300 $
011: //
012: // This program is free software; you can redistribute it and/or modify
013: // it under the terms of the GNU General Public License as published by
014: // the Free Software Foundation; either version 2 of the License, or
015: // (at your option) any later version.
016: //
017: // This program is distributed in the hope that it will be useful,
018: // but WITHOUT ANY WARRANTY; without even the implied warranty of
019: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: // GNU General Public License for more details.
021: //
022: // You should have received a copy of the GNU General Public License
023: // along with this program; if not, write to the Free Software
024: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: // Using this software in any meaning (reading, learning, copying, compiling,
027: // running) means that you agree that the Author(s) is (are) not responsible
028: // for cost, loss of data or any harm that may be caused directly or indirectly
029: // by usage of this softare or this documentation. The usage of this software
030: // is on your own risk. The installation and usage (starting/running) of this
031: // software may allow other people or application to access your computer and
032: // any attached devices and is highly dependent on the configuration of the
033: // software which must be done by the user of the software; the author(s) is
034: // (are) also not responsible for proper configuration and usage of the
035: // software, even if provoked by documentation provided together with
036: // the software.
037: //
038: // Any changes to this file according to the GPL as documented in the file
039: // gpl.txt aside this file in the shipment you received can be done to the
040: // lines that follows this copyright notice here, but changes must not be
041: // done inside the copyright notive above. A re-distribution must contain
042: // the intact and unchanged copyright notice.
043: // Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.plasma;
046:
047: import java.io.File;
048: import java.io.IOException;
049: import java.util.Random;
050: import java.util.StringTokenizer;
051:
052: import de.anomic.server.serverFileUtils;
053: import de.anomic.server.logging.serverLog;
054: import de.anomic.yacy.yacyClient;
055: import de.anomic.yacy.yacyCore;
056: import de.anomic.yacy.yacySeed;
057: import de.anomic.yacy.yacyVersion;
058:
059: public final class plasmaRankingDistribution {
060:
061: public static final String CR_OWN = "GLOBAL/010_owncr";
062: public static final String CR_OTHER = "GLOBAL/014_othercr/";
063:
064: public static final int METHOD_NONE = 0;
065: public static final int METHOD_ANYSENIOR = 1;
066: public static final int METHOD_ANYPRINCIPAL = 2;
067: public static final int METHOD_MIXEDSENIOR = 9;
068: public static final int METHOD_MIXEDPRINCIPAL = 10;
069: public static final int METHOD_FIXEDADDRESS = 99;
070:
071: private final serverLog log;
072: private File sourcePath; // where to load CR-files
073: private int method; // of peer selection
074: private int percentage; // to select any other peer
075: private String address[]; // of fixed other peer
076: private static Random random = new Random(System
077: .currentTimeMillis());
078:
079: public plasmaRankingDistribution(serverLog log, File sourcePath,
080: int method, int percentage, String addresses) {
081: this .log = log;
082: this .sourcePath = sourcePath;
083: this .method = method;
084: this .percentage = percentage;
085: StringTokenizer st = new StringTokenizer(addresses, ",");
086: int c = 0;
087: while (st.hasMoreTokens()) {
088: st.nextToken();
089: c++;
090: }
091: st = new StringTokenizer(addresses, ",");
092: this .address = new String[c];
093: c = 0;
094: while (st.hasMoreTokens()) {
095: this .address[c++] = st.nextToken();
096: }
097: }
098:
099: public void setMethod(int method, int percentage, String address[]) {
100: this .method = method;
101: this .percentage = percentage;
102: this .address = address;
103: }
104:
105: public int size() {
106: if ((sourcePath.exists()) && (sourcePath.isDirectory()))
107: return sourcePath.list().length;
108: else
109: return 0;
110: }
111:
112: public boolean transferRanking(int count)
113: throws InterruptedException {
114:
115: if (method == METHOD_NONE) {
116: log
117: .logFine("no ranking distribution: no transfer method given");
118: return false;
119: }
120: if (yacyCore.seedDB == null) {
121: log.logFine("no ranking distribution: seedDB == null");
122: return false;
123: }
124: if (yacyCore.seedDB.mySeed() == null) {
125: log.logFine("no ranking distribution: mySeed == null");
126: return false;
127: }
128: if (yacyCore.seedDB.mySeed().isVirgin()) {
129: log.logFine("no ranking distribution: status is virgin");
130: return false;
131: }
132:
133: String[] outfiles = sourcePath.list();
134:
135: if (outfiles == null) {
136: log
137: .logFine("no ranking distribution: source path does not exist");
138: return false;
139: }
140: if (outfiles.length == 0) {
141: log
142: .logFine("no ranking distribution: source path does not contain any file");
143: return false;
144: }
145:
146: if (outfiles.length < count)
147: count = outfiles.length;
148: File crfile = null;
149:
150: for (int i = 0; i < count; i++) {
151: // check for interruption
152: if (Thread.currentThread().isInterrupted())
153: throw new InterruptedException("Shutdown in progress");
154:
155: // getting the next file to transfer
156: crfile = new File(sourcePath, outfiles[i]);
157:
158: if ((method == METHOD_ANYSENIOR)
159: || (method == METHOD_ANYPRINCIPAL)) {
160: transferRankingAnySeed(crfile, 5);
161: }
162: if (method == METHOD_FIXEDADDRESS) {
163: transferRankingAddress(crfile);
164: }
165: if ((method == METHOD_MIXEDSENIOR)
166: || (method == METHOD_MIXEDPRINCIPAL)) {
167: if (random.nextInt(100) > percentage) {
168: if (!(transferRankingAddress(crfile)))
169: transferRankingAnySeed(crfile, 5);
170: } else {
171: if (!(transferRankingAnySeed(crfile, 5)))
172: transferRankingAddress(crfile);
173: }
174: }
175:
176: }
177: log.logFine("no ranking distribution: no target available");
178: return false;
179: }
180:
181: private boolean transferRankingAnySeed(File crfile, int trycount)
182: throws InterruptedException {
183: yacySeed target = null;
184: for (int j = 0; j < trycount; j++) {
185: // check for interruption
186: if (Thread.currentThread().isInterrupted())
187: throw new InterruptedException("Shutdown in progress");
188:
189: // get next target
190: target = yacyCore.seedDB
191: .anySeedVersion(yacyVersion.YACY_ACCEPTS_RANKING_TRANSMISSION);
192:
193: if (target == null)
194: continue;
195: String targetaddress = target.getPublicAddress();
196: if (transferRankingAddress(crfile, targetaddress))
197: return true;
198: }
199: return false;
200: }
201:
202: private boolean transferRankingAddress(File crfile)
203: throws InterruptedException {
204: // try all addresses
205: for (int i = 0; i < this .address.length; i++) {
206: // check for interruption
207: if (Thread.currentThread().isInterrupted())
208: throw new InterruptedException("Shutdown in progress");
209:
210: // try to transfer ranking address using the next address
211: if (transferRankingAddress(crfile, this .address[i]))
212: return true;
213: }
214: return false;
215: }
216:
217: private boolean transferRankingAddress(File crfile, String address) {
218: // do the transfer
219: long starttime = System.currentTimeMillis();
220: String result = "unknown";
221: try {
222: byte[] b = serverFileUtils.read(crfile);
223: result = yacyClient.transfer(address, crfile.getName(), b);
224: if (result == null) {
225: log
226: .logInfo("RankingDistribution - transmitted file "
227: + crfile
228: + " to "
229: + address
230: + " successfully in "
231: + ((System.currentTimeMillis() - starttime) / 1000)
232: + " seconds");
233: crfile.delete(); // the file is not needed any more locally
234: } else {
235: log
236: .logInfo("RankingDistribution - error transmitting file "
237: + crfile
238: + " to "
239: + address
240: + ": "
241: + result);
242: }
243: } catch (IOException e) {
244: log.logInfo("RankingDistribution - could not read file "
245: + crfile + ": " + e.getMessage());
246: result = "input file error: " + e.getMessage();
247: }
248:
249: // show success
250: return result == null;
251: }
252:
253: }
|