3131
3232import java .io .IOException ;
3333import java .net .DatagramPacket ;
34+ import java .net .Inet4Address ;
3435import java .net .InetAddress ;
3536import java .net .InetSocketAddress ;
3637import java .net .MulticastSocket ;
@@ -58,11 +59,11 @@ public class MulticastRegistry extends FailbackRegistry {
5859
5960 private static final int DEFAULT_MULTICAST_PORT = 1234 ;
6061
61- private final InetAddress mutilcastAddress ;
62+ private final InetAddress multicastAddress ;
6263
63- private final MulticastSocket mutilcastSocket ;
64+ private final MulticastSocket multicastSocket ;
6465
65- private final int mutilcastPort ;
66+ private final int multicastPort ;
6667
6768 private final ConcurrentMap <URL , Set <URL >> received = new ConcurrentHashMap <URL , Set <URL >>();
6869
@@ -79,23 +80,21 @@ public MulticastRegistry(URL url) {
7980 if (url .isAnyHost ()) {
8081 throw new IllegalStateException ("registry address == null" );
8182 }
82- if (!isMulticastAddress (url .getHost ())) {
83- throw new IllegalArgumentException ("Invalid multicast address " + url .getHost () + ", scope: 224.0.0.0 - 239.255.255.255" );
84- }
8583 try {
86- mutilcastAddress = InetAddress .getByName (url .getHost ());
87- mutilcastPort = url .getPort () <= 0 ? DEFAULT_MULTICAST_PORT : url .getPort ();
88- mutilcastSocket = new MulticastSocket (mutilcastPort );
89- mutilcastSocket .setLoopbackMode (false );
90- mutilcastSocket .joinGroup (mutilcastAddress );
84+ multicastAddress = InetAddress .getByName (url .getHost ());
85+ checkMulticastAddress (multicastAddress );
86+
87+ multicastPort = url .getPort () <= 0 ? DEFAULT_MULTICAST_PORT : url .getPort ();
88+ multicastSocket = new MulticastSocket (multicastPort );
89+ NetUtils .joinMulticastGroup (multicastSocket , multicastAddress );
9190 Thread thread = new Thread (new Runnable () {
9291 @ Override
9392 public void run () {
9493 byte [] buf = new byte [2048 ];
9594 DatagramPacket recv = new DatagramPacket (buf , buf .length );
96- while (!mutilcastSocket .isClosed ()) {
95+ while (!multicastSocket .isClosed ()) {
9796 try {
98- mutilcastSocket .receive (recv );
97+ multicastSocket .receive (recv );
9998 String msg = new String (recv .getData ()).trim ();
10099 int i = msg .indexOf ('\n' );
101100 if (i > 0 ) {
@@ -104,7 +103,7 @@ public void run() {
104103 MulticastRegistry .this .receive (msg , (InetSocketAddress ) recv .getSocketAddress ());
105104 Arrays .fill (buf , (byte ) 0 );
106105 } catch (Throwable e ) {
107- if (!mutilcastSocket .isClosed ()) {
106+ if (!multicastSocket .isClosed ()) {
108107 logger .error (e .getMessage (), e );
109108 }
110109 }
@@ -133,6 +132,19 @@ public void run() {
133132 }
134133 }
135134
135+ private void checkMulticastAddress (InetAddress multicastAddress ) {
136+ if (!multicastAddress .isMulticastAddress ()) {
137+ String message = "Invalid multicast address " + multicastAddress ;
138+ if (!(multicastAddress instanceof Inet4Address )) {
139+ throw new IllegalArgumentException (message + ", " +
140+ "ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255." );
141+ } else {
142+ throw new IllegalArgumentException (message + ", " + "ipv6 multicast address must start with ff, " +
143+ "for example: ff01::1" );
144+ }
145+ }
146+ }
147+
136148 private static boolean isMulticastAddress (String ip ) {
137149 int i = ip .indexOf ('.' );
138150 if (i > 0 ) {
@@ -233,25 +245,25 @@ private void receive(String msg, InetSocketAddress remoteAddress) {
233245
234246 private void broadcast (String msg ) {
235247 if (logger .isInfoEnabled ()) {
236- logger .info ("Send broadcast message: " + msg + " to " + mutilcastAddress + ":" + mutilcastPort );
248+ logger .info ("Send broadcast message: " + msg + " to " + multicastAddress + ":" + multicastPort );
237249 }
238250 try {
239251 byte [] data = (msg + "\n " ).getBytes ();
240- DatagramPacket hi = new DatagramPacket (data , data .length , mutilcastAddress , mutilcastPort );
241- mutilcastSocket .send (hi );
252+ DatagramPacket hi = new DatagramPacket (data , data .length , multicastAddress , multicastPort );
253+ multicastSocket .send (hi );
242254 } catch (Exception e ) {
243255 throw new IllegalStateException (e .getMessage (), e );
244256 }
245257 }
246258
247259 private void unicast (String msg , String host ) {
248260 if (logger .isInfoEnabled ()) {
249- logger .info ("Send unicast message: " + msg + " to " + host + ":" + mutilcastPort );
261+ logger .info ("Send unicast message: " + msg + " to " + host + ":" + multicastPort );
250262 }
251263 try {
252264 byte [] data = (msg + "\n " ).getBytes ();
253- DatagramPacket hi = new DatagramPacket (data , data .length , InetAddress .getByName (host ), mutilcastPort );
254- mutilcastSocket .send (hi );
265+ DatagramPacket hi = new DatagramPacket (data , data .length , InetAddress .getByName (host ), multicastPort );
266+ multicastSocket .send (hi );
255267 } catch (Exception e ) {
256268 throw new IllegalStateException (e .getMessage (), e );
257269 }
@@ -293,7 +305,7 @@ protected void doUnsubscribe(URL url, NotifyListener listener) {
293305 @ Override
294306 public boolean isAvailable () {
295307 try {
296- return mutilcastSocket != null ;
308+ return multicastSocket != null ;
297309 } catch (Throwable t ) {
298310 return false ;
299311 }
@@ -310,8 +322,8 @@ public void destroy() {
310322 logger .warn (t .getMessage (), t );
311323 }
312324 try {
313- mutilcastSocket .leaveGroup (mutilcastAddress );
314- mutilcastSocket .close ();
325+ multicastSocket .leaveGroup (multicastAddress );
326+ multicastSocket .close ();
315327 } catch (Throwable t ) {
316328 logger .warn (t .getMessage (), t );
317329 }
@@ -434,7 +446,7 @@ public List<URL> lookup(URL url) {
434446 }
435447
436448 public MulticastSocket getMutilcastSocket () {
437- return mutilcastSocket ;
449+ return multicastSocket ;
438450 }
439451
440452 public Map <URL , Set <URL >> getReceived () {
0 commit comments