Sensor with Web Server

Dependencies:   EthernetInterface mbed-rpc mbed-rtos mbed

Committer:
afilipem
Date:
Tue Apr 08 12:13:32 2014 +0000
Revision:
0:c385e589a779
1 version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
afilipem 0:c385e589a779 1 #include "HTTPServer.h"
afilipem 0:c385e589a779 2
afilipem 0:c385e589a779 3 #define INVALID_FORMATTER "No valid formatter specified"
afilipem 0:c385e589a779 4
afilipem 0:c385e589a779 5 bool cmp(char* a, char* b)
afilipem 0:c385e589a779 6 {
afilipem 0:c385e589a779 7 return strcmp(a,b) < 0;
afilipem 0:c385e589a779 8 }
afilipem 0:c385e589a779 9
afilipem 0:c385e589a779 10 HTTPServer::HTTPServer(Formatter *f):
afilipem 0:c385e589a779 11 socket(),
afilipem 0:c385e589a779 12 handlers(&cmp),
afilipem 0:c385e589a779 13 formatter(f),
afilipem 0:c385e589a779 14 reply(),
afilipem 0:c385e589a779 15 command()
afilipem 0:c385e589a779 16 {
afilipem 0:c385e589a779 17 }
afilipem 0:c385e589a779 18
afilipem 0:c385e589a779 19 HTTPServer::~HTTPServer()
afilipem 0:c385e589a779 20 {
afilipem 0:c385e589a779 21 for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin();
afilipem 0:c385e589a779 22 itor != handlers.end();
afilipem 0:c385e589a779 23 ++itor)
afilipem 0:c385e589a779 24 delete itor->second;
afilipem 0:c385e589a779 25
afilipem 0:c385e589a779 26 if(formatter)
afilipem 0:c385e589a779 27 delete formatter;
afilipem 0:c385e589a779 28 }
afilipem 0:c385e589a779 29
afilipem 0:c385e589a779 30 bool HTTPServer::init(int port)
afilipem 0:c385e589a779 31 {
afilipem 0:c385e589a779 32 socket.set_blocking(true);
afilipem 0:c385e589a779 33 if(socket.bind(port))
afilipem 0:c385e589a779 34 {
afilipem 0:c385e589a779 35 printf("Could not bind on port %d.\n", port);
afilipem 0:c385e589a779 36 return false;
afilipem 0:c385e589a779 37 }
afilipem 0:c385e589a779 38
afilipem 0:c385e589a779 39 if(socket.listen())
afilipem 0:c385e589a779 40 {
afilipem 0:c385e589a779 41 printf("Could not listen %d\n", port);
afilipem 0:c385e589a779 42 return false;
afilipem 0:c385e589a779 43 }
afilipem 0:c385e589a779 44
afilipem 0:c385e589a779 45 return true;
afilipem 0:c385e589a779 46 }
afilipem 0:c385e589a779 47
afilipem 0:c385e589a779 48 void HTTPServer::run()
afilipem 0:c385e589a779 49 {
afilipem 0:c385e589a779 50 TCPSocketConnection c;
afilipem 0:c385e589a779 51 while(true)
afilipem 0:c385e589a779 52 {
afilipem 0:c385e589a779 53 while(socket.accept(c));
afilipem 0:c385e589a779 54 c.set_blocking(false, 1000);
afilipem 0:c385e589a779 55 while(c.is_connected())
afilipem 0:c385e589a779 56 {
afilipem 0:c385e589a779 57 char buffer[512];
afilipem 0:c385e589a779 58 int n = c.receive_all(buffer, sizeof(buffer)-1);
afilipem 0:c385e589a779 59 if(n == 0)
afilipem 0:c385e589a779 60 {
afilipem 0:c385e589a779 61 c.close();
afilipem 0:c385e589a779 62 break;
afilipem 0:c385e589a779 63 }
afilipem 0:c385e589a779 64 else if(n != -1)
afilipem 0:c385e589a779 65 {
afilipem 0:c385e589a779 66 printf("Received data\n");
afilipem 0:c385e589a779 67 buffer[n] = '\0';
afilipem 0:c385e589a779 68 handle_request(buffer);
afilipem 0:c385e589a779 69 if(formatter != NULL)
afilipem 0:c385e589a779 70 {
afilipem 0:c385e589a779 71 printf("Sending data...");
afilipem 0:c385e589a779 72 char *page = formatter->get_page(reply);
afilipem 0:c385e589a779 73 do
afilipem 0:c385e589a779 74 {
afilipem 0:c385e589a779 75 c.send(page, strlen(page)+1);
afilipem 0:c385e589a779 76 page = formatter->get_page(reply);
afilipem 0:c385e589a779 77 }while(strlen(page)>0);
afilipem 0:c385e589a779 78 printf("done\n");
afilipem 0:c385e589a779 79 }
afilipem 0:c385e589a779 80 else
afilipem 0:c385e589a779 81 c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1);
afilipem 0:c385e589a779 82 }
afilipem 0:c385e589a779 83 else
afilipem 0:c385e589a779 84 printf("Error while receiving data\n");
afilipem 0:c385e589a779 85 break;//***new
afilipem 0:c385e589a779 86 }
afilipem 0:c385e589a779 87 break;//***new
afilipem 0:c385e589a779 88 }
afilipem 0:c385e589a779 89 }
afilipem 0:c385e589a779 90
afilipem 0:c385e589a779 91 void HTTPServer::handle_request(char *buffer)
afilipem 0:c385e589a779 92 {
afilipem 0:c385e589a779 93 char *request_type = strtok(buffer, " ");
afilipem 0:c385e589a779 94 char *request = strtok(NULL, " ");
afilipem 0:c385e589a779 95
afilipem 0:c385e589a779 96 reply[0] = '\0';
afilipem 0:c385e589a779 97 if(!strcmp(request, "/"))
afilipem 0:c385e589a779 98 return;
afilipem 0:c385e589a779 99
afilipem 0:c385e589a779 100 if(!command.decode(request))
afilipem 0:c385e589a779 101 {
afilipem 0:c385e589a779 102 strcat(reply, "Malformed request");
afilipem 0:c385e589a779 103 return;
afilipem 0:c385e589a779 104 }
afilipem 0:c385e589a779 105
afilipem 0:c385e589a779 106 std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type);
afilipem 0:c385e589a779 107 if(itor == handlers.end())
afilipem 0:c385e589a779 108 {
afilipem 0:c385e589a779 109 strcat(reply, "No request handler found for this type of request.");
afilipem 0:c385e589a779 110 return;
afilipem 0:c385e589a779 111 }
afilipem 0:c385e589a779 112 if(itor->second != NULL)
afilipem 0:c385e589a779 113 itor->second->handle(command, reply);
afilipem 0:c385e589a779 114 else
afilipem 0:c385e589a779 115 strcat(reply, "Invalid request handler");
afilipem 0:c385e589a779 116 }
afilipem 0:c385e589a779 117
afilipem 0:c385e589a779 118 void HTTPServer::add_request_handler(char *name, RequestHandler* handler)
afilipem 0:c385e589a779 119 {
afilipem 0:c385e589a779 120 handlers[name] = handler;
afilipem 0:c385e589a779 121 }
afilipem 0:c385e589a779 122