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.BufferedReader ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.net.URL ;
2005-10-05 12:45:33 +02:00
import java.util.ArrayList ;
2005-05-05 07:32:19 +02:00
import java.util.Date ;
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 ;
import de.anomic.http.httpHeader ;
import de.anomic.http.httpc ;
import de.anomic.plasma.plasmaSwitchboard ;
import de.anomic.server.serverCore ;
2005-08-03 02:52:32 +02:00
import de.anomic.server.serverDate ;
2005-05-05 07:32:19 +02:00
import de.anomic.server.serverSystem ;
import de.anomic.tools.disorderSet ;
2005-10-17 17:46:12 +02:00
import de.anomic.yacy.yacySeed ;
2005-04-07 21:19:42 +02:00
public class yacyPeerActions {
private yacySeedDB seedDB ;
private plasmaSwitchboard sb ;
private HashSet actions ;
2005-12-22 02:01:46 +01:00
private HashMap userAgents ;
2005-04-07 21:19:42 +02:00
private File superseedFile ;
private String superseedURL ;
public long juniorConnects ;
public long seniorConnects ;
public long principalConnects ;
public long disconnects ;
2005-12-07 00:51:29 +01:00
public yacyPeerActions ( yacySeedDB seedDB , plasmaSwitchboard switchboard , File superseedFile , String superseedURL ) {
2005-04-07 21:19:42 +02:00
this . seedDB = seedDB ;
this . sb = switchboard ;
this . actions = new HashSet ( ) ;
2005-12-22 02:01:46 +01:00
this . userAgents = new HashMap ( ) ;
2005-04-07 21:19:42 +02:00
this . superseedFile = superseedFile ;
2005-12-22 02:01:46 +01:00
this . superseedURL = superseedURL ;
2005-04-07 21:19:42 +02:00
this . superseedURL = superseedURL ;
this . juniorConnects = 0 ;
this . seniorConnects = 0 ;
this . principalConnects = 0 ;
this . disconnects = 0 ;
}
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
2005-08-28 18:55:52 +02:00
String newPeerName = serverCore . publicIP ( ) + yacyCore . speedKey + serverSystem . infoKey ( ) + ( System . currentTimeMillis ( ) & 99 ) ;
2005-07-31 03:54:46 +02:00
newPeerName = newPeerName . replace ( '.' , '-' ) ;
newPeerName = newPeerName . replace ( '_' , '-' ) ;
2005-07-29 10:48:36 +02:00
sb . setConfig ( " peerName " , newPeerName ) ;
}
2005-10-17 17:46:12 +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 ) ) {
2005-10-17 17:46:12 +02:00
seedDB . mySeed . put ( yacySeed . PORT , Integer . toString ( serverCore . portForwarding . getPort ( ) ) ) ;
2005-06-16 09:28:07 +02:00
} else {
2005-10-17 17:46:12 +02:00
seedDB . mySeed . put ( yacySeed . PORT , sb . getConfig ( " port " , " 8080 " ) ) ;
2005-06-16 09:28:07 +02:00
}
2005-09-27 18:28:55 +02:00
long uptime = ( ( System . currentTimeMillis ( ) - Long . parseLong ( sb . getConfig ( " startupTime " , " 0 " ) ) ) / 1000 ) / 60 ;
2005-07-18 02:44:51 +02:00
long indexedc = sb . getThread ( " 80_indexing " ) . getBusyCycles ( ) ;
2005-10-17 17:46:12 +02:00
seedDB . mySeed . put ( yacySeed . ISPEED , ( ( indexedc = = 0 ) | | ( uptime = = 0 ) ) ? " unknown " : Long . toString ( indexedc / uptime ) ) ; // the speed of indexing (pages/minute) of the peer
seedDB . mySeed . put ( yacySeed . UPTIME , Long . toString ( uptime ) ) ; // the number of minutes that the peer is up in minutes/day (moving average MA30)
seedDB . mySeed . put ( yacySeed . LCOUNT , Integer . toString ( sb . urlPool . loadedURL . size ( ) ) ) ; // the number of links that the peer has stored (LURL's)
seedDB . mySeed . put ( yacySeed . NCOUNT , Integer . toString ( sb . urlPool . noticeURL . stackSize ( ) ) ) ; // the number of links that the peer has noticed, but not loaded (NURL's)
seedDB . mySeed . put ( yacySeed . ICOUNT , Integer . toString ( sb . cacheSizeMin ( ) ) ) ; // 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
seedDB . mySeed . put ( " seedURL " , sb . getConfig ( " seedURL " , " " ) ) ;
}
seedDB . mySeed . setFlagDirectConnect ( true ) ;
2005-10-17 17:46:12 +02:00
seedDB . mySeed . put ( yacySeed . LASTSEEN , yacyCore . universalDateShortString ( new Date ( ) ) ) ;
seedDB . mySeed . put ( yacySeed . UTC , serverDate . UTCDiffString ( ) ) ;
2005-06-16 09:28:07 +02:00
seedDB . mySeed . setFlagAcceptRemoteCrawl ( sb . getConfig ( " crawlResponse " , " " ) . equals ( " true " ) ) ;
seedDB . mySeed . setFlagAcceptRemoteIndex ( sb . getConfig ( " allowReceiveIndex " , " " ) . equals ( " true " ) ) ;
//mySeed.setFlagAcceptRemoteIndex(true);
2005-04-07 21:19:42 +02:00
}
public void saveMySeed ( ) {
try {
seedDB . mySeed . save ( seedDB . myOwnSeedFile ) ;
} 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
2005-10-23 12:35:05 +02:00
yacySeed ys ;
String seedListFileURL ;
2005-04-07 21:19:42 +02:00
URL url ;
2005-10-23 12:35:05 +02:00
ArrayList seedList ;
Iterator enu ;
2005-04-07 21:19:42 +02:00
int lc ;
int sc = seedDB . sizeConnected ( ) ;
httpHeader header ;
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
// - load the superseed: a list of URL's
2005-10-23 12:35:05 +02:00
disorderSet superseed = loadSuperseed ( superseedFile , superseedURL ) ;
2005-04-07 21:19:42 +02:00
// - use the superseed to further fill up the seedDB
int ssc = 0 ;
2005-10-23 12:35:05 +02:00
for ( int i = 0 ; i < superseed . size ( ) ; i + + ) {
if ( Thread . currentThread ( ) . isInterrupted ( ) ) break ;
seedListFileURL = ( String ) superseed . any ( ) ;
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 " ) ;
2005-04-07 21:19:42 +02:00
url = new URL ( seedListFileURL ) ;
2005-10-23 12:35:05 +02:00
header = httpc . whead ( url , 5000 , null , null , this . sb . remoteProxyConfig , reqHeader ) ;
2005-04-07 21:19:42 +02:00
if ( ( header = = null ) | | ( header . lastModified ( ) = = null ) ) {
2005-08-20 14:09:27 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: seed-list URL " + seedListFileURL + " not available " ) ;
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 + + ;
2005-10-23 12:35:05 +02:00
seedList = httpc . wget ( url , 5000 , null , null , this . sb . remoteProxyConfig , reqHeader ) ;
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 ( ) ) {
ys = yacySeed . genRemoteSeed ( ( String ) enu . next ( ) , null ) ;
2005-07-31 13:11:29 +02:00
if ( ( ys ! = null ) & & ( ys . isProper ( ) = = null ) & &
2005-10-23 12:35:05 +02:00
( ( seedDB . mySeed = = null ) | | ( 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
}
2005-10-23 12:35:05 +02:00
} catch ( Exception e ) {
// this is when wget fails; may be because of missing internet connection
// we do nothing here and go silently over it
2005-08-30 23:32:59 +02:00
yacyCore . log . logSevere ( " BOOTSTRAP: failed to load seeds from seed-list URL " + seedListFileURL ) ;
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. " ) ;
}
private disorderSet loadSuperseed ( File local , String url ) {
2005-06-16 10:34:52 +02:00
// this returns a list of locations where seed list-files can be found
disorderSet supsee = new disorderSet ( ) ;
String line ;
// read in local file
2005-04-07 21:19:42 +02:00
int lc = 0 ;
2005-06-16 10:34:52 +02:00
BufferedReader br = null ;
2005-04-07 21:19:42 +02:00
try {
2005-06-16 10:34:52 +02:00
br = new BufferedReader ( new InputStreamReader ( new FileInputStream ( local ) ) ) ;
while ( ( line = br . readLine ( ) ) ! = null ) {
line = line . trim ( ) ;
//System.out.println("one line in file:" + line);
if ( line . length ( ) > 0 ) supsee . add ( line ) ;
}
br . close ( ) ;
2005-04-07 21:19:42 +02:00
lc = supsee . size ( ) ;
2005-08-07 11:26:23 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: " + lc + " seed-list URLs from superseed file " + local . toString ( ) ) ;
2005-06-16 10:34:52 +02:00
} catch ( IOException e ) {
//e.printStackTrace();
supsee = new disorderSet ( ) ;
2005-08-07 11:26:23 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: failed to load seed-list URLs from superseed file " + local . toString ( ) + " : " + e . getMessage ( ) ) ;
2005-06-16 10:34:52 +02:00
} finally {
if ( br ! = null ) try { br . close ( ) ; } catch ( Exception e ) { }
}
// read in remote file from url
2005-04-07 21:19:42 +02:00
try {
2005-10-22 15:28:04 +02:00
ArrayList remote = httpc . wget ( new URL ( url ) , 5000 , null , null , this . sb . remoteProxyConfig ) ;
2005-04-07 21:19:42 +02:00
if ( ( remote ! = null ) & & ( remote . size ( ) > 0 ) ) {
2005-10-05 12:45:33 +02:00
Iterator e = remote . iterator ( ) ;
while ( e . hasNext ( ) ) {
line = ( String ) e . next ( ) ;
2005-04-07 21:19:42 +02:00
if ( line ! = null ) {
line = line . trim ( ) ;
supsee . add ( line ) ;
}
}
}
2005-08-07 11:26:23 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: " + ( supsee . size ( ) - lc ) + " seed-list URLs from superseed URL " + url ) ;
2005-04-07 21:19:42 +02:00
} catch ( Exception e ) {
supsee = new disorderSet ( ) ;
2005-08-07 11:26:23 +02:00
yacyCore . log . logInfo ( " BOOTSTRAP: failed to load seed-list URLs from superseed URL " + url + " : " + e . getMessage ( ) ) ;
2005-04-07 21:19:42 +02:00
}
return supsee ;
}
synchronized public boolean connectPeer ( yacySeed seed , boolean direct ) {
// store a remote peer's seed
// returns true if the peer is new and previously unknown
2005-07-31 13:11:29 +02:00
String error ;
if ( seed = = null ) {
2005-08-30 23:32:59 +02:00
yacyCore . log . logSevere ( " connect: WRONG seed (NULL) " ) ;
2005-04-07 21:19:42 +02:00
return false ;
2005-07-31 13:11:29 +02:00
} else if ( ( error = seed . isProper ( ) ) ! = null ) {
2005-08-30 23:32:59 +02:00
yacyCore . log . logSevere ( " connect: WRONG seed ( " + seed . getName ( ) + " / " + seed . hash + " ): " + error ) ;
2005-04-07 21:19:42 +02:00
return false ;
} else if ( ( seedDB . mySeed ! = null ) & & ( seed . hash . equals ( seedDB . mySeed . hash ) ) ) {
yacyCore . log . logInfo ( " connect: SELF reference " + seed . getAddress ( ) ) ;
return false ;
} else {
2005-10-17 17:46:12 +02:00
String peerType = seed . get ( yacySeed . PEERTYPE , yacySeed . PEERTYPE_VIRGIN ) ;
2005-04-07 21:19:42 +02:00
// reject unqualified seeds
2005-10-17 17:46:12 +02:00
if ( ( peerType . equals ( yacySeed . PEERTYPE_VIRGIN ) ) | | ( peerType . equals ( yacySeed . PEERTYPE_JUNIOR ) ) ) {
2005-08-30 23:10:39 +02:00
yacyCore . log . logFine ( " connect: rejecting NOT QUALIFIED " + peerType + " seed " + seed . getName ( ) ) ;
2005-04-07 21:19:42 +02:00
return false ;
}
// we may store that seed, but still have different cases
2005-10-17 17:46:12 +02:00
if ( seed . get ( yacySeed . LASTSEEN , " " ) . length ( ) < 14 ) {
2005-04-07 21:19:42 +02:00
// hack for peers that do not have a LastSeen date
2005-10-17 17:46:12 +02:00
seed . put ( yacySeed . LASTSEEN , " 20040101000000 " ) ;
2005-04-07 21:19:42 +02:00
}
// connection time
2005-09-27 18:28:55 +02:00
long nowUTC0Time = System . currentTimeMillis ( ) ; // is better to have this value in a variable for debugging
long ctimeUTC0 ;
ctimeUTC0 = seed . getLastSeenTime ( ) ;
// maybe correct it slightly
/ *
2005-04-07 21:19:42 +02:00
if ( ctime > yacyCore . universalTime ( ) ) {
ctime = ( ( 2 * ctime ) + yacyCore . universalTime ( ) ) / 3 ;
2005-10-17 17:46:12 +02:00
seed . put ( yacySeed . LASTSEEN , yacyCore . shortFormatter . format ( new Date ( ctime ) ) ) ;
2005-04-07 21:19:42 +02:00
}
2005-09-27 18:28:55 +02:00
* /
if ( Math . abs ( nowUTC0Time - ctimeUTC0 ) > 3600000 ) {
2005-07-03 15:08:49 +02:00
// the new connection is out-of-age, we reject the connection
2005-09-27 18:28:55 +02:00
yacyCore . log . logFine ( " connect: rejecting out-dated peer ' " + seed . getName ( ) + " ' from " + seed . getAddress ( ) +
2005-10-20 13:43:43 +02:00
" ; nowUTC0= " + nowUTC0Time + " , seedUTC0= " + ctimeUTC0 + " , TimeDiff= " + serverDate . intervalToString ( Math . abs ( nowUTC0Time - ctimeUTC0 ) ) ) ;
2005-07-03 15:08:49 +02:00
return false ;
}
2005-04-07 21:19:42 +02:00
// disconnection time
2005-09-27 18:28:55 +02:00
long dtimeUTC0 ;
2005-04-07 21:19:42 +02:00
yacySeed disconnectedSeed = seedDB . getDisconnected ( seed . hash ) ;
if ( disconnectedSeed = = null ) {
2005-09-27 18:28:55 +02:00
dtimeUTC0 = 0 ; // never disconnected: virtually disconnected maximum time ago
2005-04-07 21:19:42 +02:00
} else try {
2005-12-07 00:51:29 +01:00
dtimeUTC0 = yacyCore . shortFormatter . parse ( disconnectedSeed . get ( " disconnected " , " 20040101000000 " ) ) . getTime ( ) - seed . getUTCDiff ( ) ;
2005-04-07 21:19:42 +02:00
} catch ( java . text . ParseException e ) {
2005-09-27 18:28:55 +02:00
dtimeUTC0 = 0 ;
2005-04-07 21:19:42 +02:00
}
if ( direct ) {
// remember the moment
2005-09-27 18:28:55 +02:00
// Date applies the local UTC offset, which is wrong
// we correct that by subtracting the local offset and adding the remote offset.
seed . setLastSeenTime ( ) ;
2005-04-07 21:19:42 +02:00
seed . setFlagDirectConnect ( true ) ;
} else {
// set connection flag
2005-09-27 18:28:55 +02:00
if ( Math . abs ( nowUTC0Time - ctimeUTC0 ) > 120000 ) seed . setFlagDirectConnect ( false ) ; // 2 minutes
2005-04-07 21:19:42 +02:00
}
2005-07-03 14:40:36 +02:00
// update latest version number
if ( seed . getVersion ( ) > yacyCore . latestVersion ) yacyCore . latestVersion = seed . getVersion ( ) ;
2005-04-07 21:19:42 +02: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 ) ) {
2005-09-27 18:28:55 +02:00
if ( ctimeUTC0 < dtimeUTC0 ) {
2005-04-07 21:19:42 +02:00
// the disconnection was later, we reject the connection
2005-08-30 23:10:39 +02:00
yacyCore . log . logFine ( " connect: rejecting disconnected peer ' " + seed . getName ( ) + " ' from " + seed . getAddress ( ) ) ;
2005-04-07 21:19:42 +02:00
return false ;
}
}
// this is a return of a lost peer
2005-08-30 23:10:39 +02:00
yacyCore . log . logFine ( " connect: returned KNOWN " + peerType + " peer ' " + seed . getName ( ) + " ' from " + seed . getAddress ( ) ) ;
2005-04-07 21:19:42 +02:00
seedDB . addConnected ( seed ) ;
2005-07-30 09:15:39 +02:00
return true ;
2005-04-07 21:19:42 +02:00
} else {
yacySeed connectedSeed = 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
2005-10-17 17:46:12 +02:00
if ( ( ctimeUTC0 < ( yacyCore . shortFormatter . parse ( connectedSeed . get ( yacySeed . LASTSEEN , " 20040101000000 " ) ) . getTime ( ) - connectedSeed . getUTCDiff ( ) + serverDate . UTCDiff ( ) ) ) & & ( ! ( direct ) ) ) {
2005-08-30 23:10:39 +02:00
yacyCore . log . logFine ( " connect: rejecting old info about peer ' " + seed . getName ( ) + " ' " ) ;
2005-04-07 21:19:42 +02:00
return false ;
}
2005-10-12 10:17:43 +02:00
if ( connectedSeed . getName ( ) ! = seed . getName ( ) ) {
// TODO: update seed name lookup cache
}
2005-04-07 21:19:42 +02:00
} catch ( java . text . ParseException e ) { }
2005-08-30 23:10:39 +02:00
yacyCore . log . logFine ( " connect: updated KNOWN " + ( ( direct ) ? " direct " : " " ) + peerType + " peer ' " + seed . getName ( ) + " ' from " + seed . getAddress ( ) ) ;
2005-04-07 21:19:42 +02:00
seedDB . addConnected ( seed ) ;
2005-07-30 09:15:39 +02:00
return true ;
2005-04-07 21:19:42 +02:00
} else {
// the seed is new
2005-12-07 00:51:29 +01:00
if ( seed . get ( yacySeed . IP , " 127.0.0.1 " ) . equals ( seedDB . mySeed . get ( yacySeed . IP , " 127.0.0.1 " ) ) ) {
2005-04-07 21:19:42 +02:00
// seed from the same IP as the calling client: can be the case if there runs another one over a NAT
2005-08-30 23:10:39 +02:00
yacyCore . log . logFine ( " connect: saved NEW seed (myself IP) " + seed . getAddress ( ) ) ;
2005-04-07 21:19:42 +02:00
} else {
// completely new seed
2005-08-30 23:10:39 +02:00
yacyCore . log . logFine ( " connect: saved NEW " + peerType + " peer ' " + seed . getName ( ) + " ' from " + seed . getAddress ( ) ) ;
2005-04-07 21:19:42 +02:00
}
2005-10-17 17:46:12 +02:00
if ( peerType . equals ( yacySeed . PEERTYPE_SENIOR ) ) seniorConnects + + ; // update statistics
if ( peerType . equals ( yacySeed . PEERTYPE_PRINCIPAL ) ) principalConnects + + ; // update statistics
2005-04-07 21:19:42 +02:00
seedDB . addConnected ( seed ) ;
return true ;
}
}
}
}
2005-10-17 17:46:12 +02:00
public void disconnectPeer ( yacySeed seed ) {
// we do this if we did not get contact with the other peer
yacyCore . log . logFine ( " connect: no contact to a " + seed . get ( yacySeed . PEERTYPE , yacySeed . PEERTYPE_VIRGIN ) + " peer ' " + seed . getName ( ) + " ' at " + seed . getAddress ( ) ) ;
synchronized ( seedDB ) {
if ( ! seedDB . hasDisconnected ( seed . hash ) ) { disconnects + + ; }
seed . put ( " disconnected " , yacyCore . universalDateShortString ( new Date ( ) ) ) ;
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 ) {
boolean res = connectPeer ( peer , direct ) ;
// perform all actions if peer is effective new
if ( res ) {
Iterator i = actions . iterator ( ) ;
while ( i . hasNext ( ) ) ( ( yacyPeerAction ) i . next ( ) ) . processPeerArrival ( peer , direct ) ;
}
return res ;
}
public void peerDeparture ( yacySeed peer ) {
//System.out.println("PEER DEPARTURE:" + peer.toString());
disconnectPeer ( peer ) ;
// perform all actions
Iterator i = actions . iterator ( ) ;
while ( i . hasNext ( ) ) ( ( yacyPeerAction ) i . next ( ) ) . processPeerDeparture ( peer ) ;
}
public void peerPing ( yacySeed peer ) {
// this is called only if the peer has junior status
seedDB . addPotential ( peer ) ;
// perform all actions
Iterator i = actions . iterator ( ) ;
while ( i . hasNext ( ) ) ( ( yacyPeerAction ) i . next ( ) ) . processPeerPing ( peer ) ;
}
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
}