-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoAPClient.java
121 lines (110 loc) · 3.99 KB
/
CoAPClient.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.californium.core.*;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
public class CoapCli {
private CoapClient _client;
private static final String DEFAULT_PROTOCOL = "coap";
private static final String DEFAULT_HOST = "localhost";
private static final int DEFAULT_PORT = 5683;
private static final Logger _Logger = Logger.getLogger(Driver.class.getName());
private String _protocol;
private String _host;
private int _port;
private String _serverAddr;
// public constructor to set parameters
public CoapCli() {
super();
_protocol = DEFAULT_PROTOCOL;
_host = DEFAULT_HOST;
_port = DEFAULT_PORT;
_serverAddr = DEFAULT_PROTOCOL + "://" + DEFAULT_HOST + ":" + DEFAULT_PORT;
_Logger.info("Broker address: " + _serverAddr);
}
// public method to implement GET
public void getMessage(String topic) {
System.out.println("getting mesage");
try {
if (_client == null) { // if client is null, a new object is created with parameters provided to
// prevent null exception
_client = new CoapClient(_serverAddr);
} else {
_client.setURI(_serverAddr + "/" + topic); // sets uniform resource identifier for the instantiated
// object
System.out.println(_client.getURI());
}
_client.get();
} catch (Exception e) {
_Logger.log(Level.SEVERE, "Something went wrong!");
}
}
// public method to implement POST
public void postMessage(String topic, String string2) {
try {
System.out.println("posting mesage for URI " + topic + " " + string2);
if (_client == null) {
_client = new CoapClient(_serverAddr);// if client is null, a new object is created with parameters
// provided to prevent null exception
} else {
_client.setURI(_serverAddr + "/" + topic);// sets uniform resource identifier for the instantiated
// object
System.out.println("posting for " + _client.getURI());
}
_client.post(string2, MediaTypeRegistry.TEXT_PLAIN);
} catch (Exception e) {
_Logger.log(Level.SEVERE, "Something went wrong!");
}
}
// public method to implement DISCOVER
public void discoverMessage(String topic) {
System.out.println("discovering");
try {
if (_client == null) {
_client = new CoapClient(_serverAddr);// if client is null, a new object is created with parameters
// provided to prevent null exception
} else {
_client.setURI(_serverAddr + "/" + topic);// sets uniform resource identifier for the instantiated
// object
}
_client.discover();
} catch (Exception e) {
_Logger.log(Level.SEVERE, "Something went wrong!");
}
}
// public method to implement PUT
public void putMessage(String topic, String string2) {
System.out.println("Putting mesage");
try {
if (_client == null) {
_client = new CoapClient(_serverAddr);// if client is null, a new object is created with parameters
// provided to prevent null exception
} else {
_client.setURI(_serverAddr + "/" + topic);// sets uniform resource identifier for the instantiated
// object
}
_client.put(topic, MediaTypeRegistry.TEXT_PLAIN);
} catch (Exception e) {
_Logger.log(Level.SEVERE, "Something went wrong!");
}
}
// public method to implement DELETE
public void deleteMessage(String topic) {
System.out.println("deleting mesage");
try {
if (_client == null) {
_client = new CoapClient(_serverAddr);// if client is null, a new object is created with parameters
// provided to prevent null exception
} else {
_client.setURI(_serverAddr + "/" + topic);// sets uniform resource identifier for the instantiated
// object
}
_client.delete();
} catch (Exception e) {
_Logger.log(Level.SEVERE, "Something went wrong!");
}
}
public String getPath(String x) {
return x;
}
}