View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.remote.comms;
17  
18  import java.io.IOException;
19  import java.net.InetAddress;
20  import java.net.MulticastSocket;
21  import java.net.UnknownHostException;
22  
23  /***
24   * @author jmccrindle
25   */
26  public class MulticastSupport {
27  
28      private int port = 8080;
29      private InetAddress group;
30      private InetAddress multicastInterface;
31      private int soTimeout = 0;
32      private int length = 8000;
33      private MulticastSocket socket;
34  
35      public void init() throws IOException {
36          socket = new MulticastSocket(port);
37          socket.setSoTimeout(soTimeout);
38          if(multicastInterface != null) {
39              socket.setInterface(multicastInterface);
40          }
41          if(group == null) {
42              group = InetAddress.getByName("224.5.5.5");
43          }
44          socket.joinGroup(group);
45      }
46  
47      public void destroy() throws UnknownHostException, IOException {
48          socket.leaveGroup(group);
49      }
50  
51      public void setGroup(String group) throws UnknownHostException {
52          this.group = InetAddress.getByName(group);
53      }
54  
55      public void setMulticastInterface(String multicastInterface) throws UnknownHostException {
56          this.multicastInterface = InetAddress.getByName(multicastInterface);
57      }
58  
59      /***
60       * @return Returns the group.
61       */
62      public InetAddress getGroup() {
63          return group;
64      }
65      /***
66       * @param group The group to set.
67       */
68      public void setGroup(InetAddress group) {
69          this.group = group;
70      }
71      /***
72       * @return Returns the length.
73       */
74      public int getLength() {
75          return length;
76      }
77      /***
78       * @param length The length to set.
79       */
80      public void setLength(int length) {
81          this.length = length;
82      }
83      /***
84       * @return Returns the multicastInterface.
85       */
86      public InetAddress getMulticastInterface() {
87          return multicastInterface;
88      }
89      /***
90       * @param multicastInterface The multicastInterface to set.
91       */
92      public void setMulticastInterface(InetAddress multicastInterface) {
93          this.multicastInterface = multicastInterface;
94      }
95      /***
96       * @return Returns the port.
97       */
98      public int getPort() {
99          return port;
100     }
101     /***
102      * @param port The port to set.
103      */
104     public void setPort(int port) {
105         this.port = port;
106     }
107     /***
108      * @return Returns the soTimeout.
109      */
110     public int getSoTimeout() {
111         return soTimeout;
112     }
113     /***
114      * @param soTimeout The soTimeout to set.
115      */
116     public void setSoTimeout(int soTimeout) {
117         this.soTimeout = soTimeout;
118     }
119     /***
120      * @return Returns the socket.
121      */
122     public MulticastSocket getSocket() {
123         return socket;
124     }
125 }