1 /*
2  * File    : Peer.java
3  * Created : 01-Dec-2003
4  * By      : parg
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 package org.gudy.azureus2.plugins.peers;
24 
25 /**
26  * @author parg
27  *
28  */
29 
30 import java.util.List;
31 
32 import org.gudy.azureus2.core3.peer.PEPeer;
33 import org.gudy.azureus2.plugins.messaging.Message;
34 import org.gudy.azureus2.plugins.network.Connection;
35 import org.gudy.azureus2.plugins.network.ConnectionStub;
36 import org.gudy.azureus2.plugins.network.RateLimiter;
37 
38 
39 public interface
40 Peer
41 {
42 	public final static int CONNECTING 		= PEPeer.CONNECTING;
43 	public final static int HANDSHAKING 	= PEPeer.HANDSHAKING;
44 	public final static int TRANSFERING 	= PEPeer.TRANSFERING;
45 	public final static int CLOSING 		= PEPeer.CLOSING;
46 	public final static int DISCONNECTED 	= PEPeer.DISCONNECTED;
47 
48 
49 	public final static Object PR_PRIORITY_CONNECTION 	= new Object();
50 	public final static Object PR_PROTOCOL				= new Object();
51 	public final static Object PR_PROTOCOL_QUALIFIER	= new Object();
52 
53 	public void
bindConnection( ConnectionStub stub )54 	bindConnection(
55 		ConnectionStub		stub );
56 
57 	public PeerManager
getManager()58 	getManager();
59 
getState()60 	public int getState();	// from above set
61 
getId()62 	public byte[] getId();
63 
64 
65 	/**
66 	 * Get the peer's local TCP connection port.
67 	 * @return local port
68 	 */
69 
getIp()70 	public String getIp();
71 
72 	/**
73 	 * Get the TCP port this peer is listening for incoming connections on.
74 	 * @return TCP port, or 0 if port is unknown
75 	 */
getTCPListenPort()76 	public int getTCPListenPort();
77 
78 	/**
79 	 * Get the UDP port this peer is listening for incoming connections on.
80 	 * @return UDP port, or 0 if port is unknown
81 	 */
getUDPListenPort()82 	public int getUDPListenPort();
83 
84 	/**
85 	 * Get the UDP port this peer is listening on for non-data connections
86 	 * @return
87 	 */
88 
89 	public int
getUDPNonDataListenPort()90 	getUDPNonDataListenPort();
91 
getPort()92 	public int getPort();
93 
getAvailable()94 	public boolean[] getAvailable();
95 	/**
96 	 * @param pieceNumber int
97 	 * @return true if this peers makes this piece available
98 	 */
isPieceAvailable(int pieceNumber)99 	public boolean isPieceAvailable(int pieceNumber);
100 
101 	public boolean
isTransferAvailable()102 	isTransferAvailable();
103 
104 		/**
105 		 * Rate control - gives the maximum number of bytes that can be read from this
106 		 * connection at this time and returns the actual number read
107 		 * @param max
108 		 * @return
109 		 */
110 
111 	public int
readBytes( int max )112 	readBytes(
113 		int	max );
114 
115 	public int
writeBytes( int max )116 	writeBytes(
117 		int	max );
118 
119 	/**
120 	 * This is much list isTransferAvailable(), except is more comprehensive.
121 	 * That is; it checks a few more factors, within the object for speed,
122 	 * so that a more timely status is considered and the caller doesn't need
123 	 * to try to check each thing on it's own.
124 	 * @return true if several factors say downloading can be tried.
125 	 */
isDownloadPossible()126 	public boolean isDownloadPossible();
127 
isChoked()128 	public boolean isChoked();
129 
isChoking()130 	public boolean isChoking();
131 
isInterested()132 	public boolean isInterested();
133 
isInteresting()134 	public boolean isInteresting();
135 
isSeed()136 	public boolean isSeed();
137 
isSnubbed()138 	public boolean isSnubbed();
139 
getSnubbedTime()140 	public long getSnubbedTime();
141 
setSnubbed( boolean b)142 	public void setSnubbed( boolean b);
143 
getStats()144 	public PeerStats getStats();
145 
isIncoming()146 	public boolean isIncoming();
147 
148 		/**
149 		 * @deprecated This erroneously returns percent in 1000 (i.e. 100% = 1000 :) Therefore replaces
150 		 * with something more accurately named!
151 		 * @return
152 		 */
153 
getPercentDone()154 	public int getPercentDone();
155 
getPercentDoneInThousandNotation()156 	public int getPercentDoneInThousandNotation();
157 
getClient()158 	public String getClient();
159 
isOptimisticUnchoke()160 	public boolean isOptimisticUnchoke();
161 
setOptimisticUnchoke( boolean is_optimistic )162 	public void setOptimisticUnchoke( boolean is_optimistic );
163 
164 	public List
getExpiredRequests()165 	getExpiredRequests();
166 
167 	public List
getRequests()168 	getRequests();
169 
170 	public int
getMaximumNumberOfRequests()171 	getMaximumNumberOfRequests();
172 
173 	public int
getNumberOfRequests()174 	getNumberOfRequests();
175 
176 	public void
cancelRequest( PeerReadRequest request )177 	cancelRequest(
178 		PeerReadRequest	request );
179 
180 	public boolean
requestAllocationStarts( int[] base_priorities )181 	requestAllocationStarts(
182 		int[]	base_priorities );
183 
184 	public int[]
getPriorityOffsets()185 	getPriorityOffsets();
186 
187 	public void
requestAllocationComplete()188 	requestAllocationComplete();
189 
190 	public boolean
addRequest( PeerReadRequest request )191 	addRequest(
192 		PeerReadRequest	request );
193 
194 
195 	public void
close( String reason, boolean closedOnError, boolean attemptReconnect )196 	close(
197 		String 		reason,
198 		boolean 	closedOnError,
199 		boolean 	attemptReconnect );
200 
201 	public int
getPercentDoneOfCurrentIncomingRequest()202 	getPercentDoneOfCurrentIncomingRequest();
203 
204 	public int[]
getOutgoingRequestedPieceNumbers()205 	getOutgoingRequestedPieceNumbers();
206 
207 	public int
getOutgoingRequestCount()208 	getOutgoingRequestCount();
209 
210 	public int
getPercentDoneOfCurrentOutgoingRequest()211 	getPercentDoneOfCurrentOutgoingRequest();
212 
213   /**
214    * Add peer listener.
215    * @param listener
216    * @deprecated use addListener( PeerListener2 )
217    */
addListener( PeerListener listener )218 	public void	addListener( PeerListener	listener );
219 
220 
221   /**
222    * Remove peer listener.
223    * @param listener
224    * @deprecated use removeListener( PeerListener2 )
225    */
removeListener( PeerListener listener )226 	public void removeListener(	PeerListener listener );
227 
228 	  /**
229 	   * Add peer listener.
230 	   * @param listener
231 	   */
addListener( PeerListener2 listener )232 	public void	addListener( PeerListener2	listener );
233 
234 
235   /**
236    * Remove peer listener.
237    * @param listener
238    */
removeListener( PeerListener2 listener )239 	public void removeListener(	PeerListener2 listener );
240 
241 
242   /**
243    * Get the network connection that backs this peer.
244    * @return connection
245    */
getConnection()246   public Connection getConnection();
247 
248 
249   /**
250    * Whether or not this peer supports the advanced messaging API.
251    * @return true if extended messaging is supported, false if not
252    */
supportsMessaging()253   public boolean supportsMessaging();
254 
255 
256   /**
257    * Get the list of messages that this peer and us mutually understand.
258    * @return messages available for use, or null of supported is yet unknown
259    */
getSupportedMessages()260   public Message[] getSupportedMessages();
261 
262   public void
setUserData( Object key, Object value )263   setUserData(
264 	Object	key,
265 	Object	value );
266 
267   public Object
getUserData( Object key )268   getUserData(
269 	Object	key );
270 
getHandshakeReservedBytes()271   public byte[] getHandshakeReservedBytes();
272 
273   public boolean
isPriorityConnection()274   isPriorityConnection();
275 
276   public void
setPriorityConnection( boolean is_priority )277   setPriorityConnection(
278 		boolean		is_priority );
279 
280 	  /**
281 	   * @since 4.7.0.3
282 	   * @param limiter		create via ConnectionManager
283 	   * @param is_upload		false -> download limit
284 	   */
285 
286   public void
addRateLimiter( RateLimiter limiter, boolean is_upload )287   addRateLimiter(
288 		  RateLimiter		limiter,
289 		  boolean			is_upload );
290 
291   public void
removeRateLimiter( RateLimiter limiter, boolean is_upload )292   removeRateLimiter(
293 		  RateLimiter		limiter,
294 		  boolean			is_upload );
295 
296   public RateLimiter[]
getRateLimiters( boolean is_upload )297   getRateLimiters(
298    		 boolean	is_upload );
299 }
300