Skip to content

Add HTTPServer example #17

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

kegilbert
Copy link
Contributor

@kegilbert kegilbert commented Jan 7, 2019

Updated version of: https://github.com/janjongboom/mbed-os-example-http-server/

Placing this example in the docs only repo as per previous discussions with @cmonr until the examples standardization work is further along.

Wanted some feedback from Jan and someone on the networking group (tagged Seppo for now). Please feel free to tag anyone else who may be interested. We don't have CI yet in this repo but I have run the code through astyle and built it with GCC_ARM, IAR, and ARM compilers.

@AnotherButler, this was one of the new examples we had discussed with @iriark01 and @SenRamakri

@janjongboom
Copy link

@kegilbert Great idea, happy to see this included.

  1. Why are the docs still mentioning easy connect whilst the code is using the default interface pattern.
  2. Same in mbed_app.json, there's a network type selection screen.

Note that I've added Greentea tests to Mbed HTTP recently. Could we included them in the CI as well for this repo?

@janjongboom
Copy link

janjongboom commented Jan 8, 2019

One of the things I would consider could use improving is to use the asynchronous socket interface for everything. I think we could get rid of the threads.

Also, I'd suggest to put the stats on a web page, instead of on the console. That'd be more fun.

@SeppoTakalo
Copy link
Contributor

Is the aim to publish this as a official example?

I would prefer this not to be part of official Handbook, but instead published somewhere in mbed.org, or do we have some kind of Cookbook.
Reason for that is, there is no team to actually maintain and keep this up to date, so we should not announce it as such.

Copy link
Contributor

@SeppoTakalo SeppoTakalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the approach where the code lives inside header files.

Can you move the HTTPresponseparser and server to .cpp files?

printf("Server could not be started... %d\n", res);
}

wait(osWaitForever);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is here?

If we rely on event-loop to run, and main() has nothing else to do, can we just same some RAM and run event-loop from main thread?

@janjongboom
Copy link

@SeppoTakalo
Copy link
Contributor

I think that is the proper location for it.

We should have clear distinction between what examples are part of API handbook, and what are just examples build on top of Mbed OS 5.

We don't have HTTP library, and therefore this example should not end up in the handbook.

But we should create some kind of Cookbook (did we already have it at some point?) that demonstrates all kind of cool things that you could build on top of Mbed OS.

@kegilbert
Copy link
Contributor Author

@SeppoTakalo Good point, there's ongoing work this release to expand and unify our examples. We don't really have many examples now that build interesting user applications off of Mbed OS, and as such we don't have a proper location yet for them to live.

Moving forward the plan is to add a section to the Handbook Tutorial page that includes more varied examples. These examples would live in a consolidated Github repo allowing CI, OS release updates, and review processes to become standardized. I would like some of the examples to use libraries outside of Mbed OS as a way to showcase how to integrate other applications and allow some really interesting projects. This does add more maintenance problems though and am open to suggestions. We'll be having some internal discussions soon, I'll be sure to include both of you in the process.

@cmonr
Copy link
Contributor

cmonr commented Jan 9, 2019

@ARMmbed/mbed-docs Thoughts?

@iriark01
Copy link

iriark01 commented Jan 9, 2019

