Measure system

Dependencies:   EthernetNetIf mbed RF12B

Committer:
benecsj
Date:
Tue May 17 16:49:23 2011 +0000
Revision:
3:799d8c61fb03
Parent:
2:afe5826411e3

        

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 #include "HTTPServer.h"
benecsj 0:8d62137f7ff4 25 #include "MeasureSystem.h"
benecsj 0:8d62137f7ff4 26
benecsj 0:8d62137f7ff4 27 //#define __DEBUG
benecsj 0:8d62137f7ff4 28 //#include "dbg/dbg.h"
benecsj 0:8d62137f7ff4 29
benecsj 3:799d8c61fb03 30
benecsj 0:8d62137f7ff4 31 //-----------------CONSTRUCTOR-------------------------------------
benecsj 0:8d62137f7ff4 32 HTTPServer::HTTPServer()
benecsj 0:8d62137f7ff4 33 {
benecsj 0:8d62137f7ff4 34 m_pTCPSocket = new TCPSocket;
benecsj 0:8d62137f7ff4 35 m_pTCPSocket->setOnEvent(this, &HTTPServer::onTCPSocketEvent);
benecsj 0:8d62137f7ff4 36 }
benecsj 0:8d62137f7ff4 37
benecsj 0:8d62137f7ff4 38 //-----------------DECONSTRUCTOR-----------------------------------
benecsj 0:8d62137f7ff4 39 HTTPServer::~HTTPServer()
benecsj 0:8d62137f7ff4 40 {
benecsj 0:8d62137f7ff4 41 delete m_pTCPSocket;
benecsj 0:8d62137f7ff4 42 }
benecsj 0:8d62137f7ff4 43
benecsj 0:8d62137f7ff4 44 //-----------------PORT BIND---------------------------------------
benecsj 0:8d62137f7ff4 45 void HTTPServer::bind(int port /*= 80*/)
benecsj 0:8d62137f7ff4 46 {
benecsj 0:8d62137f7ff4 47 Host h(IpAddr(127,0,0,1), port, "localhost");
benecsj 0:8d62137f7ff4 48 m_pTCPSocket->bind(h);
benecsj 0:8d62137f7ff4 49 m_pTCPSocket->listen(); //Listen
benecsj 0:8d62137f7ff4 50 }
benecsj 0:8d62137f7ff4 51
benecsj 0:8d62137f7ff4 52 #if 0 //Just for clarity
benecsj 0:8d62137f7ff4 53 template<typename T>
benecsj 0:8d62137f7ff4 54 void HTTPServer::addHandler(const char* path)
benecsj 0:8d62137f7ff4 55 {
benecsj 0:8d62137f7ff4 56 m_lpHandlers[path] = &T::inst;
benecsj 0:8d62137f7ff4 57
benecsj 0:8d62137f7ff4 58 }
benecsj 0:8d62137f7ff4 59 #endif
benecsj 0:8d62137f7ff4 60
benecsj 0:8d62137f7ff4 61 void HTTPServer::onTCPSocketEvent(TCPSocketEvent e)
benecsj 0:8d62137f7ff4 62 {
benecsj 0:8d62137f7ff4 63 printf("0-TCP-HTTPServer::onTCPSocketEvent : Event %d\r\n", e);
benecsj 0:8d62137f7ff4 64
benecsj 0:8d62137f7ff4 65
benecsj 0:8d62137f7ff4 66 if(e==TCPSOCKET_ACCEPT)
benecsj 0:8d62137f7ff4 67 {
benecsj 0:8d62137f7ff4 68 TCPSocket* pTCPSocket;
benecsj 0:8d62137f7ff4 69 Host client;
benecsj 0:8d62137f7ff4 70
benecsj 0:8d62137f7ff4 71 if( !!m_pTCPSocket->accept(&client, &pTCPSocket) )
benecsj 0:8d62137f7ff4 72 {
benecsj 0:8d62137f7ff4 73 printf("0-TCP-HTTPServer::onTCPSocketEvent : Could not accept connection.\r\n");
benecsj 0:8d62137f7ff4 74 return; //Error in accept, discard connection
benecsj 0:8d62137f7ff4 75 }
benecsj 0:8d62137f7ff4 76
benecsj 0:8d62137f7ff4 77 if (HandlerActive)
benecsj 0:8d62137f7ff4 78 {
benecsj 0:8d62137f7ff4 79 printf("0-TCP-Throwing away TCP IP request\r\n");
benecsj 2:afe5826411e3 80 delete pTCPSocket;
benecsj 0:8d62137f7ff4 81 return;
benecsj 0:8d62137f7ff4 82 }
benecsj 2:afe5826411e3 83 /*
benecsj 1:b26ab2467b1a 84 char buf[40];
benecsj 1:b26ab2467b1a 85 string tempstring ("");
benecsj 1:b26ab2467b1a 86 ctTime = time(NULL);
benecsj 1:b26ab2467b1a 87 ctTime += (clockoffset*3600);
benecsj 1:b26ab2467b1a 88 strftime(buf,sizeof(buf), "%Y/%m/%d %H:%M:%S", localtime(&ctTime));
benecsj 1:b26ab2467b1a 89 tempstring +="TCP-Con: ";
benecsj 1:b26ab2467b1a 90 tempstring += buf;
benecsj 1:b26ab2467b1a 91 LogWrite(tempstring);
benecsj 2:afe5826411e3 92 */
benecsj 0:8d62137f7ff4 93 HandlerActive = true;
benecsj 0:8d62137f7ff4 94 HTTPRequestDispatcher* pDispatcher = new HTTPRequestDispatcher(this, pTCPSocket); //TCPSocket ownership is passed to dispatcher
benecsj 2:afe5826411e3 95 //The dispatcher object will destroy itself when done, or will be destroyed on Server destruction
benecsj 0:8d62137f7ff4 96
benecsj 0:8d62137f7ff4 97 }
benecsj 0:8d62137f7ff4 98 printf("0-TCP-TCPSocket ownership is passed to Dispatcher\r\n");
benecsj 0:8d62137f7ff4 99 }