Permalink
Browse files

Added 4.05 response for unsupported request codes.

  • Loading branch information...
1 parent f761789 commit 337b85db38fe3992c75b4e9993bb70ba15afca9a @mkovatsc committed Sep 17, 2012
Showing with 95 additions and 88 deletions.
  1. +1 −1 californium/pom.xml
  2. +8 −8 californium/src/main/java/ch/ethz/inf/vs/californium/coap/CodeRegistry.java
  3. +1 −1 californium/src/main/java/ch/ethz/inf/vs/californium/coap/DELETERequest.java
  4. +36 −66 californium/src/main/java/ch/ethz/inf/vs/californium/coap/Message.java
  5. +8 −0 californium/src/main/java/ch/ethz/inf/vs/californium/coap/Request.java
  6. +13 −0 californium/src/main/java/ch/ethz/inf/vs/californium/coap/UnsupportedRequest.java
  7. +1 −1 californium/src/main/java/ch/ethz/inf/vs/californium/endpoint/Resource.java
  8. +17 −1 californium/src/main/java/ch/ethz/inf/vs/californium/layers/TransactionLayer.java
  9. +1 −1 cf-browser/pom.xml
  10. +1 −1 cf-client/pom.xml
  11. +1 −1 cf-helloworld-client/pom.xml
  12. +1 −1 cf-helloworld-server/pom.xml
  13. +1 −1 cf-ipso/pom.xml
  14. +1 −1 cf-plugtest-client/pom.xml
  15. +1 −1 cf-plugtest-server/pom.xml
  16. +1 −1 cf-rtt/pom.xml
  17. +1 −1 cf-server/pom.xml
  18. +1 −1 pom.xml
  19. BIN run/{cf-browser-0.8.3-SNAPSHOT.jar → cf-browser-0.8.4-SNAPSHOT.jar}
  20. BIN run/{cf-client-0.8.3-SNAPSHOT.jar → cf-client-0.8.4-SNAPSHOT.jar}
  21. BIN run/{cf-helloworld-client-0.8.3-SNAPSHOT.jar → cf-helloworld-client-0.8.4-SNAPSHOT.jar}
  22. BIN run/{cf-helloworld-server-0.8.3-SNAPSHOT.jar → cf-helloworld-server-0.8.4-SNAPSHOT.jar}
  23. BIN run/{cf-ipso-0.8.3-SNAPSHOT.jar → cf-ipso-0.8.4-SNAPSHOT.jar}
  24. BIN run/{cf-plugtest-client-0.8.3-SNAPSHOT.jar → cf-plugtest-client-0.8.4-SNAPSHOT.jar}
  25. BIN run/{cf-plugtest-server-0.8.3-SNAPSHOT.jar → cf-plugtest-server-0.8.4-SNAPSHOT.jar}
  26. BIN run/{cf-rtt-0.8.3-SNAPSHOT.jar → cf-rtt-0.8.4-SNAPSHOT.jar}
  27. BIN run/{cf-server-0.8.3-SNAPSHOT.jar → cf-server-0.8.4-SNAPSHOT.jar}
