Dirk-Willem van Gulik (NXP/mbed)
/
AmpereMeter
Example self-announcing webserver which controls a servo through a smallHTML userinterface.
RestMeterHandler.cpp@0:a259777c45a3, 2010-08-14 (annotated)
- Committer:
- dirkx
- Date:
- Sat Aug 14 15:56:01 2010 +0000
- Revision:
- 0:a259777c45a3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dirkx | 0:a259777c45a3 | 1 | #include "RestMeterHandler.h" |
dirkx | 0:a259777c45a3 | 2 | |
dirkx | 0:a259777c45a3 | 3 | RestMeterHandler::RestMeterHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket) { |
dirkx | 0:a259777c45a3 | 4 | } |
dirkx | 0:a259777c45a3 | 5 | |
dirkx | 0:a259777c45a3 | 6 | RestMeterHandler::~RestMeterHandler() { |
dirkx | 0:a259777c45a3 | 7 | } |
dirkx | 0:a259777c45a3 | 8 | |
dirkx | 0:a259777c45a3 | 9 | const char *(*RestMeterHandler::callback)(const char*) = NULL; |
dirkx | 0:a259777c45a3 | 10 | |
dirkx | 0:a259777c45a3 | 11 | void RestMeterHandler::attach(const char *(*cb)(const char * command)) { |
dirkx | 0:a259777c45a3 | 12 | callback = cb; |
dirkx | 0:a259777c45a3 | 13 | } |
dirkx | 0:a259777c45a3 | 14 | |
dirkx | 0:a259777c45a3 | 15 | void RestMeterHandler::doGet() { |
dirkx | 0:a259777c45a3 | 16 | const char * p = path().c_str(); |
dirkx | 0:a259777c45a3 | 17 | const char * q = strstr(p,"cmd="); |
dirkx | 0:a259777c45a3 | 18 | |
dirkx | 0:a259777c45a3 | 19 | if (q && q[4]) |
dirkx | 0:a259777c45a3 | 20 | { |
dirkx | 0:a259777c45a3 | 21 | q+=4; |
dirkx | 0:a259777c45a3 | 22 | } else { |
dirkx | 0:a259777c45a3 | 23 | q = "none"; |
dirkx | 0:a259777c45a3 | 24 | }; |
dirkx | 0:a259777c45a3 | 25 | |
dirkx | 0:a259777c45a3 | 26 | const char * resp = "No callback"; |
dirkx | 0:a259777c45a3 | 27 | |
dirkx | 0:a259777c45a3 | 28 | printf("Calling back with %s, cb=%p\n\r", q,callback); |
dirkx | 0:a259777c45a3 | 29 | |
dirkx | 0:a259777c45a3 | 30 | if (callback) |
dirkx | 0:a259777c45a3 | 31 | resp = (*callback)(q); |
dirkx | 0:a259777c45a3 | 32 | |
dirkx | 0:a259777c45a3 | 33 | setContentLen( strlen(resp) ); |
dirkx | 0:a259777c45a3 | 34 | respHeaders()["Connection"] = "close"; |
dirkx | 0:a259777c45a3 | 35 | writeData(resp, strlen(resp)); |
dirkx | 0:a259777c45a3 | 36 | } |
dirkx | 0:a259777c45a3 | 37 | |
dirkx | 0:a259777c45a3 | 38 | void RestMeterHandler::doPost() { |
dirkx | 0:a259777c45a3 | 39 | } |
dirkx | 0:a259777c45a3 | 40 | |
dirkx | 0:a259777c45a3 | 41 | void RestMeterHandler::doHead() { |
dirkx | 0:a259777c45a3 | 42 | } |
dirkx | 0:a259777c45a3 | 43 | |
dirkx | 0:a259777c45a3 | 44 | void RestMeterHandler::onReadable() { //Data has been read |
dirkx | 0:a259777c45a3 | 45 | } |
dirkx | 0:a259777c45a3 | 46 | |
dirkx | 0:a259777c45a3 | 47 | void RestMeterHandler::onWriteable() { //Data has been written & buf is free |
dirkx | 0:a259777c45a3 | 48 | close(); //Data written, we can close the connection |
dirkx | 0:a259777c45a3 | 49 | } |
dirkx | 0:a259777c45a3 | 50 | |
dirkx | 0:a259777c45a3 | 51 | void RestMeterHandler::onClose() { //Connection is closing |
dirkx | 0:a259777c45a3 | 52 | //Nothing to do |
dirkx | 0:a259777c45a3 | 53 | } |