Measure system

Dependencies:   EthernetNetIf mbed RF12B

Committer:
benecsj
Date:
Thu Mar 10 19:56:45 2011 +0000
Revision:
1:b26ab2467b1a
Parent:
0:8d62137f7ff4
Child:
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 1:b26ab2467b1a 24 #include "MeasureSystem.h"
benecsj 0:8d62137f7ff4 25 #include "FSHandler.h"
benecsj 0:8d62137f7ff4 26 #include "MeasureSystem.h"
benecsj 0:8d62137f7ff4 27
benecsj 0:8d62137f7ff4 28 //#define __DEBUG
benecsj 0:8d62137f7ff4 29 //#include "dbg/dbg.h"
benecsj 0:8d62137f7ff4 30
benecsj 0:8d62137f7ff4 31 #define CHUNK_SIZE 128
benecsj 0:8d62137f7ff4 32
benecsj 0:8d62137f7ff4 33 #define DEFAULT_PAGE "/index.htm"
benecsj 0:8d62137f7ff4 34
benecsj 0:8d62137f7ff4 35 FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false)
benecsj 0:8d62137f7ff4 36 {}
benecsj 0:8d62137f7ff4 37
benecsj 0:8d62137f7ff4 38 FSHandler::~FSHandler()
benecsj 0:8d62137f7ff4 39 {
benecsj 0:8d62137f7ff4 40 if(m_fp)
benecsj 0:8d62137f7ff4 41 fclose(m_fp);
benecsj 0:8d62137f7ff4 42 printf("3-FS-Handler destroyed\r\n");
benecsj 0:8d62137f7ff4 43 HandlerActive = false;
benecsj 0:8d62137f7ff4 44 }
benecsj 0:8d62137f7ff4 45
benecsj 0:8d62137f7ff4 46 //static init
benecsj 0:8d62137f7ff4 47 map<string,string> FSHandler::m_lFsPath = map<string,string>();
benecsj 0:8d62137f7ff4 48
benecsj 0:8d62137f7ff4 49 void FSHandler::mount(const string& fsPath, const string& rootPath)
benecsj 0:8d62137f7ff4 50 {
benecsj 0:8d62137f7ff4 51 m_lFsPath[rootPath]=fsPath;
benecsj 0:8d62137f7ff4 52 }
benecsj 0:8d62137f7ff4 53
benecsj 0:8d62137f7ff4 54 void FSHandler::doGet()
benecsj 0:8d62137f7ff4 55 {
benecsj 0:8d62137f7ff4 56 printf("3-FS-In FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str());
benecsj 0:8d62137f7ff4 57 //FIXME: Translate path to local/path
benecsj 0:8d62137f7ff4 58
benecsj 0:8d62137f7ff4 59
benecsj 0:8d62137f7ff4 60 if (path().compare("/log") == 0)
benecsj 0:8d62137f7ff4 61 {
benecsj 1:b26ab2467b1a 62 path() = "/data/log.txt";
benecsj 0:8d62137f7ff4 63 }
benecsj 0:8d62137f7ff4 64
benecsj 0:8d62137f7ff4 65 string checkedRootPath = rootPath();
benecsj 0:8d62137f7ff4 66 if(checkedRootPath.empty())
benecsj 0:8d62137f7ff4 67 checkedRootPath="/";
benecsj 0:8d62137f7ff4 68 string filePath = m_lFsPath[checkedRootPath];
benecsj 1:b26ab2467b1a 69 if (path().size() > 1 & path().size() < 40)
benecsj 0:8d62137f7ff4 70 {
benecsj 0:8d62137f7ff4 71 filePath += path();
benecsj 0:8d62137f7ff4 72 }
benecsj 0:8d62137f7ff4 73 else
benecsj 0:8d62137f7ff4 74 {
benecsj 0:8d62137f7ff4 75 filePath += DEFAULT_PAGE;
benecsj 0:8d62137f7ff4 76 }
benecsj 0:8d62137f7ff4 77
benecsj 0:8d62137f7ff4 78 printf("3-FS-Trying to open %s\r\n", filePath.c_str());
benecsj 0:8d62137f7ff4 79
benecsj 0:8d62137f7ff4 80 m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404
benecsj 0:8d62137f7ff4 81
benecsj 0:8d62137f7ff4 82 if (!m_fp)
benecsj 0:8d62137f7ff4 83 {
benecsj 0:8d62137f7ff4 84 string filePath = m_lFsPath["/extern"];
benecsj 0:8d62137f7ff4 85 if (path().size() > 1)
benecsj 0:8d62137f7ff4 86 {
benecsj 0:8d62137f7ff4 87 filePath += path();
benecsj 0:8d62137f7ff4 88 }
benecsj 0:8d62137f7ff4 89 else
benecsj 0:8d62137f7ff4 90 {
benecsj 0:8d62137f7ff4 91 filePath += DEFAULT_PAGE;
benecsj 0:8d62137f7ff4 92 }
benecsj 0:8d62137f7ff4 93
benecsj 0:8d62137f7ff4 94 printf("3-FS-Trying to open %s\r\n", filePath.c_str());
benecsj 0:8d62137f7ff4 95
benecsj 0:8d62137f7ff4 96 m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404
benecsj 0:8d62137f7ff4 97 }
benecsj 0:8d62137f7ff4 98
benecsj 0:8d62137f7ff4 99 if(!m_fp)
benecsj 0:8d62137f7ff4 100 {
benecsj 0:8d62137f7ff4 101 m_err404 = true;
benecsj 0:8d62137f7ff4 102 setErrCode(404);
benecsj 0:8d62137f7ff4 103 const char* msg = "File not found.";
benecsj 0:8d62137f7ff4 104 setContentLen(strlen(msg));
benecsj 0:8d62137f7ff4 105 respHeaders()["Content-Type"] = "text/html";
benecsj 0:8d62137f7ff4 106 respHeaders()["Connection"] = "close";
benecsj 0:8d62137f7ff4 107 writeData(msg,strlen(msg)); //Only send header
benecsj 0:8d62137f7ff4 108 printf("\r\n3-FS-Exit FSHandler::doGet() w Error 404\r\n");
benecsj 0:8d62137f7ff4 109 return;
benecsj 0:8d62137f7ff4 110 }
benecsj 0:8d62137f7ff4 111
benecsj 0:8d62137f7ff4 112 //Seek EOF to get length
benecsj 0:8d62137f7ff4 113 fseek(m_fp, 0, SEEK_END);
benecsj 0:8d62137f7ff4 114 setContentLen( ftell(m_fp) );
benecsj 0:8d62137f7ff4 115 fseek(m_fp, 0, SEEK_SET); //Goto SOF
benecsj 0:8d62137f7ff4 116
benecsj 0:8d62137f7ff4 117 respHeaders()["Connection"] = "close";
benecsj 0:8d62137f7ff4 118 onWriteable();
benecsj 0:8d62137f7ff4 119 printf("3-FS-Exit FSHandler::doGet()\r\n");
benecsj 0:8d62137f7ff4 120 }
benecsj 0:8d62137f7ff4 121
benecsj 0:8d62137f7ff4 122 void FSHandler::doPost()
benecsj 0:8d62137f7ff4 123 {
benecsj 0:8d62137f7ff4 124
benecsj 0:8d62137f7ff4 125 }
benecsj 0:8d62137f7ff4 126
benecsj 0:8d62137f7ff4 127 void FSHandler::doHead()
benecsj 0:8d62137f7ff4 128 {
benecsj 0:8d62137f7ff4 129
benecsj 0:8d62137f7ff4 130 }
benecsj 0:8d62137f7ff4 131
benecsj 0:8d62137f7ff4 132 void FSHandler::onReadable() //Data has been read
benecsj 0:8d62137f7ff4 133 {
benecsj 0:8d62137f7ff4 134
benecsj 0:8d62137f7ff4 135 }
benecsj 0:8d62137f7ff4 136
benecsj 0:8d62137f7ff4 137 void FSHandler::onWriteable() //Data has been written & buf is free
benecsj 0:8d62137f7ff4 138 {
benecsj 0:8d62137f7ff4 139 // DBG("\r\nFSHandler::onWriteable() event\r\n");
benecsj 0:8d62137f7ff4 140 if(m_err404)
benecsj 0:8d62137f7ff4 141 {
benecsj 0:8d62137f7ff4 142 //Error has been served, now exit
benecsj 0:8d62137f7ff4 143 close();
benecsj 0:8d62137f7ff4 144 return;
benecsj 0:8d62137f7ff4 145 }
benecsj 0:8d62137f7ff4 146
benecsj 0:8d62137f7ff4 147 static char rBuf[CHUNK_SIZE];
benecsj 0:8d62137f7ff4 148 while(true)
benecsj 0:8d62137f7ff4 149 {
benecsj 0:8d62137f7ff4 150 int len = fread(rBuf, 1, CHUNK_SIZE, m_fp);
benecsj 0:8d62137f7ff4 151 if(len>0)
benecsj 0:8d62137f7ff4 152 {
benecsj 0:8d62137f7ff4 153 int writtenLen = writeData(rBuf, len);
benecsj 0:8d62137f7ff4 154 if(writtenLen < 0) //Socket error
benecsj 0:8d62137f7ff4 155 {
benecsj 0:8d62137f7ff4 156 printf("3-FS-FSHandler: Socket error %d\r\n", writtenLen);
benecsj 0:8d62137f7ff4 157 if(writtenLen == TCPSOCKET_MEM)
benecsj 0:8d62137f7ff4 158 {
benecsj 0:8d62137f7ff4 159 fseek(m_fp, -len, SEEK_CUR);
benecsj 0:8d62137f7ff4 160 return; //Wait for the queued TCP segments to be transmitted
benecsj 0:8d62137f7ff4 161 }
benecsj 0:8d62137f7ff4 162 else
benecsj 0:8d62137f7ff4 163 {
benecsj 0:8d62137f7ff4 164 //This is a critical error
benecsj 0:8d62137f7ff4 165 close();
benecsj 0:8d62137f7ff4 166 return;
benecsj 0:8d62137f7ff4 167 }
benecsj 0:8d62137f7ff4 168 }
benecsj 0:8d62137f7ff4 169 else if(writtenLen < len) //Short write, socket's buffer is full
benecsj 0:8d62137f7ff4 170 {
benecsj 0:8d62137f7ff4 171 fseek(m_fp, writtenLen - len, SEEK_CUR);
benecsj 0:8d62137f7ff4 172 return;
benecsj 0:8d62137f7ff4 173 }
benecsj 0:8d62137f7ff4 174 }
benecsj 0:8d62137f7ff4 175 else
benecsj 0:8d62137f7ff4 176 {
benecsj 0:8d62137f7ff4 177 close(); //Data written, we can close the connection
benecsj 0:8d62137f7ff4 178 return;
benecsj 0:8d62137f7ff4 179 }
benecsj 0:8d62137f7ff4 180 }
benecsj 0:8d62137f7ff4 181 }
benecsj 0:8d62137f7ff4 182
benecsj 0:8d62137f7ff4 183 void FSHandler::onClose() //Connection is closing
benecsj 0:8d62137f7ff4 184 {
benecsj 0:8d62137f7ff4 185 if(m_fp)
benecsj 0:8d62137f7ff4 186 fclose(m_fp);
benecsj 0:8d62137f7ff4 187 }