1 /*
2  * Created on Apr 30, 2004
3  * Created by Alon Rohter
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 package com.aelitis.azureus.core.peermanager.messaging.azureus;
21 
22 import java.util.*;
23 
24 import org.gudy.azureus2.core3.util.*;
25 
26 import com.aelitis.azureus.core.peermanager.messaging.*;
27 
28 
29 
30 /**
31  * This is a helper class for creating messages with a Map'd beencode-able payload.
32  */
33 public class AZGenericMapPayload implements AZMessage {
34   private final byte version;
35   private DirectByteBuffer buffer = null;
36 
37   private final String type_id;
38   private final Map msg_map;
39 
40 
41   /**
42    * Create a new AZ message with the given message type id, with the given bencode-able map payload.
43    * @param message_type of message
44    * @param message payload (to be bencoded)
45    */
AZGenericMapPayload( String message_type, Map message, byte version )46   public AZGenericMapPayload( String message_type, Map message, byte version ) {
47     this.type_id = message_type;
48     this.msg_map = message;
49     this.version = version;
50   }
51 
52 
getID()53   public String getID() {  return type_id;  }
getIDBytes()54   public byte[] getIDBytes() {  return type_id.getBytes();  }
55 
getFeatureID()56   public String getFeatureID() {  return AZMessage.AZ_FEATURE_ID;  }
57 
getFeatureSubID()58   public int getFeatureSubID() { return AZMessage.SUBID_AZ_GENERIC_MAP;  }
59 
60 
getType()61   public int getType() {  return Message.TYPE_PROTOCOL_PAYLOAD;  }
62 
getVersion()63   public byte getVersion() { return version; };
64 
getMapPayload()65   public Map getMapPayload() {  return msg_map;  }
66 
67 
getDescription()68   public String getDescription() {   return getID();  }
69 
70 
getData()71   public DirectByteBuffer[] getData() {
72     if( buffer == null ) {
73       buffer = MessagingUtil.convertPayloadToBencodedByteStream( msg_map, DirectByteBuffer.AL_MSG );
74     }
75     return new DirectByteBuffer[]{ buffer };
76   }
77 
78 
deserialize( DirectByteBuffer data, byte version )79   public Message deserialize( DirectByteBuffer data, byte version ) throws MessageException {
80     Map payload = MessagingUtil.convertBencodedByteStreamToPayload( data, 1, getID() );
81     return new AZGenericMapPayload( getID(), payload, version );
82   }
83 
84 
destroy()85   public void destroy() {
86     if( buffer != null )  buffer.returnToPool();
87   }
88 
89 }
90