KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > common > datagram > impl > AckDatagram


1 package com.ubermq.jms.common.datagram.impl;
2
3
4 import com.ubermq.jms.common.datagram.*;
5 import com.ubermq.kernel.*;
6 import java.nio.*;
7
8 /**
9  * An implementation of an ACK (acknowledgement) datagram.
10  */

11 public class AckDatagram
12     extends AbstractDatagram
13     implements IAckDatagram
14 {
15     private MessageId msgId;
16     private boolean nack;
17     
18     /**
19      * Constructs a general purpose ACK datagram that does not refer to
20      * a message, that is either a success or failure ACK.
21      * @param nack true if NACK, false otherwise.
22      */

23     AckDatagram(boolean nack)
24     {
25         super(DatagramFactory.DGRAM_ACK, 0);
26         this.msgId = null;
27         this.nack = nack;
28     }
29     
30     /**
31      * Constructs an ACK for a specific message.
32      * @param msgId the message ID of the message that the datagram isreferring to.
33      * @param nack true if NACK, false otherwise.
34      */

35     AckDatagram(MessageId msgId, boolean nack)
36     {
37         super(DatagramFactory.DGRAM_ACK, 0);
38         this.msgId = msgId;
39         this.nack = nack;
40     }
41     
42     /**
43      * No-arg constructor for reading datagrams from byte buffers.
44      */

45     public AckDatagram()
46     {
47         super(DatagramFactory.DGRAM_ACK, 0);
48     }
49     
50     // ack-specific
51
public MessageId getAckMessageId() {return msgId;}
52     public boolean isNegativeAck() {return nack;}
53     
54     
55     /**
56      * the ACK datagram format is:
57      *
58      * <pre>
59      * -------
60      * nack? 1 non-zero for true
61      * -------
62      * msgId 12 optional, sender ID followed by sequence
63      * -------
64      * </pre>
65      */

66     public void incoming(java.nio.ByteBuffer JavaDoc bb)
67         throws java.io.IOException JavaDoc
68     {
69         super.incoming(bb);
70         setDatagramProps(DatagramFactory.DGRAM_ACK, 0);
71         
72         nack = (bb.get() != 0);
73         if (bb.remaining() > 0) {
74             msgId = new MessageId(bb.getLong(), bb.getInt());
75         } else {
76             msgId = null;
77         }
78     }
79     
80     /**
81      * allows the datagram to be output to a channel.
82      */

83     public void outgoing(java.nio.ByteBuffer JavaDoc bb)
84     {
85         super.outgoing(bb);
86         
87         bb.put(nack ? (byte)0x1 : (byte)0x0);
88         if (msgId != null) {
89             bb.putLong(msgId.getSenderId());
90             bb.putInt(msgId.getSequence());
91         }
92     }
93     
94     public String JavaDoc toString()
95     {
96         return super.toString() +
97             "\nMsgID:\t\t" + msgId +
98             "\nnack:\t\t" + nack;
99     }
100     
101     public boolean equals(Object JavaDoc o)
102     {
103         if (o instanceof IAckDatagram) {
104             IAckDatagram ad = (IAckDatagram)o;
105             
106             return ( ((ad.getAckMessageId() == null && getAckMessageId() == null) ||
107                           ad.getAckMessageId().equals(getAckMessageId())) &&
108                         ad.isNegativeAck() == isNegativeAck());
109         } else {
110             return false;
111         }
112     }
113     
114     public int hashCode() {
115         int result = 17;
116         result = 37*result + getAckMessageId().hashCode();
117         result = 37*result + (isNegativeAck() ? 0 : 1);
118         return result;
119     }
120 }
121
122
Popular Tags