1 package com.ubermq.jms.common.datagram.impl; 2 3 import com.ubermq.jms.common.datagram.*; 4 import java.util.*; 5 6 import com.ubermq.kernel.AbstractDatagram; 7 import java.nio.ByteBuffer ; 8 9 21 public class ServerMessageDatagram 22 extends AbstractDatagram 23 implements IMessageDatagram 24 { 25 private ByteBuffer buffer; 26 private String topicName; 27 28 private int propsOffset; 29 private Map props; 30 31 private static final int SENDER_ID_OFFSET = 0; 32 private static final int SEQUENCE_OFFSET = 8; 33 private static final int VARIOUS_OFFSET = 12; 34 private static final int TIMESTAMP_OFFSET = 20; 35 private static final int PROPS_LEN_OFFSET = 28; 36 private static final int TOPIC_NAME_OFFSET = 32; 37 38 39 44 ServerMessageDatagram() 45 { 46 super(DatagramFactory.DGRAM_MSG, 0); 47 } 48 49 55 public void incoming(ByteBuffer bb) throws java.io.IOException 56 { 57 super.incoming(bb); 58 this.buffer = bb.slice(); 59 60 topicName = 63 AbstractDatagram.readPascalString((ByteBuffer )buffer.position(TOPIC_NAME_OFFSET)); 64 65 propsOffset = buffer.position(); 68 props = null; 69 } 70 71 75 public void outgoing(java.nio.ByteBuffer bb) 76 { 77 super.outgoing(bb); 78 bb.put((ByteBuffer )buffer.rewind()); 79 } 80 81 84 public void setSequence(int sequence) 85 { 86 throw new UnsupportedOperationException ("mutation is not supported for server datagrams."); 87 } 88 89 94 public String getTopicName() 95 { 96 return topicName; 97 } 98 99 105 public int getSequence() 106 { 107 return buffer.getInt(SEQUENCE_OFFSET); 108 } 109 110 116 public long getSenderId() 117 { 118 return buffer.getLong(SENDER_ID_OFFSET); 119 } 120 121 127 public MessageId getMessageId() 128 { 129 return getIncomingMessageId(); 130 } 131 132 138 public MessageId getIncomingMessageId() 139 { 140 return new MessageId(getSenderId(), getSequence()); 141 } 142 143 148 public Object getStandardProperty(int property) 149 { 150 switch(property) 151 { 152 case STDPROP_PRIORITY: 153 return new Integer (MessageDatagram.getFlagBasedProperty(buffer.getLong(VARIOUS_OFFSET), MessageDatagram.PRI_SHIFT, MessageDatagram.PRI_MASK)); 154 case STDPROP_MSGTYPE: 155 return new Integer (MessageDatagram.getFlagBasedProperty(buffer.getLong(VARIOUS_OFFSET), MessageDatagram.MSGTYPE_SHIFT, MessageDatagram.MSGTYPE_MASK)); 156 case STDPROP_TTL: 157 return new Integer (MessageDatagram.getFlagBasedProperty(buffer.getLong(VARIOUS_OFFSET), MessageDatagram.TTL_SHIFT, MessageDatagram.TTL_MASK) * 100); 158 case STDPROP_REDELIVERY: 159 return new Boolean (MessageDatagram.getFlagBasedProperty(buffer.getLong(VARIOUS_OFFSET), MessageDatagram.REDELIVERED_SHIFT, MessageDatagram.REDELIVERED_MASK) != 0 ? true : false); 160 case STDPROP_TIMESTAMP: 161 return new Long (buffer.getLong(TIMESTAMP_OFFSET)); 162 case STDPROP_BODY: 163 { 164 return readTypedData((ByteBuffer )this.buffer.duplicate().position(propsOffset + buffer.getInt(PROPS_LEN_OFFSET))); 165 } 166 case STDPROP_DELIVERYMODE: 167 return new Integer (MessageDatagram.getFlagBasedProperty(buffer.getLong(VARIOUS_OFFSET), MessageDatagram.DELIVERY_SHIFT, MessageDatagram.DELIVERY_MASK)); 168 default: 169 return (hasProcessedProperties() ? props.get(new Integer (property)) : null); 170 } 171 } 172 173 public void setStandardProperty(int property, Object value) 174 { 175 switch(property) 176 { 177 case STDPROP_REDELIVERY: 178 buffer.putLong(VARIOUS_OFFSET, 179 MessageDatagram.setFlagBasedProperty(buffer.getLong(VARIOUS_OFFSET), 180 MessageDatagram.REDELIVERED_SHIFT, 181 MessageDatagram.REDELIVERED_MASK, 182 ((Boolean )value).booleanValue() ? 1 : 0)); 183 break; 184 case STDPROP_TIMESTAMP: 185 buffer.putLong(TIMESTAMP_OFFSET, ((Number )value).longValue()); 186 break; 187 default: 188 throw new UnsupportedOperationException ("mutation is not supported for server datagrams."); 189 } 190 } 191 192 200 public Object getCustomProperty(String property) 201 { 202 int props_len = buffer.getInt(PROPS_LEN_OFFSET); 203 204 if (!hasProcessedProperties()) { 205 props = new HashMap(); 206 MessageDatagram.parsePropsBuffer(((ByteBuffer )buffer.asReadOnlyBuffer().position(propsOffset).limit(propsOffset + props_len)).slice(), 207 props); 208 } 209 210 return props.get(property); 211 } 212 213 221 protected boolean hasProcessedProperties() {return (props != null);} 222 223 227 public java.util.Collection getCustomPropertyNames() 228 { 229 throw new UnsupportedOperationException ("mutation is not supported for server datagrams."); 231 } 232 233 238 public void setCustomProperty(String property, Object value) 239 { 240 throw new UnsupportedOperationException ("mutation is not supported for server datagrams."); 241 } 242 243 246 public void clearProperties() 247 { 248 throw new UnsupportedOperationException ("mutation is not supported for server datagrams."); 249 } 250 251 255 public void setTopicName(String sz) 256 { 257 throw new UnsupportedOperationException ("mutation is not supported for server datagrams."); 258 } 259 260 264 public void setSenderId(long senderId) 265 { 266 throw new UnsupportedOperationException ("mutation is not supported for server datagrams."); 267 } 268 269 276 public void prepareToSend(int deliveryMode, int pri, long ttl) 277 { 278 throw new UnsupportedOperationException ("mutation is not supported for server datagrams."); 279 } 280 281 } 282 | Popular Tags |