View
2 californium/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<contributors>
View
16 californium/src/main/java/ch/ethz/inf/vs/californium/coap/CodeRegistry.java
@@ -129,24 +129,24 @@ public static int responseClass(int code) {
return (code >> 5) & 0x7;
}
- public static Class<? extends Message> getMessageClass(int code) {
+ public static Message getMessageSubClass(int code) {
if (isRequest(code)) {
switch (code) {
case METHOD_GET:
- return GETRequest.class;
+ return new GETRequest();
case METHOD_POST:
- return POSTRequest.class;
+ return new POSTRequest();
case METHOD_PUT:
- return PUTRequest.class;
+ return new PUTRequest();
case METHOD_DELETE:
- return DELETERequest.class;
+ return new DELETERequest();
default:
- return Request.class;
+ return new UnsupportedRequest(code);
}
} else if (isResponse(code) || code == EMPTY_MESSAGE) {
- return Response.class;
+ return new Response(code);
} else {
- return Message.class;
+ return new Message(null, code);
}
}
View
2 californium/src/main/java/ch/ethz/inf/vs/californium/coap/DELETERequest.java
@@ -35,7 +35,7 @@
public DELETERequest() {
super(CodeRegistry.METHOD_DELETE, true);
}
-
+
@Override
public void dispatch(RequestHandler handler) {
handler.performDELETE(this);
View
102 californium/src/main/java/ch/ethz/inf/vs/californium/coap/Message.java
@@ -61,36 +61,37 @@
// CoAP-specific constants /////////////////////////////////////////////////////
- // number of bits used for the encoding of the CoAP version field
+ /** number of bits used for the encoding of the CoAP version field */
public static final int VERSION_BITS = 2;
- // number of bits used for the encoding of the message type field
+ /** number of bits used for the encoding of the message type field */
public static final int TYPE_BITS = 2;
- // number of bits used for the encoding of the option count field
+ /** number of bits used for the encoding of the option count field */
public static final int OPTIONCOUNT_BITS = 4;
- // number of bits used for the encoding of the request method/
- // response code field
+ /** number of bits used for the encoding of the request method/response code field */
public static final int CODE_BITS = 8;
- // number of bits used for the encoding of the transaction ID
+ /** number of bits used for the encoding of the message ID */
public static final int ID_BITS = 16;
- // number of bits used for the encoding of the option delta
+ /** number of bits used for the encoding of the option delta */
public static final int OPTIONDELTA_BITS = 4;
- // number of bits used for the encoding of the base option length field
- // if all bits in this field are set to one, the extended option length
- // field is additionally used to encode the option length
+ /**
+ * Number of bits used for the encoding of the base option length field.
+ * If all bits in this field are set to one, the extended option length field is additionally used to encode the option length.
+ */
public static final int OPTIONLENGTH_BASE_BITS = 4;
- // number of bits used for the encoding of the extended option length field
- // this field is used when all bits in the base option length field
- // are set to one
+ /**
+ * Number of bits used for the encoding of the extended option length field.
+ * This field is used when all bits in the base option length field are set to one.
+ */
public static final int OPTIONLENGTH_EXTENDED_BITS = 8;
- /*
+ /**
* The message's type which can have the following values:
*
* 0: Confirmable
@@ -105,30 +106,30 @@
RST
}
+ /** CoAP version supported by this Californium version */
+ public static final int SUPPORTED_VERSION = 1; // I-D
+
// Derived constants ///////////////////////////////////////////////////////////
- // maximum option delta that can be encoded without using fencepost options
+ /** maximum option delta that can be encoded without using fencepost options */
public static final int MAX_OPTIONDELTA = (1 << OPTIONDELTA_BITS) - 1;
- // maximum option length that can be encoded using
- // the base option length field only
+ /** maximum option length that can be encoded using the base option length field only */
public static final int MAX_OPTIONLENGTH_BASE = (1 << OPTIONLENGTH_BASE_BITS) - 2;
// Members /////////////////////////////////////////////////////////////////////
- /** The receiver for this message. */
private EndpointAddress peerAddress = null;
- /** The message's payload. */
private byte[] payload = null;
- /** The CoAP version used. For now, this must be set to 1. */
- private int version = 1;
+ /* The CoAP version used */
+ private int version = SUPPORTED_VERSION;
- /** The message type (CON, NON, ACK, or RST). */
+ /* The message type (CON, NON, ACK, or RST). */
private messageType type = null;
- /**
+ /*
* The message code:
*
* 0: Empty
@@ -137,13 +138,12 @@
*/
private int code = 0;
- /** The message ID. Set according to request or handled by {@link ch.ethz.inf.vs.californium.layers.TransactionLayer} when -1. */
+ /* The message ID. Set according to request or handled by {@link ch.ethz.inf.vs.californium.layers.TransactionLayer} when -1. */
private int messageID = -1;
- /** The list of header options set for the message. */
+ /* The list of header options set for the message. */
private Map<Integer, List<Option>> optionMap = new TreeMap<Integer, List<Option>>();
- /** A time stamp associated with the message. */
private long timestamp = -1;
private int retransmissioned = 0;
@@ -178,14 +178,14 @@ public static messageType getTypeByValue(int numeric) {
// Constructors ////////////////////////////////////////////////////////////////
- /*
+ /**
* Default constructor for a new CoAP message
*/
public Message() {
}
- /*
- * Constructor for a new CoAP message
+ /**
+ * Extended constructor for a new CoAP message, e.g., empty ACK or RST
*
* @param type the type of the CoAP message
* @param code the code of the CoAP message (See class CodeRegistry)
@@ -195,20 +195,6 @@ public Message(messageType type, int code) {
this.code = code;
}
- /*
- * Constructor for a new CoAP message
- *
- * @param uri the URI of the CoAP message
- * @param payload the payload of the CoAP message
- */
- public Message(URI address, messageType type, int code, int mid, byte[] payload) {
- this.setURI(address);
- this.type = type;
- this.code = code;
- this.messageID = mid;
- this.payload = payload;
- }
-
// Serialization ///////////////////////////////////////////////////////////////
/**
@@ -336,40 +322,29 @@ public Message(URI address, messageType type, int code, int mid, byte[] payload)
*
* @param byteArray A byte array containing the CoAP encoding of the message
*
+ * @return a parsed CoAP message as correspondingly extended Message object, e.g., GETRequest
*/
public static Message fromByteArray(byte[] byteArray) {
//Initialize DatagramReader
DatagramReader datagram = new DatagramReader(byteArray);
//Read current version
- int version = datagram.read(VERSION_BITS); // non-blocking
+ int version = datagram.read(VERSION_BITS);
+ if (version!=SUPPORTED_VERSION) {
+ return null;
+ }
//Read current type
messageType type = getTypeByValue(datagram.read(TYPE_BITS));
//Read number of options
int optionCount = datagram.read(OPTIONCOUNT_BITS);
- //Read code
- int code = datagram.read(CODE_BITS);
- if (!CodeRegistry.isValid(code)) {
- LOG.info(String.format("Received invalid message code: %d\n", code));
- return null;
- }
-
// create new message with subtype according to code number
- Message msg;
- try {
- msg = CodeRegistry.getMessageClass(code).newInstance();
- } catch (Exception e) {
- LOG.severe(String.format("Cannot instantiate Message class %d:\n", code, e.getMessage()));
- return null;
- }
+ Message msg = CodeRegistry.getMessageSubClass( datagram.read(CODE_BITS) );
- msg.version = version;
msg.type = type;
- msg.code = code;
//Read message ID
msg.messageID = datagram.read(ID_BITS);
@@ -394,17 +369,12 @@ public static Message fromByteArray(byte[] byteArray) {
//Read option length
int length = datagram.read(OPTIONLENGTH_BASE_BITS);
-
if (length > MAX_OPTIONLENGTH_BASE)
{
- //Read extended option length
- //length = datagram.read(OPTIONLENGTH_EXTENDED_BITS)
- // - (MAX_OPTIONLENGTH_BASE + 1);
-
length += datagram.read(OPTIONLENGTH_EXTENDED_BITS);
}
+
//Read option
- //Option opt = new Option (datagram.readBytes(length), currentOption);
Option opt = Option.fromNumber(currentOption);
opt.setValue(datagram.readBytes(length));
View
8 californium/src/main/java/ch/ethz/inf/vs/californium/coap/Request.java
@@ -78,6 +78,14 @@
/**
* Instantiates a new request.
+ */
+ public Request(int method) {
+ super();
+ this.setCode(method);
+ }
+
+ /**
+ * Instantiates a new request.
*
* @param method The method code of the message
* @param confirmable True if the request is to be sent as a confirmable
View
13 californium/src/main/java/ch/ethz/inf/vs/californium/coap/UnsupportedRequest.java
@@ -0,0 +1,13 @@
+package ch.ethz.inf.vs.californium.coap;
+
+public class UnsupportedRequest extends Request {
+
+ public UnsupportedRequest(int code) {
+ super(code);
+ }
+
+ @Override
+ public void send() {
+ LOG.severe("Cannot send UnsupportedRequest");
+ }
+}
View
2 californium/src/main/java/ch/ethz/inf/vs/californium/endpoint/Resource.java
@@ -390,7 +390,7 @@ public Resource getResource(String path) {
* the given path.
*
* @param path the path to the resource of interest
- * @param resource a resource that will be created at the given path or null for get only
+ * @param last set to true if the last existing resource along a path shall be returned
* @return The Resource of interest or null if not found and create is false
*/
public Resource getResource(String path, boolean last) {
View
18 californium/src/main/java/ch/ethz/inf/vs/californium/layers/TransactionLayer.java
@@ -37,9 +37,11 @@
import java.util.Timer;
import java.util.TimerTask;
+import ch.ethz.inf.vs.californium.coap.CodeRegistry;
import ch.ethz.inf.vs.californium.coap.Message;
import ch.ethz.inf.vs.californium.coap.ObservingManager;
import ch.ethz.inf.vs.californium.coap.Response;
+import ch.ethz.inf.vs.californium.coap.UnsupportedRequest;
import ch.ethz.inf.vs.californium.util.Properties;
/**
@@ -178,6 +180,20 @@ protected void doSendMessage(Message msg) throws IOException {
@Override
protected void doReceiveMessage(Message msg) {
+
+ // check if supported
+ if (msg instanceof UnsupportedRequest) {
+ try {
+ Message reply = msg.newReply(msg.isConfirmable());
+ reply.setCode(CodeRegistry.RESP_METHOD_NOT_ALLOWED);
+ reply.setPayload(String.format("Method code %d not supported.", msg.getCode()));
+ sendMessageOverLowerLayer(reply);
+ LOG.info(String.format("Replied to unsupported request code %d: %s", msg.getCode(), msg.key()));
+ } catch (IOException e) {
+ LOG.severe(String.format("Replying to unsupported request code failed: %s\n%s", msg.key(), e.getMessage()));
+ }
+ return;
+ }
// check for duplicate
if (dupCache.containsKey(msg.key())) {
@@ -191,8 +207,8 @@ protected void doReceiveMessage(Message msg) {
// retransmit reply
try {
- LOG.info(String.format("Replied to duplicate confirmable: %s", msg.key()));
sendMessageOverLowerLayer(reply);
+ LOG.info(String.format("Replied to duplicate confirmable: %s", msg.key()));
} catch (IOException e) {
LOG.severe(String.format("Replying to duplicate confirmable failed: %s\n%s", msg.key(), e.getMessage()));
}
View
2 cf-browser/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<dependencies>
View
2 cf-client/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<dependencies>
View
2 cf-helloworld-client/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<dependencies>
View
2 cf-helloworld-server/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<dependencies>
View
2 cf-ipso/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<dependencies>
View
2 cf-plugtest-client/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<contributors>
View
2 cf-plugtest-server/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<dependencies>
View
2 cf-rtt/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<dependencies>
View
2 cf-server/pom.xml
@@ -13,7 +13,7 @@
<parent>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
</parent>
<dependencies>
View
2 pom.xml
@@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>ch.ethz.inf.vs</groupId>
<artifactId>cf-root</artifactId>
- <version>0.8.3-SNAPSHOT</version>
+ <version>0.8.4-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Californium Root</name>
View
BIN run/cf-browser-0.8.3-SNAPSHOT.jar → run/cf-browser-0.8.4-SNAPSHOT.jar
Binary file not shown.
View
BIN run/cf-client-0.8.3-SNAPSHOT.jar → run/cf-client-0.8.4-SNAPSHOT.jar
Binary file not shown.
View
BIN run/cf-helloworld-client-0.8.3-SNAPSHOT.jar → run/cf-helloworld-client-0.8.4-SNAPSHOT.jar
Binary file not shown.
View
BIN run/cf-helloworld-server-0.8.3-SNAPSHOT.jar → run/cf-helloworld-server-0.8.4-SNAPSHOT.jar
Binary file not shown.
View
BIN run/cf-ipso-0.8.3-SNAPSHOT.jar → run/cf-ipso-0.8.4-SNAPSHOT.jar
Binary file not shown.
View
BIN run/cf-plugtest-client-0.8.3-SNAPSHOT.jar → run/cf-plugtest-client-0.8.4-SNAPSHOT.jar
Binary file not shown.
View
BIN run/cf-plugtest-server-0.8.3-SNAPSHOT.jar → run/cf-plugtest-server-0.8.4-SNAPSHOT.jar
Binary file not shown.
View
BIN run/cf-rtt-0.8.3-SNAPSHOT.jar → run/cf-rtt-0.8.4-SNAPSHOT.jar
Binary file not shown.
View
BIN run/cf-server-0.8.3-SNAPSHOT.jar → run/cf-server-0.8.4-SNAPSHOT.jar
Binary file not shown.

0 comments on commit 337b85d

Please sign in to comment.