001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.activemq.transport.multicast;
019    
020    import java.io.IOException;
021    import java.net.DatagramSocket;
022    import java.net.MulticastSocket;
023    import java.net.URI;
024    
025    import javax.jms.JMSException;
026    
027    import org.activemq.io.WireFormat;
028    import org.activemq.transport.udp.UdpTransportChannel;
029    
030    /**
031     * A multicast implementation of a TransportChannel
032     * 
033     * @version $Revision: 1.1.1.1 $
034     */
035    public class MulticastTransportChannel extends UdpTransportChannel {
036    
037        private boolean loopbackMode = false; // no loopback by default as we don't
038        // need to see our own messages
039     
040        private int timeToLive = 1; // don't send multicast messages beyound our local subnet
041        
042    
043        /**
044         * Connect to a remote Node - e.g. a Broker
045         * @param wireFormat
046         * @param remoteLocation
047         * @throws JMSException
048         */
049        public MulticastTransportChannel(WireFormat wireFormat, URI remoteLocation) throws JMSException {
050            super(wireFormat, remoteLocation);
051        }
052    
053        /**
054         * @param wireFormat
055         * @param socket
056         * @throws JMSException
057         */
058        public MulticastTransportChannel(WireFormat wireFormat, MulticastSocket socket) throws JMSException {
059            super(wireFormat, socket);
060        }
061    
062        /**
063         * @return true
064         */
065        public boolean isMulticast() {
066            return true;
067        }
068    
069        /**
070         * pretty print for object
071         * 
072         * @return String representation of this object
073         */
074        public String toString() {
075            return "MulticastTransportChannel: " + socket;
076        }
077        
078        /**
079         * @return Returns the timeToLive.
080         */
081        public int getTimeToLive() {
082            return timeToLive;
083        }
084        /**
085         * @param timeToLive The timeToLive to set.
086         * @throws IOException
087         */
088        public void setTimeToLive(int timeToLive) throws IOException {
089            this.timeToLive = timeToLive;
090            MulticastSocket msocket = (MulticastSocket) socket;
091            if (msocket != null){
092                msocket.setTimeToLive(timeToLive);
093            }
094        }
095    
096        protected void connect() throws IOException {
097            MulticastSocket msocket = (MulticastSocket) socket;
098            
099            //log.info("Creating multicast socket on port: " + port + " on
100            msocket.setLoopbackMode(loopbackMode);
101            msocket.setTimeToLive(getTimeToLive());
102            msocket.joinGroup(inetAddress);
103        }
104    
105        protected DatagramSocket createSocket(int port) throws IOException {
106            return new MulticastSocket(port);
107        }
108    
109    }