Simple HTTP example

Dependencies:   LWIPInterface NetworkSocketAPI events funcptr mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Fri Apr 22 03:21:03 2016 -0500
Parent:
0:9dd004b6a8b7
Commit message:
Match changes to events and take advantage of EventLoop

Changed in this revision

SimpleHTTP.cpp Show annotated file Show diff for this revision Revisions of this file
SimpleHTTP.h Show annotated file Show diff for this revision Revisions of this file
events.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 9dd004b6a8b7 -r ca2041d3a4a6 SimpleHTTP.cpp
--- a/SimpleHTTP.cpp	Thu Apr 21 18:36:07 2016 -0500
+++ b/SimpleHTTP.cpp	Fri Apr 22 03:21:03 2016 -0500
@@ -2,9 +2,8 @@
 
 #define BUFFER_SIZE 4096
 
-SimpleHTTP::SimpleHTTP(EventQueue *queue, NetworkStack *stack)
-    : _queue(queue)
-    , _stack(stack) {
+SimpleHTTP::SimpleHTTP(NetworkStack *stack)
+    : _stack(stack) {
     memset(&_stats, 0, sizeof _stats);
 }
 
@@ -13,6 +12,10 @@
 }
 
 void SimpleHTTP::start() {
+    // Startup event loop
+    _loop.start();
+
+    // Startup network code
     int err;
 
     err = _server.open(_stack);
@@ -25,12 +28,16 @@
     if (err) _stats.errors++;
 
     _server.set_blocking(false);
-    _net_event.attach(_queue, this, &SimpleHTTP::_net_cb);
+    _net_event.attach(&_loop, this, &SimpleHTTP::_net_cb);
     _server.attach(&_net_event, &Event<void()>::call);
 }
 
 void SimpleHTTP::stop() {
+    // Detach server
     _server.attach(0);
+
+    // End event loop
+    _loop.stop();
 }
 
 int SimpleHTTP::_handle(char *http) {
diff -r 9dd004b6a8b7 -r ca2041d3a4a6 SimpleHTTP.h
--- a/SimpleHTTP.h	Thu Apr 21 18:36:07 2016 -0500
+++ b/SimpleHTTP.h	Fri Apr 22 03:21:03 2016 -0500
@@ -5,6 +5,7 @@
 #define SIMPLE_HTTP_H
 
 #include "FuncPtr.h"
+#include "EventLoop.h"
 #include "Event.h"
 #include "TCPServer.h"
 #include <map>
@@ -16,7 +17,7 @@
 public:
     /** SimpleHTTP server lifetime
      */
-    SimpleHTTP(EventQueue *queue, NetworkStack *stack);
+    SimpleHTTP(NetworkStack *stack);
     ~SimpleHTTP();
 
     /** Start serving HTTP requests
@@ -44,7 +45,7 @@
     unsigned get_recv();
 
 private:
-    EventQueue *_queue;
+    EventLoop _loop;
     NetworkStack *_stack;
     TCPServer _server;
     std::map<std::string, FuncPtr<int(char *, unsigned)> > _get_cbs;
diff -r 9dd004b6a8b7 -r ca2041d3a4a6 events.lib
--- a/events.lib	Thu Apr 21 18:36:07 2016 -0500
+++ b/events.lib	Fri Apr 22 03:21:03 2016 -0500
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/geky/code/events/#dcd589b578ca
+https://developer.mbed.org/users/geky/code/events/#62767e708bb6
diff -r 9dd004b6a8b7 -r ca2041d3a4a6 main.cpp
--- a/main.cpp	Thu Apr 21 18:36:07 2016 -0500
+++ b/main.cpp	Fri Apr 22 03:21:03 2016 -0500
@@ -12,8 +12,7 @@
 unsigned count_blue = 0;
 
 LWIPInterface lwip;
-EventQueue queue;
-SimpleHTTP http(&queue, &lwip);
+SimpleHTTP http(&lwip);
 
 #define IMAGE_URL (                                     \
     "https://developer.mbed.org/media/cache/platforms/" \
@@ -112,7 +111,5 @@
     http.start();
 
     printf("Hi! I'm at %s\r\n", lwip.get_ip_address());
-
-    queue.dispatch();
 }