⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sldemo.c

📁 picoos源码。The RTOS and the TCP/IP stack will be built automatically.
💻 C
字号:
/*
 *  Copyright (c) 2004, Dennis Kuschel.
 *  All rights reserved. 
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *   3. The name of the author may not be used to endorse or promote
 *      products derived from this software without specific prior written
 *      permission. 
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 *  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 *  INDIRECT,  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/**
 * @file    httptest.c
 * @author  Dennis Kuschel
 * @brief   Demonstration program for the HTTP Demon with servlets.
 *
 * This software is from http://mycpu.mikrocontroller.net.
 * Please send questions and bug reports to dennis_k@freenet.de.
 */

#include "../src/sys.h"
#include "../src/httpd.h"
#include "../src/filesys.h"
#include "sys/types.h"
#include "sys/stat.h"



/*---------------------------------------------------------------------------
 *  TYPEDEFS and DEFINES
 *-------------------------------------------------------------------------*/

#define _stat  stat

typedef struct {
  SERVLETFUNC_t  slfunc;
  const char *slname;
  const char *sldesc;
} slet_t;



/*---------------------------------------------------------------------------
 *  FUNCTION PROTOTYPES
 *-------------------------------------------------------------------------*/

/* local */
static DWORD WINAPI  timer_thread(LPVOID arg);
static DWORD WINAPI  http_thread(LPVOID arg);
static void   start_thread(LPTHREAD_START_ROUTINE  function);
static void   mainprog(void);
static sint_t ask(char *question);
static sint_t installServlets(void);

/* imports */
extern sint_t  my_switch_servlet(SERVLET_t so);
extern sint_t  my_counter_servlet(SERVLET_t so);
extern sint_t  my_chargen_servlet1(SERVLET_t so);
extern sint_t  my_chargen_servlet2(SERVLET_t so);
extern sint_t  my_login_servlet(SERVLET_t so);



/*---------------------------------------------------------------------------
 *  GLOBAL VARIABLES
 *-------------------------------------------------------------------------*/

u32_t   jiffies = 0;
sint_t  stopTimerThread_g;

slet_t  servletList_g[] = {
  { my_counter_servlet, "counter",
    "A very simple servlet that provides a counter that is incremented "
    "by pressing a hyperlink." },
  { my_chargen_servlet1, "chargen1",
    "A more complex servlet that provides a configurable character "
    "pattern generator." },
  { my_chargen_servlet2, "chargen2",
    "This servlet provides also a configurable character pattern "
    "generator, but it uses a session to store its temporary data." },
  { my_login_servlet, "logindemo",
    "This servlet demonstrates server access via login and password. "
    "It is fully session based. (login:test pass:1234)" },
  { (SERVLETFUNC_t) NULL, NULL, NULL }
};



/*-------------------------------------------------------------------------*/


static void mainprog(void)
{
  sint_t  status;

  printf("\nThis program demonstrates the e-soft HTTP demon with servlets.\n");
  printf("I recommend to use a Mozilla web browser to test the servlets.\n");
  printf("\nCopyright (c) 2004, Dennis Kuschel\n\n");

  printf("\nStarting the RAM drive... ");
  status = fsys_init(8192, 256, 20);
  if (status != 0)
  {
    printf("failed!\n");
    return;
  }

  printf("ok.\nStarting HTTP Demon... ");
  start_thread(http_thread);
  sysSleep(1500);
  if (!httpd_running())
  {
    printf("failed, quit.\n");
    fsys_term();
    return;
  }

  printf("ok.\nInstalling Servlets... ");
  status = installServlets();
  if (status != 0)
  {
    fsys_term();
    printf("failed, quit.\n");
    return;
  }

  printf("ok.\n\n");
  printf("Now open a web browser and enter the URL:  http://127.0.0.1\n\n");


  while (ask("HTTPD is running, stop now?") == 0);

  printf("\nShutting down HTTPD...");

  if (httpd_stop() == 0)
  {
    printf(" OK\n");
  }
  else
  {
    printf(" FAILED\n");
  }
  
  printf("Shutting down filesystem...\n");
  fsys_term();

  printf("Quit.\n");  
}


/*-------------------------------------------------------------------------*/


/* tool function, prints a string into a file */
static void f_printf(sint_t file, const char *fmt, ...)
{
  char buf[200];
  va_list args;
  va_start(args, fmt);
  vsprintf(buf, fmt, args);
  va_end(args);
  fsys_write(file, buf, strlen(buf));
}


/* This function is called by the main program
 * to install the servlets.
 */
