1 /*
2  * File    : PEPeerSocket.java
3  * Created : 15-Oct-2003
4  * By      : Olivier
5  *
6  * Azureus - a Java Bittorrent client
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details ( see the LICENSE file ).
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 /*
24  * Created on 4 juil. 2003
25  *
26  */
27 package org.gudy.azureus2.core3.peer;
28 
29 
30 import java.net.InetAddress;
31 import java.util.List;
32 import java.util.Map;
33 
34 import org.gudy.azureus2.core3.disk.DiskManagerReadRequest;
35 import org.gudy.azureus2.plugins.network.Connection;
36 
37 import com.aelitis.azureus.core.networkmanager.LimitedRateGroup;
38 import com.aelitis.azureus.core.peermanager.messaging.Message;
39 import com.aelitis.azureus.core.peermanager.piecepicker.util.BitFlags;
40 import com.aelitis.azureus.core.tag.Taggable;
41 
42 
43 /**
44  * @author Olivier
45  * @author MjrTom
46  *			2005/Oct/08: lastPiece handling
47  *
48  */
49 
50 public interface
51 PEPeer
52 	extends Taggable
53 {
54 	public final static int CONNECTING 		= 10;
55 	public final static int HANDSHAKING 	= 20;
56 	public final static int TRANSFERING 	= 30;
57 	public final static int CLOSING      	= 40;
58 	public final static int DISCONNECTED 	= 50;
59 
60 
61 	// these should be maintained to match above list ordinals /10
62 	// if they don't than some debug info won't work right (not too big of a deal)
63 	public final static String[] StateNames = { "Twinkle",
64 		"Connecting", "Handshaking", "Transfering", "Closing", "Disconnected"
65 	};
66 
67 	public static final int MESSAGING_BT_ONLY = 1;
68 	public static final int MESSAGING_AZMP = 2;
69 	public static final int MESSAGING_LTEP = 3;
70 	//used for plugins, such as webseeds
71 	public static final int MESSAGING_EXTERN = 4;
72 
73 
74   /**
75    * Add peer listener.
76    * @param listener
77    */
addListener( PEPeerListener listener )78   public void addListener( PEPeerListener listener );
79 
80 
81   /**
82    * Remove peer listener.
83    * @param listener
84    */
removeListener( PEPeerListener listener )85   public void removeListener( PEPeerListener listener );
86 
87 
getPeerState()88 	public int getPeerState();	// from above set
89 
90 	public PEPeerManager
getManager()91 	getManager();
92 
93 	public String
getPeerSource()94 	getPeerSource();
95 
getId()96 	public byte[] getId();
97 
getIp()98 	public String getIp();
99 
100 	/**
101 	 * @return an ipv6 address under which the peer should be connectable if it announced one, null otherwise
102 	 */
getAlternativeIPv6()103 	public InetAddress getAlternativeIPv6();
104 
105   /**
106    * Get the peer's local TCP connection port.
107    * @return local port
108    */
getPort()109   public int getPort();
110 
111 	/**
112 	 * Gets the host name for the IP, if possible, IP as string otherwise
113    * @return hostname or IP
114    */
getIPHostName()115 	public String getIPHostName();
116 
117 
118   /**
119    * Get the TCP port this peer is listening for incoming connections on.
120    * @return TCP port, or 0 if port is unknown
121    */
getTCPListenPort()122   public int getTCPListenPort();
123 
124   /**
125    * Get the UDP port this peer is listening for incoming connections on.
126    * @return UDP port, or 0 if port is unknown
127    */
getUDPListenPort()128   public int getUDPListenPort();
129 
130   /**
131    * Get the UDP port this peer is listening on for non-data connections
132    * @return
133    */
134 
getUDPNonDataListenPort()135   public int getUDPNonDataListenPort();
136 
getAvailable()137 	public BitFlags getAvailable();
138 	/**
139 	 * @param pieceNumber int
140 	 * @return true if this peers makes this piece available
141 	 */
isPieceAvailable(int pieceNumber)142 	public boolean isPieceAvailable(int pieceNumber);
143 
144 	public boolean
transferAvailable()145 	transferAvailable();
146 
setSnubbed(boolean b)147 	public void setSnubbed(boolean b);	// explicit un-snub
148 
149   /**
150    * Is the peer choking me.
151    * @return true if I am choked by the peer, false if not
152    */
isChokingMe()153 	public boolean isChokingMe();
154 
isUnchokeOverride()155 	public boolean isUnchokeOverride();
156 
157   /**
158    * Am I choking the peer.
159    * @return true if the peer is choked, false if not
160    */
isChokedByMe()161 	public boolean isChokedByMe();
162 
163 	public void
sendChoke()164 	sendChoke();
165 
166 	public void
sendUnChoke()167 	sendUnChoke();
168 
169   /**
170    * Am I Interested in the peer.
171    * @return true if peer is interesting, false if not
172    */
isInteresting()173 	public boolean isInteresting();
174 
175   /**
176    * Is the peer Interested in me.
177    * @return true if the peer is interested in me, false if not
178    */
isInterested()179 	public boolean isInterested();
180 
181 	/**
182 	 * checks several factors within the object so the caller wouldn't need to
183 	 * for convienience and speed.
184 	 * @return true if none of several criteria indicate a request can't be made of the peer
185 	 */
isDownloadPossible()186 	public boolean isDownloadPossible();
187 
isSeed()188 	public boolean isSeed();
189 
190 	/**
191 	 * @return true if the peer is only uploading and can't use the data we have
192 	 *         to offer (we're seeding) or we can't use the data he has to offer
193 	 *         (we're downloading)
194 	 */
isRelativeSeed()195 	public boolean isRelativeSeed();
196 
isSnubbed()197 	public boolean isSnubbed();
198 
getSnubbedTime()199 	public long getSnubbedTime();
200 
getStats()201 	public PEPeerStats getStats();
202 
isIncoming()203 	public boolean isIncoming();
204 
hasReceivedBitField()205 	public boolean hasReceivedBitField();
206 
207   /**
208    * Get the peer's torrent completion percentage in thousand-notation,
209    * i.e. 53.7% is returned as the value 0537.
210    * @return the percentage the peer has complete
211    */
getPercentDoneInThousandNotation()212 	public int getPercentDoneInThousandNotation();
213 
214 
getClient()215 	public String getClient();
216 
isOptimisticUnchoke()217 	public boolean isOptimisticUnchoke();
setOptimisticUnchoke( boolean is_optimistic )218   public void setOptimisticUnchoke( boolean is_optimistic );
219 
220 	//Used in super-seed mode
221 	//The lower the better
setUploadHint(int timeToSpread)222 	public void setUploadHint(int timeToSpread);
223 
getUploadHint()224 	public int getUploadHint();
225 
setUniqueAnnounce(int uniquePieceNumber)226 	public void setUniqueAnnounce(int uniquePieceNumber);
227 
getUniqueAnnounce()228 	public int getUniqueAnnounce();
229 
getConsecutiveNoRequestCount()230 	public int getConsecutiveNoRequestCount();
setConsecutiveNoRequestCount( int num )231 	public void setConsecutiveNoRequestCount( int num );
232 
setUploadRateLimitBytesPerSecond( int bytes )233 	public void setUploadRateLimitBytesPerSecond( int bytes );
setDownloadRateLimitBytesPerSecond( int bytes )234 	public void setDownloadRateLimitBytesPerSecond( int bytes );
getUploadRateLimitBytesPerSecond()235 	public int getUploadRateLimitBytesPerSecond();
getDownloadRateLimitBytesPerSecond()236 	public int getDownloadRateLimitBytesPerSecond();
237 
238 	public void
addRateLimiter( LimitedRateGroup limiter, boolean upload )239 	addRateLimiter(
240 		LimitedRateGroup	limiter,
241 		boolean				upload );
242 
243 	public LimitedRateGroup[]
getRateLimiters( boolean upload )244 	getRateLimiters(
245 		boolean				upload );
246 
247 	public void
removeRateLimiter( LimitedRateGroup limiter, boolean upload )248 	removeRateLimiter(
249 		LimitedRateGroup	limiter,
250 		boolean				upload );
251 
252 	public void
setUploadDisabled( Object key, boolean disabled )253 	setUploadDisabled(
254 		Object				key,
255 		boolean				disabled );
256 
257 	public void
setDownloadDisabled( Object key, boolean disabled )258 	setDownloadDisabled(
259 		Object				key,
260 		boolean				disabled );
261 
262 	public boolean
isUploadDisabled()263 	isUploadDisabled();
264 
265 	public boolean
isDownloadDisabled()266 	isDownloadDisabled();
267 
268 	public void
updateAutoUploadPriority( Object key, boolean inc )269 	updateAutoUploadPriority(
270 		Object	key,
271 		boolean	inc );
272 
273   /** To retreive arbitrary objects against a peer. */
getData(String key)274   public Object getData (String key);
275   /** To store arbitrary objects against a peer. */
setData(String key, Object value)276   public void setData (String key, Object value);
277 
getUserData(Object key)278   public Object getUserData (Object key);
279   /** To store arbitrary objects against a peer. */
setUserData(Object key, Object value)280   public void setUserData (Object key, Object value);
281 
282   /**
283    * Get the connection that backs this peer.
284    * @return connection
285    */
286 
getPluginConnection()287   public Connection getPluginConnection();
288 
289 
290   /**
291    * Whether or not this peer supports the advanced messaging API.
292    * @return true if extended messaging is supported, false if not
293    */
supportsMessaging()294   public boolean supportsMessaging();
295 
296   /**
297    * @Return the handshaked messaging type, {@link PEPeer} constants
298    */
getMessagingMode()299   public int getMessagingMode();
300 
301 
302 
303   /**
304    * Returns name of encryption used by the peer
305    * @return
306    */
307   public String
getEncryption()308   getEncryption();
309 
310   public String
getProtocol()311   getProtocol();
312 
313   public String
getProtocolQualifier()314   getProtocolQualifier();
315 
316   /**
317    * Get the list of messages that this peer and us both understand.
318    * @return messages available for use, or null of supported is yet unknown or unavailable
319    */
getSupportedMessages()320   public Message[] getSupportedMessages();
321 
322   /**
323    * adds the reserved piece for piece picking by this peer
324    */
addReservedPieceNumber(int pieceNumber)325   public void addReservedPieceNumber(int pieceNumber);
326 
removeReservedPieceNumber(int pieceNumber)327   public void removeReservedPieceNumber(int pieceNumber);
328 
329   /**
330    * Get the reserved pieces for piece picking by this peer
331    */
getReservedPieceNumbers()332   public int[] getReservedPieceNumbers();
333 
getIncomingRequestCount()334   public int getIncomingRequestCount();
getOutgoingRequestCount()335   public int getOutgoingRequestCount();
336 
337   /**
338    * amount of data queued for delivery to peer
339    * @return
340    */
341 
getOutboundDataQueueSize()342   public int getOutboundDataQueueSize();
343 
344   /**
345    * get a list of piece numbers the peer has requested
346    * @return list of Long() representing the piece number requested, in order
347    */
getIncomingRequestedPieceNumbers()348   public int[] getIncomingRequestedPieceNumbers();
349 
350   /**
351    * get a list of piece numbers the we have requested from peer
352    * @return list of Long() representing the piece number requested, oldest
353    *          to newest
354    */
getOutgoingRequestedPieceNumbers()355   public int[] getOutgoingRequestedPieceNumbers();
356 
357   public int
getPercentDoneOfCurrentIncomingRequest()358   getPercentDoneOfCurrentIncomingRequest();
359 
360   public int
getPercentDoneOfCurrentOutgoingRequest()361   getPercentDoneOfCurrentOutgoingRequest();
362 
363   public long
getBytesRemaining()364   getBytesRemaining();
365 
366   	/**
367   	 * Enable suspended lazy bitfield for this peer - we will appear incomplete until this is disabled, at
368   	 * which point (well, fairly soon after) the bitfield will be completed
369   	 * @param enable
370   	 */
371 
372   public void
setSuspendedLazyBitFieldEnabled( boolean enable )373   setSuspendedLazyBitFieldEnabled(
374 		boolean	enable );
375 
376   /**
377    * Get the time since this connection was first established.
378    * NOTE: This method will always return 0 at any time before
379    * the underlying transport is fully connected, i.e. before
380    * handshaking begins.
381    * @return time count in ms
382    */
getTimeSinceConnectionEstablished()383   public long getTimeSinceConnectionEstablished();
384 
setLastPiece(int i)385 	public void setLastPiece(int i);
getLastPiece()386 	public int getLastPiece();
387 
388 	public boolean
isLANLocal()389 	isLANLocal();
390 
391 		/**
392 		 * Send a request hint to the peer.
393 		 * @param piece_number
394 		 * @param offset
395 		 * @param length
396 		 * @param life
397 		 * @return true if sent, false otherwise
398 		 */
399 
400 	public boolean
sendRequestHint( int piece_number, int offset, int length, int life )401 	sendRequestHint(
402 		int		piece_number,
403 		int		offset,
404 		int		length,
405 		int		life );
406 
407 		/**
408 		 * Get current request hint for a given piece for this peer.
409 		 * @return null if no hint int[]{ piece_number, offset, length } if hint found
410 		 */
411 
412 	public int[]
getRequestHint()413 	getRequestHint();
414 
415 	public void
clearRequestHint()416 	clearRequestHint();
417 
418 	public void
sendStatsRequest( Map request )419 	sendStatsRequest(
420 		Map		request );
421 
422 	public void
sendRejectRequest( DiskManagerReadRequest request )423 	sendRejectRequest(
424 		DiskManagerReadRequest	request );
425 
426 	public void
setHaveAggregationEnabled( boolean enabled )427 	setHaveAggregationEnabled(
428 		boolean		enabled );
429 
getHandshakeReservedBytes()430 	public byte[] getHandshakeReservedBytes();
431 
getClientNameFromPeerID()432 	public String getClientNameFromPeerID();
getClientNameFromExtensionHandshake()433 	public String getClientNameFromExtensionHandshake();
434 
isPriorityConnection()435 	public boolean isPriorityConnection();
436 
setPriorityConnection( boolean is_priority )437 	public void setPriorityConnection( boolean is_priority );
438 
439 	public boolean
isClosed()440 	isClosed();
441 }