2005-04-07 21:19:42 +02:00
// yacyPeerActions.java
// -------------------------------------
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2005
2005-10-17 17:46:12 +02:00
//
// $LastChangedDate$
// $LastChangedRevision$
// $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
//
// Using this software in any meaning (reading, learning, copying, compiling,
// running) means that you agree that the Author(s) is (are) not responsible
// for cost, loss of data or any harm that may be caused directly or indirectly
// by usage of this softare or this documentation. The usage of this software
// is on your own risk. The installation and usage (starting/running) of this
// software may allow other people or application to access your computer and
// any attached devices and is highly dependent on the configuration of the
// software which must be done by the user of the software; the author(s) is
// (are) also not responsible for proper configuration and usage of the
// software, even if provoked by documentation provided together with
// the software.
//
// Any changes to this file according to the GPL as documented in the file
// gpl.txt aside this file in the shipment you received can be done to the
// lines that follows this copyright notice here, but changes must not be
// done inside the copyright notive above. A re-distribution must contain
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.
package de.anomic.yacy ;
2005-05-05 07:32:19 +02:00
import java.io.IOException ;
2005-10-05 12:45:33 +02:00
import java.util.ArrayList ;
2005-12-22 02:01:46 +01:00
import java.util.HashMap ;
2005-05-05 07:32:19 +02:00
import java.util.HashSet ;
import java.util.Iterator ;
2006-09-30 00:27:20 +02:00
2008-04-05 15:17:16 +02:00
import de.anomic.http.HttpClient ;
2005-05-05 07:32:19 +02:00
import de.anomic.http.httpHeader ;
2007-08-22 02:59:05 +02:00
import de.anomic.plasma.plasmaCrawlNURL ;
2005-05-05 07:32:19 +02:00
import de.anomic.plasma.plasmaSwitchboard ;
import de.anomic.server.serverCore ;
2005-08-03 02:52:32 +02:00
import de.anomic.server.serverDate ;
2006-10-02 11:59:20 +02:00
import de.anomic.tools.nxTools ;
2005-04-07 21:19:42 +02:00
public class yacyPeerActions {
private yacySeedDB seedDB ;
private plasmaSwitchboard sb ;
2008-01-22 20:10:03 +01:00
private HashSet < yacyPeerAction > actions ;
private HashMap < String , String > userAgents ;
2005-04-07 21:19:42 +02:00
public long juniorConnects ;
public long seniorConnects ;
public long principalConnects ;
public long disconnects ;
2006-05-03 23:27:30 +02:00
private int bootstrapLoadTimeout ;
2005-04-07 21:19:42 +02:00
2007-06-25 16:43:57 +02:00
public yacyPeerActions ( yacySeedDB seedDB , plasmaSwitchboard switchboard ) {
2005-04-07 21:19:42 +02:00
this . seedDB = seedDB ;
this . sb = switchboard ;
2008-01-22 20:10:03 +01:00
this . actions = new HashSet < yacyPeerAction > ( ) ;
this . userAgents = new HashMap < String , String > ( ) ;
2005-04-07 21:19:42 +02:00
this . juniorConnects = 0 ;
this . seniorConnects = 0 ;
this . principalConnects = 0 ;
this . disconnects = 0 ;
2006-05-03 23:27:30 +02:00
this . bootstrapLoadTimeout = ( int ) switchboard . getConfigLong ( " bootstrapLoadTimeout " , 6000 ) ;
2005-04-07 21:19:42 +02:00
}
public void deploy ( yacyPeerAction action ) {
actions . add ( action ) ;
}
public void updateMySeed ( ) {
2005-09-12 17:33:51 +02:00
if ( sb . getConfig ( " peerName " , " anomic " ) . equals ( " anomic " ) ) {
2005-07-29 10:48:36 +02:00
// generate new peer name
2006-03-02 23:25:46 +01:00
sb . setConfig ( " peerName " , yacySeed . makeDefaultPeerName ( ) ) ;
2005-07-29 10:48:36 +02:00
}
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . put ( yacySeed . NAME , sb . getConfig ( " peerName " , " nameless " ) ) ;
2005-06-16 09:28:07 +02:00
if ( ( serverCore . portForwardingEnabled ) & & ( serverCore . portForwarding ! = null ) ) {
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . put ( yacySeed . PORT , Integer . toString ( serverCore . portForwarding . getPort ( ) ) ) ;
2005-06-16 09:28:07 +02:00
} else {
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . put ( yacySeed . PORT , Integer . toString ( serverCore . getPortNr ( sb . getConfig ( " port " , " 8080 " ) ) ) ) ;
2005-06-16 09:28:07 +02:00
}
2006-08-27 19:18:34 +02:00
2007-08-22 02:59:05 +02:00
long uptime = ( System . currentTimeMillis ( ) - serverCore . startupTime ) / 1000 ;
2007-01-31 18:05:15 +01:00
long uptimediff = uptime - sb . lastseedcheckuptime ;
long indexedcdiff = sb . indexedPages - sb . lastindexedPages ;
2007-02-01 01:17:35 +01:00
//double requestcdiff = sb.requestedQueries - sb.lastrequestedQueries;
2007-03-25 23:46:03 +02:00
if ( uptimediff > 300 | | uptimediff < = 0 | | sb . lastseedcheckuptime = = - 1 ) {
2007-01-31 18:05:15 +01:00
sb . lastseedcheckuptime = uptime ;
sb . lastindexedPages = sb . indexedPages ;
sb . lastrequestedQueries = sb . requestedQueries ;
2006-01-27 12:51:27 +01:00
}
2006-02-14 18:29:46 +01:00
//the speed of indexing (pages/minute) of the peer
2007-02-01 01:17:35 +01:00
sb . totalPPM = ( int ) ( sb . indexedPages * 60 / Math . max ( uptime , 1 ) ) ;
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . put ( yacySeed . ISPEED , Long . toString ( Math . round ( Math . max ( ( float ) indexedcdiff , 0f ) * 60f / Math . max ( ( float ) uptimediff , 1f ) ) ) ) ;
2007-02-01 01:17:35 +01:00
sb . totalQPM = sb . requestedQueries * 60d / Math . max ( ( double ) uptime , 1d ) ;
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . put ( yacySeed . RSPEED , Double . toString ( sb . totalQPM /*Math.max((float) requestcdiff, 0f) * 60f / Math.max((float) uptimediff, 1f)*/ ) ) ;
2007-01-31 16:39:11 +01:00
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . put ( yacySeed . UPTIME , Long . toString ( uptime / 60 ) ) ; // the number of minutes that the peer is up in minutes/day (moving average MA30)
2008-03-26 15:13:05 +01:00
seedDB . mySeed ( ) . put ( yacySeed . LCOUNT , Integer . toString ( sb . wordIndex . countURL ( ) ) ) ; // the number of links that the peer has stored (LURL's)
2007-10-29 02:43:20 +01:00
seedDB . mySeed ( ) . put ( yacySeed . NCOUNT , Integer . toString ( sb . crawlQueues . noticeURL . size ( ) ) ) ; // the number of links that the peer has noticed, but not loaded (NURL's)
seedDB . mySeed ( ) . put ( yacySeed . RCOUNT , Integer . toString ( sb . crawlQueues . noticeURL . stackSize ( plasmaCrawlNURL . STACK_TYPE_LIMIT ) ) ) ; // the number of links that the peer provides for remote crawling (ZURL's)
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . put ( yacySeed . ICOUNT , Integer . toString ( sb . wordIndex . size ( ) ) ) ; // the minimum number of words that the peer has indexed (as it says)
seedDB . mySeed ( ) . put ( yacySeed . SCOUNT , Integer . toString ( seedDB . sizeConnected ( ) ) ) ; // the number of seeds that the peer has stored
seedDB . mySeed ( ) . put ( yacySeed . CCOUNT , Double . toString ( ( ( int ) ( ( seedDB . sizeConnected ( ) + seedDB . sizeDisconnected ( ) + seedDB . sizePotential ( ) ) * 60 . 0 / ( uptime + 1 . 01 ) ) * 100 ) / 100 . 0 ) ) ; // the number of clients that the peer connects (as connects/hour)
seedDB . mySeed ( ) . put ( yacySeed . VERSION , sb . getConfig ( " version " , " " ) ) ;
if ( seedDB . mySeed ( ) . get ( yacySeed . PEERTYPE , " " ) . equals ( yacySeed . PEERTYPE_PRINCIPAL ) ) {
2005-06-16 09:28:07 +02:00
// attach information about seed location
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . put ( " seedURL " , sb . getConfig ( " seedURL " , " " ) ) ;
2005-06-16 09:28:07 +02:00
}
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . setFlagDirectConnect ( true ) ;
seedDB . mySeed ( ) . setLastSeenUTC ( ) ;
seedDB . mySeed ( ) . put ( yacySeed . UTC , serverDate . UTCDiffString ( ) ) ;
seedDB . mySeed ( ) . setFlagAcceptRemoteCrawl ( sb . getConfig ( " crawlResponse " , " " ) . equals ( " true " ) ) ;
seedDB . mySeed ( ) . setFlagAcceptRemoteIndex ( sb . getConfig ( " allowReceiveIndex " , " " ) . equals ( " true " ) ) ;
2005-06-16 09:28:07 +02:00
//mySeed.setFlagAcceptRemoteIndex(true);
2005-04-07 21:19:42 +02:00
}
public void saveMySeed ( ) {
try {
2007-10-01 14:30:23 +02:00
seedDB . mySeed ( ) . save ( sb . getOwnSeedFile ( ) ) ;
2005-04-07 21:19:42 +02:00
} catch ( IOException e ) { }
}
public void loadSeedLists ( ) {
2005-10-23 12:35:05 +02:00
// uses the superseed to initialize the database with known seeds
2005-04-07 21:19:42 +02:00
2008-01-22 20:10:03 +01:00
yacySeed ys ;
String seedListFileURL ;
yacyURL url ;
ArrayList < String > seedList ;
Iterator < String > enu ;
int lc ;
int sc = seedDB . sizeConnected ( ) ;
httpHeader header ;
2005-04-07 21:19:42 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: " + sc + " seeds known from previous run " ) ;
2005-10-23 12:35:05 +02:00
2005-04-07 21:19:42 +02:00
// - use the superseed to further fill up the seedDB
2007-06-25 16:43:57 +02:00
int ssc = 0 , c = 0 ;
while ( true ) {
2005-10-23 12:35:05 +02:00
if ( Thread . currentThread ( ) . isInterrupted ( ) ) break ;
2007-06-25 16:43:57 +02:00
seedListFileURL = sb . getConfig ( " network.unit.bootstrap.seedlist " + c , " " ) ;
if ( seedListFileURL . length ( ) = = 0 ) break ;
c + + ;
2005-11-03 16:28:37 +01:00
if (
seedListFileURL . startsWith ( " http:// " ) | |
seedListFileURL . startsWith ( " https:// " )
) {
2005-10-23 12:35:05 +02:00
// load the seed list
try {
httpHeader reqHeader = new httpHeader ( ) ;
reqHeader . put ( httpHeader . PRAGMA , " no-cache " ) ;
reqHeader . put ( httpHeader . CACHE_CONTROL , " no-cache " ) ;
2007-09-05 11:01:35 +02:00
url = new yacyURL ( seedListFileURL , null ) ;
2007-07-31 12:00:17 +02:00
long start = System . currentTimeMillis ( ) ;
2008-04-05 15:17:16 +02:00
header = HttpClient . whead ( url . toString ( ) , reqHeader ) ;
2007-07-31 12:00:17 +02:00
long loadtime = System . currentTimeMillis ( ) - start ;
if ( header = = null ) {
if ( loadtime > this . bootstrapLoadTimeout ) {
yacyCore . log . logWarning ( " BOOTSTRAP: seed-list URL " + seedListFileURL + " not available, time-out after " + loadtime + " milliseconds " ) ;
} else {
yacyCore . log . logWarning ( " BOOTSTRAP: seed-list URL " + seedListFileURL + " not available, no content " ) ;
}
} else if ( header . lastModified ( ) = = null ) {
yacyCore . log . logWarning ( " BOOTSTRAP: seed-list URL " + seedListFileURL + " not usable, last-modified is missing " ) ;
2005-04-07 21:19:42 +02:00
} else if ( ( header . age ( ) > 86400000 ) & & ( ssc > 0 ) ) {
2005-08-20 14:09:27 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: seed-list URL " + seedListFileURL + " too old ( " + ( header . age ( ) / 86400000 ) + " days) " ) ;
2005-04-07 21:19:42 +02:00
} else {
ssc + + ;
2008-04-09 13:02:14 +02:00
final byte [ ] content = HttpClient . wget ( url . toString ( ) , reqHeader , null , bootstrapLoadTimeout ) ;
2008-04-05 15:17:16 +02:00
seedList = nxTools . strings ( content , " UTF-8 " ) ;
2005-10-05 12:45:33 +02:00
enu = seedList . iterator ( ) ;
2005-04-07 21:19:42 +02:00
lc = 0 ;
2005-10-05 12:45:33 +02:00
while ( enu . hasNext ( ) ) {
2006-04-06 18:28:28 +02:00
ys = yacySeed . genRemoteSeed ( ( String ) enu . next ( ) , null , true ) ;
2005-07-31 13:11:29 +02:00
if ( ( ys ! = null ) & & ( ys . isProper ( ) = = null ) & &
2007-10-01 14:30:23 +02:00
( ( ! seedDB . mySeedIsDefined ( ) ) | | ( seedDB . mySeed ( ) . hash ! = ys . hash ) ) ) {
2005-04-07 21:19:42 +02:00
if ( connectPeer ( ys , false ) ) lc + + ;
//seedDB.writeMap(ys.hash, ys.getMap(), "init");
2005-10-17 17:46:12 +02:00
//System.out.println("BOOTSTRAP: received peer " + ys.get(yacySeed.NAME, "anonymous") + "/" + ys.getAddress());
2005-04-07 21:19:42 +02:00
//lc++;
}
}
2005-08-20 14:09:27 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: " + lc + " seeds from seed-list URL " + seedListFileURL + " , AGE= " + ( header . age ( ) / 3600000 ) + " h " ) ;
2005-04-07 21:19:42 +02:00
}
2006-12-17 14:21:17 +01:00
} catch ( IOException e ) {
// this is when wget fails, commonly because of timeout
2008-03-16 23:31:54 +01:00
yacyCore . log . logWarning ( " BOOTSTRAP: failed (1) to load seeds from seed-list URL " + seedListFileURL + " : " + e . getMessage ( ) ) ;
2005-10-23 12:35:05 +02:00
} catch ( Exception e ) {
// this is when wget fails; may be because of missing internet connection
2007-10-01 14:30:23 +02:00
yacyCore . log . logSevere ( " BOOTSTRAP: failed (2) to load seeds from seed-list URL " + seedListFileURL + " : " + e . getMessage ( ) , e ) ;
2005-10-23 12:35:05 +02:00
}
}
}
2005-04-07 21:19:42 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: " + ( seedDB . sizeConnected ( ) - sc ) + " new seeds while bootstraping. " ) ;
}
2006-12-21 14:14:13 +01:00
private synchronized boolean connectPeer ( yacySeed seed , boolean direct ) {
2006-02-27 23:25:27 +01:00
// store a remote peer's seed
// returns true if the peer is new and previously unknown
2005-07-31 13:11:29 +02:00
if ( seed = = null ) {
2006-02-27 23:25:27 +01:00
yacyCore . log . logSevere ( " connect: WRONG seed (NULL) " ) ;
return false ;
2006-12-20 02:07:49 +01:00
}
2006-12-21 14:14:13 +01:00
final String error = seed . isProper ( ) ;
if ( error ! = null ) {
2006-02-27 23:25:27 +01:00
yacyCore . log . logSevere ( " connect: WRONG seed ( " + seed . getName ( ) + " / " + seed . hash + " ): " + error ) ;
return false ;
2006-12-20 02:07:49 +01:00
}
2007-10-01 14:30:23 +02:00
if ( ( this . seedDB . mySeedIsDefined ( ) ) & & ( seed . hash . equals ( this . seedDB . mySeed ( ) . hash ) ) ) {
2007-04-30 00:05:34 +02:00
yacyCore . log . logInfo ( " connect: SELF reference " + seed . getPublicAddress ( ) ) ;
2006-02-27 23:25:27 +01:00
return false ;
2006-12-20 02:07:49 +01:00
}
2006-12-21 14:14:13 +01:00
final String peerType = seed . get ( yacySeed . PEERTYPE , yacySeed . PEERTYPE_VIRGIN ) ;
2006-12-20 02:07:49 +01:00
if ( ( peerType . equals ( yacySeed . PEERTYPE_VIRGIN ) ) | | ( peerType . equals ( yacySeed . PEERTYPE_JUNIOR ) ) ) {
2005-04-07 21:19:42 +02:00
// reject unqualified seeds
2006-12-20 02:07:49 +01:00
yacyCore . log . logFine ( " connect: rejecting NOT QUALIFIED " + peerType + " seed " + seed . getName ( ) ) ;
return false ;
}
2006-02-27 23:25:27 +01:00
2006-12-21 14:14:13 +01:00
final yacySeed doubleSeed = this . seedDB . lookupByIP ( seed . getInetAddress ( ) , true , false , false ) ;
2006-12-20 02:07:49 +01:00
if ( ( doubleSeed ! = null ) & & ( doubleSeed . getPort ( ) = = seed . getPort ( ) ) & & ( ! ( doubleSeed . hash . equals ( seed . hash ) ) ) ) {
// a user frauds with his peer different peer hashes
yacyCore . log . logFine ( " connect: rejecting FRAUD (double hashes " + doubleSeed . hash + " / " + seed . hash + " on same port " + seed . getPort ( ) + " ) peer " + seed . getName ( ) ) ;
return false ;
}
2006-02-27 23:25:27 +01:00
2006-12-21 14:14:13 +01:00
if ( seed . get ( yacySeed . LASTSEEN , " " ) . length ( ) ! = 14 ) {
// hack for peers that do not have a LastSeen date
2007-02-03 00:54:27 +01:00
seed . setLastSeenUTC ( ) ;
2006-12-21 14:14:13 +01:00
yacyCore . log . logFine ( " connect: reset wrong date ( " + seed . getName ( ) + " / " + seed . hash + " ) " ) ;
}
// connection time
final long nowUTC0Time = System . currentTimeMillis ( ) ; // is better to have this value in a variable for debugging
2007-02-03 00:54:27 +01:00
long ctimeUTC0 = seed . getLastSeenUTC ( ) ;
2005-09-27 18:28:55 +02:00
2007-02-03 00:54:27 +01:00
if ( ctimeUTC0 > nowUTC0Time ) {
// the peer is future-dated, correct it
seed . setLastSeenUTC ( ) ;
ctimeUTC0 = nowUTC0Time ;
assert ( seed . getLastSeenUTC ( ) - ctimeUTC0 < 100 ) ;
}
2006-12-21 14:14:13 +01:00
if ( Math . abs ( nowUTC0Time - ctimeUTC0 ) > 60 * 60 * 24 * 1000 ) {
// the new connection is out-of-age, we reject the connection
2007-12-21 01:53:46 +01:00
yacyCore . log . logFine ( " connect: rejecting out-dated peer ' " + seed . getName ( ) + " ' from " + seed . getPublicAddress ( ) + " ; nowUTC0= " + nowUTC0Time + " , seedUTC0= " + ctimeUTC0 + " , TimeDiff= " + serverDate . formatInterval ( Math . abs ( nowUTC0Time - ctimeUTC0 ) ) ) ;
2006-12-21 14:14:13 +01:00
return false ;
}
// disconnection time
long dtimeUTC0 ;
final yacySeed disconnectedSeed = seedDB . getDisconnected ( seed . hash ) ;
if ( disconnectedSeed = = null ) {
dtimeUTC0 = 0 ; // never disconnected: virtually disconnected maximum time ago
} else {
2007-11-09 10:40:42 +01:00
dtimeUTC0 = disconnectedSeed . getLong ( " dct " , 0 ) ;
2006-12-21 14:14:13 +01:00
}
if ( direct ) {
// remember the moment
// Date applies the local UTC offset, which is wrong
// we correct that by subtracting the local offset and adding
// the remote offset.
2007-02-03 00:54:27 +01:00
seed . setLastSeenUTC ( ) ;
2006-12-21 14:14:13 +01:00
seed . setFlagDirectConnect ( true ) ;
} else {
// set connection flag
if ( Math . abs ( nowUTC0Time - ctimeUTC0 ) > 120000 ) seed . setFlagDirectConnect ( false ) ; // 2 minutes
}
// update latest version number
2007-04-27 11:23:44 +02:00
if ( seed . getVersion ( ) > yacyVersion . latestRelease ) yacyVersion . latestRelease = seed . getVersion ( ) ;
2006-12-21 14:14:13 +01:00
// prepare to update
if ( disconnectedSeed ! = null ) {
// if the indirect connect aims to announce a peer that we know
// has been disconnected then we compare the dates:
// if the new peer has a LastSeen date, and that date is before
// the disconnection date, then we ignore the new peer
if ( ! direct ) {
if ( ctimeUTC0 < dtimeUTC0 ) {
// the disconnection was later, we reject the connection
2007-04-30 00:05:34 +02:00
yacyCore . log . logFine ( " connect: rejecting disconnected peer ' " + seed . getName ( ) + " ' from " + seed . getPublicAddress ( ) ) ;
2006-12-21 14:14:13 +01:00
return false ;
2006-02-27 23:25:27 +01:00
}
2005-04-07 21:19:42 +02:00
}
2006-12-21 14:14:13 +01:00
// this is a return of a lost peer
2007-04-30 00:05:34 +02:00
yacyCore . log . logFine ( " connect: returned KNOWN " + peerType + " peer ' " + seed . getName ( ) + " ' from " + seed . getPublicAddress ( ) ) ;
2006-12-21 14:14:13 +01:00
this . seedDB . addConnected ( seed ) ;
return true ;
} else {
final yacySeed connectedSeed = this . seedDB . getConnected ( seed . hash ) ;
if ( connectedSeed ! = null ) {
// the seed is known: this is an update
try {
// if the old LastSeen date is later then the other
// info, then we reject the info
2007-02-03 00:54:27 +01:00
if ( ( ctimeUTC0 < ( connectedSeed . getLastSeenUTC ( ) ) ) & & ( ! direct ) ) {
2006-12-21 14:14:13 +01:00
yacyCore . log . logFine ( " connect: rejecting old info about peer ' " + seed . getName ( ) + " ' " ) ;
2005-04-07 21:19:42 +02:00
return false ;
}
2006-02-27 23:25:27 +01:00
2006-12-21 14:14:13 +01:00
if ( connectedSeed . getName ( ) ! = seed . getName ( ) ) {
// TODO: update seed name lookup cache
}
} catch ( NumberFormatException e ) {
2007-04-30 00:05:34 +02:00
yacyCore . log . logFine ( " connect: rejecting wrong peer ' " + seed . getName ( ) + " ' from " + seed . getPublicAddress ( ) + " . Cause: " + e . getMessage ( ) ) ;
2006-12-21 14:14:13 +01:00
return false ;
}
2007-04-30 00:05:34 +02:00
yacyCore . log . logFine ( " connect: updated KNOWN " + ( ( direct ) ? " direct " : " " ) + peerType + " peer ' " + seed . getName ( ) + " ' from " + seed . getPublicAddress ( ) ) ;
2006-02-27 23:25:27 +01:00
seedDB . addConnected ( seed ) ;
return true ;
} else {
2006-12-21 14:14:13 +01:00
// the seed is new
2007-10-01 14:30:23 +02:00
if ( ( seedDB . mySeedIsDefined ( ) ) & & ( seed . get ( yacySeed . IP , " 127.0.0.1 " ) . equals ( this . seedDB . mySeed ( ) . get ( yacySeed . IP , " 127.0.0.1 " ) ) ) ) {
2006-12-21 14:14:13 +01:00
// seed from the same IP as the calling client: can be
// the case if there runs another one over a NAT
2007-04-30 00:05:34 +02:00
yacyCore . log . logFine ( " connect: saved NEW seed (myself IP) " + seed . getPublicAddress ( ) ) ;
2005-04-07 21:19:42 +02:00
} else {
2006-12-21 14:14:13 +01:00
// completely new seed
2007-04-30 00:05:34 +02:00
yacyCore . log . logFine ( " connect: saved NEW " + peerType + " peer ' " + seed . getName ( ) + " ' from " + seed . getPublicAddress ( ) ) ;
2005-04-07 21:19:42 +02:00
}
2006-12-21 14:14:13 +01:00
if ( peerType . equals ( yacySeed . PEERTYPE_SENIOR ) )
this . seniorConnects + + ; // update statistics
if ( peerType . equals ( yacySeed . PEERTYPE_PRINCIPAL ) )
this . principalConnects + + ; // update statistics
this . seedDB . addConnected ( seed ) ;
return true ;
2005-04-07 21:19:42 +02:00
}
2006-12-21 14:14:13 +01:00
}
2005-04-07 21:19:42 +02:00
}
2007-09-12 02:42:53 +02:00
private final void disconnectPeer ( yacySeed seed , String cause ) {
2005-10-17 17:46:12 +02:00
// we do this if we did not get contact with the other peer
2007-09-12 02:42:53 +02:00
yacyCore . log . logFine ( " connect: no contact to a " + seed . get ( yacySeed . PEERTYPE , yacySeed . PEERTYPE_VIRGIN ) + " peer ' " + seed . getName ( ) + " ' at " + seed . getPublicAddress ( ) + " . Cause: " + cause ) ;
2005-10-17 17:46:12 +02:00
synchronized ( seedDB ) {
if ( ! seedDB . hasDisconnected ( seed . hash ) ) { disconnects + + ; }
2007-02-03 12:22:23 +01:00
seed . put ( " dct " , Long . toString ( System . currentTimeMillis ( ) ) ) ;
2005-10-17 17:46:12 +02:00
seedDB . addDisconnected ( seed ) ; // update info
}
2005-04-07 21:19:42 +02:00
}
2005-10-17 17:46:12 +02:00
2005-04-07 21:19:42 +02:00
public boolean peerArrival ( yacySeed peer , boolean direct ) {
2006-03-20 21:31:51 +01:00
if ( peer = = null ) return false ;
2005-04-07 21:19:42 +02:00
boolean res = connectPeer ( peer , direct ) ;
// perform all actions if peer is effective new
if ( res ) {
2008-01-22 20:10:03 +01:00
Iterator < yacyPeerAction > i = actions . iterator ( ) ;
while ( i . hasNext ( ) ) i . next ( ) . processPeerArrival ( peer , direct ) ;
2005-04-07 21:19:42 +02:00
}
return res ;
}
2007-09-12 02:42:53 +02:00
public void peerDeparture ( yacySeed peer , String cause ) {
2006-03-20 21:31:51 +01:00
if ( peer = = null ) return ;
2007-09-12 02:42:53 +02:00
disconnectPeer ( peer , cause ) ;
2005-04-07 21:19:42 +02:00
// perform all actions
2008-01-22 20:10:03 +01:00
Iterator < yacyPeerAction > i = actions . iterator ( ) ;
while ( i . hasNext ( ) ) i . next ( ) . processPeerDeparture ( peer ) ;
2005-04-07 21:19:42 +02:00
}
public void peerPing ( yacySeed peer ) {
2006-03-20 21:31:51 +01:00
if ( peer = = null ) return ;
2005-04-07 21:19:42 +02:00
// this is called only if the peer has junior status
seedDB . addPotential ( peer ) ;
// perform all actions
2008-01-22 20:10:03 +01:00
Iterator < yacyPeerAction > i = actions . iterator ( ) ;
while ( i . hasNext ( ) ) i . next ( ) . processPeerPing ( peer ) ;
2005-04-07 21:19:42 +02:00
}
2005-12-22 02:01:46 +01:00
public void setUserAgent ( String IP , String userAgent ) {
userAgents . put ( IP , userAgent ) ;
}
public String getUserAgent ( String IP ) {
String userAgent = ( String ) userAgents . get ( IP ) ;
return ( userAgent = = null ) ? " " : userAgent ;
}
2005-04-07 21:19:42 +02:00
}