본문

CoAP Ping 명령 로직(JAVA)

반응형
Description)
IP(Ipv4/Ipv6)를 구분하여 CoAP Ping 명령을 내리는 로직


Source1) CoAP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.source.common;
 
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
/**
 * CoAP.java
 * 
 * @author Sung Han LIM
 * 
 */
 
public class CoAP {
    private static Log log = LogFactory.getLog(CoAP.class);
    private static String regexIPv4 = "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$";
    private static String regexIPv6 = "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$";
    private static String regexIPv4andIPv6 = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$";
 
    public static void main(String[] args) {
        String ipAddress = "127.0.0.1";
        String result;
 
        result = coapPing(ipAddress);
 
        System.out.println(result);
 
    }
 
    public static String coapPing(String ipAddress) {
        Target target = new Target();
        boolean result = false;
        Pattern pattern;
 
        pattern = Pattern.compile(regexIPv4andIPv6);
        if (ipAddress == null || pattern.matcher(ipAddress).matches() == false) {
            return "FAIL";
        }
 
        // IPv4일 경우
        pattern = Pattern.compile(regexIPv4);
        if (pattern.matcher(ipAddress).matches() == true) {
            target.setIpAddr(ipAddress);
        }
 
        // IPv6일 경우
        pattern = Pattern.compile(regexIPv6);
        if (pattern.matcher(ipAddress).matches() == true) {
            target.setIpv6Addr(ipAddress);
        }
 
        target.setPort(5683);
        SyncCoapCommand syncCoapCommand = new SyncCoapCommand();
        result = syncCoapCommand.sendCoapPing(target);
 
        if (!result) {
            return "Request Timeout";
        } else {
            return "SUCCESS";
        }
    }
 
}
 
 
cs

Source2) SyncCoapCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.source.common;
 
import org.eclipse.californium.core.CoapClient;
 
/**
 * CoAP.java
 * 
 * @author Sung Han LIM
 * 
 */
 
public class SyncCoapCommand extends org.eclipse.californium.core.CoapClient {
    public boolean sendCoapPing(Target target) {
        String coapUrl = null;
 
        // IPv4일 경우
        if (target.getIpAddr() != null) {
            coapUrl = "coap://" + target.getIpAddr() + ":5683";
        }
 
        // IPv6일 경우
        if (target.getIpv6Addr() != null) {
            coapUrl = "coap://[" + target.getIpv6Addr() + "]:5683";
        }
 
        CoapClient clientPing = new CoapClient(coapUrl);
 
        // 5초 이상 COAP-Ping 'false'
        if (!clientPing.ping(5000)) {
            return false;
        } else {
            return true;
        }
    }
}
 
 
cs


Source3) Target.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.source.common;
 
/**
 * CoAP.java
 * 
 * @author Sung Han LIM
 * 
 */
 
public class Target {
    String ipAddr;
    String ipv6Addr;
    int port = 0;
 
    public int getPort() {
        return port;
    }
 
    public void setPort(int port) {
        this.port = port;
    }
 
    public String getIpAddr() {
        return ipAddr;
    }
 
    public void setIpAddr(String ipAddr) {
        this.ipAddr = ipAddr;
    }
 
    public String getIpv6Addr() {
        return ipv6Addr;
    }
 
    public void setIpv6Addr(String ipv6Addr) {
        this.ipv6Addr = ipv6Addr;
    }
 
}
 
 
cs

Result)
1
2
3
4
5
6
7
8
9
8월 252016 11:27:58 오후 org.eclipse.californium.core.network.config.NetworkConfig createStandardWithFile
정보: Loading standard properties from file Californium.properties
8월 252016 11:27:58 오후 org.eclipse.californium.core.network.CoapEndpoint start
정보: Starting endpoint at 0.0.0.0/0.0.0.0:0
8월 252016 11:27:58 오후 org.eclipse.californium.core.network.EndpointManager createDefaultEndpoint
정보: Created implicit default endpoint 0.0.0.0/0.0.0.0:57251
Request Timeout
 
 
cs

Dependency)
1
2
3
4
5
6
7
<!-- CoAP -->
<dependency>
    <groupId>org.eclipse.californium</groupId>
    <artifactId>californium-core</artifactId>
    <version>1.0.4</version>
</dependency>
 
cs



반응형

공유

댓글