1 9 package org.jboss.remoting.detection.multicast; 10 11 import java.io.ByteArrayInputStream ; 12 import java.io.ByteArrayOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.ObjectOutputStream ; 15 import java.net.DatagramPacket ; 16 import java.net.InetAddress ; 17 import java.net.MulticastSocket ; 18 import org.jboss.remoting.detection.AbstractDetector; 19 import org.jboss.remoting.detection.Detection; 20 21 30 public class MulticastDetector extends AbstractDetector implements MulticastDetectorMBean 31 { 32 33 private String defaultIP = "224.1.9.1"; 34 35 private InetAddress addr; 36 private InetAddress bindAddr; 37 private int port = 2410; 38 private MulticastSocket socket; 39 private Listener listener = new Listener(); 40 41 42 45 public String getDefaultIP() 46 { 47 return defaultIP; 48 } 49 50 53 public void setDefaultIP(String defaultIP) 54 { 55 this.defaultIP = defaultIP; 56 } 57 58 63 public InetAddress getAddress() 64 { 65 return addr; 66 } 67 68 73 public void setAddress(InetAddress ip) 74 { 75 this.addr = ip; 76 } 77 78 83 public InetAddress getBindAddress() 84 { 85 return bindAddr; 86 } 87 88 93 public void setBindAddress(InetAddress ip) 94 { 95 this.bindAddr = ip; 96 } 97 98 103 public int getPort() 104 { 105 return port; 106 } 107 108 113 public void setPort(int port) 114 { 115 this.port = port; 116 } 117 118 123 public void start() throws Exception 124 { 125 if(addr == null) 126 { 127 this.addr = InetAddress.getByName(defaultIP); 128 } 129 InetAddress localHost = InetAddress.getLocalHost(); 131 if(bindAddr == null && localHost.getHostAddress().equals("127.0.0.1")) 132 { 133 this.bindAddr = localHost; 135 } 136 socket = new MulticastSocket (port); 137 if(bindAddr != null) 138 { 139 socket.setInterface(bindAddr); 140 } 141 socket.joinGroup(addr); 142 143 super.start(); 144 145 if(listener == null) 146 { 147 listener = new Listener(); 148 } 149 listener.start(); 150 } 151 152 157 public void stop() throws Exception 158 { 159 super.stop(); 160 listener.running = false; 161 listener.interrupt(); 162 listener = null; 163 socket.leaveGroup(addr); 164 socket.close(); 165 socket = null; 166 } 167 168 172 protected void heartbeat() 173 { 174 if(socket != null) 175 { 176 Detection msg = createDetection(); 177 try 178 { 179 if(log.isTraceEnabled()) 180 { 181 log.trace("sending heartbeat: " + msg); 182 } 183 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 184 ObjectOutputStream objectOut = new ObjectOutputStream (byteOut); 185 objectOut.writeObject(msg); 186 objectOut.flush(); 187 byteOut.flush(); 188 byte buf[] = byteOut.toByteArray(); 189 DatagramPacket p = new DatagramPacket (buf, buf.length, addr, port); 190 socket.send(p); 191 } 192 catch(Throwable ex) 193 { 194 log.debug("heartbeat failed", ex); 196 } 197 } 198 } 199 200 private void listen(DatagramPacket p, byte[] buf) 201 { 202 if(socket != null) 203 { 204 try 205 { 206 socket.receive(p); 208 209 ByteArrayInputStream byteInput = new ByteArrayInputStream (buf); 211 ObjectInputStream objectInput = new ObjectInputStream (byteInput); 212 Detection msg = (Detection) objectInput.readObject(); 213 if(log.isTraceEnabled()) 214 { 215 log.trace("received detection: " + msg); 216 } 217 218 detect(msg); 220 } 221 catch(Throwable e) 222 { 223 if(e instanceof java.io.InvalidClassException ) 224 { 225 return; 226 } 227 if(socket != null) 228 { 229 log.debug("Error receiving detection", e); 230 } 231 } 232 } 233 } 234 235 private final class Listener extends Thread 236 { 237 boolean running = true; 238 239 public void run() 240 { 241 byte[] buf = new byte[4000]; 242 DatagramPacket p = new DatagramPacket (buf, 0, buf.length); 243 while(running) 246 { 247 listen(p, buf); 248 } 249 } 250 } 251 } 252 | Popular Tags |