-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessageType.java
More file actions
286 lines (239 loc) · 8.92 KB
/
messageType.java
File metadata and controls
286 lines (239 loc) · 8.92 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package dummy;
public class messageType {
//12.1.1 method code
public static final int method_GET = 1;
public static final int method_POST = 2;
public static final int method_PUT = 3;
public static final int method_DELETE = 4;
//12.1.2. Response Codes
public static final int RESP_CLASS_SUCCESS = 2;
public static final int RESP_CLASS_CLIENT_ERROR = 4;
public static final int RESP_CLASS_SERVER_ERROR = 5;
public static final int RESP_CREATED = 65;
public static final int RESP_DELETED = 66;
public static final int RESP_VALID = 67;
public static final int RESP_CHANGED = 68;
public static final int RESP_CONTENT = 69;
public static final int RESP_BAD_REQUEST = 128;
public static final int RESP_UNAUTHORIZED = 129;
public static final int RESP_BAD_OPTION = 130;
public static final int RESP_FORBIDDEN = 131;
public static final int RESP_NOT_FOUND = 132;
public static final int RESP_METHOD_NOT_ALLOWED = 133;
public static final int RESP_REQUEST_ENTITY_TOO_LARGE = 141;
public static final int RESP_UNSUPPORTED_MEDIA_TYPE = 143;
public static final int RESP_INTERNAL_SERVER_ERROR = 160;
public static final int RESP_NOT_IMPLEMENTED = 161;
public static final int RESP_BAD_GATEWAY = 162;
public static final int RESP_SERVICE_UNAVAILABLE = 163;
public static final int RESP_GATEWAY_TIMEOUT = 164;
public static final int RESP_PROXYING_NOT_SUPPORTED = 165;
// from draft-ietf-core-block-03
public static final int RESP_REQUEST_ENTITY_INCOMPLETE = 136;
// 12.1.2. Response Codes
public static final int V3_RESP_CONTINUE = 40;
public static final int V3_RESP_OK = 80;
public static final int V3_RESP_CREATED = 81;
public static final int V3_RESP_NOT_MODIFIED = 124;
public static final int V3_RESP_BAD_REQUEST = 160;
public static final int V3_RESP_NOT_FOUND = 164;
public static final int V3_RESP_METHOD_NOT_ALLOWED = 165;
public static final int V3_RESP_UNSUPPORTED_MEDIA_TYPE = 175;
public static final int V3_RESP_INTERNAL_SERVER_ERROR = 200;
public static final int V3_RESP_BAD_GATEWAY = 202;
public static final int V3_RESP_SERVICE_UNAVAILABLE = 203;
public static final int V3_RESP_GATEWAY_TIMEOUT = 204;
public static final int V3_RESP_TOKEN_OPTION_REQUIRED = 240;
public static final int V3_RESP_URI_AUTHORITY_OPTION_REQUIRED = 241;
public static final int V3_RESP_CRITICAL_OPTION_NOT_SUPPORTED = 242;
public static final int EMPTY_MESSAGE= 0;
//message type
public static final int CON = 0;
public static final int NON = 1;
public static final int ACK = 2;
public static final int RST = 3;
//Checks whether a code indicates a request
public static boolean isRequest(int code) {
return (code >= 1) && (code <= 31);
}
//code indicates a response
public static boolean isResponse(int code) {
return (code >= 40) && (code <= 242);
}
//to check whether a code is valid
public static boolean isValid(int code) {
return (code >= 0) && (code <= 255);
}
//return response class
public static int responseClass(int code) {
return (code >> 5) & 0x7;
}
private messageType(){
}
/**
* CoAP defines four types of messages:
* Confirmable, Non-confirmable, Acknowledgment, Reset.
*/
public enum Type {
CON(0),
NON(1),ACK(2),RST(3);
public final int value;
Type(int value) {
this.value = value;
}
public static Type valueOf(int value) {
switch (value) {
case 0: return CON;
case 1: return NON;
case 2: return ACK;
case 3: return RST;
default: throw new IllegalArgumentException("Unknown CoAP type "+value);
}
}
}
public enum Code {
/** The GET code. */
GET(1),
/** The POST code. */
POST(2),
/** The PUT code. */
PUT(3),
/** The DELETE code. */
DELETE(4);
/** The code value. */
public final int value;
/**
* Instantiates a new code with the specified code value.
*
* @param value the integer value of the code
*/
Code(int value) {
this.value = value;
}
}
/**
* Converts the specified integer value to a request code.
*
* @param value the integer value
* @return the request code
* @throws IllegalArgumentException if the integer value is unrecognized
*/
public static Class<? extends MessageFormat> getMessageClass(int code) {
if (isRequest(code)) {
switch (code) {
case method_GET: return GETRequest.class;
case method_POST: return POSTRequest.class;
case method_PUT: return PUTRequest.class;
case method_DELETE: return DELETERequest.class;
default: return Request.class;
}
} else if (isResponse(code)) {
return Response.class;
} else if (code == EMPTY_MESSAGE) {
// empty messages are handled as responses
// in order to handle ACK/RST messages consistent
// with actual responses
return Response.class;
} else if (isValid(code)) {
return MessageFormat.class;
} else {
return null;
}
}
public static String toString(int code) {
switch (code) {
case EMPTY_MESSAGE:
return "Empty Message";
case 1:
return "GET Request";
case 2:
return "POST Request";
case 3:
return "PUT Request";
case 4:
return "DELETE Request";
case RESP_CREATED:
return "2.01 Created";
case RESP_DELETED:
return "2.02 Deleted";
case RESP_VALID:
return "2.03 Valid";
case RESP_CHANGED:
return "2.04 Changed";
case RESP_CONTENT:
return "2.05 Content";
case RESP_BAD_REQUEST:
return "4.00 Bad Request";
case RESP_UNAUTHORIZED:
return "4.01 Unauthorized";
case RESP_BAD_OPTION:
return "4.02 Bad Option";
case RESP_FORBIDDEN:
return "4.03 Forbidden";
case RESP_NOT_FOUND:
return "4.04 Not Found";
case RESP_METHOD_NOT_ALLOWED:
return "4.05 Method Not Allowed";
case RESP_REQUEST_ENTITY_INCOMPLETE:
return "4.08 Request Entity Incomplete";
case RESP_REQUEST_ENTITY_TOO_LARGE:
return "4.13 Request Entity Too Large";
case RESP_UNSUPPORTED_MEDIA_TYPE:
return "4.15 Unsupported Media Type";
case RESP_INTERNAL_SERVER_ERROR:
return "5.00 Internal Server Error";
case RESP_NOT_IMPLEMENTED:
return "5.01 Not Implemented";
case RESP_BAD_GATEWAY:
return "5.02 Bad Gateway";
case RESP_SERVICE_UNAVAILABLE:
return "5.03 Service Unavailable";
case RESP_GATEWAY_TIMEOUT:
return "5.04 Gateway Timeout";
case RESP_PROXYING_NOT_SUPPORTED:
return "5.05 Proxying Not Supported";
// Deprecated (Draft 3)
case V3_RESP_CONTINUE:
return "100 Continue";
case V3_RESP_OK:
return "200 OK";
case V3_RESP_CREATED:
return "201 Created";
case V3_RESP_NOT_MODIFIED:
return "304 Not Modified";
//case V3_RESP_BAD_REQUEST:
// return "400 Bad Request";
//case V3_RESP_NOT_FOUND:
// return "404 Not Found";
//case V3_RESP_METHOD_NOT_ALLOWED:
// return "405 Method Not Allowed";
case V3_RESP_UNSUPPORTED_MEDIA_TYPE:
return "415 Unsupported Media Type";
case V3_RESP_INTERNAL_SERVER_ERROR:
return "500 Internal Server Error";
case V3_RESP_BAD_GATEWAY:
return "502 Bad Gateway";
case V3_RESP_SERVICE_UNAVAILABLE:
return "503 Service Unavailable";
case V3_RESP_GATEWAY_TIMEOUT:
return "504 Gateway Timeout";
case V3_RESP_TOKEN_OPTION_REQUIRED:
return "Token Option required by server";
case V3_RESP_URI_AUTHORITY_OPTION_REQUIRED:
return "Uri-Authority Option required by server";
case V3_RESP_CRITICAL_OPTION_NOT_SUPPORTED:
return "Critical Option not supported";
}
if (isValid(code)) {
if (isRequest(code)) {
return String.format("Unknown Request [code %d]", code);
} else if (isResponse(code)) {
return String.format("Unknown Response [code %d]", code);
} else {
return String.format("Reserved [code %d]", code);
}
} else {
return String.format("Invalid Message [code %d]", code);
}
}
}