KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > multicast > MulticastTransport


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
30 import java.net.DatagramSocket JavaDoc;
31 import java.net.InetAddress JavaDoc;
32 import java.net.InetSocketAddress JavaDoc;
33 import java.net.MulticastSocket JavaDoc;
34 import java.net.SocketAddress JavaDoc;
35 import java.net.SocketException JavaDoc;
36 import java.net.URI JavaDoc;
37 import java.net.UnknownHostException JavaDoc;
38
39 /**
40  * A multicast based transport.
41  *
42  * @version $Revision: 426366 $
43  */

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 JavaDoc socket;
51     private InetAddress JavaDoc 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 JavaDoc remoteLocation) throws UnknownHostException JavaDoc, IOException JavaDoc {
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 JavaDoc getProtocolName() {
86         return "Multicast";
87     }
88
89     protected String JavaDoc getProtocolUriScheme() {
90         return "multicast://";
91     }
92
93     protected void bind(DatagramSocket socket, SocketAddress JavaDoc localAddress) throws SocketException JavaDoc {
94     }
95
96     protected void doStop(ServiceStopper stopper) throws Exception JavaDoc {
97         super.doStop(stopper);
98         if (socket != null) {
99             try {
100                 socket.leaveGroup(getMulticastAddress());
101             }
102             catch (IOException JavaDoc e) {
103                 stopper.onException(this, e);
104             }
105             socket.close();
106         }
107     }
108
109     protected CommandChannel createCommandChannel() throws IOException JavaDoc {
110         socket = new MulticastSocket JavaDoc(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 JavaDoc getMulticastAddress() {
123         return mcastAddress;
124     }
125
126     protected MulticastSocket JavaDoc getSocket() {
127         return socket;
128     }
129
130     protected void setSocket(MulticastSocket JavaDoc socket) {
131         this.socket = socket;
132     }
133
134     protected InetSocketAddress JavaDoc createAddress(URI JavaDoc remoteLocation) throws UnknownHostException JavaDoc, IOException JavaDoc {
135         this.mcastAddress = InetAddress.getByName(remoteLocation.getHost());
136         this.mcastPort = remoteLocation.getPort();
137         return new InetSocketAddress JavaDoc(mcastAddress, mcastPort);
138     }
139
140     protected DatagramHeaderMarshaller createDatagramHeaderMarshaller() {
141         return new MulticastDatagramHeaderMarshaller("udp://dummyHostName:" + getPort());
142     }
143
144 }
145
Popular Tags