Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing a coap server #2

Open
GoogleCodeExporter opened this issue Aug 18, 2015 · 2 comments
Open

Writing a coap server #2

GoogleCodeExporter opened this issue Aug 18, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 19 May 2011 at 10:49

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 21 May 2011 at 8:46

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 30 May 2011 at 1:08

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant