1 /*
2  * Created on Jan 11, 2005
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.networkmanager.impl;
21 
22 import org.gudy.azureus2.core3.util.DirectByteBuffer;
23 
24 import com.aelitis.azureus.core.networkmanager.RawMessage;
25 import com.aelitis.azureus.core.peermanager.messaging.Message;
26 import com.aelitis.azureus.core.peermanager.messaging.MessageException;
27 
28 
29 /**
30  *
31  * Basic raw message implementation used internally for
32  * Message-->RawMessage conversions.
33  */
34 public class RawMessageImpl implements RawMessage {
35   private final Message message;
36   private final DirectByteBuffer[] payload;
37   private final int priority;
38   private boolean is_no_delay;
39   private final Message[] to_remove;
40 
41 
42 
43   /**
44    * Create a new raw message using the given parameters.
45    * @param source original message
46    * @param raw_payload headers + original message data
47    * @param priority in queue
48    * @param is_no_delay is an urgent message
49    * @param to_remove message types to auto-remove upon queue
50    */
RawMessageImpl( Message source, DirectByteBuffer[] raw_payload, int _priority, boolean _is_no_delay, Message[] _to_remove )51   public RawMessageImpl( Message source,
52                             DirectByteBuffer[] raw_payload,
53                             int _priority,
54                             boolean _is_no_delay,
55                             Message[] _to_remove ) {
56     this.message = source;
57     this.payload = raw_payload;
58     this.priority = _priority;
59     this.is_no_delay = _is_no_delay;
60     this.to_remove = _to_remove;
61   }
62 
63   //message impl
getID()64   public String getID() {  return message.getID();  }
getIDBytes()65   public byte[] getIDBytes() {  return message.getIDBytes();  }
66 
getFeatureID()67   public String getFeatureID() {  return message.getFeatureID();  }
68 
getFeatureSubID()69   public int getFeatureSubID() {  return message.getFeatureSubID();  }
70 
getType()71   public int getType() {  return message.getType();  }
72 
getVersion()73   public byte getVersion() { return message.getVersion(); }
74 
getDescription()75   public String getDescription() {  return message.getDescription();  }
76 
getData()77   public DirectByteBuffer[] getData() {  return message.getData();  }
78 
deserialize( DirectByteBuffer data, byte version )79   public Message deserialize( DirectByteBuffer data, byte version ) throws MessageException {
80     return message.deserialize( data, version );
81   }
82 
83 
84   //rawmessage impl
getRawData()85   public DirectByteBuffer[] getRawData() {  return payload;  }
86 
getPriority()87   public int getPriority() {  return priority;  }
88 
isNoDelay()89   public boolean isNoDelay() {  return is_no_delay;  }
90 
setNoDelay()91   public void setNoDelay() { is_no_delay = true; }
92 
messagesToRemove()93   public Message[] messagesToRemove() {  return to_remove;  }
94 
getBaseMessage()95   public Message getBaseMessage() {  return message;  }
96 
97 
destroy()98   public void destroy() {
99     //NOTE: Assumes that the raw payload is made up of the original
100     //      message data buffers plus some header data, so returning
101     //      the raw buffers will therefore also take care of the data
102     //      buffers return.
103     for( int i=0; i < payload.length; i++ ) {
104       payload[i].returnToPool();
105     }
106   }
107 
108 
109 
110   /*
111   public boolean equals( Object obj ) {
112     //ensure we are comparing the underlying Message (and its equals() override if exists)
113     if( obj instanceof RawMessage ) {
114       obj = ((RawMessage)obj).getBaseMessage();
115     }
116 
117     return message.equals( obj );
118   }
119 
120 
121   public int hashCode() {
122     return message.hashCode();
123   }
124   */
125 
126 }
127