@SeppoTakalo it should be in the handbook (the cookbook is no longer supported), just not in the API references (that's for code snippets). We are absolutely looking for solid, interesting and useful examples to include in the docs.

@cmonr
Copy link
Contributor

cmonr commented Jan 26, 2019

@iriark01 @SeppoTakalo @kegilbert Thoughts on moving this forward, or should this be closed?

@iriark01
Copy link

Sorry, I was waiting for @SeppoTakalo to reply.

@iriark01
Copy link

Also, @AnotherButler is collecting these

Copy link
Contributor

@SeppoTakalo SeppoTakalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,

If this is going to go into the handbook, and we don't have the cookbook anymore, then I need to request some changes to it.

  1. Documentation is outdated, it refers to easy-connect, but does not use it. It also mentions outdated ESP8266 patches.
  2. mbed_app.json defines parameters that are not used in this app. Those were used by easy-connect.
  3. Implementation needs to go into .cpp files. We should not have all the code in header files.
  4. Other small changes suggested.

Other than those, this is a good example.
Who is maintaining tha HTTP library that this depends on?

if (recv_ret < -3000 || recv_ret == 0) {
return;
} else {
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this sequence supposed to do?
Any negative value should be handled as a error. You should not continue reading the socket anymore.

https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/Socket.h#L108..L113

@@ -0,0 +1,191 @@
Apache License
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need full license text? Other examples do not have it, they just refer to Apache 2.0 in the source files.


## To build

1. Open ``mbed_app.json`` and change the `network-interface` option to your connectivity method ([more info](https://github.com/ARMmbed/easy-connect)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not prefer to use easy-connect. It is not something we maintain.

"mbed-http.http-buffer-size": 2048,
"platform.stdio-baud-rate": 115200,
"platform.thread-stats-enabled": 1,
"nsapi.socket-stats-enable": 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would convert this example to use the default network interface and limit this file to only

{
    "target_overrides": {
        "*": {
            "mbed-http.http-buffer-size": 2048,
            "platform.stdio-baud-rate": 115200,
            "platform.thread-stats-enabled": 1,
            "nsapi.socket-stats-enable": 1
            }
     }
}


// Connect to the network (see mbed_app.json for the connectivity method used)
NetworkInterface *network = NetworkInterface::get_default_instance();
if (!network) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NetworkInterface::get_default_instance() will give you the interface, but it does not connect.

You need to also add network->connect()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HttpServer class constructor connects with the provided interface.


* K64F with Ethernet.
* NUCLEO_F411RE with ESP8266.
* For ESP8266, you need [this patch](https://github.com/ARMmbed/esp8266-driver/pull/41).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is outdated, the external repository is not used anymore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still valid.

#include "http_parser.h"
#include "http_parsed_url.h"

static const char *get_http_status_string(uint16_t status_code)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static functions in header files?

@kegilbert
Copy link
Contributor Author

Apologies for the delay! I've done some work to remove references to easy-connect and some minor cleanup. I'm a bit tied up at the moment with the example consolidation project but I'll get back to this ASAP to resolve Seppo's other comments.

Thanks for the review!

@cmonr
Copy link
Contributor

cmonr commented Feb 14, 2019

@kegilbert Ping

@kegilbert
Copy link
Contributor Author

Sorry about that, flu and example consolidation sort of knocked me out for a good while. Hoping to polish this off end of this week/beginning of next week.

@kegilbert
Copy link
Contributor Author

Large scale refactor finished and ran through astyle. Still need to clean up some comments but functionally mostly there.

@janjongboom I poked around a bit with getting the thread stats to display on the webpage but my Javascript fu was a bit lacking. I wasn't able to get the XMLHttpRequest onstagechange to work correctly (think the object was being killed as it fell out of scope before the response could be sent). I can poke around a bit more later if you think it'd be worth it.

@janjongboom
Copy link

@kegilbert Reviewed, and added some changes here: kegilbert#1

mbed-webserver

@kegilbert
Copy link
Contributor Author

@janjongboom Merged and test with three concurrent connections. Looks pretty awesome! My only concern is the addition of another external library, who maintains rapidjson?

@janjongboom
Copy link

@kegilbert Tencent. It's not an Mbed specific library.

@kegilbert
Copy link
Contributor Author

@SeppoTakalo @janjongboom Any further thoughts/feedback? Going forward into 5.13 this will be tested along with every other example. Is the Tencent RapidJSON library a stable enough product do you think to not impact CI?

Copy link
Contributor

@SeppoTakalo SeppoTakalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small changes requested.
I think this is now leaking memory.

m->thread = t;
socket_threads.push_back(m);
} else {
delete clt_sock;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete nullptr?
This else-block does not have any effect.


* K64F with Ethernet.
* NUCLEO_F411RE with ESP8266.
* For ESP8266, you need [this patch](https://github.com/ARMmbed/esp8266-driver/pull/41).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still valid.


void HttpServer::receive_data()
{
TCPSocket *socket = sockets.back();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to use .pop_back(), because now it looks like the sockets vector is growing constantly as I cannot find any place where sockets would be removed from there.

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

Successfully merging this pull request may close these issues.

5 participants