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.

531 lines
13 KiB

  1. //
  2. // Client.cpp - client implementation
  3. //
  4. #include "precomp.h"
  5. #include "bidispl.h"
  6. #define IS_ARG(c) (((c) == '-') || ((c) == '/'))
  7. #ifndef MODULE
  8. #define MODULE "BIDITEST:"
  9. #endif
  10. #ifdef DEBUG
  11. MODULE_DEBUG_INIT( DBG_ERROR | DBG_WARNING | DBG_TRACE | DBG_INFO , DBG_ERROR );
  12. #else
  13. MODULE_DEBUG_INIT( DBG_ERROR | DBG_WARNING, DBG_ERROR );
  14. #endif
  15. VOID DbgBreakPoint (VOID)
  16. {
  17. }
  18. DWORD gdwCount;
  19. IBidiRequest ** gpIRequestArray;
  20. DWORD gdwContainerCount;
  21. IBidiRequestContainer ** gpIRequestContainerArray;
  22. DWORD gdwSplCount;
  23. IBidiSpl ** gpIBidiSplArray;
  24. DWORD gdwMainLoopCount;
  25. DWORD gdwLoopCount;
  26. LPWSTR gpPrinterName;
  27. LONG gdwRef;
  28. HANDLE ghEvent;
  29. VOID
  30. CreateRequest (DWORD dwCount)
  31. {
  32. HRESULT hr;
  33. IBidiRequest * pIReq = NULL ;
  34. typedef IBidiRequest * PIBidiRequest;
  35. gdwCount = dwCount;
  36. gpIRequestArray = new PIBidiRequest[gdwCount];
  37. for (DWORD i = 0; i < gdwCount; i++) {
  38. hr = ::CoCreateInstance(CLSID_BidiRequest,
  39. NULL,
  40. CLSCTX_INPROC_SERVER,
  41. IID_IBidiRequest,
  42. (void**)&pIReq) ;
  43. gpIRequestArray [i] = pIReq;
  44. }
  45. }
  46. VOID
  47. ReleaseRequest ()
  48. {
  49. for (DWORD i = 0; i < gdwCount; i++) {
  50. DWORD dwRef = gpIRequestArray [i]->Release ();
  51. SPLASSERT (dwRef == 0);
  52. if (dwRef != 0) {
  53. printf("ReleaseRequest: Ref count not zero!\n");
  54. }
  55. }
  56. }
  57. VOID
  58. CreateContainer (DWORD dwContainerCount)
  59. {
  60. HRESULT hr;
  61. ULONG dwRef;
  62. IBidiRequestContainer * pIReqContainer = NULL ;
  63. typedef IBidiRequestContainer * PIBidiRequestContainer;
  64. gdwContainerCount = dwContainerCount;
  65. gpIRequestContainerArray = new PIBidiRequestContainer[dwContainerCount];
  66. for (DWORD i = 0; i < dwContainerCount; i++) {
  67. hr = ::CoCreateInstance(CLSID_BidiRequestContainer,
  68. NULL,
  69. CLSCTX_INPROC_SERVER,
  70. IID_IBidiRequestContainer,
  71. (void**)&pIReqContainer) ;
  72. gpIRequestContainerArray [i] = pIReqContainer;
  73. }
  74. }
  75. VOID
  76. ReleaseContainer ()
  77. {
  78. for (DWORD i = 0; i < gdwContainerCount; i++) {
  79. DWORD dwRef = gpIRequestContainerArray [i]->Release ();
  80. SPLASSERT (dwRef == 0);
  81. if (dwRef != 0) {
  82. printf("ReleaseRequest: Ref count not zero!\n");
  83. }
  84. }
  85. }
  86. VOID
  87. CreateSpl (DWORD dwCount)
  88. {
  89. HRESULT hr;
  90. ULONG dwRef;
  91. IBidiSpl * pIBidiSpl = NULL ;
  92. typedef IBidiSpl * PIBidiSpl;
  93. gdwSplCount = dwCount;
  94. gpIBidiSplArray = new PIBidiSpl[dwCount];
  95. for (DWORD i = 0; i < dwCount; i++) {
  96. hr = ::CoCreateInstance(CLSID_BidiSpl,
  97. NULL,
  98. CLSCTX_INPROC_SERVER,
  99. IID_IBidiSpl,
  100. (void**)&pIBidiSpl) ;
  101. gpIBidiSplArray[i] = pIBidiSpl;
  102. }
  103. }
  104. VOID
  105. ReleaseSpl ()
  106. {
  107. for (DWORD i = 0; i < gdwSplCount; i++) {
  108. DWORD dwRef = gpIBidiSplArray [i]->Release ();
  109. SPLASSERT (dwRef == 0);
  110. if (dwRef != 0) {
  111. printf("ReleaseRequest: Ref count not zero!\n");
  112. }
  113. }
  114. }
  115. VOID
  116. FillInRequest (DWORD dwIndex)
  117. {
  118. IBidiRequest * pIReq = gpIRequestArray[dwIndex];
  119. DWORD dwValidSize;
  120. HRESULT hr;
  121. BIDI_TYPE dwBidiType;
  122. #define BIDI_SCHEMA_DUPLEX L"/Printer/Installableoption/Duplexunit"
  123. #define BIDI_SCHEMA_MULTICHANNEL L"/Capability/MultiChannel"
  124. #define BIDI_SCHEMA_VERSION L"/Communication/Version"
  125. #define BIDI_SCHEMA_BIDIPROTOCOL L"/Communication/BidiProtocol"
  126. #define BIDI_SCHEMA_INK_LEVEL L"/Printer/BlackInk1/Level"
  127. #define BIDI_SCHEMA_ALERTS L"/Printer/Alerts"
  128. static PWSTR pSchemaArray[] = {
  129. BIDI_SCHEMA_DUPLEX,
  130. BIDI_SCHEMA_MULTICHANNEL,
  131. BIDI_SCHEMA_VERSION,
  132. BIDI_SCHEMA_BIDIPROTOCOL,
  133. BIDI_SCHEMA_INK_LEVEL,
  134. BIDI_SCHEMA_ALERTS
  135. };
  136. if (pIReq) {
  137. hr = pIReq->SetSchema (pSchemaArray[rand() %( sizeof (pSchemaArray) / sizeof (pSchemaArray[0]))]);
  138. dwBidiType = (BIDI_TYPE) (rand() % 7);
  139. switch (dwBidiType) {
  140. case BIDI_NULL:
  141. dwValidSize = 0;
  142. break;
  143. case BIDI_INT:
  144. dwValidSize = sizeof (INT);
  145. break;
  146. case BIDI_FLOAT:
  147. dwValidSize = sizeof (FLOAT);
  148. break;
  149. case BIDI_BOOL:
  150. dwValidSize = sizeof (BOOL);
  151. break;
  152. default:
  153. dwValidSize = rand () %(1024*1024);
  154. break;
  155. }
  156. PBYTE pByte = new BYTE [dwValidSize];
  157. hr = pIReq->SetInputData (dwBidiType, pByte, dwValidSize);
  158. }
  159. }
  160. VOID
  161. FillInContainer (DWORD dwIndex)
  162. {
  163. IBidiRequestContainer * pIReqContainer = gpIRequestContainerArray[dwIndex];
  164. if (pIReqContainer) {
  165. DWORD dwRequestCount = rand () % gdwCount;
  166. for (DWORD i = 0; i < dwRequestCount; i++) {
  167. HRESULT hr = pIReqContainer->AddRequest (gpIRequestArray[i]);
  168. }
  169. }
  170. }
  171. VOID
  172. AccessRequest (IBidiRequest *pIRequest)
  173. {
  174. DWORD dwTotoal, i;
  175. HRESULT hr, hResult;
  176. LPWSTR pszSchema = NULL;
  177. BYTE *pData = NULL;
  178. DWORD uSize;
  179. DWORD dwType;
  180. DWORD dwTotal;
  181. if (pIRequest) {
  182. hr = pIRequest->GetResult (&hResult);
  183. hr = pIRequest->GetEnumCount (&dwTotal);
  184. for (i = 0; i < dwTotal; i++) {
  185. hr = pIRequest->GetOutputData (i, &pszSchema, &dwType, &pData, &uSize);
  186. if (pszSchema) {
  187. CoTaskMemFree (pszSchema);
  188. }
  189. if (pData) {
  190. CoTaskMemFree (pData);
  191. }
  192. }
  193. }
  194. }
  195. VOID
  196. AccessRequest (DWORD dwIndex)
  197. {
  198. IBidiRequest *pIRequest = gpIRequestArray[dwIndex];
  199. AccessRequest (pIRequest);
  200. }
  201. VOID
  202. AccessContainer (IBidiRequestContainer * pIReqContainer)
  203. {
  204. DWORD dwTotal;
  205. DBGMSG (DBG_TRACE, ("Enter AccessContainer\n"));
  206. if (pIReqContainer) {
  207. HRESULT hr = pIReqContainer->GetRequestCount (&dwTotal);
  208. if (dwTotal > 0) {
  209. IEnumUnknown *pEnumIunk;
  210. IEnumUnknown *pEnumIunk2;
  211. hr = pIReqContainer->GetEnumObject (&pEnumIunk) ;
  212. if (pEnumIunk) {
  213. IUnknown ** pIunkArray;
  214. if ((rand()%2) == 1) {
  215. hr = pEnumIunk->Reset ();
  216. }
  217. else {
  218. hr = pEnumIunk->Clone (&pEnumIunk2);
  219. pEnumIunk->Release ();
  220. pEnumIunk = pEnumIunk2;
  221. }
  222. DWORD dwEnumIndex = 0;
  223. pIunkArray = new IUnknown* [dwTotal];
  224. while (dwEnumIndex < dwTotal + 2) {
  225. IUnknown *pIunk;
  226. DWORD dwFetched;
  227. DWORD dwEnumCount = rand() % (dwTotal + 1);
  228. hr = pEnumIunk->Next (dwEnumCount, pIunkArray, &dwFetched);
  229. if (SUCCEEDED (hr)) {
  230. for (DWORD i = 0; i < dwFetched; i++) {
  231. IBidiRequest *pIRequest = NULL;
  232. hr = pIunkArray[i]->QueryInterface (IID_IBidiRequest, (void **) & pIRequest);
  233. pIunkArray[i]->Release ();
  234. AccessRequest (pIRequest);
  235. pIRequest->Release ();
  236. }
  237. dwEnumIndex +=dwEnumCount;
  238. }
  239. else
  240. break;
  241. }
  242. pEnumIunk->Release ();
  243. delete [] pIunkArray;
  244. }
  245. }
  246. }
  247. DBGMSG (DBG_TRACE, ("Leave AccessContainer\n"));
  248. }
  249. VOID
  250. AccessContainer (DWORD dwIndex)
  251. {
  252. IBidiRequestContainer * pIReqContainer = gpIRequestContainerArray[dwIndex];
  253. AccessContainer (pIReqContainer);
  254. }
  255. VOID
  256. AccessBidiSpl (IBidiSpl * pIBidiSpl, PWSTR pPrinterName, DWORD dwCount)
  257. {
  258. HRESULT hr;
  259. ULONG dwRef;
  260. IBidiRequestContainer *pIReqContainer;
  261. // Test Open/Close
  262. //hr = pIBidiSpl->BindDevice (L"No such Printer", 0);
  263. hr = pIBidiSpl->BindDevice (pPrinterName, BIDI_ACCESS_USER);
  264. for (DWORD i = 0; i < dwCount; i++) {
  265. DWORD dwContainerIndex = rand() % gdwContainerCount;
  266. DBGMSG (DBG_INFO, ("Before MultiSendRecv ... "));
  267. hr = pIBidiSpl->MultiSendRecv (BIDI_ACTION_GET, gpIRequestContainerArray[dwContainerIndex]);
  268. DBGMSG (DBG_INFO, ("MultiSendRecv hr=0x%x\n", hr))
  269. AccessContainer (dwContainerIndex);
  270. }
  271. hr = pIBidiSpl->UnbindDevice ();
  272. }
  273. VOID StartTestThread (void)
  274. {
  275. DWORD i;
  276. printf("New Thrread ..\n");
  277. for (DWORD i = 0; i < gdwMainLoopCount; i++) {
  278. DWORD dwSplIndex = rand () % (gdwSplCount - 1);
  279. AccessBidiSpl (gpIBidiSplArray[dwSplIndex], gpPrinterName, rand()%gdwLoopCount);
  280. }
  281. DWORD dwRef = InterlockedDecrement (&gdwRef);
  282. if (dwRef == 0) {
  283. SetEvent (ghEvent);
  284. }
  285. printf("Quit Thrread (dwRef = %d)\n", dwRef);
  286. }
  287. VOID
  288. StartTest (DWORD dwThreadCount)
  289. {
  290. for (DWORD i = 0; i < dwThreadCount; i++) {
  291. CreateThread (NULL, 16*4096, (LPTHREAD_START_ROUTINE ) StartTestThread, NULL,NULL, NULL);
  292. }
  293. }
  294. void usage()
  295. {
  296. printf("\n"
  297. "usage: biditest [-p pname] [-t n] [-m n] [-l n] [-r n] [-c n] [-s n]\n"
  298. "\n"
  299. "where: -p printer name\n"
  300. " -t Thread Number (default = 1) \n"
  301. " -m Main loop count (default = 10)\n"
  302. " -l Loop count (default = 10)\n"
  303. " -r Request count (default = 100)\n"
  304. " -c Container count (default = 100)\n"
  305. " -s Spooler Bidi count (default = 10)\n"
  306. );
  307. exit (0);
  308. }
  309. extern "C"
  310. INT
  311. _cdecl
  312. _tmain(
  313. int argc,
  314. TCHAR **argv)
  315. {
  316. DWORD dwReqCount = 100;
  317. DWORD dwContainerCount = 100;
  318. DWORD dwSplCount = 20;
  319. DWORD dwMainLoopCount = 10;
  320. DWORD dwLoopCount = 10;
  321. WCHAR szName [] = L"Test";
  322. PWSTR pPrinterName = szName;
  323. DWORD dwThreadCount = 1;
  324. HRESULT hr;
  325. for (--argc, ++argv; argc; --argc, ++argv) {
  326. if (IS_ARG(**argv)) {
  327. switch (tolower(*++*argv)) {
  328. case L'?':
  329. usage();
  330. break;
  331. case L'p':
  332. ++argv;
  333. --argc;
  334. pPrinterName = *argv;
  335. break;
  336. case L't':
  337. ++argv;
  338. --argc;
  339. dwThreadCount = _ttol (*argv);
  340. break;
  341. case L'm':
  342. ++argv;
  343. --argc;
  344. dwMainLoopCount = _ttol (*argv);
  345. break;
  346. case L'l':
  347. ++argv;
  348. --argc;
  349. dwLoopCount = _ttol (*argv);
  350. break;
  351. case L'r':
  352. ++argv;
  353. --argc;
  354. dwReqCount = _ttol (*argv);
  355. break;
  356. case L'c':
  357. ++argv;
  358. --argc;
  359. dwContainerCount = _ttol (*argv);
  360. break;
  361. case L's':
  362. ++argv;
  363. --argc;
  364. dwSplCount = _ttol (*argv);
  365. break;
  366. default:
  367. usage();
  368. break;
  369. }
  370. }
  371. else {
  372. printf("Invalid Argument %s\n", *argv);
  373. exit (1);
  374. }
  375. }
  376. printf("Start Test ..\n");
  377. hr = CoInitializeEx (NULL, COINIT_MULTITHREADED) ;
  378. gdwMainLoopCount = dwMainLoopCount;
  379. gdwLoopCount = dwLoopCount;
  380. gpPrinterName = pPrinterName;
  381. ghEvent = CreateEvent (NULL, NULL, FALSE, NULL);
  382. for (;;) {
  383. printf("Create Components ..\n");
  384. CreateRequest (dwReqCount);
  385. CreateContainer ( dwContainerCount);
  386. CreateSpl (dwSplCount);
  387. printf("Fill in data ..\n");
  388. for (DWORD i = 0; i < dwReqCount; i++) {
  389. FillInRequest (i);
  390. }
  391. for (i = 0; i < dwContainerCount; i++) {
  392. FillInContainer (i);
  393. }
  394. printf("Test ..\n");
  395. gdwRef = dwThreadCount;
  396. StartTest (dwThreadCount);
  397. WaitForSingleObject (ghEvent, INFINITE);
  398. ResetEvent (ghEvent);
  399. printf("Cleanup ..\n");
  400. ReleaseSpl();
  401. ReleaseContainer();
  402. ReleaseRequest();
  403. }
  404. CoUninitialize() ;
  405. printf("Test successfully finished!\n");
  406. }