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.SocketTimeoutException ; 34 import java.net.UnknownHostException ; 35 36 import org.objectweb.jonas.common.Log; 37 import org.objectweb.jonas.common.NetUtils; 38 import org.objectweb.jonas.service.ServiceException; 39 import org.objectweb.util.monolog.api.BasicLevel; 40 import org.objectweb.util.monolog.api.Logger; 41 42 52 public class DiscoveryGreetingResponder extends DiscoveryComm { 53 56 protected DatagramSocket recvUnicastSocket; 57 58 61 protected int greetingPort; 62 63 66 protected int greetingTimeOut; 67 68 71 private DiscGreeting greeting = null; 72 73 76 private static Logger logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX); 77 78 84 public DiscoveryGreetingResponder(DiscoveryManager dm) { 85 super(dm); 86 this.greetingPort = dm.getGreetingListeningPort(); 87 this.greetingTimeOut = dm.getGreetingAckTimeOut(); 88 } 89 90 91 100 protected DiscGreeting createDiscGreeting(boolean startup) { 101 String theHostAddress; 102 try { 103 theHostAddress = NetUtils.getLocalAddress(); 104 } catch (UnknownHostException e) { 105 logger.log(BasicLevel.ERROR, "Unknown host", e); 106 return null; 107 } 108 109 return new DiscGreeting(theHostAddress, this.greetingPort, 110 this.jonasName, this.domainName, startup, this.serverId); 111 } 112 113 122 private void handleReceivedMessage(DiscGreeting msg) 123 throws DuplicateServerNameException { 124 125 if (logger.isLoggable(BasicLevel.ERROR)) { 126 logger.log(BasicLevel.ERROR, "A server with the given name already" 127 + " exists in the domain, received a rejection message: " 128 + msg); 129 } 130 stop(); 131 132 } 133 134 135 138 public void run() { 139 if(logger.isLoggable(BasicLevel.DEBUG)){ 140 logger.log(BasicLevel.DEBUG, "Sending the greeting."); 141 } 142 if (greeting != null) { 144 sendNotif(greeting); 145 } 146 } 147 148 149 156 public void handleGreeting() throws DuplicateServerNameException { 157 DatagramPacket datagram = new DatagramPacket ( 159 new byte[RECEIVE_BUFFER_SIZE], RECEIVE_BUFFER_SIZE); 160 Object objReceived = null; 162 ObjectInputStream in = null; 163 164 join(); 166 greeting = createDiscGreeting(true); 168 169 try { 171 recvUnicastSocket = new DatagramSocket (greetingPort); 172 } catch (SocketException e2) { 173 logger.log(BasicLevel.ERROR, 174 "DiscComm : Unable to create a Datagram socket", e2); 175 throw new ServiceException("Unable to create a datagram socket on port " + greetingPort + 177 " to listen for rejections. Port is still in use."); 178 } 179 180 try { 181 recvUnicastSocket.setSoTimeout(greetingTimeOut); 184 185 long lastTime = greetingTimeOut + System.currentTimeMillis(); 187 188 Thread sender = new Thread (this, "greeting sender"); 190 191 boolean msgSent = false; 192 193 while ((notStopped) && System.currentTimeMillis() <= lastTime) { 196 if(!msgSent){ 199 sender.start(); 200 msgSent = true; 201 } 202 recvUnicastSocket.receive(datagram); 205 in = new ObjectInputStream (new ByteArrayInputStream (datagram 206 .getData())); 207 objReceived = in.readObject(); 208 if (objReceived != null) { 209 if (objReceived instanceof DiscGreeting) { 210 DiscGreeting greet = (DiscGreeting) objReceived; 211 greet.setSourceAddress(datagram.getAddress().getHostAddress()); 213 handleReceivedMessage(greet); 214 } 215 } 216 } 217 218 } catch (SocketException e) { 219 logger.log(BasicLevel.ERROR, 220 "DiscoveryGreetingResponder : Socket closed", e); 221 notStopped = false; 222 } catch (IOException e1) { 223 if (e1 instanceof SocketTimeoutException ) { 224 if (logger.isLoggable(BasicLevel.WARN)) { 225 logger.log(BasicLevel.WARN, 226 "No rejections receieved within the set timeout period: " 227 + greetingTimeOut 228 + " ms. Assuming the server ID used is unique in the domain." 229 + " The timeout period can be changed under jonas.properties:" 230 + " jonas.service.discovery.greeting.timeout"); 231 } 232 } else { 233 logger.log(BasicLevel.ERROR, 234 "DiscoveryGreetingResponder IOException", e1); 235 } 236 } catch (ClassNotFoundException e) { 237 logger.log(BasicLevel.ERROR, 238 "DiscoveryGreetingResponder ClassNotFoundException ", e); 239 } 240 241 } 242 243 247 public void stop() { 248 throw new DuplicateServerNameException(); 249 } 250 251 } 252 | Popular Tags |