1 /*
2  * Created on Jan 19, 2007
3  * Created by Paul Gardner
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  */
19 
20 
21 package com.aelitis.azureus.core.peermanager.messaging.azureus;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 
26 import org.gudy.azureus2.core3.util.DirectByteBuffer;
27 
28 import com.aelitis.azureus.core.peermanager.messaging.Message;
29 import com.aelitis.azureus.core.peermanager.messaging.MessageException;
30 import com.aelitis.azureus.core.peermanager.messaging.MessagingUtil;
31 
32 public class
33 AZRequestHint
34 	implements AZMessage
35 {
36 	private final byte version;
37 	private DirectByteBuffer buffer = null;
38 
39 	private int		piece_number;
40 	private int		offset;
41 	private int		length;
42 	private int		life;
43 
44 	public
AZRequestHint( int _piece_number, int _offset, int _length, int _life, byte _version )45 	AZRequestHint(
46 		int		_piece_number,
47 		int		_offset,
48 		int		_length,
49 		int		_life,
50 		byte	_version )
51 	{
52 		piece_number	= _piece_number;
53 		offset			= _offset;
54 		length			= _length;
55 		life			= _life;
56 		version			= _version;
57 	}
58 
59 	public String
getID()60 	getID()
61 	{
62 		return( AZMessage.ID_AZ_REQUEST_HINT );
63 	}
64 
65 	public byte[]
getIDBytes()66 	getIDBytes()
67 	{
68 		return( AZMessage.ID_AZ_REQUEST_HINT_BYTES );
69 	}
70 
71 	public String
getFeatureID()72 	getFeatureID()
73 	{
74 		return( AZMessage.AZ_FEATURE_ID );
75 	}
76 
77 	public int
getFeatureSubID()78 	getFeatureSubID()
79 	{
80 		return( AZMessage.SUBID_ID_AZ_REQUEST_HINT );
81 	}
82 
83 	public int
getType()84 	getType()
85 	{
86 		return( Message.TYPE_PROTOCOL_PAYLOAD );
87 	}
88 
getVersion()89 	public byte getVersion() { return version; };
90 
91 	public String
getDescription()92 	getDescription()
93 	{
94 		return( getID() + " piece #" + piece_number + ":" + offset + "->" + (offset + length -1) + "/" + life );
95 	}
96 
97 	public int
getPieceNumber()98 	getPieceNumber()
99 	{
100 		return( piece_number );
101 	}
102 
103 	public int
getOffset()104 	getOffset()
105 	{
106 		return( offset );
107 	}
108 	public int
getLength()109 	getLength()
110 	{
111 		return( length );
112 	}
113 	public int
getLife()114 	getLife()
115 	{
116 		return( life );
117 	}
118 
119 	public DirectByteBuffer[]
getData()120 	getData()
121 	{
122 		if ( buffer == null ){
123 
124 			Map	map = new HashMap();
125 
126 			map.put( "piece", new Long( piece_number ));
127 			map.put( "offset", new Long( offset ));
128 			map.put( "length", new Long( length ));
129 			map.put( "life", new Long( life ));
130 
131 			buffer = MessagingUtil.convertPayloadToBencodedByteStream( map, DirectByteBuffer.AL_MSG );
132 		}
133 
134 		return new DirectByteBuffer[]{ buffer };
135 	}
136 
137 	public Message
deserialize( DirectByteBuffer data, byte version )138 	deserialize(
139 		DirectByteBuffer 	data,
140 		byte				version )
141 
142 		throws MessageException
143 	{
144 		Map payload = MessagingUtil.convertBencodedByteStreamToPayload( data, 1, getID() );
145 
146 		int	piece_number	= ((Long)payload.get( "piece")).intValue();
147 		int	offset			= ((Long)payload.get( "offset")).intValue();
148 		int	length			= ((Long)payload.get( "length")).intValue();
149 		int	life			= ((Long)payload.get( "life" )).intValue();
150 
151 		return( new AZRequestHint( piece_number, offset, length, life, version ));
152 	}
153 
154 	public void
destroy()155 	destroy()
156 	{
157 		if ( buffer != null ){
158 
159 			buffer.returnToPool();
160 		}
161 	}
162 }
163