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.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 
28 import org.gudy.azureus2.core3.util.DirectByteBuffer;
29 
30 import com.aelitis.azureus.core.peermanager.messaging.Message;
31 import com.aelitis.azureus.core.peermanager.messaging.MessageException;
32 import com.aelitis.azureus.core.peermanager.messaging.MessagingUtil;
33 
34 public class
35 AZHave
36 	implements AZMessage
37 {
38 	private final byte version;
39 	private DirectByteBuffer buffer = null;
40 
41 	private int[]	piece_numbers;
42 
43 	public
AZHave( int[] _piece_numbers, byte _version )44 	AZHave(
45 		int[]	_piece_numbers,
46 		byte	_version )
47 	{
48 		piece_numbers	= _piece_numbers;
49 		version			= _version;
50 	}
51 
52 	public String
getID()53 	getID()
54 	{
55 		return( AZMessage.ID_AZ_HAVE );
56 	}
57 
58 	public byte[]
getIDBytes()59 	getIDBytes()
60 	{
61 		return( AZMessage.ID_AZ_HAVE_BYTES );
62 	}
63 
64 	public String
getFeatureID()65 	getFeatureID()
66 	{
67 		return( AZMessage.AZ_FEATURE_ID );
68 	}
69 
70 	public int
getFeatureSubID()71 	getFeatureSubID()
72 	{
73 		return( AZMessage.SUBID_ID_AZ_HAVE );
74 	}
75 
76 	public int
getType()77 	getType()
78 	{
79 		return( Message.TYPE_PROTOCOL_PAYLOAD );
80 	}
81 
getVersion()82 	public byte getVersion() { return version; };
83 
84 	public String
getDescription()85 	getDescription()
86 	{
87 		StringBuffer	str = new StringBuffer(piece_numbers.length*10);
88 
89 		for (int i=0;i<piece_numbers.length;i++){
90 
91 			if ( i > 0 ){
92 				str.append(",");
93 			}
94 
95 			str.append( piece_numbers[i] );
96 		}
97 
98 		return( getID() + " " + str );
99 	}
100 
101 	public int[]
getPieceNumbers()102 	getPieceNumbers()
103 	{
104 		return( piece_numbers );
105 	}
106 
107 	public DirectByteBuffer[]
getData()108 	getData()
109 	{
110 		if ( buffer == null ){
111 
112 			Map	map = new HashMap();
113 
114 			List l = new ArrayList( piece_numbers.length );
115 
116 			for (int i=0;i<piece_numbers.length;i++){
117 
118 				l.add( new Long( piece_numbers[i] ));
119 			}
120 
121 			map.put( "pieces", l );
122 
123 			buffer = MessagingUtil.convertPayloadToBencodedByteStream( map, DirectByteBuffer.AL_MSG );
124 		}
125 
126 		return new DirectByteBuffer[]{ buffer };
127 	}
128 
129 	public Message
deserialize( DirectByteBuffer data, byte version )130 	deserialize(
131 		DirectByteBuffer 	data,
132 		byte				version )
133 
134 		throws MessageException
135 	{
136 		Map payload = MessagingUtil.convertBencodedByteStreamToPayload( data, 1, getID() );
137 
138 		List	l	= (List)payload.get( "pieces");
139 
140 		int[]	pieces = new int[l.size()];
141 
142 		for (int i=0;i<pieces.length;i++){
143 
144 			pieces[i] = ((Long)l.get(i)).intValue();
145 		}
146 
147 		AZHave message =  new AZHave( pieces, version );
148 
149 		return( message );
150 	}
151 
152 	public void
destroy()153 	destroy()
154 	{
155 		if ( buffer != null ){
156 
157 			buffer.returnToPool();
158 		}
159 	}
160 }
161