static sint_t installServlets(void)
{
  sint_t i, status;
  sint_t file;

  /* install servlets */
  i = 0;
  do
  {
    status = hsl_create(servletList_g[i].slfunc, servletList_g[i].slname);
    i++;
  }
  while ((status == 0) && (servletList_g[i].slfunc != NULL));

  if (status == 0)
    status = hsl_create(my_switch_servlet, "switch");

  if (status != 0)
    printf("Failed to create servlet!\n");

  /* generate index.html */
  file = fsys_open("index.html", FSO_CREAT | FSO_TRUNC);
  if (file < 0)
  {
    printf("Failed to create index.html!\n");
  }
  else
  {
    f_printf(file, "<html><head><title>Servlet Demos</title></head>\r\n");
    f_printf(file, "<body><h1>Servlet Demos</h1><br>\r\n");
    f_printf(file, "<table border=\"0\">\r\n");
    i = 0;
    do
    {
      f_printf(file, "<tr><td align=\"left\" width=\"100\">");
      f_printf(file, "<a href=\"%s\">%s</a>",
               servletList_g[i].slname, servletList_g[i].slname);
      f_printf(file, "</td><td>%s</td></tr><tr><td>&nbsp;</td></tr>\r\n",
               servletList_g[i].sldesc);
      i++;
    }
    while (servletList_g[i].slfunc != NULL);
  }
  f_printf(file, "</table><br>\r\n");

  f_printf(file, "Or push one of these buttons to toggle a digital switch:");
  f_printf(file, "<br><br>\r\n<table><tr><td>\r\n");
  f_printf(file, "<form action=\"switch\" method=\"get\">\r\n");
  f_printf(file, "<input type=\"hidden\" name=\"switch\" value=\"1\">\r\n");
  f_printf(file, "<input type=\"hidden\" name=\"state\" value=\"on\">\r\n");
  f_printf(file, "<input type=\"submit\" value=\"on\">\r\n");
  f_printf(file, "</form>\r\n");
  f_printf(file, "</td><td>&nbsp;</td><td>\r\n");
  f_printf(file, "<form action=\"switch\" method=\"get\">\r\n");
  f_printf(file, "<input type=\"hidden\" name=\"switch\" value=\"1\">\r\n");
  f_printf(file, "<input type=\"hidden\" name=\"state\" value=\"off\">\r\n");
  f_printf(file, "<input type=\"submit\" value=\"off\">\r\n");
  f_printf(file, "</form>\r\n");
  f_printf(file, "</td></tr></table>\r\n");

  f_printf(file, "</body></html>\r\n");

  fsys_close(file);

  return status;
}


/*-------------------------------------------------------------------------*/


static sint_t  ask(char *question)
{
  char inbuf[100];
  do
  {
    printf("%s <Y/N>: ", question);
    gets(inbuf);
  }
  while ((strlen(inbuf) != 1) ||
         ((tolower(inbuf[0]) != 'y') && (tolower(inbuf[0]) != 'n')));
  return (tolower(inbuf[0]) == 'y') ? 1 : 0;
}


/*-------------------------------------------------------------------------*/


static DWORD WINAPI timer_thread(LPVOID arg)
{
  (void) arg;

  SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
  while (!stopTimerThread_g)
  {
    sysSleep(10);
    jiffies++;
  }
  return 0;
}


static DWORD WINAPI http_thread(LPVOID arg)
{
  (void) arg;

  httpd_start("/", 0x8000, 0x0800);
  return 0;
}


static void start_thread(LPTHREAD_START_ROUTINE  function)
{
  DWORD threadid;
  CreateThread(NULL, 0, function, NULL, 0, &threadid);
}


/*-------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
  WORD    wVersionRequested;
  WSADATA wsaData;
 
  (void) argc;
  (void) argv;

  /* initialize the socket layer */
  wVersionRequested = MAKEWORD(2,0);
  if (WSAStartup(wVersionRequested, &wsaData) != 0)
  {
    printf("Sorry, no usable winsock.dll found.\n");
    return 1;
  }
  if ((LOBYTE(wsaData.wVersion) != 2) ||
      (HIBYTE(wsaData.wVersion) != 0))
  {
    printf("Sorry, no usable winsock.dll found.\n");
    WSACleanup();
    return 1; 
  }

  /* create timer thread */
  stopTimerThread_g = 0;
  start_thread(timer_thread);

  mainprog();

  /* stop the timer thread */
  stopTimerThread_g = 1;
  sysSleep(20);

  WSACleanup();
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -