1 /*
2  * File    : TRHostExternalTorrent.java
3  * Created : 19-Nov-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.core3.tracker.host.impl;
24 
25 /**
26  * @author parg
27  *
28  */
29 
30 import java.util.*;
31 import java.net.*;
32 import java.io.*;
33 
34 import org.gudy.azureus2.core3.internat.*;
35 import org.gudy.azureus2.core3.torrent.*;
36 import org.gudy.azureus2.core3.util.*;
37 
38 public class
39 TRHostExternalTorrent
40 	implements TOTorrent
41 {
42 	private byte[]		name;
43 	private byte[]		hash;
44 	private HashWrapper	hash_wrapper;
45 	private URL			announce_url;
46 
47 	protected Map		additional_properties = new HashMap();
48 
49 	protected AEMonitor this_mon 	= new AEMonitor( "TRHostExternalTorrent" );
50 
51 	protected
TRHostExternalTorrent( byte[] _hash, URL _announce_url )52 	TRHostExternalTorrent(
53 		byte[]	_hash,
54 		URL		_announce_url  )
55 	{
56 		hash			= _hash;
57 		hash_wrapper	= new HashWrapper( hash );
58 		announce_url	= _announce_url;
59 
60 		name = ByteFormatter.nicePrint( hash, true ).getBytes();
61 
62 		try{
63 			LocaleTorrentUtil.setDefaultTorrentEncoding( this );
64 
65 		}catch( LocaleUtilEncodingException e ){
66 
67 			Debug.printStackTrace( e );
68 		}
69 	}
70 
71 	public byte[]
getName()72 	getName()
73 	{
74 		return( name );
75 	}
76 
getUTF8Name()77 	public String getUTF8Name() {
78 		return null;
79 	}
80 
81 
82 	public boolean
isSimpleTorrent()83 	isSimpleTorrent()
84 	{
85 		return( true );
86 	}
87 
88 
89 	public byte[]
getComment()90 	getComment()
91 	{
92 		return( null );
93 	}
94 
95 	public void
setComment( String comment )96 	setComment(
97 		String		comment )
98 	{
99 	}
100 
101 
102 	public long
getCreationDate()103 	getCreationDate()
104 	{
105 		return(0);
106 	}
107 
108 	public void
setCreationDate( long date )109 	setCreationDate(
110 		long		date )
111 	{
112 	}
113 
114 	public byte[]
getCreatedBy()115 	getCreatedBy()
116 	{
117 		return( null );
118 	}
119 
120  	public void
setCreatedBy( byte[] cb )121 	setCreatedBy(
122 		byte[]		cb )
123    	{
124    	}
125 
126 	public boolean
isCreated()127 	isCreated()
128 	{
129 		return( false );
130 	}
131 
132 	public boolean
isDecentralised()133 	isDecentralised()
134 	{
135 		return( TorrentUtils.isDecentralised( getAnnounceURL()));
136 	}
137 
138 	public URL
getAnnounceURL()139 	getAnnounceURL()
140 	{
141 		return( announce_url );
142 	}
143 
144 	public boolean
setAnnounceURL( URL url )145 	setAnnounceURL(
146 		URL		url )
147 	{
148 		return false;
149 	}
150 
151 	public TOTorrentAnnounceURLGroup
getAnnounceURLGroup()152 	getAnnounceURLGroup()
153 	{
154 		return(
155 			new TOTorrentAnnounceURLGroup()
156 			{
157 				public TOTorrentAnnounceURLSet[]
158                	getAnnounceURLSets()
159 				{
160 					return( new TOTorrentAnnounceURLSet[0] );
161 				}
162 
163                	public void
164                	setAnnounceURLSets(
165                		TOTorrentAnnounceURLSet[]	sets )
166 				{
167 				}
168 
169                	public TOTorrentAnnounceURLSet
170                	createAnnounceURLSet(
171                		URL[]	urls )
172 				{
173 					return( new TOTorrentAnnounceURLSet()
174 							{
175 								public URL[]
176 						       	getAnnounceURLs()
177 								{
178 									return( new URL[0]);
179 								}
180 
181 						       	public void
182 						       	setAnnounceURLs(
183 						       		URL[]		urls )
184 								{
185 								}
186 							});
187 				}
188 			});
189 	}
190 
191 	public void
192 	addTorrentAnnounceURLSet(
193 		URL[] urls )
194 	{
195 	}
196 
197 
198 	public byte[][]
199 	getPieces()
200 	{
201 		return( new byte[0][] );
202 	}
203 
204 	public void
205 	setPieces(
206 		byte[][]	b )
207 	{
208 	}
209 
210 	public int
211 	getNumberOfPieces()
212 	{
213 		return( 0);
214 	}
215 
216 	public long
217 	getPieceLength()
218 	{
219 		return( -1 );
220 	}
221 
222 	public long
223 	getSize()
224 	{
225 		return( -1 );
226 	}
227 
228    	public int
229 	getFileCount()
230    	{
231    		return( 0 );
232    	}
233 
234 	public TOTorrentFile[]
235 	getFiles()
236 	{
237 		return( new TOTorrentFile[0]);
238 	}
239 
240 	public byte[]
241 	getHash()
242 
243 		throws TOTorrentException
244 	{
245 		return( hash );
246 	}
247 
248 	public HashWrapper
249 	getHashWrapper()
250 
251 		throws TOTorrentException
252 	{
253 		return( hash_wrapper );
254 	}
255 
256    	public void
257 	setHashOverride(
258 		byte[] hash )
259 
260 		throws TOTorrentException
261 	{
262 		throw( new TOTorrentException( "Not supported", TOTorrentException.RT_HASH_FAILS ));
263 	}
264 
265 	public boolean
266 	getPrivate()
267 	{
268 		return( false );
269 	}
270 
271 	public void
272 	setPrivate(
273 		boolean	_private )
274 
275 		throws TOTorrentException
276 	{
277 	}
278 
279 	public boolean
280 	hasSameHashAs(
281 		TOTorrent		other )
282 	{
283 		try{
284 			byte[]	other_hash = other.getHash();
285 
286 			return( Arrays.equals( hash, other_hash ));
287 
288 		}catch( TOTorrentException e ){
289 
290 			Debug.printStackTrace( e );
291 
292 			return( false );
293 		}
294 	}
295 
296 	public void
297 	setAdditionalStringProperty(
298 		String		name,
299 		String		value )
300 	{
301 		try{
302 
303 			additional_properties.put(name,value.getBytes(Constants.DEFAULT_ENCODING));
304 
305 		}catch( Throwable e ){
306 
307 			Debug.printStackTrace( e );
308 		}
309 	}
310 
311 	public String
312 	getAdditionalStringProperty(
313 		String		name )
314 	{
315 		try{
316 			Object	obj = additional_properties.get(name);
317 
318 			if ( obj == null ){
319 
320 				return( null );
321 			}
322 
323 			if ( !( obj instanceof byte[] )){
324 
325 				Debug.out( "property '" + name + "' is not a byte[]: " + obj );
326 
327 				return( null );
328 			}
329 
330 			return( new String((byte[])obj,Constants.DEFAULT_ENCODING));
331 
332 		}catch( Throwable e ){
333 
334 			Debug.printStackTrace( e );
335 
336 			return( null );
337 		}
338 	}
339 
340 	public void
341 	setAdditionalByteArrayProperty(
342 		String		name,
343 		byte[]		value )
344 	{
345 		additional_properties.put(name,value);
346 	}
347 
348 	public byte[]
349 	getAdditionalByteArrayProperty(
350 		String		name )
351 	{
352 		return( (byte[])additional_properties.get(name));
353 	}
354 
355 	public void
356 	setAdditionalLongProperty(
357 		String		name,
358 		Long		value )
359 	{
360 		additional_properties.put(name,value);
361 	}
362 
363 	public void
364 	setAdditionalProperty(
365 		String		name,
366 		Object		value )
367 	{
368 		if ( value instanceof String ){
369 
370 			setAdditionalStringProperty(name,(String)value);
371 
372 		}else{
373 
374 			additional_properties.put( name, value );
375 		}
376 	}
377 
378 	public Long
379 	getAdditionalLongProperty(
380 		String		name )
381 	{
382 		return((Long)additional_properties.get(name));
383 	}
384 
385 
386 	public void
387 	setAdditionalListProperty(
388 		String		name,
389 		List		value )
390 	{
391 		additional_properties.put(name,value);
392 	}
393 
394 	public List
395 	getAdditionalListProperty(
396 		String		name )
397 	{
398 		return((List)additional_properties.get(name));
399 	}
400 
401 	public void
402 	setAdditionalMapProperty(
403 		String	name,
404 		Map		value )
405 	{
406 		additional_properties.put(name,value);
407 	}
408 
409 	public Map
410 	getAdditionalMapProperty(
411 		String		name )
412 	{
413 		return( (Map)additional_properties.get(name));
414 	}
415 
416 	public Object
417 	getAdditionalProperty(
418 		String		name )
419 	{
420 		return( additional_properties.get(name));
421 	}
422 
423 	public void
424 	removeAdditionalProperty(
425 		String name )
426 	{
427 		additional_properties.remove( name );
428 	}
429 
430 	public void
431 	removeAdditionalProperties()
432 	{
433 		additional_properties.clear();
434 	}
435 
436 	public void
437 	serialiseToBEncodedFile(
438 		File		file )
439 
440 		throws TOTorrentException
441 	{
442 		throw( new TOTorrentException("External Torrent", TOTorrentException.RT_WRITE_FAILS ));
443 	}
444 
445 
446 	public Map
447 	serialiseToMap()
448 
449 		throws TOTorrentException
450 	{
451 		throw( new TOTorrentException("External Torrent", TOTorrentException.RT_WRITE_FAILS ));
452 	}
453 
454    public void
455    serialiseToXMLFile(
456 	   File		file )
457 
458 	   throws TOTorrentException
459 	{
460 		throw( new TOTorrentException("External Torrent", TOTorrentException.RT_WRITE_FAILS ));
461 	}
462 
463  	public void
464 	addListener(
465 		TOTorrentListener		l )
466 	{
467 	}
468 
469 	public void
470 	removeListener(
471 		TOTorrentListener		l )
472 	{
473 	}
474 
475    	public AEMonitor
476 	getMonitor()
477    	{
478    		return( this_mon );
479    	}
480 
481 	public void
482 	print()
483 	{
484 	}
485 }
486