Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
9.1 KiB

  1. ///////////////////////////////////////////////////////////////////////////
  2. // File: WinHttpStressScheduler.cpp
  3. //
  4. // Copyright (c) 2001 Microsoft Corporation. All Rights Reserved.
  5. //
  6. // Purpose:
  7. // Global interfaces for the WinHttpStressScheduler project.
  8. //
  9. // History:
  10. // 02/05/01 DennisCh Created
  11. ///////////////////////////////////////////////////////////////////////////
  12. //////////////////////////////////////////////////////////////////////
  13. //
  14. // Includes
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. //
  18. // Win32 headers
  19. //
  20. //
  21. // Project headers
  22. //
  23. #include "WinHttpStressScheduler.h"
  24. #include "ServerCommands.h"
  25. #include "NetworkTools.h"
  26. //////////////////////////////////////////////////////////////////////
  27. //
  28. // Globals and statics
  29. //
  30. //////////////////////////////////////////////////////////////////////
  31. HINSTANCE g_hInstance;
  32. HWND g_hWnd;
  33. ServerCommands g_objServerCommands;
  34. // Forward function definitions
  35. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  36. BOOL SystemTray_UpdateIcon(HWND hwnd, DWORD dwMessage, UINT uID, HICON hIcon, PSTR pszTip);
  37. BOOL Show_IconShortCutMenu();
  38. BOOL OS_IsSupported();
  39. ////////////////////////////////////////////////////////////
  40. // Function: WinMain( HINSTANCE, HINSTANCE, LPWSTR, int )
  41. //
  42. // Purpose:
  43. // This is the entry-point into WinHttpStressScheduler.
  44. //
  45. // Called by:
  46. // [System]
  47. ////////////////////////////////////////////////////////////
  48. int
  49. WINAPI
  50. WinMain
  51. (
  52. HINSTANCE hInstance, // [IN] handle to the process instance
  53. HINSTANCE hPrecInstance, // [IN] handle to the previous instance
  54. LPTSTR lpCmdLine, // [IN] command line
  55. int nShowCmd // [IN] show command
  56. )
  57. {
  58. MSG msg;
  59. WNDCLASSEX wndClass;
  60. wndClass.cbSize = sizeof(WNDCLASSEX);
  61. wndClass.style = CS_HREDRAW | CS_VREDRAW;
  62. wndClass.lpfnWndProc = MainWndProc;
  63. wndClass.cbClsExtra = 0;
  64. wndClass.cbWndExtra = 0;
  65. wndClass.hInstance = hInstance;
  66. wndClass.hIcon = NULL;
  67. wndClass.hCursor = NULL;
  68. wndClass.hbrBackground = NULL;
  69. wndClass.lpszMenuName = NULL;
  70. wndClass.lpszClassName = WINHTTP_STRESS_SCHEDULER__NAME;
  71. wndClass.hIconSm = NULL;
  72. RegisterClassEx(&wndClass);
  73. // cache our hInstance
  74. g_hInstance = hInstance;
  75. // Create window.
  76. g_hWnd = NULL;
  77. g_hWnd = CreateWindow(
  78. WINHTTP_STRESS_SCHEDULER__NAME,
  79. NULL,
  80. WS_OVERLAPPEDWINDOW,
  81. CW_USEDEFAULT,
  82. 0,
  83. CW_USEDEFAULT,
  84. 0,
  85. NULL,
  86. NULL,
  87. hInstance,
  88. NULL);
  89. if (!g_hWnd)
  90. return FALSE;
  91. // Verify that we're running a supported version of Windows
  92. if (!OS_IsSupported())
  93. return FALSE;
  94. // Add icon to the system tray icon
  95. if (!SystemTray_UpdateIcon(g_hWnd, NIM_ADD, 0, LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON)), WINHTTP_STRESS_SCHEDULER__NAME))
  96. return FALSE;
  97. // Create timer to ping the Command Server for commands
  98. SetTimer(g_hWnd, IDT_QUERY_COMMAND_SERVER, g_objServerCommands.Get_CommandServerUpdateInterval(), (TIMERPROC) NULL);
  99. // Message loop
  100. while (GetMessage(&msg, NULL, 0, 0))
  101. {
  102. TranslateMessage(&msg);
  103. DispatchMessage(&msg);
  104. }
  105. // remove the icon from the system tray
  106. if (WM_QUIT == msg.message)
  107. SystemTray_UpdateIcon(g_hWnd, NIM_DELETE, 0, NULL, NULL);
  108. return msg.wParam;
  109. }
  110. ////////////////////////////////////////////////////////////
  111. // Function: MainWndProc( HWND, UINT, WPARAM, LPARAM)
  112. //
  113. // Purpose:
  114. // Window callback procedure for UI.
  115. //
  116. // Called by:
  117. // WinMain
  118. ////////////////////////////////////////////////////////////
  119. LRESULT
  120. CALLBACK
  121. MainWndProc
  122. (
  123. HWND hwnd, // [IN] Handle to current window
  124. UINT iMsg, // [IN] Incoming message
  125. WPARAM wParam, // [IN] Parameter
  126. LPARAM lParam // [IN] Parameter
  127. )
  128. {
  129. switch (iMsg)
  130. {
  131. case MYWM_NOTIFYICON:
  132. // Notifications sent for the System Tray icon
  133. switch (lParam)
  134. {
  135. case WM_LBUTTONDOWN:
  136. case WM_RBUTTONDOWN:
  137. Show_IconShortCutMenu();
  138. return 0;
  139. default:
  140. break;
  141. }
  142. return 0;
  143. case WM_COMMAND:
  144. // User clicked on the popup menu
  145. switch (LOWORD(wParam))
  146. {
  147. case IDM_BEGIN_STRESS:
  148. // begin stress only if it's time to
  149. if (g_objServerCommands.IsTimeToBeginStress())
  150. g_objServerCommands.BeginStress();
  151. else
  152. g_objServerCommands.QueryServerForCommands();
  153. break;
  154. case IDM_END_STRESS:
  155. // end stress only if it's time to.
  156. if (!g_objServerCommands.IsTimeToBeginStress())
  157. g_objServerCommands.EndStress();
  158. break;
  159. case IDM_WINHTTP_HOME:
  160. ShellExecute(g_hWnd, "open", WINHTTP_WINHTTP_HOME_URL, NULL, NULL, SW_SHOW);
  161. break;
  162. case IDM_OPENSTRESSADMIN:
  163. ShellExecute(g_hWnd, "open", WINHTTP_STRESSADMIN_URL, NULL, NULL, SW_SHOW);
  164. break;
  165. case IDM_EXIT:
  166. g_objServerCommands.EndStress();
  167. PostQuitMessage(0);
  168. break;
  169. }
  170. return 0;
  171. case WM_TIMER:
  172. switch (wParam)
  173. {
  174. case IDT_QUERY_COMMAND_SERVER:
  175. // Query the server for commands
  176. g_objServerCommands.QueryServerForCommands();
  177. // Update the timer timeout
  178. KillTimer(g_hWnd, IDT_QUERY_COMMAND_SERVER);
  179. SetTimer(
  180. g_hWnd,
  181. IDT_QUERY_COMMAND_SERVER,
  182. g_objServerCommands.Get_CommandServerUpdateInterval(),
  183. (TIMERPROC) NULL);
  184. // ***************************
  185. // ***************************
  186. // ** Act accordingly based on Command Server messages
  187. // **
  188. // *********************************
  189. // ** EXIT stressScheduler
  190. if (g_objServerCommands.IsTimeToExitStress())
  191. {
  192. g_objServerCommands.EndStress();
  193. // quit stressScehduler
  194. PostQuitMessage(0);
  195. return 0;
  196. }
  197. // *********************************
  198. // ** BEGIN/END stress
  199. // Begin/end stress if it's time
  200. if (g_objServerCommands.IsTimeToBeginStress())
  201. g_objServerCommands.BeginStress();
  202. else
  203. g_objServerCommands.EndStress();
  204. return 0;
  205. break;
  206. }
  207. return 0;
  208. case WM_CREATE:
  209. return 0;
  210. case WM_DESTROY:
  211. return 0;
  212. default:
  213. return DefWindowProc (hwnd, iMsg, wParam, lParam);
  214. }
  215. }
  216. ////////////////////////////////////////////////////////////
  217. // Function: SystemTray_UpdateIcon(HWND hDlg, DWORD dwMessage, UINT uID, WORD wIconResource, PSTR pszTip)
  218. //
  219. // Purpose:
  220. // This add/modifies/removes an icon from the system tray.
  221. //
  222. // Called by:
  223. // WinMain
  224. ////////////////////////////////////////////////////////////
  225. BOOL
  226. SystemTray_UpdateIcon(
  227. HWND hwnd, // [IN] handle to the window object
  228. DWORD dwMessage, // [IN] option to apply to the icon
  229. UINT uID, // [IN] ID of the icon
  230. HICON hIcon, // [IN] handle to an icon if we're loading one
  231. PSTR pszTip // [IN] string containing the tool tip text
  232. )
  233. {
  234. BOOL bSuccess;
  235. NOTIFYICONDATA tnd;
  236. tnd.cbSize = sizeof(NOTIFYICONDATA);
  237. tnd.hWnd = hwnd;
  238. tnd.uID = uID;
  239. tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  240. tnd.uCallbackMessage = MYWM_NOTIFYICON;
  241. tnd.hIcon = hIcon;
  242. if (pszTip)
  243. lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
  244. else
  245. tnd.szTip[0] = '\0';
  246. bSuccess = Shell_NotifyIcon(dwMessage, &tnd);
  247. if (hIcon)
  248. DestroyIcon(hIcon);
  249. return bSuccess;
  250. }
  251. ////////////////////////////////////////////////////////////
  252. // Function: Show_IconShortCutMenu()
  253. //
  254. // Purpose:
  255. // This will show the popup menu at the position of the mouse
  256. // pointer.
  257. //
  258. // Called by:
  259. // MainWndProc
  260. ////////////////////////////////////////////////////////////
  261. BOOL
  262. Show_IconShortCutMenu()
  263. {
  264. POINT ptMouse;
  265. HMENU hPopUpMenu = NULL;
  266. HMENU hMenu = NULL;
  267. MENUINFO menuInfo;
  268. BOOL bResult = FALSE;
  269. // Get the current mouse position
  270. if (0 != GetCursorPos(&ptMouse))
  271. {
  272. // show the popup menu
  273. hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUPMENU));
  274. if (!hMenu)
  275. return FALSE;
  276. hPopUpMenu = GetSubMenu(hMenu, 0);
  277. if (!hPopUpMenu)
  278. return FALSE;
  279. /*
  280. // Make the menu go away after mouseover
  281. ZeroMemory(&menuInfo, sizeof(MENUINFO));
  282. menuInfo.cbSize = sizeof(MENUINFO);
  283. menuInfo.fMask = MIM_APPLYTOSUBMENUS | MIM_STYLE;
  284. menuInfo.dwStyle = MNS_AUTODISMISS;
  285. BOOL temp = SetMenuInfo(hPopUpMenu, &menuInfo);
  286. */
  287. bResult =
  288. TrackPopupMenuEx(
  289. hPopUpMenu,
  290. TPM_RIGHTALIGN | TPM_BOTTOMALIGN,
  291. ptMouse.x,
  292. ptMouse.y,
  293. g_hWnd,
  294. NULL);
  295. }
  296. DestroyMenu(hMenu);
  297. return bResult;
  298. }
  299. ////////////////////////////////////////////////////////////
  300. // Function: OS_IsSupported()
  301. //
  302. // Purpose:
  303. // Returns TRUE if this APP is supported in the OS and FALSE if not.
  304. // As of now, winhttp is only supported on NT platforms. NT4, Win2k, and WinXP.
  305. //
  306. // Called by:
  307. // MainWndProc
  308. ////////////////////////////////////////////////////////////
  309. BOOL
  310. OS_IsSupported()
  311. {
  312. BOOL bSupported = TRUE;
  313. OSVERSIONINFO osVI;
  314. osVI.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  315. if (GetVersionEx(&osVI))
  316. {
  317. if (VER_PLATFORM_WIN32_NT == osVI.dwPlatformId)
  318. bSupported = TRUE;
  319. else
  320. bSupported = FALSE;
  321. }
  322. else
  323. bSupported = FALSE;
  324. return bSupported;
  325. }