KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > discovery > DiscMessage


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  */

21
22 package org.objectweb.jonas.discovery;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.net.DatagramPacket JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:Takoua.Abdellatif@inria.fr">Takoua Abdellatif </a>
32  * @version 1.0
33  */

34 public class DiscMessage implements Serializable JavaDoc {
35
36     /**
37      * The protocol version for the Discovery messages.
38      */

39     public static final String JavaDoc DISCOVERY_PROTOCOL_VERSION = "1.0";
40
41
42     /**
43      *
44      */

45     private String JavaDoc sourceAddress;
46     /**
47      *
48      */

49     private int sourcePort;
50
51     /**
52      * The version for this message
53      */

54     private String JavaDoc version;
55
56
57
58
59     /**
60      * Creates a new <code>DiscoveryRequest</code>
61      * @param sourceAddress
62      * source address to use to send discovery responses.
63      * @param sourcePort
64      * source port to use to send disovery responses.
65      *
66      */

67     public DiscMessage(String JavaDoc sourceAddress, int sourcePort) {
68         this.sourceAddress = sourceAddress;
69         this.sourcePort = sourcePort;
70         this.version = DISCOVERY_PROTOCOL_VERSION;
71     }
72
73     /**
74      * Returns the destinationAddress value in String type.
75      *
76      * @return Returns the destinationAddress.
77      */

78     public String JavaDoc getSourceAddress() {
79         return sourceAddress;
80     }
81
82     /**
83      * Returns the sourcePort value.
84      *
85      * @return Returns the sourcePort.
86      */

87     public int getSourcePort() {
88         return sourcePort;
89     }
90
91     /**
92      * Sets the source address.
93      *
94      * @param sourceAddress
95      */

96     public void setSourceAddress(String JavaDoc sourceAddress) {
97         this.sourceAddress = sourceAddress;
98     }
99
100     /**
101      * Sets the source port.
102      *
103      * @param sourcePort
104      */

105     public void setSourcePort(int sourcePort) {
106         this.sourcePort = sourcePort;
107     }
108
109     /**
110      * Converts the given object to an array of bytes.
111      *
112      * @param obj
113      * the object to convert
114      * @return an array of bytes
115      */

116     public static byte[] objectToBytes(Serializable JavaDoc obj) {
117         if (obj == null) {
118             return null;
119         }
120         try {
121             ByteArrayOutputStream JavaDoc byteStream = new ByteArrayOutputStream JavaDoc();
122             new ObjectOutputStream JavaDoc(byteStream).writeObject(obj);
123             return byteStream.toByteArray();
124         } catch (IOException JavaDoc ex) {
125             throw new IllegalArgumentException JavaDoc(ex.toString());
126         }
127     }
128
129     /**
130      * Converts the current object to a Datagram
131      * @param o the object to be converted.
132      * @return a datagramPacket
133      */

134     private static DatagramPacket JavaDoc getDatagram(Serializable JavaDoc o) {
135         byte[] content = objectToBytes(o);
136         if (content == null) {
137             return null;
138         }
139         DatagramPacket JavaDoc dp = new DatagramPacket JavaDoc(content, content.length);
140         return dp;
141     }
142
143     /**
144      * @return the object trandformed into a String
145      */

146     public String JavaDoc toString() {
147         String JavaDoc messageString = null;
148         messageString = sourceAddress + ":" + sourcePort;
149         return messageString;
150     }
151
152
153     public String JavaDoc getVersion() {
154         return version;
155     }
156
157 }
Popular Tags