KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > messages > DatagramMessage


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.messages;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.io.Serializable JavaDoc;
33 import java.net.DatagramPacket JavaDoc;
34
35 import org.objectweb.tribe.common.IpAddress;
36
37 /**
38  * This class defines a DatagramMessage
39  *
40  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
41  * @version 1.0
42  */

43 public class DatagramMessage implements Serializable JavaDoc
44 {
45   private IpAddress sourceAddress;
46   private IpAddress destinationAddress;
47   private byte[] content = null;
48
49   /**
50    * Creates a new <code>DatagramMessage</code> object
51    *
52    * @param sourceAddress source address
53    * @param destinationAddress destination address
54    */

55   public DatagramMessage(IpAddress sourceAddress, IpAddress destinationAddress)
56   {
57     this.sourceAddress = sourceAddress;
58     this.destinationAddress = destinationAddress;
59   }
60
61   /**
62    * Returns the destinationAddress value.
63    *
64    * @return Returns the destinationAddress.
65    */

66   public IpAddress getDestinationAddress()
67   {
68     return destinationAddress;
69   }
70
71   /**
72    * Returns the sourceAddress value.
73    *
74    * @return Returns the sourceAddress.
75    */

76   public IpAddress getSourceAddress()
77   {
78     return sourceAddress;
79   }
80
81   /**
82    * Returns the content value.
83    *
84    * @return Returns the content.
85    */

86   public byte[] getContent()
87   {
88     return content;
89   }
90
91   /**
92    * Sets the content value.
93    *
94    * @param content The content to set.
95    */

96   public void setContent(byte[] content)
97   {
98     this.content = content;
99   }
100
101   /**
102    * Returns the message content size
103    *
104    * @return message content size
105    */

106   public int getContentSize()
107   {
108     if (content == null)
109       return 0;
110     else
111       return content.length;
112   }
113
114   /**
115    * Return the current message as a DatagramPacket to send.
116    *
117    * @return a DatagramPacket corresponding to the current message.
118    */

119   public DatagramPacket JavaDoc getDatagramPacket()
120   {
121     if (content == null)
122       content = objectToBytes(this);
123     return new DatagramPacket JavaDoc(content, content.length, getDestinationAddress()
124         .getAddress(), getDestinationAddress().getPort());
125   }
126
127   /**
128    * Return the content of this datagram as an object.
129    *
130    * @param datagram the datagram to convert.
131    * @return the Object contained in the datagram or null if the conversion
132    * fails.
133    */

134   public static Object JavaDoc getObjectFromDatagram(DatagramPacket JavaDoc datagram)
135   {
136     try
137     {
138       ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
139           datagram.getData()));
140       return in.readObject();
141     }
142     catch (IOException JavaDoc e)
143     {
144       return null;
145     }
146     catch (ClassNotFoundException JavaDoc e)
147     {
148       return null;
149     }
150   }
151
152   /**
153    * Converts the given object to an array of bytes.
154    *
155    * @param obj the object to convert
156    * @return an array of bytes
157    */

158   public static byte[] objectToBytes(Serializable JavaDoc obj)
159   {
160     if (obj == null)
161       return null;
162     try
163     {
164       ByteArrayOutputStream JavaDoc byteStream = new ByteArrayOutputStream JavaDoc();
165       new ObjectOutputStream JavaDoc(byteStream).writeObject(obj);
166       return byteStream.toByteArray();
167     }
168     catch (IOException JavaDoc ex)
169     {
170       throw new IllegalArgumentException JavaDoc(ex.toString());
171     }
172   }
173
174 }
Popular Tags