webserver.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <string.h>
00037 #include <stdio.h>
00038
00039 #include "contiki.h"
00040 #include "ctk/ctk.h"
00041
00042 #include "http-strings.h"
00043 #include "webserver.h"
00044 #include "httpd.h"
00045
00046
00047 static struct ctk_window mainwindow;
00048
00049 static struct ctk_label message =
00050 {CTK_LABEL(0, 0, 15, 1, "Latest requests")};
00051
00052 PROCESS(webserver_process, "Web server");
00053
00054 AUTOSTART_PROCESSES(&webserver_process);
00055
00056 #define LOG_WIDTH 38
00057 #define LOG_HEIGHT 16
00058 static char log[LOG_WIDTH*LOG_HEIGHT];
00059
00060 static struct ctk_label loglabel =
00061 {CTK_LABEL(0, 1, LOG_WIDTH, LOG_HEIGHT, log)};
00062
00063 PROCESS_THREAD(webserver_process, ev, data)
00064 {
00065 PROCESS_BEGIN();
00066
00067 ctk_window_new(&mainwindow, LOG_WIDTH, LOG_HEIGHT+1, "Web server");
00068
00069 CTK_WIDGET_ADD(&mainwindow, &message);
00070 CTK_WIDGET_ADD(&mainwindow, &loglabel);
00071
00072 httpd_init();
00073
00074 ctk_window_open(&mainwindow);
00075
00076 while(1) {
00077 PROCESS_WAIT_EVENT();
00078
00079 if(ev == ctk_signal_window_close ||
00080 ev == PROCESS_EVENT_EXIT) {
00081 ctk_window_close(&mainwindow);
00082 process_exit(&webserver_process);
00083 LOADER_UNLOAD();
00084 } else if(ev == tcpip_event) {
00085 httpd_appcall(data);
00086 }
00087 }
00088
00089 PROCESS_END();
00090 }
00091
00092 void
00093 webserver_log_file(uip_ipaddr_t *requester, char *file)
00094 {
00095 int size;
00096
00097
00098 memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
00099
00100
00101 size = sprintf(&log[LOG_WIDTH * (LOG_HEIGHT - 1)],
00102 "%d.%d.%d.%d: ",
00103 requester->u8[0],
00104 requester->u8[1],
00105 requester->u8[2],
00106 requester->u8[3]);
00107
00108
00109 strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1) + size], file, LOG_WIDTH - size);
00110
00111
00112 CTK_WIDGET_REDRAW(&loglabel);
00113 }
00114
00115 void
00116 webserver_log(char *msg)
00117 {
00118
00119 memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
00120
00121
00122 strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1)], msg, LOG_WIDTH);
00123
00124
00125 CTK_WIDGET_REDRAW(&loglabel);
00126 }
00127