001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as published by the
008: * Free Software Foundation; either version 2.1 of the License, or any later
009: * version.
010: *
011: * This library is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this library; if not, write to the Free Software Foundation,
018: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
019: * --------------------------------------------------------------------------
020: * $Id: DiscoveryGreetingListener.java 9358 2006-08-04 11:46:51Z durieuxp $
021: * --------------------------------------------------------------------------
022: */package org.objectweb.jonas.discovery;
023:
024: import java.io.IOException;
025: import java.net.DatagramPacket;
026: import java.net.DatagramSocket;
027: import java.net.InetAddress;
028: import java.net.SocketException;
029:
030: import org.objectweb.jonas.common.Log;
031:
032: import org.objectweb.util.monolog.api.BasicLevel;
033: import org.objectweb.util.monolog.api.Logger;
034:
035: /**
036: * This class is the first thing started by the discovery manager when
037: * starting the discovery service. It listens to the multicast group for
038: * greeting messages from new servers and if the message received contains
039: * the same server ID as that of this instance more than once (one message from
040: * the same instance at first) this class sends a response to inform the new server
041: * that it violates uniqueness of the server ID.
042: * @author Vivek Lakshmanan
043: * @version 1.0
044: */
045: public class DiscoveryGreetingListener extends
046: DiscoveryGreetingResponder {
047: /**
048: * logger
049: */
050: private static Logger logger = Log
051: .getLogger(Log.JONAS_DISCOVERY_PREFIX);
052:
053: /**
054: * The number of times a message with the same server ID has been received
055: * in the current run.
056: */
057: private int mesgsSameServerIDCount;
058:
059: /**
060: * Constructs a DiscoveryGreetingListener associated to the DiscoveryManager
061: *
062: * @param dm
063: * DiscoveryManager to which this thread is associated
064: */
065: public DiscoveryGreetingListener(DiscoveryManager dm) {
066:
067: super (dm);
068:
069: mesgsSameServerIDCount = 0;
070:
071: }
072:
073: /**
074: * @see java.lang.Runnable#run()
075: */
076: public void run() {
077: // Set this to 0 so number of messages with the same server ID can be counted,
078: // must not receive more than 1 such message in a run.
079: mesgsSameServerIDCount = 0;
080:
081: // Join the group in order to receive multicast messages
082: join();
083:
084: // Create the socket to be used for responding
085: try {
086: unicastSocket = new DatagramSocket();
087: } catch (SocketException e3) {
088: logger.log(BasicLevel.ERROR, "Socket exception", e3);
089: return;
090: }
091: try {
092: while (notStopped) {
093: DatagramPacket datagram = getDatagram(RECEIVE_BUFFER_SIZE);
094: multicastSocket.receive(datagram);
095: Object objReceived = DiscoveryHelper
096: .bytesToObject(datagram.getData());
097: if (objReceived != null) {
098: // If received object is of type DiscGreeting
099: if (objReceived instanceof DiscGreeting) {
100: DiscGreeting request = (DiscGreeting) objReceived;
101: if (logger.isLoggable(BasicLevel.DEBUG)) {
102: logger.log(BasicLevel.DEBUG,
103: "DiscGreeting received on multicast:\n"
104: + request);
105: }
106: // If server ID in the message is the same as
107: // that of this instance and domain is the same, make
108: // sure that only 1 such message is encountered by
109: // the system: one from itself.
110: if (request != null
111: && request.getServerId().equals(
112: this .serverId)
113: && request.getDomainName().equals(
114: this .domainName)) {
115: // Got a message with the same server ID and the same
116: // domain so up the count.
117: mesgsSameServerIDCount++;
118: if (mesgsSameServerIDCount > 1) {
119: // Trust the datagram packet itself for the
120: // source address.
121: InetAddress destAddress = datagram
122: .getAddress();
123: int destPort = request.getSourcePort();
124: DiscGreeting msg = createDiscGreeting(false);
125: sendResponse(msg, destAddress, destPort);
126: }
127:
128: }
129: }
130: }
131: datagram = null;
132: }
133: } catch (SocketException e) {
134: logger.log(BasicLevel.ERROR, "Socket closed: ", e);
135: notStopped = false;
136: } catch (IOException e1) {
137: logger.log(BasicLevel.ERROR, e1);
138: } catch (ClassNotFoundException e) {
139: logger.log(BasicLevel.ERROR, e);
140: }
141: }
142:
143: /**
144: * Stop
145: */
146: public void stop() {
147: Thread.interrupted();
148:
149: }
150:
151: }
|