TimeHandler test program for the HTTPServer

Dependencies:   EthernetNetIf mbed HTTPServer

Files at this revision

API Documentation at this revision

Comitter:
rinosh2
Date:
Thu Feb 03 17:51:51 2011 +0000
Commit message:
Test version

Changed in this revision

EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
HTTPServer.lib Show annotated file Show diff for this revision Revisions of this file
TimeHandler.cpp Show annotated file Show diff for this revision Revisions of this file
TimeHandler.h Show annotated file Show diff for this revision Revisions of this file
TimeHandlerTest.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b37980ea1c2f EthernetNetIf.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetNetIf.lib	Thu Feb 03 17:51:51 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r b37980ea1c2f HTTPServer.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPServer.lib	Thu Feb 03 17:51:51 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPServer/#d753966e4d97
diff -r 000000000000 -r b37980ea1c2f TimeHandler.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TimeHandler.cpp	Thu Feb 03 17:51:51 2011 +0000
@@ -0,0 +1,105 @@
+#include "TimeHandler.h"
+#include <time.h>
+
+#include "dbg/dbg.h"
+
+TimeHandler::TimeHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket)
+{
+}
+
+TimeHandler::~TimeHandler()
+{
+  DBG("Handler destroyed\r\n");
+}
+
+const char* res1 = 
+"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n\
+<HTML>\n\
+<HEAD>\n\
+<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html;charset=Shift_JIS\">\n\
+<META HTTP-EQUIV=\"Content-Script-Type\" CONTENT=\"text/javascript\">\n\
+<TITLE>DiffTime</TITLE>\n\
+</HEAD>\n\
+<BODY>\n\
+<SCRIPT LANGUAGE=\"javascript\" TYPE=\"text/javascript\">\n\
+<!--\n\
+var d_ini = new Date();\n\
+var d_svr = new Date(\"";
+
+const char* res3 = 
+"\");\n\
+document.write(\"<FORM NAME='form1'><INPUT TYPE='text' NAME='text1' SIZE=100><br><INPUT TYPE='text' NAME='text2' SIZE=100></FORM>\");\n\
+ShowDiff();\n\
+function ShowDiff(){\n\
+	var d_pc = new Date();\n\
+	var d_ts = new Date(d_svr - (d_ini - d_pc));\n\
+	document.form1.text1.value = \"TS:\" + d_ts.toString();\n\
+	document.form1.text2.value = \"PC:\" + d_pc.toString() + \" (Diff:\" + Math.round((d_ini - d_svr) / 1000) + \" [sec])\";\n\
+	setTimeout(\"ShowDiff()\",1000);\n\
+}\n\
+// -->\n\
+</SCRIPT>\n\
+</BODY>\n\
+</HTML>\n";
+
+void setTestRTC(){
+    struct tm t;
+    t.tm_year = 2011 - 1900; //109 year since 1900
+    t.tm_mon  =  2 - 1; //9 0-11
+    t.tm_mday =  4; //28 1-31
+    t.tm_hour =  2; //11 0-23
+    t.tm_min  = 36; //42 0-59
+    t.tm_sec  =  0; //37 0-59
+    set_time(mktime(&t));
+}
+
+void TimeHandler::doGet()
+{
+  DBG("In TimeHandler::doGet()\r\n");
+  
+  //setTestRTC(); // for diff debug
+  
+  time_t now = time(0);
+  struct tm *t = localtime(&now);
+  char res2[100];
+  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);
+  printf("HTTPD TimeHandler RTC=%s\n", res2);
+
+  int len1 = strlen(res1);
+  int len2 = strlen(res2);
+  int len3 = strlen(res3);
+
+  setContentLen( len1 + len2 + len3);
+  respHeaders()["Connection"] = "close";
+  writeData(res1, len1);
+  writeData(res2, len2);
+  writeData(res3, len3);
+  DBG("Exit TimeHandler::doGet()\r\n");
+}
+
+void TimeHandler::doPost()
+{
+
+}
+
+void TimeHandler::doHead()
+{
+
+}
+
+  
+void TimeHandler::onReadable() //Data has been read
+{
+
+}
+
+void TimeHandler::onWriteable() //Data has been written & buf is free
+{
+  DBG("TimeHandler::onWriteable() event\r\n");
+  close(); //Data written, we can close the connection
+}
+
+void TimeHandler::onClose() //Connection is closing
+{
+  //Nothing to do
+}
diff -r 000000000000 -r b37980ea1c2f TimeHandler.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TimeHandler.h	Thu Feb 03 17:51:51 2011 +0000
@@ -0,0 +1,24 @@
+#ifndef TIME_HANDLER_H
+#define TIME_HANDLER_H
+
+#include "../HTTPRequestHandler.h"
+
+class TimeHandler : public HTTPRequestHandler
+{
+public:
+  TimeHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket);
+  virtual ~TimeHandler();
+
+//protected:
+  static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new TimeHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one
+
+  virtual void doGet();
+  virtual void doPost();
+  virtual void doHead();
+  
+  virtual void onReadable(); //Data has been read
+  virtual void onWriteable(); //Data has been written & buf is free
+  virtual void onClose(); //Connection is closing
+};
+
+#endif
diff -r 000000000000 -r b37980ea1c2f TimeHandlerTest.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TimeHandlerTest.cpp	Thu Feb 03 17:51:51 2011 +0000
@@ -0,0 +1,58 @@
+#include "mbed.h"
+#include "EthernetNetIf.h"
+#include "HTTPServer.h"
+
+#include "TimeHandler.h"
+
+DigitalOut led1(LED1, "led1");
+DigitalOut led2(LED2, "led2");
+DigitalOut led3(LED3, "led3");
+DigitalOut led4(LED4, "led4");
+
+LocalFileSystem fs("webfs");
+
+EthernetNetIf eth;  
+HTTPServer svr;
+
+int main() {
+  Base::add_rpc_class<DigitalOut>();
+
+  printf("Setting up...\n");
+  EthernetErr ethErr = eth.setup();
+  if(ethErr)
+  {
+    printf("Error %d in setup.\n", ethErr);
+    return -1;
+  }
+  printf("Setup OK\n");
+  
+  FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
+  FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
+  
+  svr.addHandler<SimpleHandler>("/hello");
+  svr.addHandler<RPCHandler>("/rpc");
+  svr.addHandler<FSHandler>("/files");
+  svr.addHandler<TimeHandler>("/time");
+  svr.addHandler<FSHandler>("/"); //Default handler
+  //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
+  
+  svr.bind(80);
+  
+  printf("Listening...\n");
+    
+  Timer tm;
+  tm.start();
+  //Listen indefinitely
+  while(true)
+  {
+    Net::poll();
+    if(tm.read()>.5)
+    {
+      led1=!led1; //Show that we are alive
+      tm.start();
+    }
+  }
+  
+  return 0;
+
+}
diff -r 000000000000 -r b37980ea1c2f mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Feb 03 17:51:51 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da