1 #include <HttpServer.h>
2 #include <HttpServerLib.h>
3 #include <HttpServCon.h>
4 #include <time.h>
5 #include <smx.h>
6 #include <pcutil.h>
7
8
9 class SetGetTimePage : public HttpPage
10 {
11 public:
12 SetGetTimePage();
13 private:
14 static void service(HttpPage* o,
15 HttpRequest* request,
16 HttpResponse* response);
17 void doPost(HttpRequest* request, HttpResponse* response);
18 void doGet(HttpResponse* response);
19 void setRTC(struct tm* tm);
20 };
21
22 SetGetTimePage::SetGetTimePage() : HttpPage(service, "index.html")
23 {
24 }
25
26 void
27 SetGetTimePage::service(HttpPage* o,
28 HttpRequest* request,
29 HttpResponse* response)
30 {
31
32 response->write("<html><head>"
33 "<title>Set or get SMX time</title>"
34 "</head><body>");
35
36 if(request->getMethodType() == HttpRequest_Post)
37 ((SetGetTimePage*)o)->doPost(request,response);
38 else
39 ((SetGetTimePage*)o)->doGet(response);
40
41 response->write("</body></html>");
42 }
43
44
45 void
46 SetGetTimePage::doGet(HttpResponse* response)
47 {
48 static const char* wd[7] = {
49 "Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
50 };
51 static const char* months[12] = {
52 "Jan","Feb", "Mar", "Apr", "May", "Jun",
53 "Jul","Aug", "Sep", "Oct", "Nov", "Dec"
54 };
55 struct tm tm;
56 dword time = get_stime();
57 httpTime2tm(&tm, time);
58 response->write("<p>Current day and time is: ");
59 response->printf("%s, %02d %s %d %02d:%02d:%02d GMT",
60 wd[tm.tm_wday],
61 tm.tm_mday,
62 months[tm.tm_mon],
63 tm.tm_year+1900,
64 tm.tm_hour,
65 tm.tm_min,
66 tm.tm_sec);
67 response->write("</p>");
68 response->write(
69 "<p>"
70 "<form method='post'>"
71 "<input type='text' name='time' size='30'/>"
72 "<br/>"
73 "<input type='submit'/>"
74 "</form>"
75 "</p>");
76
77 }
78
79
80 void
81 SetGetTimePage::doPost(HttpRequest* request, HttpResponse* response)
82 {
83 HttpTime time;
84 struct tm tm;
85 const char* timeStr = request->getParameter("time");
86 if(timeStr == NULL || (time=httpParseDate(timeStr)) == 0)
87 {
88 response->printf(
89 "<p>"
90 "Cannot parse date string. "
91 "Date string must be in RFC1123 or RFC850 format.<br/>"
92 "<a href='%s'>Try again</a>"
93 "</p>",
94 getName());
95 }
96 else
97 {
98 httpTime2tm(&tm, time);
99 setRTC(&tm);
100 response->sendRedirect(response->encodeRedirectURL(getName()));
101 }
102 }
103
104
105 void
106 SetGetTimePage::setRTC(struct tm* tm)
107 {
108 #if PC_RTC
109 Set_RTC_Time(tm);
110 Set_RTC_Date(tm);
111 #endif
112 }
113
114
115
116 void
117 barracudaMain()
118 {
119 #ifdef WIN32
120 WORD wVersionRequested;
121 WSADATA wsaData;
122 wVersionRequested = MAKEWORD(1, 1);
123 if(WSAStartup( wVersionRequested, &wsaData ))
124 perror("WSAStartup");
125 #endif
126
127 int port=8080;
128
129 //Create the socket dispatcher object.
130 HttpDispatcher dispatcher;
131 //Create the Web-Server
132 HttpServer server(&dispatcher);
133 //Create a server socket listener that listen on all interfaces.
134 HttpServCon serverCon(&server, port, AF_INET);
135
136 if( !serverCon.isValid() )
137 { //A primitive error handling
138 assert(0);
139 return;
140 }
141
142 HttpDir rootDir; //Root dir takes no parameters i.e. no name
143 SetGetTimePage setGetTimePage;
144 rootDir.insertPage(&setGetTimePage);
145 server.insertRootDir(&rootDir);
146
147 #ifdef WIN32
148 for(;;)
149 {
150 struct timeval t;
151 t.tv_sec=0;
152 t.tv_usec=0;
153 dispatcher.run(&t);
154 Thread::sleep(50); /*50 mSec. */
155 }
156 #else
157 dispatcher.run();//Never returns
158 #endif
159 }
160
161
162 void
163 startBarracuda(void)
164 {
165 startx(create_task(barracudaMain, ct->priority, 4000));
166 }