gillesDD / jcoap

Automatically exported from code.google.com/p/jcoap
0 stars 0 forks source link

Writing a coap server #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,

i'm trying to write a litte coap server.

but i'am quite lost :

I think i have to implement CoapServerChannel.

I open a Datagram Socket, and i listen to port 61616. (in the listen method)

When established, i can deal with received data, put them in a CoapMessage.
But i think that i should in that method build a CoapChannel, if i want to use 
it to send a response ?
Is there any examples of how to use your code ?
Thanks.

Original issue reported on code.google.com by sylvain....@gmail.com on 19 May 2011 at 10:49

GoogleCodeExporter commented 9 years ago
Hi,
just some modifications of your code, to get the correct length and value of a 
long option (makes an error while checking .well-knonw/core URI)...

*Thanks for your work.

------------------------------------------------------------------------

here's the patch

------------------------------------------------------------------------

+++ src/org/ws4d/coap/messages/CoapHeaderOptions.java   (working copy)
@@ -55,20 +55,13 @@
        CoapHeaderOptions result = new CoapHeaderOptions();
        /* again, we need to keep track of this since we only receive deltas
         * and never concrete numbers */
-       int lastOptionNumber = -1;
+       int lastOptionNumber = 0;
        int arrayIndex = offset;
        for (int i = 0; i<option_count; i++) {
            CoapHeaderOption tmp = new CoapHeaderOption();
            /* Calculate Option Number from Delta */
-           if (lastOptionNumber < 1) {
-               /* ContentType-Option appears to be mandatory and thus 
-                * necessarily the first option-number to expect */
-               tmp.setOptionNumber(HeaderOptionNumber.Content_Type);
-               lastOptionNumber = 1;
-           } else {
-               tmp.setOptionNumber(((bytes[arrayIndex] & 0xF0) >> 4) + lastOptionNumber);
-               lastOptionNumber = tmp.getOptionNumber();
-           }
+           tmp.setOptionNumber(((bytes[arrayIndex] & 0xF0) >> 4) + lastOptionNumber);
+           lastOptionNumber = tmp.getOptionNumber();
            result.length+=1; /* keep track of length */

            /* Calculate length fields and real length */
@@ -80,7 +73,7 @@
            } else {
                tmp.setShortLength(bytes[arrayIndex++] & 0x0F);
                tmp.setLongLength(bytes[arrayIndex++]);
-               tmpLength = tmp.getLongLength();
+               tmpLength = tmp.getShortLength() + tmp.getLongLength();
                result.length += 1; /* additional length byte */
            }
            result.length += tmpLength;
@@ -105,6 +98,16 @@
    public void addOption(int option_number, byte[] value) throws Exception {
        headerOptions.add(new CoapHeaderOption(option_number, value));
    }
+
+   public CoapHeaderOption getOption(int option_number) {
+       for(CoapHeaderOption opt : headerOptions) {
+           if (opt.getOptionNumber() > option_number)
+               break;
+           if (opt.getOptionNumber() == option_number)
+               return opt;
+       }
+       return null;
+   }

    public int getLength() {
        return this.length;
Index: src/org/ws4d/coap/messages/CoapHeader.java
===================================================================
--- src/org/ws4d/coap/messages/CoapHeader.java  (revision 28)
+++ src/org/ws4d/coap/messages/CoapHeader.java  (working copy)
@@ -76,6 +76,12 @@
    public void addOption(int option_number, byte[] value) {
        this.addOption(new CoapHeaderOption(option_number, value));
    }
+
+   public CoapHeaderOption getOption(int option_number) {
+       if (this.options == null)
+           return null;
+       return this.options.getOption(option_number);
+   }

    public MessageCode getCode() {
        return code.getCode();

Original comment by sylvain....@gmail.com on 21 May 2011 at 8:46

GoogleCodeExporter commented 9 years ago
Hi, there's now a Server implementation in the repository. Could you please try 
that? You'll find an example in the test project...

Thanks for providing a patch. It would be nice if you could create your own 
repository clone, make your changes and file a pull request/rewie. Cloning is 
an option in Google Code at "Source"....

Original comment by n...@nclm.de on 30 May 2011 at 1:08