
Code snipet
Dependencies: NySDFileSystem libMiMic mbed-rtos mbed
Fork of AsyncHttpdSample by
main.cpp@9:c6427be12f0d, 2013-11-16 (annotated)
- Committer:
- nyatla
- Date:
- Sat Nov 16 01:25:37 2013 +0000
- Revision:
- 9:c6427be12f0d
- Parent:
- 8:384c8fb9f401
- Child:
- 10:8a9023e05971
AsyncHttpdSample
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nyatla | 0:ec1e45489427 | 1 | #include "mimic.h" |
nyatla | 8:384c8fb9f401 | 2 | #include "rtos.h" |
nyatla | 8:384c8fb9f401 | 3 | /** |
nyatla | 8:384c8fb9f401 | 4 | * @file |
nyatla | 8:384c8fb9f401 | 5 | * This program is a sample which starts httpd by a subtask. |
nyatla | 8:384c8fb9f401 | 6 | * The value counted up by a main thread is returned by Httpd of a subtask. |
nyatla | 8:384c8fb9f401 | 7 | */ |
nyatla | 6:bcf3fe4d0ba1 | 8 | |
nyatla | 6:bcf3fe4d0ba1 | 9 | |
nyatla | 8:384c8fb9f401 | 10 | static unsigned int counter=0; |
nyatla | 8:384c8fb9f401 | 11 | |
nyatla | 8:384c8fb9f401 | 12 | |
nyatla | 8:384c8fb9f401 | 13 | LocalFileSystem2 lf("local"); |
nyatla | 9:c6427be12f0d | 14 | class AsyncHttpd:public MiMic::Httpd |
nyatla | 0:ec1e45489427 | 15 | { |
nyatla | 0:ec1e45489427 | 16 | private: |
nyatla | 0:ec1e45489427 | 17 | ModUrl modurl; //basic URL parser |
nyatla | 0:ec1e45489427 | 18 | public: |
nyatla | 9:c6427be12f0d | 19 | AsyncHttpd(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort()) |
nyatla | 0:ec1e45489427 | 20 | { |
nyatla | 0:ec1e45489427 | 21 | } |
nyatla | 0:ec1e45489427 | 22 | virtual void onRequest(HttpdConnection& i_connection) |
nyatla | 0:ec1e45489427 | 23 | { |
nyatla | 6:bcf3fe4d0ba1 | 24 | char url[32]; |
nyatla | 0:ec1e45489427 | 25 | int method; |
nyatla | 8:384c8fb9f401 | 26 | unsigned int v; |
nyatla | 8:384c8fb9f401 | 27 | i_connection.lockHttpd(); |
nyatla | 8:384c8fb9f401 | 28 | v=counter; |
nyatla | 8:384c8fb9f401 | 29 | i_connection.unlockHttpd(); |
nyatla | 7:8d030ae8ddc3 | 30 | |
nyatla | 8:384c8fb9f401 | 31 | |
nyatla | 2:28fd59d6be76 | 32 | //call ModUrl module. |
nyatla | 6:bcf3fe4d0ba1 | 33 | if(this->modurl.execute(i_connection,url,32,&method)){ |
nyatla | 6:bcf3fe4d0ba1 | 34 | //send 200 OK and requested URL |
nyatla | 6:bcf3fe4d0ba1 | 35 | i_connection.sendHeader(200,"text/html",NULL); |
nyatla | 8:384c8fb9f401 | 36 | //show current counter value. |
nyatla | 8:384c8fb9f401 | 37 | i_connection.sendBodyF( |
nyatla | 8:384c8fb9f401 | 38 | "<html><body><h1>Asynchronous test</h1><div>Counter: %u</body></html>",v); |
nyatla | 0:ec1e45489427 | 39 | return; |
nyatla | 0:ec1e45489427 | 40 | } |
nyatla | 7:8d030ae8ddc3 | 41 | |
nyatla | 0:ec1e45489427 | 42 | return; |
nyatla | 0:ec1e45489427 | 43 | } |
nyatla | 0:ec1e45489427 | 44 | }; |
nyatla | 7:8d030ae8ddc3 | 45 | |
nyatla | 8:384c8fb9f401 | 46 | |
nyatla | 7:8d030ae8ddc3 | 47 | NetConfig cfg; //create network configulation |
nyatla | 0:ec1e45489427 | 48 | int main() |
nyatla | 0:ec1e45489427 | 49 | { |
nyatla | 7:8d030ae8ddc3 | 50 | Net net; //create a net instance. |
nyatla | 7:8d030ae8ddc3 | 51 | |
nyatla | 7:8d030ae8ddc3 | 52 | //try to override setting by local file. |
nyatla | 7:8d030ae8ddc3 | 53 | cfg.loadFromFile("/local/mimic.cfg"); |
nyatla | 7:8d030ae8ddc3 | 54 | |
nyatla | 9:c6427be12f0d | 55 | AsyncHttpd httpd(cfg); //create a httpd instance. |
nyatla | 7:8d030ae8ddc3 | 56 | net.start(cfg); |
nyatla | 8:384c8fb9f401 | 57 | httpd.loopTask(); //start httpd loop with new task |
nyatla | 8:384c8fb9f401 | 58 | for(;;){ |
nyatla | 8:384c8fb9f401 | 59 | httpd.lock();//prepare to access shared resource |
nyatla | 8:384c8fb9f401 | 60 | counter++; |
nyatla | 8:384c8fb9f401 | 61 | httpd.unlock();//release a lock. |
nyatla | 8:384c8fb9f401 | 62 | Thread::wait(1000); |
nyatla | 8:384c8fb9f401 | 63 | } |
nyatla | 0:ec1e45489427 | 64 | return 0; |
nyatla | 0:ec1e45489427 | 65 | } |
nyatla | 6:bcf3fe4d0ba1 | 66 |