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

📄 tgiex2.c

📁 《Web编程专家指南》
💻 C
字号:
/*
 *  Task  Gateway  Interface
 *  A. Montefusco
 *  June 10, 1995
 *
 *  Example 2
 *
 *  TGI program with more than one instance.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <process.h>
#include <dos.h>
#include "tgi.h"


/*
 *   htPrintf
 *
 *   printf like function to emit a HTML output stream from TGI callback
 *   function
 */

int htPrintf (HREQUEST hReq, const char *fmt, ...)
{
   char    szBuf[1024];
   va_list argptr;
   int     cnt;

   va_start(argptr, fmt);
   cnt = vsprintf(szBuf, fmt, argptr);
   va_end(argptr);

   TgiPutHtml (hReq, szBuf, (long)cnt);

   return(cnt);
}



/*
 *   HTML 2.0 compatible header... 
 */

static char szHtmlHeader [] =
              "Content-type: text/html\r\n\r\n"                    
              "<html>"                                             
              "<head>"                                             
              "<title>Returned from TGI executable !!! </title>"   
              "</head>"                                            
              "<body>"                                             
              "<h1>"                                               
              "TGI TEST"                                           
              "</h1>"                                              
              "<p>"                                                
              "Returned from Task Gateway Interface executable." ;

/*
 *   ... and trailer
 */

static char szHtmlTrailer [] =  "</body>" 
                                "</html>" ;



/*
 *   TGI  callback function
 *
 *   hReq parameter must be used in all following calls to TGI API
 *   pCbData does not point to a data struct but contains the program
 *   start timestamp
 *
 *   if in URL's query part there is a valid integer, the function
 *   delay an equal number of seconds
 */

long CbExample2 (HREQUEST hReq, void *pCbData)
{
   printf ("Callback: recalled with: %p *p\n", (void *)hReq, pCbData);


   TgiPutHtml (hReq, szHtmlHeader, strlen(szHtmlHeader));

   {
      char *pszPi;
      char *pszQi;
      char *pszFd;
      int   delayTime;

      htPrintf (hReq, "<p><p><pre>");

      TgiGetPathInfo  ( hReq, &pszPi ) ;
      TgiGetQueryInfo ( hReq, &pszQi ) ;
      TgiGetFormData  ( hReq, &pszFd ) ;

      htPrintf (hReq, "PathInfo  : %s<p>", pszPi);
      htPrintf (hReq, "QueryInfo : %s<p>", pszQi);
      htPrintf (hReq, "FormData  : %s<p>", pszFd);

      htPrintf (hReq, "<p></pre>");

      // compute delay time

      if (sscanf (pszQi, "%d", &delayTime) == 1) sleep (delayTime);
   }

   {
      time_t  timeNow = time(0);
      time_t  timeStart = *((time_t *)pCbData);   // program start timestamp

      htPrintf (hReq, "<p>Run @ %s<p>", asctime (localtime(&timeNow)));
      htPrintf (hReq, "<p>Tgi module start @ %s<p>", asctime (localtime(&timeStart)));
   }

   TgiPutHtml (hReq, szHtmlTrailer, strlen(szHtmlTrailer));

   return TGI_OK;
}


// used to encapsulate thread's parameters

typedef struct {
   HTGI   hTgi;
   time_t timeStart;
} THREAD_PARAM;


void  TgiThread (void *pPrm)
{
   THREAD_PARAM  *pTp = (THREAD_PARAM *)pPrm;
   long   rc;

   rc = TgiProcessModule ( pTp->hTgi, &(pTp->timeStart) );

   TGI_FATAL(rc);
}



int main (const int argc, const char *argv[])
{
   int    nInst = 1, i;
   long   rc;
   HTGI   hTgi;                        // handle of the TGI extension
   char  *pszModName = "example_2";    // TGI extension name
   time_t timeStart;                   // program start timestamp

   THREAD_PARAM  *pThPrm;


   if (argc > 1) {
      // read from the command line the number of instances that 
      // must be registered with the server
      sscanf (argv[1], "%d", &nInst);
   } /* endif */


   // compute and store the program start timestamp
   timeStart = time(0);

   rc = TgiRegisterModule ( pszModName, CbExample2, &hTgi, 0 );

   TGI_FATAL(rc);


   // start threads ...
   pThPrm = (THREAD_PARAM *) malloc (nInst * sizeof (THREAD_PARAM));

   for (i = 0; i < nInst; ++i) {
      pThPrm[i].hTgi      = hTgi;
      pThPrm[i].timeStart = timeStart;
      _beginthread(TgiThread, 30000, &pThPrm[i]);
   } /* endfor */


   // wait user event
   printf ("Press ENTER to terminate....");
   getchar ();


   rc = TgiDeregisterModule ( hTgi );

   TGI_FATAL(rc);

   return 0;
}

⌨️ 快捷键说明

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