home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 September / CHIPCD_9_99.iso / software / serwery_www / sambar / _setup.1 / winmain.c < prev    next >
C/C++ Source or Header  |  1998-05-19  |  14KB  |  550 lines

  1. /*
  2. ** WINMAIN
  3. **
  4. **      This is the main driver for the Windows driver of the Sambar
  5. **        Server. 
  6. **
  7. **        Confidential Property of Tod Sambar
  8. **        (c) Copyright Tod Sambar 1995-1998
  9. **        All rights reserved.
  10. **
  11. **
  12. ** Syntax:
  13. **
  14. **      server
  15. **
  16. **
  17. ** History:
  18. ** Chg#    Date    Description                                                Resp
  19. ** ----    -------    -------------------------------------------------------    ----
  20. **        9MAR97     Created                                                    sambar
  21. **        5NOV97     Simplified interface                                    sambar
  22. **        18FEB98    Added real-time log display.                            sambar
  23. */
  24.  
  25. #include    <windows.h>
  26. #include    <stdio.h>
  27. #include    <io.h>
  28. #include    <fcntl.h>
  29. #include    <sys/types.h>
  30. #include    <sys/stat.h>
  31. #include    <share.h>
  32. #include    <process.h>
  33. #include    <sambar.h>
  34. #include    <resource.h>
  35.  
  36. /*
  37. ** Local Defines
  38. */
  39. #define SERVER_ID     1001
  40. #define SERVER_MSG     WM_USER + 69
  41. #define NAME        "Sambar Server"
  42.  
  43. static int            LogCount = 0;
  44. static HWND            MainWindow = NULL;
  45. static HWND            MainList = NULL;
  46. static HWND            HideButton = NULL;
  47. static HWND            PauseButton = NULL;
  48. static HWND            ClearButton = NULL;
  49. static HWND            ConnStats = NULL;
  50. static HWND            HConnStats = NULL;
  51. static HWND            ThreadStats = NULL;
  52. static HANDLE        MainInstance;
  53. static BOOL            ShellTray = FALSE;
  54. static BOOL            Showing = TRUE;
  55. static BOOL            Paused = FALSE;
  56.  
  57.  
  58. /*
  59. ** Local Prototypes
  60. */
  61. long __stdcall         WndProc(
  62.                     HWND         hWnd, 
  63.                     UINT         message, 
  64.                     WPARAM         wParam,
  65.                     LPARAM         lParam
  66.                     );
  67. void                DisplayMenu(
  68.                     HWND         hWnd
  69.                     );
  70. void                RestartServer(
  71.                     HWND         hWnd
  72.                     );
  73. void                ShutdownServer(
  74.                     HWND         hWnd
  75.                     );
  76. BOOL                 DisplayLoglines(
  77.                     HWND         hWnd
  78.                     );
  79. void                 HTTPLog(
  80.                     SA_CTX         *ctx,
  81.                     SA_HTTPLOG    *httplog
  82.                     );
  83.  
  84. int     __stdcall
  85. WinMain(HANDLE Instance, HANDLE PrevInstance, LPSTR CmdLine, int CmdShow)
  86. {
  87.     unsigned long    thread;
  88.     SA_BOOL            ver3;
  89.     SA_BOOL            stopped;
  90.     SA_INT            count;
  91.     SA_CTX            *ctx;
  92.     DWORD            dwVersion;        // To hold return value from GetVersion
  93.     MSG                message;
  94.     HWND            hWnd;
  95.     WNDCLASS        MainClass;
  96.     HICON            ServerIcon;
  97.     NOTIFYICONDATA    IconData;
  98.     SA_INT            len;
  99.     SA_INT            port;
  100.     char            tmp[256];
  101.     char            name[256];
  102.     char            homedir[1024];
  103.     char            buffer[2048];
  104.  
  105.  
  106.     /* Save the application Instance                                    */
  107.     MainInstance = Instance;
  108.  
  109.     /* If a Server is running, show it                                    */
  110.     hWnd = FindWindow(NAME, NULL);
  111.     if (hWnd)
  112.     {
  113.         if (IsIconic(hWnd))
  114.             ShowWindow(hWnd, SW_RESTORE);
  115.  
  116.         SetForegroundWindow(hWnd);
  117.         return (0);
  118.     } 
  119.  
  120.     /* Create the tray Icon resource                                    */
  121.     ServerIcon = LoadIcon(Instance, "SERVER_ICON");
  122.  
  123.     /* Create a window class                                            */
  124.     MainClass.style =             CS_HREDRAW | CS_VREDRAW;
  125.     MainClass.lpfnWndProc =        WndProc;
  126.     MainClass.cbClsExtra =         0;
  127.     MainClass.cbWndExtra =         0;
  128.     MainClass.hInstance =         Instance;
  129.     MainClass.hIcon =             ServerIcon;
  130.     MainClass.hCursor =         LoadCursor(NULL, IDC_ARROW);
  131.     MainClass.hbrBackground =     GetStockObject(LTGRAY_BRUSH);
  132.     MainClass.lpszMenuName =     NULL;
  133.     MainClass.lpszClassName =     NAME;
  134.  
  135.     if (!RegisterClass(&MainClass))
  136.         return (0);
  137.  
  138.     /* Determine if this is windows 3.51 -- change the window type        */
  139.     ver3 = 0;
  140.     dwVersion = GetVersion();
  141.     if (dwVersion < 0x80000000) 
  142.     {
  143.         if ((DWORD)(LOBYTE(LOWORD(dwVersion))) == 3)
  144.             ver3 = 1;
  145.     }
  146.  
  147.     /*
  148.     ** Start the Sambar Server.
  149.     ** On failure, the server will shutdown and destroy the MainWindow.
  150.     */
  151.     thread = _beginthread(sa_server, 0, (SA_VOID *)&MainWindow);
  152.     if (thread == -1)
  153.         return (0);
  154.  
  155.     /*
  156.     ** Wait for the server context to start up...
  157.     */
  158.     count = 0;
  159.     stopped = FALSE;
  160.     ctx = (SA_CTX *)NULL;
  161.     while ((!stopped) && (count < 60) && (ctx == (SA_CTX *)NULL))
  162.     {
  163.         if (sa_ctx_global(&ctx) != SA_SUCCEED)
  164.         {
  165.             Sleep(2000);
  166.  
  167.             /* Determine if the server has prematurely shut down        */
  168.             stopped = sa_stopped();
  169.         }
  170.  
  171.         count++;
  172.     }
  173.  
  174.     if (ctx == (SA_CTX *)NULL)
  175.     {
  176.         MessageBox(NULL, "Failure initializing server, see server.log", 
  177.             NAME, MB_OK | MB_ICONSTOP );
  178.         return (0);
  179.     }
  180.  
  181.     /* Create the main window                                            */
  182.     MainWindow = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, NAME, NAME,
  183.         (WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX),
  184.         CW_USEDEFAULT, 0, 600, 470, NULL, NULL, Instance, NULL);
  185.  
  186.     if (!MainWindow)
  187.     {
  188.         (SA_VOID)sa_shutdown(0);
  189.         return (0);
  190.     }
  191.  
  192.     /*
  193.     ** Display the Sambar Server machine/port
  194.     */
  195.     name[0] = '\0';
  196.     (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_SERVERSW, 
  197.         (SA_BYTE *)name, 255, &len);
  198.     hWnd = CreateWindow("STATIC", "Version", WS_CHILD | WS_VISIBLE, 
  199.         20, 15, 300, 20, MainWindow, NULL, Instance, NULL);
  200.     SetWindowText(hWnd, name);
  201.  
  202.     (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_BUILDID, 
  203.         (SA_BYTE *)tmp, 255, &len);
  204.     wsprintf(buffer, "Build: %s", tmp);
  205.     hWnd = CreateWindow("STATIC", "Build", WS_CHILD | WS_VISIBLE, 
  206.         350, 15, 300, 20, MainWindow, NULL, Instance, NULL);
  207.     SetWindowText(hWnd, buffer);
  208.  
  209.     hWnd = CreateWindow("STATIC", "Company", WS_CHILD | WS_VISIBLE, 
  210.         20, 40, 300, 20, MainWindow, NULL, Instance, NULL);
  211.     SetWindowText(hWnd, "Sambar Technologies");
  212.  
  213.     hWnd = CreateWindow("STATIC", "Copyright", WS_CHILD | WS_VISIBLE, 
  214.         20, 60, 300, 20, MainWindow, NULL, Instance, NULL);
  215.     SetWindowText(hWnd, "Copyright Tod Sambar 1996-1998");
  216.  
  217.     hWnd = CreateWindow("STATIC", "Reserved", WS_CHILD | WS_VISIBLE, 
  218.         20, 80, 300, 20, MainWindow, NULL, Instance, NULL);
  219.     SetWindowText(hWnd, "All rights reserved.");
  220.  
  221.     /* Setup the "real-time" statistics windows...                        */
  222.     ThreadStats = CreateWindow("STATIC", "Threads", WS_CHILD | WS_VISIBLE, 
  223.         350, 40, 300, 20, MainWindow, NULL, Instance, NULL);
  224.     SetWindowText(ThreadStats, "Server Threads: 0");
  225.  
  226.     HConnStats = CreateWindow("STATIC", "HConnections", WS_CHILD | WS_VISIBLE, 
  227.         350, 60, 300, 20, MainWindow, NULL, Instance, NULL);
  228.     SetWindowText(HConnStats, "HTTP Connections: 0");
  229.  
  230.     ConnStats = CreateWindow("STATIC", "Connections", WS_CHILD | WS_VISIBLE, 
  231.         350, 80, 300, 20, MainWindow, NULL, Instance, NULL);
  232.     SetWindowText(ConnStats, "Network Connections: 0");
  233.  
  234.     name[0] = '\0';
  235.     (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_SERVERNAME, 
  236.         (SA_BYTE *)name, 255, &len);
  237.     tmp[0] = '\0';
  238.     (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_SERVERIP, 
  239.         (SA_BYTE *)tmp, 64, &len);
  240.     (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_SERVERPORT, 
  241.         (SA_BYTE *)&port, sizeof(SA_INT), &len);
  242.     homedir[0] = '\0';
  243.     (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_HOMEDIR, 
  244.         (SA_BYTE *)homedir, 1024, &len);
  245.  
  246.     hWnd = CreateWindow("STATIC", "Server", WS_CHILD | WS_VISIBLE, 
  247.         20, 105, 300, 20, MainWindow, NULL, Instance, NULL);
  248.     wsprintf(buffer, "Sambar Server  '%s:%d'", name, port);
  249.     SetWindowText(hWnd, buffer);
  250.  
  251.     hWnd = CreateWindow("STATIC", "ipaddress", WS_CHILD | WS_VISIBLE, 
  252.         20, 125, 300, 20, MainWindow, NULL, Instance, NULL);
  253.     wsprintf(buffer, "IP Address: %s", tmp);
  254.     SetWindowText(hWnd, buffer);
  255.  
  256.     hWnd = CreateWindow("STATIC", "homedir", WS_CHILD | WS_VISIBLE, 
  257.         20, 145, 300, 20, MainWindow, NULL, Instance, NULL);
  258.     wsprintf(buffer, "Server Dir: %s", homedir);
  259.     SetWindowText(hWnd, buffer);
  260.  
  261.     /* Create a list box for the log display                            */
  262.     MainList = CreateWindow("LISTBOX", NULL, 
  263.                     WS_CHILD|WS_BORDER|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL, 
  264.                     20, 170, 550, 240, MainWindow, (HMENU)1000, 
  265.                     Instance, NULL);
  266.  
  267.     if (!MainList)
  268.     {
  269.         MessageBox(NULL, "Failure creating list box.", 
  270.             NAME, MB_OK | MB_ICONSTOP );
  271.         (SA_VOID)sa_shutdown(0);
  272.         return (0);
  273.     }
  274.  
  275.     SendMessage(MainList, LB_SETHORIZONTALEXTENT, 2048, 0L);
  276.  
  277.     /*
  278.     ** Put the buttons in.
  279.     */
  280.     ClearButton = CreateWindow("BUTTON", "Clear", WS_CHILD | WS_VISIBLE, 
  281.         20, 400, 50, 25, MainWindow, NULL, Instance, NULL);
  282.     SetWindowText(ClearButton, "Clear");
  283.     if (!ClearButton)
  284.     {
  285.         MessageBox(NULL, "Failure creating clear button.", 
  286.             NAME, MB_OK | MB_ICONSTOP );
  287.         (SA_VOID)sa_shutdown(0);
  288.         return (0);
  289.     }
  290.  
  291.     PauseButton = CreateWindow("BUTTON", "Pause", WS_CHILD | WS_VISIBLE, 
  292.         240, 400, 90, 25, MainWindow, NULL, Instance, NULL);
  293.     SetWindowText(PauseButton, "Pause");
  294.     if (!PauseButton)
  295.     {
  296.         MessageBox(NULL, "Failure creating pause button.", 
  297.             NAME, MB_OK | MB_ICONSTOP );
  298.         (SA_VOID)sa_shutdown(0);
  299.         return (0);
  300.     }
  301.  
  302.     HideButton = CreateWindow("BUTTON", "Hide", WS_CHILD | WS_VISIBLE, 
  303.         520, 400, 50, 25, MainWindow, NULL, Instance, NULL);
  304.     SetWindowText(HideButton, "Hide");
  305.     if (!HideButton)
  306.     {
  307.         MessageBox(NULL, "Failure creating hide button.", 
  308.             NAME, MB_OK | MB_ICONSTOP );
  309.         (SA_VOID)sa_shutdown(0);
  310.         return (0);
  311.     }
  312.  
  313.     /* Add tray icon to system tray                                        */
  314.     IconData.cbSize             = sizeof(NOTIFYICONDATA);
  315.     IconData.hWnd                 = MainWindow;
  316.     IconData.uID                 = SERVER_ID;
  317.     IconData.uFlags             = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  318.     IconData.uCallbackMessage    = SERVER_MSG;
  319.     IconData.hIcon                = ServerIcon;
  320.     strcpy(IconData.szTip, "Sambar Server is Active");
  321.  
  322.     /* Win95 and NT4.0 only                                                */
  323.     if (!Shell_NotifyIcon(NIM_ADD, &IconData))
  324.     {
  325.         ShowWindow(MainWindow, CmdShow);
  326.         UpdateWindow(MainWindow);
  327.     }
  328.     else
  329.     {
  330.         Showing = TRUE;
  331.         ShellTray = TRUE;
  332.         ShowWindow(MainWindow, SW_SHOW);
  333.     }
  334.  
  335.     /* Register the HTTP Log callback.                                    */
  336.     (SA_VOID)sa_ctx_props(ctx, SA_SET, SA_CTXPROP_HTTPLOGFUNC, 
  337.         (SA_BYTE *)HTTPLog, sizeof(SA_VOID *), (SA_INT *)NULL);
  338.  
  339.     /* Message loop                                                        */
  340.     while (GetMessage(&message, NULL, 0, 0))
  341.         DispatchMessage(&message);
  342.  
  343.     /* Shutdown the Sambar Server                                        */
  344.     (SA_VOID)sa_shutdown(0);
  345.     Sleep(4000);
  346.  
  347.     Shell_NotifyIcon(NIM_DELETE, &IconData);
  348.     UnregisterClass(NAME, Instance);
  349.  
  350.     return (message.wParam);
  351. }
  352.  
  353. long __stdcall 
  354. WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  355. {
  356.     switch (message) 
  357.     {
  358.     case SERVER_MSG:
  359.         if ((wParam == SERVER_ID) && (lParam == WM_RBUTTONDOWN))
  360.         {
  361.             DisplayMenu(hWnd);
  362.         }
  363.         else if ((wParam == SERVER_ID) && (lParam == WM_LBUTTONDBLCLK))
  364.         {
  365.             Showing = TRUE;
  366.             ShowWindow(hWnd, SW_SHOW);
  367.         }
  368.         break;
  369.  
  370.     case WM_COMMAND:
  371.         if ((HWND)lParam == HideButton)
  372.         {
  373.             if (ShellTray)
  374.                 ShowWindow(MainWindow, SW_HIDE);
  375.             else
  376.                 ShowWindow(MainWindow, SW_MINIMIZE);
  377.  
  378.             LogCount = 1;
  379.             Showing = FALSE;
  380.             SendMessage(MainList, LB_RESETCONTENT, 0, 0);
  381.         }
  382.         else if ((HWND)lParam == PauseButton)
  383.         {
  384.             if (Paused)
  385.             {
  386.                 Paused = FALSE;
  387.                 SetWindowText(PauseButton, "Pause");
  388.             }
  389.             else
  390.             {
  391.                 Paused = TRUE;
  392.                 SetWindowText(PauseButton, "Un-Pause");
  393.             }
  394.         }
  395.         else if ((HWND)lParam == ClearButton)
  396.         {
  397.             /* Clear the list box                                        */
  398.             LogCount = 1;
  399.             SendMessage(MainList, LB_RESETCONTENT, 0, 0);
  400.         }
  401.         else
  402.         {
  403.             /* Unhandled message                                        */
  404.             DefWindowProc(hWnd, message, wParam, lParam);
  405.         }
  406.         break;
  407.  
  408.     case WM_DESTROY:
  409.         /* Shutdown the main window                                        */
  410.         PostQuitMessage(0);
  411.         break;
  412.  
  413.     default:
  414.         /* Unhandled Messages end up here (DefWindowProc)                */
  415.         return DefWindowProc(hWnd, message, wParam, lParam);
  416.     }
  417.  
  418.     return(0);
  419. }
  420.  
  421. void
  422. DisplayMenu(HWND hWnd)
  423. {
  424.     HMENU     MenuHnd;
  425.     POINT     MousePos;
  426.     int     ScreenWidth;
  427.     int     ScreenHeight;
  428.     int     SelItem;
  429.  
  430.     MenuHnd = CreatePopupMenu();
  431.     AppendMenu(MenuHnd, MF_ENABLED, 1, "Open");
  432.     AppendMenu(MenuHnd, MF_SEPARATOR, 0, NULL);
  433.     AppendMenu(MenuHnd, MF_ENABLED, 2, "Restart");
  434.     AppendMenu(MenuHnd, MF_ENABLED, 3, "Shutdown");
  435.  
  436.     //Get Mouse Pos
  437.     GetCursorPos(&MousePos);
  438.  
  439.     //Get Screen Metrics
  440.     ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  441.     ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  442.  
  443.     SetForegroundWindow(MainWindow);
  444.  
  445.     //Handle the different possible task bar locations
  446.     if ((MousePos.x >= (ScreenWidth / 2)) && (MousePos.y >= (ScreenHeight / 2)))
  447.     {
  448.         //Bottom or Right
  449.         SelItem = TrackPopupMenu(MenuHnd,
  450.             TPM_BOTTOMALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
  451.             MousePos.x, ScreenHeight, 0, MainWindow, NULL);
  452.     }
  453.     else if (MousePos.y < (ScreenHeight / 2)) 
  454.     {
  455.         //Top
  456.         SelItem = TrackPopupMenu(MenuHnd,
  457.             TPM_TOPALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
  458.             MousePos.x, MousePos.y, 0, MainWindow, NULL);
  459.     }
  460.     else 
  461.     {
  462.         //Left
  463.         SelItem = TrackPopupMenu(MenuHnd,
  464.             TPM_BOTTOMALIGN | TPM_LEFTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
  465.             MousePos.x, ScreenHeight, 0, MainWindow, NULL);
  466.     }
  467.  
  468.     SetForegroundWindow(MainWindow);
  469.     DestroyMenu(MenuHnd);
  470.  
  471.     switch (SelItem)
  472.     {
  473.     case 1:
  474.         Showing = TRUE;
  475.         ShowWindow(MainWindow, SW_SHOW);
  476.         break;
  477.     case 2:
  478.         RestartServer(MainWindow);
  479.         break;
  480.     case 3:
  481.         ShutdownServer(MainWindow);
  482.         break;
  483.     default:
  484.         break;
  485.     }
  486. }
  487.  
  488. void
  489. ShutdownServer(HWND MainWindow)
  490. {
  491.     int Answer;
  492.  
  493.     Answer = MessageBox(MainWindow, 
  494.                 "Are you sure you want to shutdown the Sambar Server?",
  495.                 NAME, MB_YESNO | MB_ICONQUESTION);
  496.  
  497.     // If they do destroy the main window and let the the rest fall in place...
  498.     if (Answer == IDYES)
  499.         DestroyWindow(MainWindow);
  500. }
  501.  
  502. void
  503. RestartServer(HWND MainWindow)
  504. {
  505.     int Answer;
  506.  
  507.     Answer = MessageBox(MainWindow, 
  508.                 "Are you sure you want to restart the Sambar Server?",
  509.                 NAME, MB_YESNO | MB_ICONQUESTION);
  510.  
  511.     // If they do destroy the main window and let the the rest fall in place...
  512.     if (Answer == IDYES)
  513.         sa_shutdown(1);
  514. }
  515.  
  516.  
  517. void
  518. HTTPLog(SA_CTX *ctx, SA_HTTPLOG    *httplog)
  519. {
  520.     int        n;
  521.     char    str[2048];
  522.  
  523.     if ((!MainList) || (Paused) || (!Showing))
  524.         return;
  525.  
  526.     LogCount++;
  527.     if (LogCount > 200)
  528.     {
  529.         LogCount = 1;
  530.         SendMessage(MainList, LB_RESETCONTENT, 0, 0);
  531.     }
  532.  
  533.     sprintf(str, "[%s]  %s  %s  %ld  %s  %s    %ld", httplog->vhost, 
  534.         httplog->timestamp, httplog->user, httplog->status, 
  535.         httplog->method, httplog->request, httplog->size);
  536.     SendMessage(MainList, LB_ADDSTRING, 0, (LPARAM)(LPSTR)str);
  537.     
  538.     n = SendMessage(MainList, LB_GETCOUNT, 0, 0);
  539.     SendMessage(MainList, LB_SETCURSEL, (unsigned int)n - 1, 0);
  540.  
  541.     sprintf(str, "Server Threads: %ld", httplog->threads);
  542.     SetWindowText(ThreadStats, str);
  543.     sprintf(str, "HTTP Connections: %ld", httplog->httpconns);
  544.     SetWindowText(HConnStats, str);
  545.     sprintf(str, "Network Connections: %ld", httplog->allconns);
  546.     SetWindowText(ConnStats, str);
  547.             
  548.     return;
  549. }
  550.