home *** CD-ROM | disk | FTP | other *** search
- /*
- ** WINMAIN
- **
- ** This is the main driver for the Windows driver of the Sambar
- ** Server.
- **
- ** Confidential Property of Tod Sambar
- ** (c) Copyright Tod Sambar 1995-2001
- ** All rights reserved.
- **
- **
- ** Syntax:
- **
- ** server [-bnst] [-c <configdir>]
- **
- ** -b Start a browser and load the home page.
- ** -c Path to the config directory for use.
- ** -n Don't check for an existing server instance.
- ** -s Show the GUI on startup.
- ** -t Don't trace to GUI display when in icon tray
- ** (performance optimization).
- **
- **
- ** History:
- ** Chg# Date Description Resp
- ** ---- ------- ------------------------------------------------------- ----
- ** 9MAR97 Created sambar
- ** 5NOV97 Simplified interface sambar
- ** 18FEB98 Added real-time log display. sambar
- */
-
- #include <windows.h>
- #include <stdio.h>
- #include <io.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <share.h>
- #include <process.h>
- #include <sambar.h>
- #include <resource.h>
-
- /*
- ** Local Defines
- */
- #define SERVER_ID 1001
- #define SERVER_MSG WM_USER + 69
- #define NAME "Sambar Server"
-
- static int LogCount = 0;
- static int MainX = 20;
- static int MainY = 135;
- static HWND MainWindow = NULL;
- static HWND MainList = NULL;
- static HWND PauseButton = NULL;
- static HWND ClearButton = NULL;
- static HWND ConnStats = NULL;
- static HWND HConnStats = NULL;
- static HWND ThreadStats = NULL;
- static HINSTANCE MainInstance;
- static BOOL DebugStatus = FALSE;
- static BOOL ShellTray = FALSE;
- static BOOL Showing = FALSE;
- static BOOL Paused = FALSE;
- static BOOL AlwaysTrace = TRUE;
- static BOOL StartBrowser = FALSE;
- static BOOL CheckExisting = TRUE;
- static char ConfigDir[256];
- static NOTIFYICONDATA IconData;
-
-
- /*
- ** Local Prototypes
- */
- long __stdcall WndProc(
- HWND hWnd,
- UINT message,
- WPARAM wParam,
- LPARAM lParam
- );
- void WndShutdown(
- void
- );
- void DisplayMenu(
- HWND hWnd
- );
- void RestartServer(
- HWND hWnd
- );
- void ShutdownServer(
- HWND hWnd
- );
- void StatusPopup(
- SA_CTX *ctx,
- char *msg
- );
- BOOL DisplayLoglines(
- HWND hWnd
- );
- void HTTPLog(
- SA_CTX *ctx,
- SA_HTTPLOG *httplog
- );
- void GetArgs(
- LPSTR cmdline,
- SA_INIT *sainit
- );
-
- int __stdcall
- WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CmdLine, int CmdShow)
- {
- unsigned long thread;
- SA_BOOL ver3;
- SA_BOOL stopped;
- SA_INT count;
- SA_INT maxcount;
- SA_CTX *ctx;
- DWORD dwVersion; // To hold return value from GetVersion
- MSG message;
- HWND hWnd;
- WNDCLASS MainClass;
- HICON ServerIcon;
- SA_INT len;
- SA_INT port;
- char tmp[256];
- char name[256];
- char homedir[1024];
- char buffer[2048];
- SA_INIT sainit;
-
- /* Initialization */
- memset(&sainit, 0, sizeof(SA_INIT));
-
- /* Save the application Instance */
- MainInstance = Instance;
-
- /* Process any command line arguments. */
- GetArgs(CmdLine, &sainit);
-
- /* If a Server is running, show it */
- if (CheckExisting)
- {
- hWnd = FindWindow(NAME, NULL);
- if (hWnd)
- {
- if (IsIconic(hWnd))
- ShowWindow(hWnd, SW_RESTORE);
-
- SetForegroundWindow(hWnd);
- return (0);
- }
- }
-
- SetErrorMode(SEM_FAILCRITICALERRORS |
- SEM_NOGPFAULTERRORBOX |
- SEM_NOOPENFILEERRORBOX);
-
- /* Create the tray Icon resource */
- ServerIcon = LoadIcon(Instance, "SERVER_ICON");
-
- /* Create a window class */
- MainClass.style = CS_HREDRAW | CS_VREDRAW;
- MainClass.lpfnWndProc = WndProc;
- MainClass.cbClsExtra = 0;
- MainClass.cbWndExtra = 0;
- MainClass.hInstance = Instance;
- MainClass.hIcon = ServerIcon;
- MainClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- MainClass.hbrBackground = GetStockObject(LTGRAY_BRUSH);
- MainClass.lpszMenuName = NULL;
- MainClass.lpszClassName = NAME;
-
- if (!RegisterClass(&MainClass))
- return (0);
-
- /* Determine if this is windows 3.51 -- change the window type */
- ver3 = 0;
- dwVersion = GetVersion();
- if (dwVersion < 0x80000000)
- {
- if ((DWORD)(LOBYTE(LOWORD(dwVersion))) == 3)
- ver3 = 1;
- }
-
- /*
- ** Start the Sambar Server.
- ** On failure, the server will shutdown and destroy the MainWindow.
- */
- sainit.i_hWnd = (SA_VOID *)&MainWindow;
- sainit.i_build = 1;
-
- maxcount = 30;
- if (DebugStatus)
- {
- sainit.i_statusfunc = StatusPopup;
- maxcount = 200;
- }
-
- thread = _beginthread(sa_server2, 0, (SA_VOID *)&sainit);
- if (thread == -1)
- return (0);
-
- /*
- ** Wait for the server context to start up...
- */
- count = 0;
- stopped = FALSE;
- ctx = (SA_CTX *)NULL;
- while ((!stopped) && (count < maxcount) && (ctx == (SA_CTX *)NULL))
- {
- if (sa_ctx_global(&ctx) != SA_SUCCEED)
- {
- Sleep(2000);
-
- /* Determine if the server has prematurely shut down */
- stopped = sa_stopped();
- }
-
- count++;
- }
-
- if (ctx == (SA_CTX *)NULL)
- {
- MessageBox(NULL, "Failure initializing server, see server.log",
- NAME, MB_OK | MB_ICONSTOP );
- return (0);
- }
-
- /* Create the main window */
- MainWindow = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, NAME, NAME,
- (WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX),
- CW_USEDEFAULT, 0, 600, 470, NULL, NULL, Instance, NULL);
-
- if (!MainWindow)
- {
- (SA_VOID)sa_shutdown(0);
- return (0);
- }
-
- /*
- ** Display the Sambar Server machine/port
- */
- name[0] = '\0';
- (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_SERVERSW,
- (SA_BYTE *)name, 255, &len);
- hWnd = CreateWindow("STATIC", "Version", WS_CHILD | WS_VISIBLE,
- 20, 15, 300, 20, MainWindow, NULL, Instance, NULL);
- SetWindowText(hWnd, name);
-
- (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_BUILDID,
- (SA_BYTE *)tmp, 255, &len);
- wsprintf(buffer, "Build: %s", tmp);
- hWnd = CreateWindow("STATIC", "Build", WS_CHILD | WS_VISIBLE,
- 350, 15, 300, 20, MainWindow, NULL, Instance, NULL);
- SetWindowText(hWnd, buffer);
-
- /* Setup the "real-time" statistics windows... */
- ThreadStats = CreateWindow("STATIC", "Threads", WS_CHILD | WS_VISIBLE,
- 350, 40, 300, 20, MainWindow, NULL, Instance, NULL);
- SetWindowText(ThreadStats, "Server Threads: 0");
-
- HConnStats = CreateWindow("STATIC", "HConnections", WS_CHILD | WS_VISIBLE,
- 350, 60, 300, 20, MainWindow, NULL, Instance, NULL);
- SetWindowText(HConnStats, "HTTP Connections: 0");
-
- ConnStats = CreateWindow("STATIC", "Connections", WS_CHILD | WS_VISIBLE,
- 350, 80, 300, 20, MainWindow, NULL, Instance, NULL);
- SetWindowText(ConnStats, "Network Connections: 0");
-
- name[0] = '\0';
- (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_SERVERNAME,
- (SA_BYTE *)name, 255, &len);
- tmp[0] = '\0';
- (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_SERVERIP,
- (SA_BYTE *)tmp, 64, &len);
- (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_SERVERPORT,
- (SA_BYTE *)&port, sizeof(SA_INT), &len);
- homedir[0] = '\0';
- (SA_VOID)sa_ctx_props(ctx, SA_GET, SA_CTXPROP_HOMEDIR,
- (SA_BYTE *)homedir, 1024, &len);
-
- hWnd = CreateWindow("STATIC", "Server", WS_CHILD | WS_VISIBLE,
- 20, 40, 300, 20, MainWindow, NULL, Instance, NULL);
- wsprintf(buffer, "Sambar Server '%s:%d'", name, port);
- SetWindowText(hWnd, buffer);
-
- hWnd = CreateWindow("STATIC", "ipaddress", WS_CHILD | WS_VISIBLE,
- 20, 60, 300, 20, MainWindow, NULL, Instance, NULL);
- wsprintf(buffer, "IP Address: %s", tmp);
- SetWindowText(hWnd, buffer);
-
- hWnd = CreateWindow("STATIC", "homedir", WS_CHILD | WS_VISIBLE,
- 20, 80, 300, 20, MainWindow, NULL, Instance, NULL);
- wsprintf(buffer, "Server Dir: %s", homedir);
- SetWindowText(hWnd, buffer);
-
- /*
- ** Put the buttons in.
- */
- ClearButton = CreateWindow("BUTTON", "Clear", WS_CHILD | WS_VISIBLE,
- MainX, MainY - 30, 50, 25, MainWindow, NULL, Instance, NULL);
- SetWindowText(ClearButton, "Clear");
- if (!ClearButton)
- {
- MessageBox(NULL, "Failure creating clear button.",
- NAME, MB_OK | MB_ICONSTOP );
- (SA_VOID)sa_shutdown(0);
- return (0);
- }
-
- PauseButton = CreateWindow("BUTTON", "Pause", WS_CHILD | WS_VISIBLE,
- MainX + 85, MainY - 30, 90, 25, MainWindow, NULL, Instance, NULL);
- SetWindowText(PauseButton, "Pause");
- if (!PauseButton)
- {
- MessageBox(NULL, "Failure creating pause button.",
- NAME, MB_OK | MB_ICONSTOP );
- (SA_VOID)sa_shutdown(0);
- return (0);
- }
-
- /* Create a list box for the log display */
- MainList = CreateWindow("LISTBOX", NULL,
- WS_CHILD|WS_BORDER|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL,
- MainX, MainY, 550, 240, MainWindow, (HMENU)1000,
- Instance, NULL);
-
- if (!MainList)
- {
- MessageBox(NULL, "Failure creating list box.",
- NAME, MB_OK | MB_ICONSTOP );
- (SA_VOID)sa_shutdown(0);
- return (0);
- }
-
- SendMessage(MainList, LB_SETHORIZONTALEXTENT, 2048, 0L);
-
- /* Add tray icon to system tray */
- IconData.cbSize = sizeof(NOTIFYICONDATA);
- IconData.hWnd = MainWindow;
- IconData.uID = SERVER_ID;
- IconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- IconData.uCallbackMessage = SERVER_MSG;
- IconData.hIcon = ServerIcon;
- strcpy(IconData.szTip, "Sambar Server is Active");
-
- /* Win95 and NT4.0 only */
- if (!Shell_NotifyIcon(NIM_ADD, &IconData))
- {
- ShowWindow(MainWindow, CmdShow);
- UpdateWindow(MainWindow);
- }
- else
- {
- ShellTray = TRUE;
- ShowWindow(MainWindow, Showing ? SW_SHOW : SW_HIDE);
- }
-
- /* Register the HTTP Log callback. */
- (SA_VOID)sa_ctx_props(ctx, SA_SET, SA_CTXPROP_HTTPLOGFUNC,
- (SA_BYTE *)HTTPLog, sizeof(SA_VOID *), (SA_INT *)NULL);
-
- /* Start the Web Browser to the home page */
- if (StartBrowser)
- {
- if (port != 80)
- wsprintf(buffer, "http://localhost:%d/", port);
- else
- strcpy(buffer, "http://localhost/");
-
- ShellExecute(GetTopWindow(NULL), "open", buffer, NULL, NULL,
- SW_SHOWNORMAL);
- }
-
- atexit(WndShutdown);
-
- /* Message loop */
- while (GetMessage(&message, NULL, 0, 0))
- DispatchMessage(&message);
-
- /* Shutdown the Sambar Server */
- (SA_VOID)sa_shutdown(0);
-
- Shell_NotifyIcon(NIM_DELETE, &IconData);
- UnregisterClass(NAME, Instance);
-
- return (message.wParam);
- }
-
- void WndShutdown()
- {
- Shell_NotifyIcon(NIM_DELETE, &IconData);
- }
-
- long __stdcall
- WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int hi;
- int lo;
- RECT rect;
-
- switch (message)
- {
- case SERVER_MSG:
- if ((wParam == SERVER_ID) && (lParam == WM_RBUTTONDOWN))
- {
- DisplayMenu(hWnd);
- }
- else if ((wParam == SERVER_ID) && (lParam == WM_LBUTTONDBLCLK))
- {
- Showing = TRUE;
- ShowWindow(hWnd, SW_SHOW);
- }
- break;
-
- case WM_CLOSE:
- if (ShellTray)
- {
- ShowWindow(MainWindow, SW_HIDE);
- Showing = FALSE;
- }
- else
- {
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
-
- case WM_COMMAND:
- if ((HWND)lParam == PauseButton)
- {
- if (Paused)
- {
- Paused = FALSE;
- sa_pause(TRUE);
- SetWindowText(PauseButton, "Pause");
- }
- else
- {
- Paused = TRUE;
- sa_pause(FALSE);
- SetWindowText(PauseButton, "Un-Pause");
- }
- }
- else if ((HWND)lParam == ClearButton)
- {
- /* Clear the list box */
- LogCount = 1;
- SendMessage(MainList, LB_RESETCONTENT, 0, 0);
- }
- else
- {
- /* Unhandled message */
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
-
- case WM_SIZE:
- hi = HIWORD(lParam);
- lo = LOWORD(lParam);
- GetClientRect(MainList, &rect);
- if ((LOWORD(lParam) - 40 == rect.right) &&
- (HIWORD(lParam) - 140 == rect.bottom))
- {
- return 0;
- }
-
- /* Adjust the log box's window */
- MoveWindow(MainList, MainX, MainY, LOWORD(lParam) - 40,
- HIWORD(lParam) - 140, TRUE);
- break;
-
- case WM_QUERYENDSESSION:
- return((long)TRUE);
-
- case WM_ENDSESSION:
- if (wParam)
- PostQuitMessage(0);
- break;
-
- case WM_DESTROY:
- /* Shutdown the main window */
- PostQuitMessage(0);
- break;
-
- default:
- /* Unhandled Messages end up here (DefWindowProc) */
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
-
- return(0);
- }
-
- void
- DisplayMenu(HWND hWnd)
- {
- HMENU MenuHnd;
- POINT MousePos;
- int ScreenWidth;
- int ScreenHeight;
- int SelItem;
-
- MenuHnd = CreatePopupMenu();
- AppendMenu(MenuHnd, MF_ENABLED, 1, "Open");
- AppendMenu(MenuHnd, MF_SEPARATOR, 0, NULL);
- AppendMenu(MenuHnd, MF_ENABLED, 2, "Restart");
- AppendMenu(MenuHnd, MF_ENABLED, 3, "Shutdown");
-
- //Get Mouse Pos
- GetCursorPos(&MousePos);
-
- //Get Screen Metrics
- ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
- ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
-
- SetForegroundWindow(MainWindow);
-
- //Handle the different possible task bar locations
- if ((MousePos.x >= (ScreenWidth / 2)) && (MousePos.y >= (ScreenHeight / 2)))
- {
- //Bottom or Right
- SelItem = TrackPopupMenu(MenuHnd,
- TPM_BOTTOMALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
- MousePos.x, ScreenHeight, 0, MainWindow, NULL);
- }
- else if (MousePos.y < (ScreenHeight / 2))
- {
- //Top
- SelItem = TrackPopupMenu(MenuHnd,
- TPM_TOPALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
- MousePos.x, MousePos.y, 0, MainWindow, NULL);
- }
- else
- {
- //Left
- SelItem = TrackPopupMenu(MenuHnd,
- TPM_BOTTOMALIGN | TPM_LEFTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
- MousePos.x, ScreenHeight, 0, MainWindow, NULL);
- }
-
- SetForegroundWindow(MainWindow);
- DestroyMenu(MenuHnd);
-
- switch (SelItem)
- {
- case 1:
- Showing = TRUE;
- ShowWindow(MainWindow, SW_SHOW);
- break;
- case 2:
- RestartServer(MainWindow);
- break;
- case 3:
- ShutdownServer(MainWindow);
- break;
- default:
- break;
- }
- }
-
- void
- ShutdownServer(HWND MainWindow)
- {
- int Answer;
-
- Answer = MessageBox(MainWindow,
- "Are you sure you want to shutdown the Sambar Server?",
- NAME, MB_YESNO | MB_ICONQUESTION);
-
- // If they do destroy the main window and let the the rest fall in place...
- if (Answer == IDYES)
- DestroyWindow(MainWindow);
- }
-
- void
- RestartServer(HWND MainWindow)
- {
- int Answer;
-
- Answer = MessageBox(MainWindow,
- "Are you sure you want to restart the Sambar Server?",
- NAME, MB_YESNO | MB_ICONQUESTION);
-
- // If they do destroy the main window and let the the rest fall in place...
- if (Answer == IDYES)
- sa_shutdown(1);
- }
-
- void
- StatusPopup(SA_CTX *ctx, char *msg)
- {
- (void)MessageBox(MainWindow, msg, NAME, MB_OK | MB_ICONSTOP );
- }
-
-
- void
- HTTPLog(SA_CTX *ctx, SA_HTTPLOG *httplog)
- {
- int i;
- int n;
- char timestr[32];
- char str[2048];
-
- if (!MainList)
- return;
-
- if ((!Showing) && (!AlwaysTrace))
- return;
-
- LogCount++;
- if (LogCount > 200)
- {
- LogCount = 1;
- SendMessage(MainList, LB_RESETCONTENT, 0, 0);
- }
-
- i = 0;
- while ((httplog->timestamp[i] != '\0') && (httplog->timestamp[i] != ':'))
- i++;
-
- n = 0;
- while ((httplog->timestamp[i] != '\0') &&
- (httplog->timestamp[i] != ' ') &&
- (n < 16))
- {
- timestr[n] = httplog->timestamp[i];
- n++;
- i++;
- }
-
- timestr[0] = '[';
- timestr[n] = ']';
- timestr[n+1] = '\0';
-
- sprintf(str, "[%s] %s %s %ld %s %s %ld",
- httplog->vhost, timestr, httplog->user, httplog->status,
- httplog->method, httplog->request, httplog->size);
- SendMessage(MainList, LB_ADDSTRING, 0, (LPARAM)(LPSTR)str);
-
- n = SendMessage(MainList, LB_GETCOUNT, 0, 0);
- SendMessage(MainList, LB_SETCURSEL, (unsigned int)n - 1, 0);
-
- sprintf(str, "Server Threads: %ld", httplog->threads);
- SetWindowText(ThreadStats, str);
- sprintf(str, "HTTP Connections: %ld", httplog->httpconns);
- SetWindowText(HConnStats, str);
- sprintf(str, "Network Connections: %ld", httplog->allconns);
- SetWindowText(ConnStats, str);
-
- return;
- }
-
- void
- GetArgs(LPSTR CmdLine, SA_INIT *sainit)
- {
- int i;
- char *p;
-
- if (CmdLine == NULL)
- return;
-
- p = CmdLine;
- while (*p != '\0')
- {
- switch ((int)*p)
- {
- case 'd':
- case 'D':
- DebugStatus = TRUE;
- break;
- case 'b':
- case 'B':
- StartBrowser = TRUE;
- break;
- case 'n':
- case 'N':
- CheckExisting = FALSE;
- break;
- case 's':
- case 'S':
- Showing = TRUE;
- break;
- case 't':
- case 'T':
- AlwaysTrace = FALSE;
- break;
- case 'c':
- case 'C':
- p++;
- while ((*p != '\0') && (isspace(*p)))
- p++;
-
- i = 0;
- while ((i < 250) && (*p != '\0') && (!isspace(*p)))
- {
- ConfigDir[i] = *p;
- p++;
- i++;
- }
-
- if (i > 0)
- {
- ConfigDir[i] = '\0';
- sainit->i_configdir = ConfigDir;
- }
- break;
- case '-':
- default:
- break;
- }
-
- p++;
- }
-
- return;
- }
-