webserver.c

00001 /*
00002  * Copyright (c) 2002, Adam Dunkels.
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution. 
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.  
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00029  *
00030  * This file is part of the Contiki desktop environment for the C64.
00031  *
00032  * $Id: webserver.c,v 1.1 2009/03/12 19:15:25 adamdunkels Exp $
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 /* The main window. */
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   /* Scroll previous entries upwards */
00098   memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
00099 
00100   /* Print out IP address of requesting host. */
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   /* Copy filename into last line. */            
00109   strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1) + size], file, LOG_WIDTH - size);
00110            
00111   /* Update log display. */
00112   CTK_WIDGET_REDRAW(&loglabel);
00113 }
00114 /*-----------------------------------------------------------------------------------*/
00115 void
00116 webserver_log(char *msg)
00117 {
00118   /* Scroll previous entries upwards */
00119   memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
00120 
00121   /* Copy filename into last line. */            
00122   strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1)], msg, LOG_WIDTH);
00123   
00124   /* Update log display. */
00125   CTK_WIDGET_REDRAW(&loglabel);
00126 }
00127 /*-----------------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:42 2011 for Contiki 2.5 by  doxygen 1.6.1