2005-10-09 15:18:48 +02:00
// yacyCore.java
2005-04-07 21:19:42 +02:00
// -------------------------------------
2008-07-20 19:14:51 +02:00
// (C) by Michael Peter Christen; mc@yacy.net
2005-04-07 21:19:42 +02:00
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
2005-10-09 15:18:48 +02:00
//
// $LastChangedDate$
// $LastChangedRevision$
2007-04-14 12:11:37 +02:00
// $LastChangedBy$
2005-04-07 21:19:42 +02:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/ *
the yacy process of getting in touch of other peers starts as follows :
- init seed cache . It is needed to determine the right peer for the Hello - Process
- create a own seed . This can be a new one or one loaded from a file
- The httpd must start up then first
- the own seed is completed by performing the ' yacyHello ' process . This
process will result in a request back to the own peer to check if it runs
in server mode . This is the reason that the httpd must be started in advance .
* /
// contributions:
// principal peer status via file generation by Alexander Schier [AS]
2011-09-25 18:59:06 +02:00
package net.yacy.peers ;
2005-04-07 21:19:42 +02:00
2005-05-05 07:32:19 +02:00
import java.net.MalformedURLException ;
2005-06-23 13:00:26 +02:00
import java.util.Collections ;
2007-10-31 16:47:48 +01:00
import java.util.Date ;
2005-10-05 12:45:33 +02:00
import java.util.HashMap ;
2006-01-23 00:14:37 +01:00
import java.util.Iterator ;
2005-06-23 13:00:26 +02:00
import java.util.LinkedList ;
import java.util.List ;
2006-01-23 00:14:37 +01:00
import java.util.Map ;
2010-06-01 15:02:11 +02:00
import java.util.concurrent.ConcurrentHashMap ;
2009-10-20 00:34:44 +02:00
import java.util.concurrent.Semaphore ;
2006-09-30 00:27:20 +02:00
2011-01-03 21:52:54 +01:00
import net.yacy.cora.date.GenericFormatter ;
2011-05-27 10:24:54 +02:00
import net.yacy.cora.document.ASCII ;
2010-05-25 14:54:57 +02:00
import net.yacy.cora.document.RSSFeed ;
import net.yacy.cora.document.RSSMessage ;
2009-10-11 02:12:19 +02:00
import net.yacy.kelondro.data.meta.DigestURI ;
2009-10-10 01:13:30 +02:00
import net.yacy.kelondro.logging.Log ;
2011-09-25 18:59:06 +02:00
import net.yacy.peers.dht.PeerSelection ;
import net.yacy.peers.operation.yacySeedUploadFile ;
import net.yacy.peers.operation.yacySeedUploadFtp ;
import net.yacy.peers.operation.yacySeedUploadScp ;
import net.yacy.peers.operation.yacySeedUploader ;
import net.yacy.search.Switchboard ;
2006-02-17 11:53:20 +01:00
import de.anomic.server.serverCore ;
2005-04-07 21:19:42 +02:00
2011-10-04 11:06:24 +02:00
public class Network {
2005-10-09 15:18:48 +02:00
2005-04-07 21:19:42 +02:00
// statics
2007-09-13 19:05:53 +02:00
public static final ThreadGroup publishThreadGroup = new ThreadGroup ( " publishThreadGroup " ) ;
2008-02-10 17:13:13 +01:00
public static final HashMap < String , String > seedUploadMethods = new HashMap < String , String > ( ) ;
2009-01-31 00:33:47 +01:00
public static final Log log = new Log ( " YACY " ) ;
2008-03-12 01:05:18 +01:00
public static long lastOnlineTime = 0 ;
/** pseudo-random key derived from a time-interval while YaCy startup*/
2011-04-29 04:00:37 +02:00
public static long speedKey = 0 ;
public static long magic = System . currentTimeMillis ( ) ;
2011-10-04 11:06:24 +02:00
public static final Map < String , Accessible > amIAccessibleDB = new ConcurrentHashMap < String , Accessible > ( ) ; // Holds PeerHash / yacyAccessible Relations
2009-09-15 11:47:02 +02:00
// constants for PeerPing behavior
2008-03-12 01:05:18 +01:00
private static final int PING_INITIAL = 10 ;
private static final int PING_MAX_RUNNING = 3 ;
private static final int PING_MIN_RUNNING = 1 ;
private static final int PING_MIN_DBSIZE = 5 ;
private static final int PING_MIN_PEERSEEN = 1 ; // min. accessible to force senior
2007-09-13 19:05:53 +02:00
private static final long PING_MAX_DBAGE = 15 * 60 * 1000 ; // in milliseconds
2011-07-14 09:07:06 +02:00
2005-10-09 15:18:48 +02:00
// public static yacyShare shareManager = null;
// public static boolean terminate = false;
2005-04-07 21:19:42 +02:00
// class variables
2009-07-19 22:37:44 +02:00
Switchboard sb ;
2005-10-09 15:18:48 +02:00
2005-04-07 21:19:42 +02:00
public static int yacyTime ( ) {
// the time since startup of yacy in seconds
2008-07-11 09:05:08 +02:00
return Math . max ( 0 , ( int ) ( ( System . currentTimeMillis ( ) - serverCore . startupTime ) / 1000 ) ) ;
2005-04-07 21:19:42 +02:00
}
2005-10-09 15:18:48 +02:00
2011-10-04 11:06:24 +02:00
public Network ( final Switchboard sb ) {
2008-08-02 14:12:04 +02:00
final long time = System . currentTimeMillis ( ) ;
2005-10-09 15:18:48 +02:00
2008-05-06 01:13:47 +02:00
this . sb = sb ;
sb . setConfig ( " yacyStatus " , " " ) ;
2011-07-14 09:07:06 +02:00
2008-04-27 00:53:04 +02:00
// create a peer news channel
2011-10-04 11:06:24 +02:00
final RSSFeed peernews = EventChannel . channels ( EventChannel . PEERNEWS ) ;
2008-05-07 01:05:48 +02:00
peernews . addMessage ( new RSSMessage ( " YaCy started " , " " , " " ) ) ;
2011-07-14 09:07:06 +02:00
2008-06-29 20:35:15 +02:00
// ensure that correct IP is used
final String staticIP = sb . getConfig ( " staticIP " , " " ) ;
2011-10-04 11:06:24 +02:00
if ( staticIP . length ( ) ! = 0 & & Seed . isProperIP ( staticIP ) = = null ) {
2008-06-29 20:35:15 +02:00
serverCore . useStaticIP = true ;
2009-05-28 16:26:05 +02:00
sb . peers . mySeed ( ) . setIP ( staticIP ) ;
2008-08-02 15:57:00 +02:00
log . logInfo ( " staticIP set to " + staticIP ) ;
2008-06-29 20:35:15 +02:00
} else {
serverCore . useStaticIP = false ;
}
2005-10-09 15:18:48 +02:00
2005-05-17 10:25:04 +02:00
loadSeedUploadMethods ( ) ;
2005-10-09 15:18:48 +02:00
2005-08-30 23:10:39 +02:00
log . logConfig ( " CORE INITIALIZED " ) ;
2005-05-17 10:25:04 +02:00
// ATTENTION, VERY IMPORTANT: before starting the thread, the httpd yacy server must be running!
2005-10-09 15:18:48 +02:00
2005-05-17 10:25:04 +02:00
speedKey = System . currentTimeMillis ( ) - time ;
lastOnlineTime = 0 ;
2005-04-07 21:19:42 +02:00
}
synchronized static public void triggerOnlineAction ( ) {
2005-09-27 18:28:55 +02:00
lastOnlineTime = System . currentTimeMillis ( ) ;
2005-04-07 21:19:42 +02:00
}
2011-07-14 09:07:06 +02:00
2007-09-13 19:05:53 +02:00
public final void publishSeedList ( ) {
2008-09-03 02:30:21 +02:00
if ( log . isFine ( ) ) log . logFine ( " yacyCore.publishSeedList: Triggered Seed Publish " ) ;
2005-10-09 15:18:48 +02:00
2005-04-07 21:19:42 +02:00
/ *
2005-10-17 17:46:12 +02:00
if ( oldIPStamp . equals ( ( String ) seedDB . mySeed . get ( yacySeed . IP , " 127.0.0.1 " ) ) )
2005-07-25 16:46:36 +02:00
yacyCore . log . logDebug ( " ***DEBUG publishSeedList: oldIP is equal " ) ;
2005-04-07 21:19:42 +02:00
if ( seedCacheSizeStamp = = seedDB . sizeConnected ( ) )
2005-07-25 16:46:36 +02:00
yacyCore . log . logDebug ( " ***DEBUG publishSeedList: sizeConnected is equal " ) ;
2005-04-07 21:19:42 +02:00
if ( canReachMyself ( ) )
2005-07-25 16:46:36 +02:00
yacyCore . log . logDebug ( " ***DEBUG publishSeedList: I can reach myself " ) ;
2005-04-07 21:19:42 +02:00
* /
2005-10-09 15:18:48 +02:00
2011-07-14 09:07:06 +02:00
if ( ( this . sb . peers . lastSeedUpload_myIP . equals ( this . sb . peers . mySeed ( ) . getIP ( ) ) ) & &
( this . sb . peers . lastSeedUpload_seedDBSize = = this . sb . peers . sizeConnected ( ) ) & &
2007-09-13 19:05:53 +02:00
( canReachMyself ( ) ) & &
2011-07-14 09:07:06 +02:00
( System . currentTimeMillis ( ) - this . sb . peers . lastSeedUpload_timeStamp < 1000 * 60 * 60 * 24 ) & &
( this . sb . peers . mySeed ( ) . isPrincipal ( ) )
2005-06-28 13:27:31 +02:00
) {
2008-09-03 02:30:21 +02:00
if ( log . isFine ( ) ) log . logFine ( " yacyCore.publishSeedList: not necessary to publish: oldIP is equal, sizeConnected is equal and I can reach myself under the old IP. " ) ;
2005-05-17 10:25:04 +02:00
return ;
2005-06-28 13:27:31 +02:00
}
2005-10-09 15:18:48 +02:00
2005-05-17 10:25:04 +02:00
// getting the seed upload method that should be used ...
2008-05-06 01:13:47 +02:00
final String seedUploadMethod = this . sb . getConfig ( " seedUploadMethod " , " " ) ;
2005-10-09 15:18:48 +02:00
2005-06-17 10:38:22 +02:00
if (
2005-10-09 15:18:48 +02:00
( ! seedUploadMethod . equalsIgnoreCase ( " none " ) ) | |
2008-05-06 01:13:47 +02:00
( ( seedUploadMethod . equals ( " " ) ) & & ( this . sb . getConfig ( " seedFTPPassword " , " " ) . length ( ) > 0 ) ) | |
( ( seedUploadMethod . equals ( " " ) ) & & ( this . sb . getConfig ( " seedFilePath " , " " ) . length ( ) > 0 ) )
2005-06-17 10:38:22 +02:00
) {
if ( seedUploadMethod . equals ( " " ) ) {
2008-05-06 01:13:47 +02:00
if ( this . sb . getConfig ( " seedFTPPassword " , " " ) . length ( ) > 0 ) {
this . sb . setConfig ( " seedUploadMethod " , " Ftp " ) ;
2007-09-13 19:05:53 +02:00
}
2008-05-06 01:13:47 +02:00
if ( this . sb . getConfig ( " seedFilePath " , " " ) . length ( ) > 0 ) {
this . sb . setConfig ( " seedUploadMethod " , " File " ) ;
2007-09-13 19:05:53 +02:00
}
2005-06-17 10:38:22 +02:00
}
2005-05-17 10:25:04 +02:00
// we want to be a principal...
2011-07-14 09:07:06 +02:00
saveSeedList ( this . sb ) ;
2005-04-07 21:19:42 +02:00
} else {
2007-09-13 19:05:53 +02:00
if ( seedUploadMethod . equals ( " " ) ) {
2008-05-06 01:13:47 +02:00
this . sb . setConfig ( " seedUploadMethod " , " none " ) ;
2007-09-13 19:05:53 +02:00
}
2008-09-03 02:30:21 +02:00
if ( log . isFine ( ) ) log . logFine ( " yacyCore.publishSeedList: No uploading method configured " ) ;
2005-05-17 10:25:04 +02:00
return ;
2005-04-07 21:19:42 +02:00
}
}
2005-10-09 15:18:48 +02:00
2007-09-13 19:05:53 +02:00
public final void peerPing ( ) {
2011-07-14 09:07:06 +02:00
if ( ( this . sb . isRobinsonMode ( ) ) & & ( this . sb . getConfig ( " cluster.mode " , " " ) . equals ( " privatepeer " ) ) ) {
2007-09-13 19:05:53 +02:00
// in case this peer is a privat peer we omit the peer ping
// all other robinson peer types do a peer ping:
// the privatecluster does the ping to the other cluster members
// the publiccluster does the ping to all peers, but prefers the own peer
// the publicpeer does the ping to all peers
return ;
2007-04-26 11:51:51 +02:00
}
2005-10-09 15:18:48 +02:00
2005-04-07 21:19:42 +02:00
// before publishing, update some seed data
2011-07-14 09:07:06 +02:00
this . sb . updateMySeed ( ) ;
2005-10-09 15:18:48 +02:00
2005-04-07 21:19:42 +02:00
// publish own seed to other peer, this can every peer, but makes only sense for senior peers
2011-07-14 09:07:06 +02:00
if ( this . sb . peers . sizeConnected ( ) = = 0 ) {
2005-04-07 21:19:42 +02:00
// reload the seed lists
2011-07-14 09:07:06 +02:00
this . sb . loadSeedLists ( ) ;
log . logInfo ( " re-initialized seed list. received " + this . sb . peers . sizeConnected ( ) + " new peer(s) " ) ;
2005-04-07 21:19:42 +02:00
}
2005-10-09 15:18:48 +02:00
final int newSeeds = publishMySeed ( false ) ;
2007-09-13 19:05:53 +02:00
if ( newSeeds > 0 ) {
2011-07-14 09:07:06 +02:00
log . logInfo ( " received " + newSeeds + " new peer(s), know a total of " + this . sb . peers . sizeConnected ( ) + " different peers " ) ;
2007-09-13 19:05:53 +02:00
}
2005-04-07 21:19:42 +02:00
}
2005-10-09 15:18:48 +02:00
2007-04-30 02:39:53 +02:00
private boolean canReachMyself ( ) { // TODO: check if this method is necessary - depending on the used router it will not work
2005-04-07 21:19:42 +02:00
// returns true if we can reach ourself under our known peer address
// if we cannot reach ourself, we call a forced publishMySeed and return false
2011-10-04 11:06:24 +02:00
final long [ ] callback = Protocol . queryUrlCount ( this . sb . peers . mySeed ( ) ) ;
2011-04-29 04:00:37 +02:00
if ( callback [ 0 ] > = 0 & & callback [ 1 ] = = magic ) {
2011-07-14 09:07:06 +02:00
this . sb . peers . mySeed ( ) . setLastSeenUTC ( ) ;
2005-04-07 21:19:42 +02:00
return true ;
}
log . logInfo ( " re-connect own seed " ) ;
2011-07-14 09:07:06 +02:00
final String oldAddress = this . sb . peers . mySeed ( ) . getPublicAddress ( ) ;
2005-12-05 15:24:13 +01:00
/*final int newSeeds =*/ publishMySeed ( true ) ;
2011-07-14 09:07:06 +02:00
return ( oldAddress ! = null & & oldAddress . equals ( this . sb . peers . mySeed ( ) . getPublicAddress ( ) ) ) ;
2005-04-07 21:19:42 +02:00
}
2011-07-14 09:07:06 +02:00
2011-02-25 13:23:00 +01:00
// use our own formatter to prevent concurrency locks with other processes
2011-02-25 22:11:53 +01:00
private final static GenericFormatter my_SHORT_SECOND_FORMATTER = new GenericFormatter ( GenericFormatter . FORMAT_SHORT_SECOND , GenericFormatter . time_second ) ;
2005-04-07 21:19:42 +02:00
2005-10-09 15:18:48 +02:00
protected class publishThread extends Thread {
2008-03-04 23:45:45 +01:00
int added ;
2011-10-04 11:06:24 +02:00
private final Seed seed ;
2009-10-20 00:34:44 +02:00
private final Semaphore sync ;
2008-01-23 22:23:17 +01:00
private final List < Thread > syncList ;
2005-10-09 15:18:48 +02:00
2011-10-04 11:06:24 +02:00
public publishThread ( final ThreadGroup tg , final Seed seed ,
2009-10-20 00:34:44 +02:00
final Semaphore sync , final List < Thread > syncList ) throws InterruptedException {
2005-06-23 13:00:26 +02:00
super ( tg , " PublishSeed_ " + seed . getName ( ) ) ;
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
this . sync = sync ;
2009-10-20 00:34:44 +02:00
this . sync . acquire ( ) ;
2005-06-23 13:00:26 +02:00
this . syncList = syncList ;
2005-10-09 15:18:48 +02:00
2005-04-07 21:19:42 +02:00
this . seed = seed ;
this . added = 0 ;
}
2007-09-13 19:05:53 +02:00
public final void run ( ) {
2005-10-09 15:18:48 +02:00
try {
2011-10-04 11:06:24 +02:00
this . added = Protocol . hello ( Network . this . sb . peers . mySeed ( ) , Network . this . sb . peers . peerActions , this . seed . getClusterAddress ( ) , this . seed . hash , this . seed . getName ( ) ) ;
2005-06-23 13:00:26 +02:00
if ( this . added < 0 ) {
2005-04-07 21:19:42 +02:00
// no or wrong response, delete that address
2008-08-02 14:12:04 +02:00
final String cause = " peer ping to peer resulted in error response (added < 0) " ;
2011-10-04 11:06:24 +02:00
log . logInfo ( " publish: disconnected " + this . seed . get ( Seed . PEERTYPE , Seed . PEERTYPE_SENIOR ) + " peer ' " + this . seed . getName ( ) + " ' from " + this . seed . getPublicAddress ( ) + " : " + cause ) ;
Network . this . sb . peers . peerActions . peerDeparture ( this . seed , cause ) ;
2005-04-07 21:19:42 +02:00
} else {
// success! we have published our peer to a senior peer
// update latest news from the other peer
2011-10-04 11:06:24 +02:00
log . logInfo ( " publish: handshaked " + this . seed . get ( Seed . PEERTYPE , Seed . PEERTYPE_SENIOR ) + " peer ' " + this . seed . getName ( ) + " ' at " + this . seed . getPublicAddress ( ) ) ;
2007-10-31 16:47:48 +01:00
// check if seed's lastSeen has been updated
2011-10-04 11:06:24 +02:00
final Seed newSeed = Network . this . sb . peers . getConnected ( this . seed . hash ) ;
2007-10-31 16:47:48 +01:00
if ( newSeed ! = null ) {
2008-08-08 15:56:29 +02:00
if ( ! newSeed . isOnline ( ) ) {
2011-10-04 11:06:24 +02:00
if ( log . isFine ( ) ) log . logFine ( " publish: recently handshaked " + this . seed . get ( Seed . PEERTYPE , Seed . PEERTYPE_SENIOR ) +
2008-08-08 15:56:29 +02:00
" peer ' " + this . seed . getName ( ) + " ' at " + this . seed . getPublicAddress ( ) + " is not online. " +
" Removing Peer from connected " ) ;
2011-10-04 11:06:24 +02:00
Network . this . sb . peers . peerActions . peerDeparture ( newSeed , " peer not online " ) ;
2008-08-08 15:56:29 +02:00
} else
2007-10-31 16:47:48 +01:00
if ( newSeed . getLastSeenUTC ( ) < ( System . currentTimeMillis ( ) - 10000 ) ) {
// update last seed date
if ( newSeed . getLastSeenUTC ( ) > = this . seed . getLastSeenUTC ( ) ) {
2011-10-04 11:06:24 +02:00
if ( log . isFine ( ) ) log . logFine ( " publish: recently handshaked " + this . seed . get ( Seed . PEERTYPE , Seed . PEERTYPE_SENIOR ) +
2007-10-31 17:29:07 +01:00
" peer ' " + this . seed . getName ( ) + " ' at " + this . seed . getPublicAddress ( ) + " with old LastSeen: ' " +
2011-02-25 13:23:00 +01:00
my_SHORT_SECOND_FORMATTER . format ( new Date ( newSeed . getLastSeenUTC ( ) ) ) + " ' " ) ;
2007-10-31 16:47:48 +01:00
newSeed . setLastSeenUTC ( ) ;
2011-10-04 11:06:24 +02:00
Network . this . sb . peers . peerActions . peerArrival ( newSeed , true ) ;
2007-10-31 16:47:48 +01:00
} else {
2011-10-04 11:06:24 +02:00
if ( log . isFine ( ) ) log . logFine ( " publish: recently handshaked " + this . seed . get ( Seed . PEERTYPE , Seed . PEERTYPE_SENIOR ) +
2007-10-31 17:29:07 +01:00
" peer ' " + this . seed . getName ( ) + " ' at " + this . seed . getPublicAddress ( ) + " with old LastSeen: ' " +
2011-02-25 13:23:00 +01:00
my_SHORT_SECOND_FORMATTER . format ( new Date ( newSeed . getLastSeenUTC ( ) ) ) + " ', this is more recent: ' " +
my_SHORT_SECOND_FORMATTER . format ( new Date ( this . seed . getLastSeenUTC ( ) ) ) + " ' " ) ;
2007-10-31 16:47:48 +01:00
this . seed . setLastSeenUTC ( ) ;
2011-10-04 11:06:24 +02:00
Network . this . sb . peers . peerActions . peerArrival ( this . seed , true ) ;
2007-10-31 16:47:48 +01:00
}
}
} else {
2011-10-04 11:06:24 +02:00
if ( log . isFine ( ) ) log . logFine ( " publish: recently handshaked " + this . seed . get ( Seed . PEERTYPE , Seed . PEERTYPE_SENIOR ) + " peer ' " + this . seed . getName ( ) + " ' at " + this . seed . getPublicAddress ( ) + " not in connectedDB " ) ;
2007-10-31 16:47:48 +01:00
}
2005-04-07 21:19:42 +02:00
}
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2011-07-14 09:07:06 +02:00
log . logSevere ( " publishThread: error with target seed " + this . seed . toString ( ) + " : " + e . getMessage ( ) , e ) ;
2005-06-23 13:00:26 +02:00
} finally {
this . syncList . add ( this ) ;
2009-10-20 00:34:44 +02:00
this . sync . release ( ) ;
2005-04-07 21:19:42 +02:00
}
}
}
2005-10-09 15:18:48 +02:00
2008-08-02 14:12:04 +02:00
private int publishMySeed ( final boolean force ) {
2005-06-23 13:00:26 +02:00
try {
// call this after the httpd was started up
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// we need to find out our own ip
// This is not always easy, since the application may
// live behind a firewall or nat.
// the normal way to do this is either measure the value that java gives us,
// but this is not correct if the peer lives behind a NAT/Router or has several
// addresses and not the right one can be found out.
// We have several alternatives:
// 1. ask another peer. This should be normal and the default method.
// but if no other peer lives, or we don't know them, we cannot do that
// 2. ask own NAT. This is only an option if the NAT is a DI604, because this is the
// only supported for address retrieval
// 3. ask ip respond services in the internet. There are several, and they are all
// probed until we get a valid response.
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// init yacyHello-process
2011-10-04 11:06:24 +02:00
Map < String , Seed > seeds ; // hash/yacySeed relation
2005-10-09 15:18:48 +02:00
2011-07-14 09:07:06 +02:00
int attempts = this . sb . peers . sizeConnected ( ) ;
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// getting a list of peers to contact
2011-10-04 11:06:24 +02:00
if ( this . sb . peers . mySeed ( ) . get ( Seed . PEERTYPE , Seed . PEERTYPE_VIRGIN ) . equals ( Seed . PEERTYPE_VIRGIN ) ) {
2008-03-12 01:05:18 +01:00
if ( attempts > PING_INITIAL ) { attempts = PING_INITIAL ; }
2009-07-19 22:37:44 +02:00
final Map < byte [ ] , String > ch = Switchboard . getSwitchboard ( ) . clusterhashes ;
2011-07-14 09:07:06 +02:00
seeds = PeerSelection . seedsByAge ( this . sb . peers , true , attempts - ( ( ch = = null ) ? 0 : ch . size ( ) ) ) ; // best for fast connection
2007-04-29 02:08:38 +02:00
// add also all peers from cluster if this is a public robinson cluster
2008-06-06 18:01:27 +02:00
if ( ch ! = null ) {
2009-04-16 17:29:00 +02:00
final Iterator < Map . Entry < byte [ ] , String > > i = ch . entrySet ( ) . iterator ( ) ;
2009-04-29 13:18:17 +02:00
String hash ;
2009-04-16 17:29:00 +02:00
Map . Entry < byte [ ] , String > entry ;
2011-10-04 11:06:24 +02:00
Seed seed ;
2007-09-13 19:05:53 +02:00
while ( i . hasNext ( ) ) {
2008-01-23 22:23:17 +01:00
entry = i . next ( ) ;
2011-05-27 10:24:54 +02:00
hash = ASCII . String ( entry . getKey ( ) ) ;
2008-06-06 18:01:27 +02:00
seed = seeds . get ( hash ) ;
2007-09-13 19:05:53 +02:00
if ( seed = = null ) {
2011-07-14 09:07:06 +02:00
seed = this . sb . peers . get ( hash ) ;
2008-03-12 01:05:18 +01:00
if ( seed = = null ) continue ;
2007-09-13 19:05:53 +02:00
}
2008-06-06 18:01:27 +02:00
seed . setAlternativeAddress ( entry . getValue ( ) ) ;
2009-04-29 13:18:17 +02:00
seeds . put ( hash , seed ) ;
2008-03-12 01:05:18 +01:00
}
2007-04-29 02:08:38 +02:00
}
2005-06-23 13:00:26 +02:00
} else {
2008-03-12 01:05:18 +01:00
int diff = PING_MIN_DBSIZE - amIAccessibleDB . size ( ) ;
if ( diff > PING_MIN_RUNNING ) {
diff = Math . min ( diff , PING_MAX_RUNNING ) ;
if ( attempts > diff ) { attempts = diff ; }
} else {
if ( attempts > PING_MIN_RUNNING ) { attempts = PING_MIN_RUNNING ; }
}
2011-07-14 09:07:06 +02:00
seeds = PeerSelection . seedsByAge ( this . sb . peers , false , attempts ) ; // best for seed list maintenance/cleaning
2005-06-23 13:00:26 +02:00
}
2008-03-12 01:05:18 +01:00
2009-12-02 01:37:59 +01:00
if ( seeds = = null | | seeds . isEmpty ( ) ) { return 0 ; }
2008-03-12 01:05:18 +01:00
if ( seeds . size ( ) < attempts ) { attempts = seeds . size ( ) ; }
// This will try to get Peers that are not currently in amIAccessibleDB
2011-10-04 11:06:24 +02:00
final Iterator < Seed > si = seeds . values ( ) . iterator ( ) ;
Seed seed ;
2005-10-09 15:18:48 +02:00
2005-07-29 16:10:19 +02:00
// include a YaCyNews record to my seed
try {
2011-10-04 11:06:24 +02:00
final NewsDB . Record record = this . sb . peers . newsPool . myPublication ( ) ;
2005-10-09 15:18:48 +02:00
if ( record = = null ) {
2011-07-14 09:07:06 +02:00
this . sb . peers . mySeed ( ) . put ( " news " , " " ) ;
2005-10-09 15:18:48 +02:00
} else {
2011-07-14 09:07:06 +02:00
this . sb . peers . mySeed ( ) . put ( " news " , de . anomic . tools . crypt . simpleEncode ( record . toString ( ) ) ) ;
2005-10-09 15:18:48 +02:00
}
2009-12-10 00:27:26 +01:00
} catch ( final Exception e ) {
2005-08-30 23:32:59 +02:00
log . logSevere ( " publishMySeed: problem with news encoding " , e ) ;
2005-07-29 16:10:19 +02:00
}
2011-07-14 09:07:06 +02:00
this . sb . peers . mySeed ( ) . setUnusedFlags ( ) ;
2006-01-23 00:14:37 +01:00
int newSeeds = - 1 ;
//if (seeds.length > 1) {
2005-06-23 13:00:26 +02:00
// holding a reference to all started threads
int contactedSeedCount = 0 ;
2008-01-23 22:23:17 +01:00
final List < Thread > syncList = Collections . synchronizedList ( new LinkedList < Thread > ( ) ) ; // memory for threads
2009-10-20 00:34:44 +02:00
final Semaphore sync = new Semaphore ( attempts ) ;
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// going through the peer list and starting a new publisher thread for each peer
2007-10-20 13:56:15 +02:00
int i = 0 ;
2008-05-14 22:30:44 +02:00
while ( si . hasNext ( ) ) {
2008-06-06 18:01:27 +02:00
seed = si . next ( ) ;
2007-10-20 14:02:31 +02:00
if ( seed = = null ) {
2009-10-20 00:34:44 +02:00
sync . acquire ( ) ;
2007-10-20 14:02:31 +02:00
continue ;
}
2007-10-20 13:56:15 +02:00
i + + ;
2005-10-09 15:18:48 +02:00
2007-05-07 22:48:24 +02:00
final String address = seed . getClusterAddress ( ) ;
2011-10-04 11:06:24 +02:00
if ( log . isFine ( ) ) log . logFine ( " HELLO # " + i + " to peer ' " + seed . get ( Seed . NAME , " " ) + " ' at " + address ) ; // debug
2008-08-02 14:12:04 +02:00
final String seederror = seed . isProper ( false ) ;
2008-03-12 01:05:18 +01:00
if ( ( address = = null ) | | ( seederror ! = null ) ) {
2005-06-23 13:00:26 +02:00
// we don't like that address, delete it
2011-07-14 09:07:06 +02:00
this . sb . peers . peerActions . peerDeparture ( seed , " peer ping to peer resulted in address = " + address + " ; seederror = " + seederror ) ;
2009-10-20 00:34:44 +02:00
sync . acquire ( ) ;
2005-06-23 13:00:26 +02:00
} else {
// starting a new publisher thread
contactedSeedCount + + ;
2011-10-04 11:06:24 +02:00
( new publishThread ( Network . publishThreadGroup , seed , sync , syncList ) ) . start ( ) ;
2005-06-23 13:00:26 +02:00
}
}
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// receiving the result of all started publisher threads
2006-01-23 02:42:26 +01:00
for ( int j = 0 ; j < contactedSeedCount ; j + + ) {
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// waiting for the next thread to finish
2009-10-20 00:34:44 +02:00
sync . acquire ( ) ;
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// if this is true something is wrong ...
2006-01-23 00:14:37 +01:00
if ( syncList . isEmpty ( ) ) {
log . logWarning ( " PeerPing: syncList.isEmpty()==true " ) ;
continue ;
//return 0;
}
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// getting a reference to the finished thread
2005-10-09 15:18:48 +02:00
final publishThread t = ( publishThread ) syncList . remove ( 0 ) ;
2005-06-23 13:00:26 +02:00
// getting the amount of new reported seeds
if ( t . added > = 0 ) {
2007-09-13 19:05:53 +02:00
if ( newSeeds = = - 1 ) {
2005-10-09 15:18:48 +02:00
newSeeds = t . added ;
} else {
newSeeds + = t . added ;
}
}
2005-06-23 13:00:26 +02:00
}
2005-10-09 15:18:48 +02:00
2006-01-23 00:14:37 +01:00
int accessible = 0 ;
int notaccessible = 0 ;
2007-09-13 19:05:53 +02:00
final long cutofftime = System . currentTimeMillis ( ) - PING_MAX_DBAGE ;
2006-01-23 00:14:37 +01:00
final int dbSize ;
2007-09-13 19:05:53 +02:00
synchronized ( amIAccessibleDB ) {
2006-01-23 00:14:37 +01:00
dbSize = amIAccessibleDB . size ( ) ;
2008-08-02 14:12:04 +02:00
final Iterator < String > ai = amIAccessibleDB . keySet ( ) . iterator ( ) ;
2006-01-23 00:14:37 +01:00
while ( ai . hasNext ( ) ) {
2011-10-04 11:06:24 +02:00
final Accessible ya = amIAccessibleDB . get ( ai . next ( ) ) ;
2006-01-23 00:14:37 +01:00
if ( ya . lastUpdated < cutofftime ) {
2007-09-13 19:05:53 +02:00
ai . remove ( ) ;
2006-01-23 00:14:37 +01:00
} else {
2007-09-13 19:05:53 +02:00
if ( ya . IWasAccessed ) {
accessible + + ;
} else {
notaccessible + + ;
}
2006-01-23 00:14:37 +01:00
}
}
2008-09-03 02:30:21 +02:00
if ( log . isFine ( ) ) log . logFine ( " DBSize before -> after Cleanup: " + dbSize + " -> " + amIAccessibleDB . size ( ) ) ;
2006-01-23 00:14:37 +01:00
}
log . logInfo ( " PeerPing: I am accessible for " + accessible +
2006-01-24 02:16:22 +01:00
" peer(s), not accessible for " + notaccessible + " peer(s). " ) ;
2006-01-23 00:14:37 +01:00
2008-03-12 01:05:18 +01:00
if ( ( accessible + notaccessible ) > 0 ) {
2006-01-23 00:14:37 +01:00
final String newPeerType ;
// At least one other Peer told us our type
2008-03-12 01:05:18 +01:00
if ( ( accessible > = PING_MIN_PEERSEEN ) | |
( accessible > = notaccessible ) ) {
2006-01-23 00:14:37 +01:00
// We can be reached from a majority of other Peers
2011-07-14 09:07:06 +02:00
if ( this . sb . peers . mySeed ( ) . isPrincipal ( ) ) {
2011-10-04 11:06:24 +02:00
newPeerType = Seed . PEERTYPE_PRINCIPAL ;
2006-01-23 00:14:37 +01:00
} else {
2011-10-04 11:06:24 +02:00
newPeerType = Seed . PEERTYPE_SENIOR ;
2006-01-23 00:14:37 +01:00
}
} else {
// We cannot be reached from the outside
2011-10-04 11:06:24 +02:00
newPeerType = Seed . PEERTYPE_JUNIOR ;
2006-01-23 00:14:37 +01:00
}
2011-07-14 09:07:06 +02:00
if ( this . sb . peers . mySeed ( ) . orVirgin ( ) . equals ( newPeerType ) ) {
log . logInfo ( " PeerPing: myType is " + this . sb . peers . mySeed ( ) . orVirgin ( ) ) ;
2006-01-23 00:14:37 +01:00
} else {
2011-07-14 09:07:06 +02:00
log . logInfo ( " PeerPing: changing myType from ' " + this . sb . peers . mySeed ( ) . orVirgin ( ) + " ' to ' " + newPeerType + " ' " ) ;
2011-10-04 11:06:24 +02:00
this . sb . peers . mySeed ( ) . put ( Seed . PEERTYPE , newPeerType ) ;
2006-01-23 00:14:37 +01:00
}
} else {
2011-07-14 09:07:06 +02:00
log . logInfo ( " PeerPing: No data, staying at myType: " + this . sb . peers . mySeed ( ) . orVirgin ( ) ) ;
2006-01-23 00:14:37 +01:00
}
2008-05-14 22:30:44 +02:00
// success! we have published our peer to a senior peer
// update latest news from the other peer
// log.logInfo("publish: handshaked " + t.seed.get(yacySeed.PEERTYPE, yacySeed.PEERTYPE_SENIOR) + " peer '" + t.seed.getName() + "' at " + t.seed.getAddress());
2011-07-14 09:07:06 +02:00
this . sb . peers . saveMySeed ( ) ;
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
// if we have an address, we do nothing
2011-07-14 09:07:06 +02:00
if ( this . sb . peers . mySeed ( ) . isProper ( true ) = = null & & ! force ) { return 0 ; }
2008-05-14 22:30:44 +02:00
if ( newSeeds > 0 ) return newSeeds ;
2011-07-14 09:07:06 +02:00
2005-06-23 13:00:26 +02:00
// still no success: ask own NAT or internet responder
2007-03-16 16:21:24 +01:00
//final boolean DI604use = switchboard.getConfig("DI604use", "false").equals("true");
//final String DI604pw = switchboard.getConfig("DI604pw", "");
2011-07-14 09:07:06 +02:00
final String ip = this . sb . getConfig ( " staticIP " , " " ) ;
2007-03-16 16:21:24 +01:00
//if (ip.equals("")) ip = natLib.retrieveIP(DI604use, DI604pw);
2011-07-14 09:07:06 +02:00
2007-03-16 16:21:24 +01:00
// yacyCore.log.logDebug("DEBUG: new IP=" + ip);
2011-10-04 11:06:24 +02:00
if ( Seed . isProperIP ( ip ) = = null ) this . sb . peers . mySeed ( ) . setIP ( ip ) ;
if ( this . sb . peers . mySeed ( ) . get ( Seed . PEERTYPE , Seed . PEERTYPE_JUNIOR ) . equals ( Seed . PEERTYPE_JUNIOR ) ) // ???????????????
this . sb . peers . mySeed ( ) . put ( Seed . PEERTYPE , Seed . PEERTYPE_SENIOR ) ; // to start bootstraping, we need to be recognised as PEERTYPE_SENIOR peer
2007-03-16 16:21:24 +01:00
log . logInfo ( " publish: no recipient found, our address is " +
2011-07-14 09:07:06 +02:00
( ( this . sb . peers . mySeed ( ) . getPublicAddress ( ) = = null ) ? " unknown " : this . sb . peers . mySeed ( ) . getPublicAddress ( ) ) ) ;
this . sb . peers . saveMySeed ( ) ;
2005-06-23 13:00:26 +02:00
return 0 ;
2008-08-02 14:12:04 +02:00
} catch ( final InterruptedException e ) {
2005-07-07 15:58:54 +02:00
try {
2005-07-07 23:00:11 +02:00
log . logInfo ( " publish: Interruption detected while publishing my seed. " ) ;
2005-10-09 15:18:48 +02:00
2005-07-07 15:58:54 +02:00
// consuming the theads interrupted signal
Thread . interrupted ( ) ;
2005-10-09 15:18:48 +02:00
2005-07-07 23:00:11 +02:00
// interrupt all already started publishThreads
2011-10-04 11:06:24 +02:00
log . logInfo ( " publish: Signaling shutdown to " + Network . publishThreadGroup . activeCount ( ) + " remaining publishing threads ... " ) ;
Network . publishThreadGroup . interrupt ( ) ;
2005-10-09 15:18:48 +02:00
2005-07-07 15:58:54 +02:00
// waiting some time for the publishThreads to finish execution
2008-08-02 14:12:04 +02:00
try { Thread . sleep ( 500 ) ; } catch ( final InterruptedException ex ) { }
2005-10-09 15:18:48 +02:00
2005-07-07 23:44:31 +02:00
// getting the amount of remaining publishing threads
2011-10-04 11:06:24 +02:00
int threadCount = Network . publishThreadGroup . activeCount ( ) ;
2005-10-09 15:18:48 +02:00
final Thread [ ] threadList = new Thread [ threadCount ] ;
2011-10-04 11:06:24 +02:00
threadCount = Network . publishThreadGroup . enumerate ( threadList ) ;
2005-10-09 15:18:48 +02:00
2005-07-07 23:44:31 +02:00
// we need to use a timeout here because of missing interruptable session threads ...
2011-10-04 11:06:24 +02:00
if ( log . isFine ( ) ) log . logFine ( " publish: Waiting for " + Network . publishThreadGroup . activeCount ( ) + " remaining publishing threads to finish shutdown ... " ) ;
2007-09-13 19:05:53 +02:00
for ( int currentThreadIdx = 0 ; currentThreadIdx < threadCount ; currentThreadIdx + + ) {
2005-10-09 15:18:48 +02:00
final Thread currentThread = threadList [ currentThreadIdx ] ;
2005-07-07 23:44:31 +02:00
if ( currentThread . isAlive ( ) ) {
2008-09-03 02:30:21 +02:00
if ( log . isFine ( ) ) log . logFine ( " publish: Waiting for remaining publishing thread ' " + currentThread . getName ( ) + " ' to finish shutdown " ) ;
2008-08-02 14:12:04 +02:00
try { currentThread . join ( 500 ) ; } catch ( final InterruptedException ex ) { }
2005-07-07 23:44:31 +02:00
}
2005-10-09 15:18:48 +02:00
}
2005-07-07 23:44:31 +02:00
log . logInfo ( " publish: Shutdown off all remaining publishing thread finished. " ) ;
2005-10-09 15:18:48 +02:00
2008-08-02 14:12:04 +02:00
} catch ( final Exception ee ) {
2007-09-13 19:05:53 +02:00
log . logWarning ( " publish: Unexpected error while trying to shutdown all remaining publishing threads. " , e ) ;
2005-06-23 13:00:26 +02:00
}
2005-10-09 15:18:48 +02:00
2005-06-23 13:00:26 +02:00
return 0 ;
}
2005-04-07 21:19:42 +02:00
}
2005-10-09 15:18:48 +02:00
2008-01-23 22:23:17 +01:00
@SuppressWarnings ( " unchecked " )
public static HashMap < String , String > getSeedUploadMethods ( ) {
2011-10-04 11:06:24 +02:00
synchronized ( Network . seedUploadMethods ) {
return ( HashMap < String , String > ) Network . seedUploadMethods . clone ( ) ;
2005-10-09 15:18:48 +02:00
}
2005-05-17 10:25:04 +02:00
}
2005-10-09 15:18:48 +02:00
2008-08-02 14:12:04 +02:00
public static yacySeedUploader getSeedUploader ( final String methodname ) {
2005-05-17 10:25:04 +02:00
String className = null ;
2011-10-04 11:06:24 +02:00
synchronized ( Network . seedUploadMethods ) {
if ( Network . seedUploadMethods . containsKey ( methodname ) ) {
className = Network . seedUploadMethods . get ( methodname ) ;
2005-05-17 10:25:04 +02:00
}
2005-10-09 15:18:48 +02:00
}
if ( className = = null ) { return null ; }
2005-05-17 10:25:04 +02:00
try {
2008-01-23 22:23:17 +01:00
final Class < ? > uploaderClass = Class . forName ( className ) ;
2005-10-09 15:18:48 +02:00
final Object uploader = uploaderClass . newInstance ( ) ;
2005-05-17 10:25:04 +02:00
return ( yacySeedUploader ) uploader ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2005-05-17 10:25:04 +02:00
return null ;
}
}
2005-10-09 15:18:48 +02:00
2005-05-17 10:25:04 +02:00
public static void loadSeedUploadMethods ( ) {
2009-09-15 11:47:02 +02:00
yacySeedUploader uploader ;
uploader = new yacySeedUploadFile ( ) ;
2011-10-04 11:06:24 +02:00
Network . seedUploadMethods . put ( uploader . getClass ( ) . getSimpleName ( ) . substring ( " yacySeedUpload " . length ( ) ) , uploader . getClass ( ) . getCanonicalName ( ) ) ;
2009-09-15 11:47:02 +02:00
uploader = new yacySeedUploadFtp ( ) ;
2011-10-04 11:06:24 +02:00
Network . seedUploadMethods . put ( uploader . getClass ( ) . getSimpleName ( ) . substring ( " yacySeedUpload " . length ( ) ) , uploader . getClass ( ) . getCanonicalName ( ) ) ;
2009-09-15 11:47:02 +02:00
uploader = new yacySeedUploadScp ( ) ;
2011-10-04 11:06:24 +02:00
Network . seedUploadMethods . put ( uploader . getClass ( ) . getSimpleName ( ) . substring ( " yacySeedUpload " . length ( ) ) , uploader . getClass ( ) . getCanonicalName ( ) ) ;
2005-05-17 10:25:04 +02:00
}
2008-08-02 14:12:04 +02:00
public static boolean changeSeedUploadMethod ( final String method ) {
2007-09-13 19:05:53 +02:00
if ( method = = null | | method . length ( ) = = 0 ) { return false ; }
2005-10-09 15:18:48 +02:00
2007-09-13 19:05:53 +02:00
if ( method . equalsIgnoreCase ( " none " ) ) { return true ; }
2005-10-09 15:18:48 +02:00
2011-10-04 11:06:24 +02:00
synchronized ( Network . seedUploadMethods ) {
return Network . seedUploadMethods . containsKey ( method ) ;
2005-10-09 15:18:48 +02:00
}
2005-05-17 10:25:04 +02:00
}
2005-10-09 15:18:48 +02:00
2009-07-19 22:37:44 +02:00
public static final String saveSeedList ( final Switchboard sb ) {
2005-05-17 10:25:04 +02:00
try {
2005-08-02 17:36:10 +02:00
// return an error if this is not successful, and NULL if everything is fine
String logt ;
2005-10-09 15:18:48 +02:00
2005-08-02 17:36:10 +02:00
// be shure that we have something to say
2009-05-28 16:26:05 +02:00
if ( sb . peers . mySeed ( ) . getPublicAddress ( ) = = null ) {
2005-10-09 15:18:48 +02:00
final String errorMsg = " We have no valid IP address until now " ;
2005-08-02 17:36:10 +02:00
log . logWarning ( " SaveSeedList: " + errorMsg ) ;
return errorMsg ;
}
2005-10-09 15:18:48 +02:00
2005-08-02 17:36:10 +02:00
// getting the configured seed uploader
2005-10-09 15:18:48 +02:00
String seedUploadMethod = sb . getConfig ( " seedUploadMethod " , " " ) ;
2005-08-02 17:36:10 +02:00
// for backward compatiblity ....
2007-09-13 19:05:53 +02:00
if ( seedUploadMethod . equalsIgnoreCase ( " Ftp " ) | |
( seedUploadMethod . equals ( " " ) & &
sb . getConfig ( " seedFTPPassword " , " " ) . length ( ) > 0 ) ) {
2005-08-02 17:36:10 +02:00
seedUploadMethod = " Ftp " ;
2007-09-13 19:05:53 +02:00
sb . setConfig ( " seedUploadMethod " , seedUploadMethod ) ;
} else if ( seedUploadMethod . equalsIgnoreCase ( " File " ) | |
( seedUploadMethod . equals ( " " ) & &
sb . getConfig ( " seedFilePath " , " " ) . length ( ) > 0 ) ) {
2005-08-02 17:36:10 +02:00
seedUploadMethod = " File " ;
2007-09-13 19:05:53 +02:00
sb . setConfig ( " seedUploadMethod " , seedUploadMethod ) ;
2005-08-02 17:36:10 +02:00
}
2005-10-09 15:18:48 +02:00
// determine the seed uploader that should be used ...
if ( seedUploadMethod . equalsIgnoreCase ( " none " ) ) { return " no uploader specified " ; }
2008-08-02 14:12:04 +02:00
final yacySeedUploader uploader = getSeedUploader ( seedUploadMethod ) ;
2005-08-02 17:36:10 +02:00
if ( uploader = = null ) {
2005-10-09 15:18:48 +02:00
final String errorMsg = " Unable to get the proper uploader-class for seed uploading method ' " + seedUploadMethod + " '. " ;
2005-08-02 17:36:10 +02:00
log . logWarning ( " SaveSeedList: " + errorMsg ) ;
2005-10-09 15:18:48 +02:00
return errorMsg ;
2005-08-02 17:36:10 +02:00
}
2005-10-09 15:18:48 +02:00
2005-08-02 17:36:10 +02:00
// ensure that the seed file url is configured properly
2009-10-11 02:12:19 +02:00
DigestURI seedURL ;
2007-09-13 19:05:53 +02:00
try {
2011-10-04 11:06:24 +02:00
final String seedURLStr = sb . peers . mySeed ( ) . get ( Seed . SEEDLISTURL , " " ) ;
2005-10-09 15:18:48 +02:00
if ( seedURLStr . length ( ) = = 0 ) { throw new MalformedURLException ( " The seed-file url must not be empty. " ) ; }
2005-11-03 16:28:37 +01:00
if ( ! (
seedURLStr . toLowerCase ( ) . startsWith ( " http:// " ) | |
seedURLStr . toLowerCase ( ) . startsWith ( " https:// " )
2007-09-13 19:05:53 +02:00
) ) {
throw new MalformedURLException ( " Unsupported protocol. " ) ;
2005-11-03 16:28:37 +01:00
}
2010-10-26 18:10:20 +02:00
seedURL = new DigestURI ( seedURLStr ) ;
2008-08-02 14:12:04 +02:00
} catch ( final MalformedURLException e ) {
2011-10-04 11:06:24 +02:00
final String errorMsg = " Malformed seed file URL ' " + sb . peers . mySeed ( ) . get ( Seed . SEEDLISTURL , " " ) + " '. " + e . getMessage ( ) ;
2005-10-09 15:18:48 +02:00
log . logWarning ( " SaveSeedList: " + errorMsg ) ;
2005-08-02 17:36:10 +02:00
return errorMsg ;
2005-10-09 15:18:48 +02:00
}
2005-08-02 17:36:10 +02:00
// upload the seed-list using the configured uploader class
2011-10-04 11:06:24 +02:00
String prevStatus = sb . peers . mySeed ( ) . get ( Seed . PEERTYPE , Seed . PEERTYPE_JUNIOR ) ;
if ( prevStatus . equals ( Seed . PEERTYPE_PRINCIPAL ) ) { prevStatus = Seed . PEERTYPE_SENIOR ; }
2005-10-09 15:18:48 +02:00
2005-08-02 17:36:10 +02:00
try {
2011-10-04 11:06:24 +02:00
sb . peers . mySeed ( ) . put ( Seed . PEERTYPE , Seed . PEERTYPE_PRINCIPAL ) ; // this information shall also be uploaded
2005-10-09 15:18:48 +02:00
2008-09-03 02:30:21 +02:00
if ( log . isFine ( ) ) log . logFine ( " SaveSeedList: Using seed uploading method ' " + seedUploadMethod + " ' for seed-list uploading. " +
2011-10-04 11:06:24 +02:00
" \ n \ tPrevious peerType is ' " + sb . peers . mySeed ( ) . get ( Seed . PEERTYPE , Seed . PEERTYPE_JUNIOR ) + " '. " ) ;
2005-10-09 15:18:48 +02:00
2011-01-27 00:21:33 +01:00
logt = sb . peers . uploadSeedList ( uploader , sb , sb . peers , seedURL ) ;
2005-08-02 17:36:10 +02:00
if ( logt ! = null ) {
if ( logt . indexOf ( " Error " ) > = 0 ) {
2011-10-04 11:06:24 +02:00
sb . peers . mySeed ( ) . put ( Seed . PEERTYPE , prevStatus ) ;
2005-10-09 15:18:48 +02:00
final String errorMsg = " SaveSeedList: seed upload failed using " + uploader . getClass ( ) . getName ( ) + " (error): " + logt . substring ( logt . indexOf ( " Error " ) + 6 ) ;
2005-08-30 23:32:59 +02:00
log . logSevere ( errorMsg ) ;
2005-08-02 17:36:10 +02:00
return errorMsg ;
}
log . logInfo ( logt ) ;
2005-04-07 21:19:42 +02:00
}
2005-10-09 15:18:48 +02:00
2005-08-02 17:36:10 +02:00
// finally, set the principal status
2011-10-04 11:06:24 +02:00
sb . setConfig ( " yacyStatus " , Seed . PEERTYPE_PRINCIPAL ) ;
2005-08-02 17:36:10 +02:00
return null ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2011-10-04 11:06:24 +02:00
sb . peers . mySeed ( ) . put ( Seed . PEERTYPE , prevStatus ) ;
2005-08-02 17:36:10 +02:00
sb . setConfig ( " yacyStatus " , prevStatus ) ;
2005-10-09 15:18:48 +02:00
final String errorMsg = " SaveSeedList: Seed upload failed (IO error): " + e . getMessage ( ) ;
2007-09-13 19:05:53 +02:00
log . logInfo ( errorMsg , e ) ;
2005-08-02 17:36:10 +02:00
return errorMsg ;
2005-04-07 21:19:42 +02:00
}
2005-08-02 17:36:10 +02:00
} finally {
2009-05-28 16:26:05 +02:00
sb . peers . lastSeedUpload_seedDBSize = sb . peers . sizeConnected ( ) ;
sb . peers . lastSeedUpload_timeStamp = System . currentTimeMillis ( ) ;
sb . peers . lastSeedUpload_myIP = sb . peers . mySeed ( ) . getIP ( ) ;
2005-04-07 21:19:42 +02:00
}
}
2005-10-31 13:25:40 +01:00
}