View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.relacs.events;
20  
21  import java.io.IOException;
22  import java.io.ObjectInput;
23  import java.io.ObjectOutput;
24  
25  import jgroup.core.MemberId;
26  import jgroup.relacs.types.Flag;
27  import jgroup.relacs.types.GroupIndex;
28  import jgroup.relacs.types.MemberIdImpl;
29  import jgroup.relacs.types.MessageId;
30  import jgroup.util.InMessage;
31  
32  /**
33   *  The <code>DeliveryEvent</code> class represents a local message 
34   *  exchanged between members and the daemon; it is used by a daemon 
35   *  to notify members that a message is ready to be delivered.
36   *  
37   *  @author Alberto Montresor
38   *  @since  Jgroup 0.2
39   */
40  public final class DeliveryEvent
41    extends Event
42  {
43  
44    ////////////////////////////////////////////////////////////////////////////////////////////
45    // Fields
46    ////////////////////////////////////////////////////////////////////////////////////////////
47  
48    private static final long serialVersionUID = -327290828044626238L;
49  
50    /** Identifier of the sender of the message */
51    private MemberId sender;
52  
53    /** Index of the sender host in the current view */
54    private int hpos;
55  
56    /** True if the payload contains a single object */
57    private boolean isObject;
58  
59    /** Message identifier */
60    private int mid;
61  
62    /** True if an ack for this message is needed */
63    private boolean ackRequired;
64  
65    /** Message contents */
66    private InMessage payload;
67  
68  
69    ////////////////////////////////////////////////////////////////////////////////////////////
70    // Constructors
71    ////////////////////////////////////////////////////////////////////////////////////////////
72  
73    /**
74     *  Default constructor for externalization.
75     */
76    public DeliveryEvent() { }
77  
78  
79    /**
80     *  Builds a new <CODE>DeliveryEvent</CODE> object.
81     * 
82     *  @param gid
83     *    Group identifier
84     *  @param isObject
85     *    True if the payload contains a single object; false if it
86     *    contains a stream
87     *  @param mid
88     *    Message identifier
89     *  @param sender
90     *    Sender identifier
91     *  @param ackRequired
92     *    True if acknowledgement is required; false otherwise
93     *  @param payload
94     *    A stream or object payload
95     */
96    public DeliveryEvent(int gid, boolean isObject, int mid, int hpos, 
97      MemberId sender, boolean ackRequired, InMessage payload)
98    {
99      super(DELIVERY_EVENT, gid);
100     this.isObject = isObject;
101     this.mid = mid;
102     this.hpos = hpos;
103     this.sender = sender;
104     this.ackRequired = ackRequired;
105     this.payload = payload;
106   }
107 
108 
109   ////////////////////////////////////////////////////////////////////////////////////////////
110   // Accessor methods
111   ////////////////////////////////////////////////////////////////////////////////////////////
112 
113   /**
114    *  Returns the sender of this multicast message.
115    */
116   public MemberId getSender()
117   {
118     return sender;
119   }
120   
121   /**
122    *  
123    */
124   public int getHostIndex()
125   {
126     return hpos;
127   }
128 
129   /**
130    *  Returns the message identifier for this multicast message.
131    */
132   public int getMessageId()
133   {
134     return mid;
135   }
136   
137   /**
138    *  
139    */
140   public boolean isObject()
141   {
142     return isObject;
143   }
144   
145   /**
146    *  
147    */
148   public boolean isAckRequired()
149   {
150     return ackRequired;
151   }
152 
153   /**
154    *  
155    */
156   public InMessage getPayload()
157   {
158     return payload;
159   }
160 
161 
162   ////////////////////////////////////////////////////////////////////////////////////////////
163   // Object methods
164   ////////////////////////////////////////////////////////////////////////////////////////////
165 
166   public String toString()
167   {
168     StringBuilder buf = new StringBuilder();
169     buf.append("[");
170     buf.append(super.toString());
171     buf.append(", hpos=");
172     buf.append(hpos);
173     buf.append(", mid=");
174     buf.append(mid);
175     buf.append(", sender=");
176     buf.append(sender);
177     buf.append(", isObj=");
178     buf.append(isObject);
179     buf.append(", ackRequired=");
180     buf.append(ackRequired);
181     buf.append(", payload=");
182     buf.append(payload);
183     return buf.toString();    
184   }
185 
186 
187   ////////////////////////////////////////////////////////////////////////////////////////////
188   // Methods from Externalizable
189   ////////////////////////////////////////////////////////////////////////////////////////////
190 
191   /**
192    *  Restores the content of this object from the marshalled data contained
193    *  in the specified input stream.
194    * 
195    *  @param in the stream to be read
196    */
197   public void readExternal(ObjectInput in)
198     throws IOException, ClassNotFoundException
199   {
200     super.readExternal(in);
201     sender = new MemberIdImpl();
202     sender.readExternal(in);
203     hpos = GroupIndex.unmarshal(in);
204     isObject = Flag.unmarshal(in);
205     ackRequired = Flag.unmarshal(in);
206     mid = MessageId.unmarshal(in);
207     payload = new InMessage();
208     payload.readExternal(in);
209   }
210 
211 
212   /**
213    *  Marshals the content of this object to the specified output stream.
214    * 
215    *  @param out the stream to be written
216    */
217   public void writeExternal(ObjectOutput out)
218     throws IOException
219   {
220     super.writeExternal(out);
221     sender.writeExternal(out);
222     GroupIndex.marshal(out, hpos);
223     Flag.marshal(out, isObject);
224     Flag.marshal(out, ackRequired);
225     MessageId.marshal(out, mid);
226     payload.writeExternal(out);
227   }
228 
229 } // END DeliveryEvent