1 23 24 package org.objectweb.jonas.discovery; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.IOException ; 28 import java.io.ObjectInputStream ; 29 import java.net.DatagramPacket ; 30 import java.net.DatagramSocket ; 31 import java.net.InetAddress ; 32 import java.net.SocketException ; 33 import java.net.UnknownHostException ; 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 50 public class DiscoveryGreetingListener extends DiscoveryGreetingResponder { 51 54 private static Logger logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX); 55 56 59 private String theHostAddress = null; 60 61 65 private int mesgsSameServerIDCount; 66 67 73 public DiscoveryGreetingListener(DiscoveryManager dm) { 74 75 super(dm); 76 77 mesgsSameServerIDCount = 0; 78 79 try { 80 theHostAddress = NetUtils.getLocalAddress(); 81 } catch (UnknownHostException e) { 82 logger.log(BasicLevel.ERROR, 84 "Could not get address for local host", e); 85 } 86 } 87 88 91 public void run() { 92 mesgsSameServerIDCount = 0; 95 DatagramPacket datagram = new DatagramPacket ( 97 new byte[RECEIVE_BUFFER_SIZE], RECEIVE_BUFFER_SIZE); 98 Object objReceived = null; 100 ObjectInputStream in = null; 101 102 join(); 104 105 try { 107 unicastSocket = new DatagramSocket (); 108 } catch (SocketException 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 (new ByteArrayInputStream (datagram 116 .getData())); 117 objReceived = in.readObject(); 118 119 if (objReceived != null) { 120 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 (request != null 133 && request.getServerId().equals(this.serverId) 134 && request.getDomainName().equals( 135 this.domainName)) { 136 mesgsSameServerIDCount++; 139 if (mesgsSameServerIDCount > 1) { 140 InetAddress 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 e) { 154 logger.log(BasicLevel.ERROR, "Socket closed: ", e); 155 notStopped = false; 156 } catch (IOException e1) { 157 logger.log(BasicLevel.ERROR, e1); 158 } catch (ClassNotFoundException e) { 159 logger.log(BasicLevel.ERROR, e); 160 } 161 } 162 163 166 public void stop() { 167 Thread.interrupted(); 168 169 } 170 171 } 172 | Popular Tags |