1 18 package org.apache.activemq.transport.multicast; 19 20 import org.apache.activemq.openwire.OpenWireFormat; 21 import org.apache.activemq.transport.udp.CommandChannel; 22 import org.apache.activemq.transport.udp.CommandDatagramSocket; 23 import org.apache.activemq.transport.udp.DatagramHeaderMarshaller; 24 import org.apache.activemq.transport.udp.UdpTransport; 25 import org.apache.activemq.util.ServiceStopper; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import java.io.IOException ; 30 import java.net.DatagramSocket ; 31 import java.net.InetAddress ; 32 import java.net.InetSocketAddress ; 33 import java.net.MulticastSocket ; 34 import java.net.SocketAddress ; 35 import java.net.SocketException ; 36 import java.net.URI ; 37 import java.net.UnknownHostException ; 38 39 44 public class MulticastTransport extends UdpTransport { 45 46 private static final Log log = LogFactory.getLog(MulticastTransport.class); 47 48 private static final int DEFAULT_IDLE_TIME = 5000; 49 50 private MulticastSocket socket; 51 private InetAddress mcastAddress; 52 private int mcastPort; 53 private int timeToLive = 1; 54 private boolean loopBackMode = false; 55 private long keepAliveInterval = DEFAULT_IDLE_TIME; 56 57 public MulticastTransport(OpenWireFormat wireFormat, URI remoteLocation) throws UnknownHostException , IOException { 58 super(wireFormat, remoteLocation); 59 } 60 61 public long getKeepAliveInterval() { 62 return keepAliveInterval; 63 } 64 65 public void setKeepAliveInterval(long keepAliveInterval) { 66 this.keepAliveInterval = keepAliveInterval; 67 } 68 69 public boolean isLoopBackMode() { 70 return loopBackMode; 71 } 72 73 public void setLoopBackMode(boolean loopBackMode) { 74 this.loopBackMode = loopBackMode; 75 } 76 77 public int getTimeToLive() { 78 return timeToLive; 79 } 80 81 public void setTimeToLive(int timeToLive) { 82 this.timeToLive = timeToLive; 83 } 84 85 protected String getProtocolName() { 86 return "Multicast"; 87 } 88 89 protected String getProtocolUriScheme() { 90 return "multicast://"; 91 } 92 93 protected void bind(DatagramSocket socket, SocketAddress localAddress) throws SocketException { 94 } 95 96 protected void doStop(ServiceStopper stopper) throws Exception { 97 super.doStop(stopper); 98 if (socket != null) { 99 try { 100 socket.leaveGroup(getMulticastAddress()); 101 } 102 catch (IOException e) { 103 stopper.onException(this, e); 104 } 105 socket.close(); 106 } 107 } 108 109 protected CommandChannel createCommandChannel() throws IOException { 110 socket = new MulticastSocket (mcastPort); 111 socket.setLoopbackMode(loopBackMode); 112 socket.setTimeToLive(timeToLive); 113 114 log.debug("Joining multicast address: " + getMulticastAddress()); 115 socket.joinGroup(getMulticastAddress()); 116 socket.setSoTimeout((int) keepAliveInterval); 117 118 return new CommandDatagramSocket(this, getWireFormat(), getDatagramSize(), getTargetAddress(), 119 createDatagramHeaderMarshaller(), getSocket()); 120 } 121 122 protected InetAddress getMulticastAddress() { 123 return mcastAddress; 124 } 125 126 protected MulticastSocket getSocket() { 127 return socket; 128 } 129 130 protected void setSocket(MulticastSocket socket) { 131 this.socket = socket; 132 } 133 134 protected InetSocketAddress createAddress(URI remoteLocation) throws UnknownHostException , IOException { 135 this.mcastAddress = InetAddress.getByName(remoteLocation.getHost()); 136 this.mcastPort = remoteLocation.getPort(); 137 return new InetSocketAddress (mcastAddress, mcastPort); 138 } 139 140 protected DatagramHeaderMarshaller createDatagramHeaderMarshaller() { 141 return new MulticastDatagramHeaderMarshaller("udp://dummyHostName:" + getPort()); 142 } 143 144 } 145 | Popular Tags |