平成24年中国四国技術職員研修用
Dependencies: MSCUsbHost NetServices RPCInterface TextLCD mbed
NetServices/services/http/server/HTTPServer.cpp@0:fd83f3540b1a, 2012-08-18 (annotated)
- Committer:
- yueee_yt
- Date:
- Sat Aug 18 07:26:48 2012 +0000
- Revision:
- 0:fd83f3540b1a
NTP????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yueee_yt | 0:fd83f3540b1a | 1 | |
yueee_yt | 0:fd83f3540b1a | 2 | /* |
yueee_yt | 0:fd83f3540b1a | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
yueee_yt | 0:fd83f3540b1a | 4 | |
yueee_yt | 0:fd83f3540b1a | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yueee_yt | 0:fd83f3540b1a | 6 | of this software and associated documentation files (the "Software"), to deal |
yueee_yt | 0:fd83f3540b1a | 7 | in the Software without restriction, including without limitation the rights |
yueee_yt | 0:fd83f3540b1a | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yueee_yt | 0:fd83f3540b1a | 9 | copies of the Software, and to permit persons to whom the Software is |
yueee_yt | 0:fd83f3540b1a | 10 | furnished to do so, subject to the following conditions: |
yueee_yt | 0:fd83f3540b1a | 11 | |
yueee_yt | 0:fd83f3540b1a | 12 | The above copyright notice and this permission notice shall be included in |
yueee_yt | 0:fd83f3540b1a | 13 | all copies or substantial portions of the Software. |
yueee_yt | 0:fd83f3540b1a | 14 | |
yueee_yt | 0:fd83f3540b1a | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yueee_yt | 0:fd83f3540b1a | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yueee_yt | 0:fd83f3540b1a | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yueee_yt | 0:fd83f3540b1a | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yueee_yt | 0:fd83f3540b1a | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yueee_yt | 0:fd83f3540b1a | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yueee_yt | 0:fd83f3540b1a | 21 | THE SOFTWARE. |
yueee_yt | 0:fd83f3540b1a | 22 | */ |
yueee_yt | 0:fd83f3540b1a | 23 | |
yueee_yt | 0:fd83f3540b1a | 24 | #include "HTTPServer.h" |
yueee_yt | 0:fd83f3540b1a | 25 | |
yueee_yt | 0:fd83f3540b1a | 26 | //#define __DEBUG |
yueee_yt | 0:fd83f3540b1a | 27 | #include "dbg/dbg.h" |
yueee_yt | 0:fd83f3540b1a | 28 | |
yueee_yt | 0:fd83f3540b1a | 29 | HTTPServer::HTTPServer() |
yueee_yt | 0:fd83f3540b1a | 30 | { |
yueee_yt | 0:fd83f3540b1a | 31 | m_pTCPSocket = new TCPSocket; |
yueee_yt | 0:fd83f3540b1a | 32 | m_pTCPSocket->setOnEvent(this, &HTTPServer::onTCPSocketEvent); |
yueee_yt | 0:fd83f3540b1a | 33 | } |
yueee_yt | 0:fd83f3540b1a | 34 | |
yueee_yt | 0:fd83f3540b1a | 35 | HTTPServer::~HTTPServer() |
yueee_yt | 0:fd83f3540b1a | 36 | { |
yueee_yt | 0:fd83f3540b1a | 37 | delete m_pTCPSocket; |
yueee_yt | 0:fd83f3540b1a | 38 | } |
yueee_yt | 0:fd83f3540b1a | 39 | |
yueee_yt | 0:fd83f3540b1a | 40 | void HTTPServer::bind(int port /*= 80*/) |
yueee_yt | 0:fd83f3540b1a | 41 | { |
yueee_yt | 0:fd83f3540b1a | 42 | Host h(IpAddr(127,0,0,1), port, "localhost"); |
yueee_yt | 0:fd83f3540b1a | 43 | m_pTCPSocket->bind(h); |
yueee_yt | 0:fd83f3540b1a | 44 | m_pTCPSocket->listen(); //Listen |
yueee_yt | 0:fd83f3540b1a | 45 | } |
yueee_yt | 0:fd83f3540b1a | 46 | |
yueee_yt | 0:fd83f3540b1a | 47 | #if 0 //Just for clarity |
yueee_yt | 0:fd83f3540b1a | 48 | template<typename T> |
yueee_yt | 0:fd83f3540b1a | 49 | void HTTPServer::addHandler(const char* path) |
yueee_yt | 0:fd83f3540b1a | 50 | { |
yueee_yt | 0:fd83f3540b1a | 51 | m_lpHandlers[path] = &T::inst; |
yueee_yt | 0:fd83f3540b1a | 52 | |
yueee_yt | 0:fd83f3540b1a | 53 | } |
yueee_yt | 0:fd83f3540b1a | 54 | #endif |
yueee_yt | 0:fd83f3540b1a | 55 | |
yueee_yt | 0:fd83f3540b1a | 56 | void HTTPServer::onTCPSocketEvent(TCPSocketEvent e) |
yueee_yt | 0:fd83f3540b1a | 57 | { |
yueee_yt | 0:fd83f3540b1a | 58 | |
yueee_yt | 0:fd83f3540b1a | 59 | DBG("\r\nHTTPServer::onTCPSocketEvent : Event %d\r\n", e); |
yueee_yt | 0:fd83f3540b1a | 60 | |
yueee_yt | 0:fd83f3540b1a | 61 | if(e==TCPSOCKET_ACCEPT) |
yueee_yt | 0:fd83f3540b1a | 62 | { |
yueee_yt | 0:fd83f3540b1a | 63 | TCPSocket* pTCPSocket; |
yueee_yt | 0:fd83f3540b1a | 64 | Host client; |
yueee_yt | 0:fd83f3540b1a | 65 | |
yueee_yt | 0:fd83f3540b1a | 66 | if( !!m_pTCPSocket->accept(&client, &pTCPSocket) ) |
yueee_yt | 0:fd83f3540b1a | 67 | { |
yueee_yt | 0:fd83f3540b1a | 68 | DBG("\r\nHTTPServer::onTCPSocketEvent : Could not accept connection.\r\n"); |
yueee_yt | 0:fd83f3540b1a | 69 | return; //Error in accept, discard connection |
yueee_yt | 0:fd83f3540b1a | 70 | } |
yueee_yt | 0:fd83f3540b1a | 71 | |
yueee_yt | 0:fd83f3540b1a | 72 | HTTPRequestDispatcher* pDispatcher = new HTTPRequestDispatcher(this, pTCPSocket); //TCPSocket ownership is passed to dispatcher |
yueee_yt | 0:fd83f3540b1a | 73 | //The dispatcher object will destroy itself when done, or will be destroyed on Server destruction |
yueee_yt | 0:fd83f3540b1a | 74 | |
yueee_yt | 0:fd83f3540b1a | 75 | } |
yueee_yt | 0:fd83f3540b1a | 76 | |
yueee_yt | 0:fd83f3540b1a | 77 | } |