You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by hy...@apache.org on 2020/03/21 14:30:35 UTC

[dubbo] branch master updated: refactor: remove duplicate code in MulticastGroup and MulticastExchangeGroup(#5898)

This is an automated email from the ASF dual-hosted git repository.

hyunkun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 7019f31  refactor: remove duplicate code in MulticastGroup and MulticastExchangeGroup(#5898)
7019f31 is described below

commit 7019f3186e3ffc1f25f0598a2f9db52bd2b4c0d3
Author: DIscord010 <32...@users.noreply.github.com>
AuthorDate: Sat Mar 21 22:30:25 2020 +0800

    refactor: remove duplicate code in MulticastGroup and MulticastExchangeGroup(#5898)
---
 .../java/org/apache/dubbo/common/utils/NetUtils.java | 20 +++++++++++++++++++-
 .../org/apache/dubbo/common/utils/NetUtilsTest.java  |  6 ++++++
 .../p2p/exchange/support/MulticastExchangeGroup.java | 16 ++--------------
 .../dubbo/remoting/p2p/support/MulticastGroup.java   | 16 ++--------------
 4 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
index 48c4f65..f16dd06 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
@@ -351,7 +351,7 @@ public class NetUtils {
     /**
      * Get the suitable {@link NetworkInterface}
      *
-     * @return If no {@ink NetworkInterface} is available , return <code>null</code>
+     * @return If no {@link NetworkInterface} is available , return <code>null</code>
      * @since 2.7.6
      */
     public static NetworkInterface findNetworkInterface() {
@@ -568,6 +568,24 @@ public class NetUtils {
         return true;
     }
 
+    /**
+     * is multicast address or not
+     *
+     * @param host ipv4 address
+     * @return {@code true} if is multicast address
+     */
+    public static boolean isMulticastAddress(String host) {
+        int i = host.indexOf('.');
+        if (i > 0) {
+            String prefix = host.substring(0, i);
+            if (StringUtils.isInteger(prefix)) {
+                int p = Integer.parseInt(prefix);
+                return p >= 224 && p <= 239;
+            }
+        }
+        return false;
+    }
+
     private static boolean ipPatternContainExpression(String pattern) {
         return pattern.contains("*") || pattern.contains("-");
     }
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
index 9e0abde..d9e4326 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
@@ -311,4 +311,10 @@ public class NetUtilsTest {
         assertTrue(NetUtils.isValidLocalHost(NetUtils.getLocalHost()));
         assertFalse(NetUtils.isInvalidLocalHost(NetUtils.getLocalHost()));
     }
+
+    @Test
+    public void testIsMulticastAddress() {
+        assertTrue(NetUtils.isMulticastAddress("224.0.0.1"));
+        assertFalse(NetUtils.isMulticastAddress("127.0.0.1"));
+    }
 }
diff --git a/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeGroup.java b/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeGroup.java
index ab6beff..e808086 100644
--- a/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeGroup.java
+++ b/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeGroup.java
@@ -17,7 +17,7 @@
 package org.apache.dubbo.remoting.p2p.exchange.support;
 
 import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.common.utils.NetUtils;
 import org.apache.dubbo.remoting.RemotingException;
 import org.apache.dubbo.remoting.exchange.ExchangeHandler;
 import org.apache.dubbo.remoting.p2p.exchange.ExchangePeer;
@@ -43,7 +43,7 @@ public class MulticastExchangeGroup extends AbstractExchangeGroup {
 
     public MulticastExchangeGroup(URL url) {
         super(url);
-        if (!isMulticastAddress(url.getHost())) {
+        if (!NetUtils.isMulticastAddress(url.getHost())) {
             throw new IllegalArgumentException("Invalid multicast address " + url.getHost() + ", scope: 224.0.0.0 - 239.255.255.255");
         }
         try {
@@ -73,18 +73,6 @@ public class MulticastExchangeGroup extends AbstractExchangeGroup {
         }
     }
 
-    private static boolean isMulticastAddress(String ip) {
-        int i = ip.indexOf('.');
-        if (i > 0) {
-            String prefix = ip.substring(0, i);
-            if (StringUtils.isInteger(prefix)) {
-                int p = Integer.parseInt(prefix);
-                return p >= 224 && p <= 239;
-            }
-        }
-        return false;
-    }
-
     private void send(String msg) throws RemotingException {
         DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), mutilcastAddress, mutilcastSocket.getLocalPort());
         try {
diff --git a/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/MulticastGroup.java b/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/MulticastGroup.java
index 574ad5e..e070d10 100644
--- a/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/MulticastGroup.java
+++ b/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/MulticastGroup.java
@@ -17,7 +17,7 @@
 package org.apache.dubbo.remoting.p2p.support;
 
 import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.common.utils.NetUtils;
 import org.apache.dubbo.remoting.ChannelHandler;
 import org.apache.dubbo.remoting.RemotingException;
 import org.apache.dubbo.remoting.p2p.Peer;
@@ -43,7 +43,7 @@ public class MulticastGroup extends AbstractGroup {
 
     public MulticastGroup(URL url) {
         super(url);
-        if (!isMulticastAddress(url.getHost())) {
+        if (!NetUtils.isMulticastAddress(url.getHost())) {
             throw new IllegalArgumentException("Invalid multicast address " + url.getHost() + ", scope: 224.0.0.0 - 239.255.255.255");
         }
         try {
@@ -73,18 +73,6 @@ public class MulticastGroup extends AbstractGroup {
         }
     }
 
-    private static boolean isMulticastAddress(String ip) {
-        int i = ip.indexOf('.');
-        if (i > 0) {
-            String prefix = ip.substring(0, i);
-            if (StringUtils.isInteger(prefix)) {
-                int p = Integer.parseInt(prefix);
-                return p >= 224 && p <= 239;
-            }
-        }
-        return false;
-    }
-
     private void send(String msg) throws RemotingException {
         DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), mutilcastAddress, mutilcastSocket.getLocalPort());
         try {