TimeHandler test program for the HTTPServer

Dependencies:   EthernetNetIf mbed HTTPServer

Committer:
rinosh2
Date:
Thu Feb 03 17:51:51 2011 +0000
Revision:
0:b37980ea1c2f
Test version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rinosh2 0:b37980ea1c2f 1 #include "TimeHandler.h"
rinosh2 0:b37980ea1c2f 2 #include <time.h>
rinosh2 0:b37980ea1c2f 3
rinosh2 0:b37980ea1c2f 4 #include "dbg/dbg.h"
rinosh2 0:b37980ea1c2f 5
rinosh2 0:b37980ea1c2f 6 TimeHandler::TimeHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket)
rinosh2 0:b37980ea1c2f 7 {
rinosh2 0:b37980ea1c2f 8 }
rinosh2 0:b37980ea1c2f 9
rinosh2 0:b37980ea1c2f 10 TimeHandler::~TimeHandler()
rinosh2 0:b37980ea1c2f 11 {
rinosh2 0:b37980ea1c2f 12 DBG("Handler destroyed\r\n");
rinosh2 0:b37980ea1c2f 13 }
rinosh2 0:b37980ea1c2f 14
rinosh2 0:b37980ea1c2f 15 const char* res1 =
rinosh2 0:b37980ea1c2f 16 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n\
rinosh2 0:b37980ea1c2f 17 <HTML>\n\
rinosh2 0:b37980ea1c2f 18 <HEAD>\n\
rinosh2 0:b37980ea1c2f 19 <META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html;charset=Shift_JIS\">\n\
rinosh2 0:b37980ea1c2f 20 <META HTTP-EQUIV=\"Content-Script-Type\" CONTENT=\"text/javascript\">\n\
rinosh2 0:b37980ea1c2f 21 <TITLE>DiffTime</TITLE>\n\
rinosh2 0:b37980ea1c2f 22 </HEAD>\n\
rinosh2 0:b37980ea1c2f 23 <BODY>\n\
rinosh2 0:b37980ea1c2f 24 <SCRIPT LANGUAGE=\"javascript\" TYPE=\"text/javascript\">\n\
rinosh2 0:b37980ea1c2f 25 <!--\n\
rinosh2 0:b37980ea1c2f 26 var d_ini = new Date();\n\
rinosh2 0:b37980ea1c2f 27 var d_svr = new Date(\"";
rinosh2 0:b37980ea1c2f 28
rinosh2 0:b37980ea1c2f 29 const char* res3 =
rinosh2 0:b37980ea1c2f 30 "\");\n\
rinosh2 0:b37980ea1c2f 31 document.write(\"<FORM NAME='form1'><INPUT TYPE='text' NAME='text1' SIZE=100><br><INPUT TYPE='text' NAME='text2' SIZE=100></FORM>\");\n\
rinosh2 0:b37980ea1c2f 32 ShowDiff();\n\
rinosh2 0:b37980ea1c2f 33 function ShowDiff(){\n\
rinosh2 0:b37980ea1c2f 34 var d_pc = new Date();\n\
rinosh2 0:b37980ea1c2f 35 var d_ts = new Date(d_svr - (d_ini - d_pc));\n\
rinosh2 0:b37980ea1c2f 36 document.form1.text1.value = \"TS:\" + d_ts.toString();\n\
rinosh2 0:b37980ea1c2f 37 document.form1.text2.value = \"PC:\" + d_pc.toString() + \" (Diff:\" + Math.round((d_ini - d_svr) / 1000) + \" [sec])\";\n\
rinosh2 0:b37980ea1c2f 38 setTimeout(\"ShowDiff()\",1000);\n\
rinosh2 0:b37980ea1c2f 39 }\n\
rinosh2 0:b37980ea1c2f 40 // -->\n\
rinosh2 0:b37980ea1c2f 41 </SCRIPT>\n\
rinosh2 0:b37980ea1c2f 42 </BODY>\n\
rinosh2 0:b37980ea1c2f 43 </HTML>\n";
rinosh2 0:b37980ea1c2f 44
rinosh2 0:b37980ea1c2f 45 void setTestRTC(){
rinosh2 0:b37980ea1c2f 46 struct tm t;
rinosh2 0:b37980ea1c2f 47 t.tm_year = 2011 - 1900; //109 year since 1900
rinosh2 0:b37980ea1c2f 48 t.tm_mon = 2 - 1; //9 0-11
rinosh2 0:b37980ea1c2f 49 t.tm_mday = 4; //28 1-31
rinosh2 0:b37980ea1c2f 50 t.tm_hour = 2; //11 0-23
rinosh2 0:b37980ea1c2f 51 t.tm_min = 36; //42 0-59
rinosh2 0:b37980ea1c2f 52 t.tm_sec = 0; //37 0-59
rinosh2 0:b37980ea1c2f 53 set_time(mktime(&t));
rinosh2 0:b37980ea1c2f 54 }
rinosh2 0:b37980ea1c2f 55
rinosh2 0:b37980ea1c2f 56 void TimeHandler::doGet()
rinosh2 0:b37980ea1c2f 57 {
rinosh2 0:b37980ea1c2f 58 DBG("In TimeHandler::doGet()\r\n");
rinosh2 0:b37980ea1c2f 59
rinosh2 0:b37980ea1c2f 60 //setTestRTC(); // for diff debug
rinosh2 0:b37980ea1c2f 61
rinosh2 0:b37980ea1c2f 62 time_t now = time(0);
rinosh2 0:b37980ea1c2f 63 struct tm *t = localtime(&now);
rinosh2 0:b37980ea1c2f 64 char res2[100];
rinosh2 0:b37980ea1c2f 65 sprintf(res2, "%04d/%02d/%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
rinosh2 0:b37980ea1c2f 66 printf("HTTPD TimeHandler RTC=%s\n", res2);
rinosh2 0:b37980ea1c2f 67
rinosh2 0:b37980ea1c2f 68 int len1 = strlen(res1);
rinosh2 0:b37980ea1c2f 69 int len2 = strlen(res2);
rinosh2 0:b37980ea1c2f 70 int len3 = strlen(res3);
rinosh2 0:b37980ea1c2f 71
rinosh2 0:b37980ea1c2f 72 setContentLen( len1 + len2 + len3);
rinosh2 0:b37980ea1c2f 73 respHeaders()["Connection"] = "close";
rinosh2 0:b37980ea1c2f 74 writeData(res1, len1);
rinosh2 0:b37980ea1c2f 75 writeData(res2, len2);
rinosh2 0:b37980ea1c2f 76 writeData(res3, len3);
rinosh2 0:b37980ea1c2f 77 DBG("Exit TimeHandler::doGet()\r\n");
rinosh2 0:b37980ea1c2f 78 }
rinosh2 0:b37980ea1c2f 79
rinosh2 0:b37980ea1c2f 80 void TimeHandler::doPost()
rinosh2 0:b37980ea1c2f 81 {
rinosh2 0:b37980ea1c2f 82
rinosh2 0:b37980ea1c2f 83 }
rinosh2 0:b37980ea1c2f 84
rinosh2 0:b37980ea1c2f 85 void TimeHandler::doHead()
rinosh2 0:b37980ea1c2f 86 {
rinosh2 0:b37980ea1c2f 87
rinosh2 0:b37980ea1c2f 88 }
rinosh2 0:b37980ea1c2f 89
rinosh2 0:b37980ea1c2f 90
rinosh2 0:b37980ea1c2f 91 void TimeHandler::onReadable() //Data has been read
rinosh2 0:b37980ea1c2f 92 {
rinosh2 0:b37980ea1c2f 93
rinosh2 0:b37980ea1c2f 94 }
rinosh2 0:b37980ea1c2f 95
rinosh2 0:b37980ea1c2f 96 void TimeHandler::onWriteable() //Data has been written & buf is free
rinosh2 0:b37980ea1c2f 97 {
rinosh2 0:b37980ea1c2f 98 DBG("TimeHandler::onWriteable() event\r\n");
rinosh2 0:b37980ea1c2f 99 close(); //Data written, we can close the connection
rinosh2 0:b37980ea1c2f 100 }
rinosh2 0:b37980ea1c2f 101
rinosh2 0:b37980ea1c2f 102 void TimeHandler::onClose() //Connection is closing
rinosh2 0:b37980ea1c2f 103 {
rinosh2 0:b37980ea1c2f 104 //Nothing to do
rinosh2 0:b37980ea1c2f 105 }