Measure system

Dependencies:   EthernetNetIf mbed RF12B

Committer:
benecsj
Date:
Tue May 17 16:49:23 2011 +0000
Revision:
3:799d8c61fb03
Parent:
0:8d62137f7ff4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benecsj 0:8d62137f7ff4 1
benecsj 0:8d62137f7ff4 2 /*
benecsj 0:8d62137f7ff4 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
benecsj 0:8d62137f7ff4 4
benecsj 0:8d62137f7ff4 5 Permission is hereby granted, free of charge, to any person obtaining a copy
benecsj 0:8d62137f7ff4 6 of this software and associated documentation files (the "Software"), to deal
benecsj 0:8d62137f7ff4 7 in the Software without restriction, including without limitation the rights
benecsj 0:8d62137f7ff4 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
benecsj 0:8d62137f7ff4 9 copies of the Software, and to permit persons to whom the Software is
benecsj 0:8d62137f7ff4 10 furnished to do so, subject to the following conditions:
benecsj 0:8d62137f7ff4 11
benecsj 0:8d62137f7ff4 12 The above copyright notice and this permission notice shall be included in
benecsj 0:8d62137f7ff4 13 all copies or substantial portions of the Software.
benecsj 0:8d62137f7ff4 14
benecsj 0:8d62137f7ff4 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
benecsj 0:8d62137f7ff4 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
benecsj 0:8d62137f7ff4 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
benecsj 0:8d62137f7ff4 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
benecsj 0:8d62137f7ff4 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
benecsj 0:8d62137f7ff4 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
benecsj 0:8d62137f7ff4 21 THE SOFTWARE.
benecsj 0:8d62137f7ff4 22 */
benecsj 0:8d62137f7ff4 23
benecsj 0:8d62137f7ff4 24 /** \file
benecsj 0:8d62137f7ff4 25 HTTP Server header file
benecsj 0:8d62137f7ff4 26 */
benecsj 0:8d62137f7ff4 27
benecsj 0:8d62137f7ff4 28 #ifndef HTTP_SERVER_H
benecsj 0:8d62137f7ff4 29 #define HTTP_SERVER_H
benecsj 0:8d62137f7ff4 30
benecsj 0:8d62137f7ff4 31 #include "core/net.h"
benecsj 0:8d62137f7ff4 32 #include "HTTPRequestHandler.h"
benecsj 0:8d62137f7ff4 33 #include "HTTPRequestDispatcher.h"
benecsj 0:8d62137f7ff4 34 #include "MeasureSystem.h"
benecsj 0:8d62137f7ff4 35
benecsj 0:8d62137f7ff4 36 #include <string>
benecsj 0:8d62137f7ff4 37 using std::string;
benecsj 0:8d62137f7ff4 38
benecsj 0:8d62137f7ff4 39 #include <map>
benecsj 0:8d62137f7ff4 40 using std::map;
benecsj 0:8d62137f7ff4 41
benecsj 0:8d62137f7ff4 42 ///A simple HTTP server implementation
benecsj 0:8d62137f7ff4 43 /**
benecsj 0:8d62137f7ff4 44 The HTTPServer is composed of:
benecsj 0:8d62137f7ff4 45 - The actual server (HTTPServer)
benecsj 0:8d62137f7ff4 46 - A request dispatcher, instanciated on each request (HTTPRequestDispatcher)
benecsj 0:8d62137f7ff4 47 - Request handlers instanciated by the dispatcher(deriving from HTTPRequestHandler)
benecsj 0:8d62137f7ff4 48 */
benecsj 0:8d62137f7ff4 49 class HTTPServer
benecsj 0:8d62137f7ff4 50 {
benecsj 0:8d62137f7ff4 51 public:
benecsj 0:8d62137f7ff4 52 ///Instantiates the HTTP Server
benecsj 0:8d62137f7ff4 53 HTTPServer();
benecsj 0:8d62137f7ff4 54 ~HTTPServer();
benecsj 0:8d62137f7ff4 55
benecsj 0:8d62137f7ff4 56 struct handlersComp //Used to order handlers in the right way
benecsj 0:8d62137f7ff4 57 {
benecsj 0:8d62137f7ff4 58 bool operator() (const string& handler1, const string& handler2) const
benecsj 0:8d62137f7ff4 59 {
benecsj 0:8d62137f7ff4 60 //The first handler is longer than the second one
benecsj 0:8d62137f7ff4 61 if (handler1.length() > handler2.length())
benecsj 0:8d62137f7ff4 62 return true; //Returns true if handler1 is to appear before handler2
benecsj 0:8d62137f7ff4 63 else if (handler1.length() < handler2.length())
benecsj 0:8d62137f7ff4 64 return false;
benecsj 0:8d62137f7ff4 65 else //To avoid the == case, sort now by address
benecsj 0:8d62137f7ff4 66 return ((&handler1)>(&handler2));
benecsj 0:8d62137f7ff4 67 }
benecsj 0:8d62137f7ff4 68 };
benecsj 0:8d62137f7ff4 69
benecsj 0:8d62137f7ff4 70 ///Adds a handler
benecsj 0:8d62137f7ff4 71 /**
benecsj 0:8d62137f7ff4 72 Appends a handler to the handlers list
benecsj 0:8d62137f7ff4 73 @param T : class which will be instanciated to serve these requests
benecsj 0:8d62137f7ff4 74 @param path : requests starting with this path will be served using this handler
benecsj 0:8d62137f7ff4 75 */
benecsj 0:8d62137f7ff4 76 template<typename T>
benecsj 0:8d62137f7ff4 77 void addHandler(const char* path) //Template decl in header
benecsj 0:8d62137f7ff4 78 { m_lpHandlers[path] = &T::inst; }
benecsj 0:8d62137f7ff4 79
benecsj 0:8d62137f7ff4 80 ///Starts listening
benecsj 0:8d62137f7ff4 81 /**
benecsj 0:8d62137f7ff4 82 Binds server to a specific port and starts listening
benecsj 0:8d62137f7ff4 83 @param port : port on which to listen for incoming connections
benecsj 0:8d62137f7ff4 84 */
benecsj 0:8d62137f7ff4 85 void bind(int port = 80);
benecsj 0:8d62137f7ff4 86
benecsj 0:8d62137f7ff4 87 private:
benecsj 0:8d62137f7ff4 88 friend class HTTPRequestDispatcher;
benecsj 0:8d62137f7ff4 89
benecsj 0:8d62137f7ff4 90 void onTCPSocketEvent(TCPSocketEvent e);
benecsj 0:8d62137f7ff4 91
benecsj 0:8d62137f7ff4 92 TCPSocket* m_pTCPSocket;
benecsj 0:8d62137f7ff4 93 map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*), handlersComp > m_lpHandlers;
benecsj 0:8d62137f7ff4 94
benecsj 0:8d62137f7ff4 95 };
benecsj 0:8d62137f7ff4 96
benecsj 0:8d62137f7ff4 97 //Including handlers here for more convenience
benecsj 0:8d62137f7ff4 98 #include "impl/FSHandler.h"
benecsj 0:8d62137f7ff4 99 #include "impl/SimpleHandler.h"
benecsj 0:8d62137f7ff4 100
benecsj 0:8d62137f7ff4 101 #endif