KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 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  * $Id: DiscoveryGreetingListener.java,v 1.4 2005/05/22 03:38:51 vivekl Exp $
21  * --------------------------------------------------------------------------
22  */

23
24 package org.objectweb.jonas.discovery;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.net.DatagramPacket JavaDoc;
30 import java.net.DatagramSocket JavaDoc;
31 import java.net.InetAddress JavaDoc;
32 import java.net.SocketException JavaDoc;
33 import java.net.UnknownHostException JavaDoc;
34
35 import org.objectweb.jonas.common.Log;
36 import org.objectweb.jonas.common.NetUtils;
37 import org.objectweb.util.monolog.api.BasicLevel;
38 import org.objectweb.util.monolog.api.Logger;
39
40 /**
41  * This class is the first thing started by the discovery manager when
42  * starting the discovery service. It listens to the multicast group for
43  * greeting messages from new servers and if the message received contains
44  * the same server ID as that of this instance more than once (one message from
45  * the same instance at first) this class sends a response to inform the new server
46  * that it violates uniqueness of the server ID.
47  * @author Vivek Lakshmanan
48  * @version 1.0
49  */

50 public class DiscoveryGreetingListener extends DiscoveryGreetingResponder {
51     /**
52      * logger
53      */

54     private static Logger logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX);
55
56     /**
57      * Address associated with the machine hosting this instance.
58      */

59     private String JavaDoc theHostAddress = null;
60
61     /**
62      * The number of times a message with the same server ID has been received
63      * in the current run.
64      */

65     private int mesgsSameServerIDCount;
66
67     /**
68      * Constructs a DiscoveryGreetingListener associated to the DiscoveryManager
69      *
70      * @param dm
71      * DiscoveryManager to which this thread is associated
72      */

73     public DiscoveryGreetingListener(DiscoveryManager dm) {
74
75         super(dm);
76
77         mesgsSameServerIDCount = 0;
78
79         try {
80             theHostAddress = NetUtils.getLocalAddress();
81         } catch (UnknownHostException JavaDoc e) {
82             // Should not happen
83
logger.log(BasicLevel.ERROR,
84                     "Could not get address for local host", e);
85         }
86     }
87
88     /**
89      * @see java.lang.Runnable#run()
90      */

91     public void run() {
92         // Set this to 0 so number of messages with the same server ID can be counted,
93
// must not receive more than 1 such message in a run.
94
mesgsSameServerIDCount = 0;
95         // Create a DatagramPacket to receive messages
96
DatagramPacket JavaDoc datagram = new DatagramPacket JavaDoc(
97                 new byte[RECEIVE_BUFFER_SIZE], RECEIVE_BUFFER_SIZE);
98         // Object received in a message
99
Object JavaDoc objReceived = null;
100         ObjectInputStream JavaDoc in = null;
101
102         // Join the group in order to receive multicast messages
103
join();
104
105         // Create the socket to be used for responding
106
try {
107             unicastSocket = new DatagramSocket JavaDoc();
108         } catch (SocketException JavaDoc e3) {
109             logger.log(BasicLevel.ERROR, "Socket exception", e3);
110             return;
111         }
112         try {
113             while (notStopped) {
114                 multicastSocket.receive(datagram);
115                 in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(datagram
116                         .getData()));
117                 objReceived = in.readObject();
118
119                 if (objReceived != null) {
120                     // If received object is of type DiscGreeting
121
if (objReceived instanceof DiscGreeting) {
122                         DiscGreeting request = (DiscGreeting) objReceived;
123                         if (logger.isLoggable(BasicLevel.DEBUG)) {
124                             logger.log(BasicLevel.DEBUG,
125                                     "DiscGreeting received on multicast:\n"
126                                             + request);
127                         }
128                         // If server ID in the message is the same as
129
// that of this instance and domain is the same, make
130
// sure that only 1 such message is encountered by
131
// the system: one from itself.
132
if (request != null
133                                 && request.getServerId().equals(this.serverId)
134                                 && request.getDomainName().equals(
135                                         this.domainName)) {
136                             // Got a message with the same server ID and the same
137
// domain so up the count.
138
mesgsSameServerIDCount++;
139                             if (mesgsSameServerIDCount > 1) {
140                                 // Trust the datagram packet itself for the
141
// source address.
142
InetAddress JavaDoc destAddress = datagram.getAddress();
143                                 int destPort = request.getSourcePort();
144                                 DiscGreeting msg = createDiscGreeting(false);
145                                 sendResponse(msg, destAddress, destPort);
146                             }
147
148                         }
149                     }
150
151                 }
152             }
153         } catch (SocketException JavaDoc e) {
154             logger.log(BasicLevel.ERROR, "Socket closed: ", e);
155             notStopped = false;
156         } catch (IOException JavaDoc e1) {
157             logger.log(BasicLevel.ERROR, e1);
158         } catch (ClassNotFoundException JavaDoc e) {
159             logger.log(BasicLevel.ERROR, e);
160         }
161     }
162
163     /**
164      * Stop
165      */

166     public void stop() {
167         Thread.interrupted();
168
169     }
170
171 }
172
Popular Tags