layer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "prio.h"
  6. #include "prprf.h"
  7. #include "prlog.h"
  8. #include "prnetdb.h"
  9. #include "prthread.h"
  10. #include "plerror.h"
  11. #include "plgetopt.h"
  12. #include "prwin16.h"
  13. #include <stdlib.h>
  14. #include <string.h>
  15. /*
  16. ** Testing layering of I/O
  17. **
  18. ** The layered server
  19. ** A thread that acts as a server. It creates a TCP listener with a dummy
  20. ** layer pushed on top. Then listens for incoming connections. Each connection
  21. ** request for connection will be layered as well, accept one request, echo
  22. ** it back and close.
  23. **
  24. ** The layered client
  25. ** Pretty much what you'd expect.
  26. */
  27. static PRFileDesc *logFile;
  28. static PRDescIdentity identity;
  29. static PRNetAddr server_address;
  30. static PRIOMethods myMethods;
  31. typedef enum Verbosity {silent, quiet, chatty, noisy} Verbosity;
  32. static PRIntn minor_iterations = 5;
  33. static PRIntn major_iterations = 1;
  34. static Verbosity verbosity = quiet;
  35. #ifdef DEBUG
  36. #define PORT_INC_DO +100
  37. #else
  38. #define PORT_INC_DO
  39. #endif
  40. #ifdef IS_64
  41. #define PORT_INC_3264 +200
  42. #else
  43. #define PORT_INC_3264
  44. #endif
  45. static PRUint16 default_port = 12273 PORT_INC_DO PORT_INC_3264;
  46. static PRFileDesc *PushLayer(PRFileDesc *stack)
  47. {
  48. PRFileDesc *layer = PR_CreateIOLayerStub(identity, &myMethods);
  49. PRStatus rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer);
  50. if (verbosity > quiet) {
  51. PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer, stack);
  52. }
  53. PR_ASSERT(PR_SUCCESS == rv);
  54. return stack;
  55. } /* PushLayer */
  56. static PRFileDesc *PushNewLayers(PRFileDesc *stack)
  57. {
  58. PRDescIdentity tmp_identity;
  59. PRFileDesc *layer;
  60. PRStatus rv;
  61. /* push a dummy layer */
  62. tmp_identity = PR_GetUniqueIdentity("Dummy 1");
  63. layer = PR_CreateIOLayerStub(tmp_identity, PR_GetDefaultIOMethods());
  64. rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer);
  65. if (verbosity > quiet)
  66. PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer,
  67. stack);
  68. PR_ASSERT(PR_SUCCESS == rv);
  69. /* push a data processing layer */
  70. layer = PR_CreateIOLayerStub(identity, &myMethods);
  71. rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer);
  72. if (verbosity > quiet)
  73. PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer,
  74. stack);
  75. PR_ASSERT(PR_SUCCESS == rv);
  76. /* push another dummy layer */
  77. tmp_identity = PR_GetUniqueIdentity("Dummy 2");
  78. layer = PR_CreateIOLayerStub(tmp_identity, PR_GetDefaultIOMethods());
  79. rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer);
  80. if (verbosity > quiet)
  81. PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer,
  82. stack);
  83. PR_ASSERT(PR_SUCCESS == rv);
  84. return stack;
  85. } /* PushLayer */
  86. #if 0
  87. static PRFileDesc *PopLayer(PRFileDesc *stack)
  88. {
  89. PRFileDesc *popped = PR_PopIOLayer(stack, identity);
  90. if (verbosity > quiet) {
  91. PR_fprintf(logFile, "Popped layer(0x%x) from stack(0x%x)\n", popped, stack);
  92. }
  93. popped->dtor(popped);
  94. return stack;
  95. } /* PopLayer */
  96. #endif
  97. static void PR_CALLBACK Client(void *arg)
  98. {
  99. PRStatus rv;
  100. PRUint8 buffer[100];
  101. PRIntn empty_flags = 0;
  102. PRIntn bytes_read, bytes_sent;
  103. PRFileDesc *stack = (PRFileDesc*)arg;
  104. /* Initialize the buffer so that Purify won't complain */
  105. memset(buffer, 0, sizeof(buffer));
  106. rv = PR_Connect(stack, &server_address, PR_INTERVAL_NO_TIMEOUT);
  107. PR_ASSERT(PR_SUCCESS == rv);
  108. while (minor_iterations-- > 0)
  109. {
  110. bytes_sent = PR_Send(
  111. stack, buffer, sizeof(buffer), empty_flags, PR_INTERVAL_NO_TIMEOUT);
  112. PR_ASSERT(sizeof(buffer) == bytes_sent);
  113. if (verbosity > chatty) {
  114. PR_fprintf(logFile, "Client sending %d bytes\n", bytes_sent);
  115. }
  116. bytes_read = PR_Recv(
  117. stack, buffer, bytes_sent, empty_flags, PR_INTERVAL_NO_TIMEOUT);
  118. if (verbosity > chatty) {
  119. PR_fprintf(logFile, "Client receiving %d bytes\n", bytes_read);
  120. }
  121. PR_ASSERT(bytes_read == bytes_sent);
  122. }
  123. if (verbosity > quiet) {
  124. PR_fprintf(logFile, "Client shutting down stack\n");
  125. }
  126. rv = PR_Shutdown(stack, PR_SHUTDOWN_BOTH); PR_ASSERT(PR_SUCCESS == rv);
  127. } /* Client */
  128. static void PR_CALLBACK Server(void *arg)
  129. {
  130. PRStatus rv;
  131. PRUint8 buffer[100];
  132. PRFileDesc *service;
  133. PRUintn empty_flags = 0;
  134. PRIntn bytes_read, bytes_sent;
  135. PRFileDesc *stack = (PRFileDesc*)arg;
  136. PRNetAddr client_address;
  137. service = PR_Accept(stack, &client_address, PR_INTERVAL_NO_TIMEOUT);
  138. if (verbosity > quiet) {
  139. PR_fprintf(logFile, "Server accepting connection\n");
  140. }
  141. do
  142. {
  143. bytes_read = PR_Recv(
  144. service, buffer, sizeof(buffer), empty_flags, PR_INTERVAL_NO_TIMEOUT);
  145. if (0 != bytes_read)
  146. {
  147. if (verbosity > chatty) {
  148. PR_fprintf(logFile, "Server receiving %d bytes\n", bytes_read);
  149. }
  150. PR_ASSERT(bytes_read > 0);
  151. bytes_sent = PR_Send(
  152. service, buffer, bytes_read, empty_flags, PR_INTERVAL_NO_TIMEOUT);
  153. if (verbosity > chatty) {
  154. PR_fprintf(logFile, "Server sending %d bytes\n", bytes_sent);
  155. }
  156. PR_ASSERT(bytes_read == bytes_sent);
  157. }
  158. } while (0 != bytes_read);
  159. if (verbosity > quiet) {
  160. PR_fprintf(logFile, "Server shutting down and closing stack\n");
  161. }
  162. rv = PR_Shutdown(service, PR_SHUTDOWN_BOTH); PR_ASSERT(PR_SUCCESS == rv);
  163. rv = PR_Close(service); PR_ASSERT(PR_SUCCESS == rv);
  164. } /* Server */
  165. static PRInt32 PR_CALLBACK MyRecv(
  166. PRFileDesc *fd, void *buf, PRInt32 amount,
  167. PRIntn flags, PRIntervalTime timeout)
  168. {
  169. char *b = (char*)buf;
  170. PRFileDesc *lo = fd->lower;
  171. PRInt32 rv, readin = 0, request = 0;
  172. rv = lo->methods->recv(lo, &request, sizeof(request), flags, timeout);
  173. if (verbosity > chatty) PR_fprintf(
  174. logFile, "MyRecv sending permission for %d bytes\n", request);
  175. if (0 < rv)
  176. {
  177. if (verbosity > chatty) PR_fprintf(
  178. logFile, "MyRecv received permission request for %d bytes\n", request);
  179. rv = lo->methods->send(
  180. lo, &request, sizeof(request), flags, timeout);
  181. if (0 < rv)
  182. {
  183. if (verbosity > chatty) PR_fprintf(
  184. logFile, "MyRecv sending permission for %d bytes\n", request);
  185. while (readin < request)
  186. {
  187. rv = lo->methods->recv(
  188. lo, b + readin, amount - readin, flags, timeout);
  189. if (rv <= 0) {
  190. break;
  191. }
  192. if (verbosity > chatty) PR_fprintf(
  193. logFile, "MyRecv received %d bytes\n", rv);
  194. readin += rv;
  195. }
  196. rv = readin;
  197. }
  198. }
  199. return rv;
  200. } /* MyRecv */
  201. static PRInt32 PR_CALLBACK MySend(
  202. PRFileDesc *fd, const void *buf, PRInt32 amount,
  203. PRIntn flags, PRIntervalTime timeout)
  204. {
  205. PRFileDesc *lo = fd->lower;
  206. const char *b = (const char*)buf;
  207. PRInt32 rv, wroteout = 0, request;
  208. if (verbosity > chatty) PR_fprintf(
  209. logFile, "MySend asking permission to send %d bytes\n", amount);
  210. rv = lo->methods->send(lo, &amount, sizeof(amount), flags, timeout);
  211. if (0 < rv)
  212. {
  213. rv = lo->methods->recv(
  214. lo, &request, sizeof(request), flags, timeout);
  215. if (0 < rv)
  216. {
  217. PR_ASSERT(request == amount);
  218. if (verbosity > chatty) PR_fprintf(
  219. logFile, "MySend got permission to send %d bytes\n", request);
  220. while (wroteout < request)
  221. {
  222. rv = lo->methods->send(
  223. lo, b + wroteout, request - wroteout, flags, timeout);
  224. if (rv <= 0) {
  225. break;
  226. }
  227. if (verbosity > chatty) PR_fprintf(
  228. logFile, "MySend wrote %d bytes\n", rv);
  229. wroteout += rv;
  230. }
  231. rv = amount;
  232. }
  233. }
  234. return rv;
  235. } /* MySend */
  236. static Verbosity ChangeVerbosity(Verbosity verbosity, PRIntn delta)
  237. {
  238. PRIntn verbage = (PRIntn)verbosity + delta;
  239. if (verbage < (PRIntn)silent) {
  240. verbage = (PRIntn)silent;
  241. }
  242. else if (verbage > (PRIntn)noisy) {
  243. verbage = (PRIntn)noisy;
  244. }
  245. return (Verbosity)verbage;
  246. } /* ChangeVerbosity */
  247. int main(int argc, char **argv)
  248. {
  249. PRStatus rv;
  250. PRIntn mits;
  251. PLOptStatus os;
  252. PRFileDesc *client, *service;
  253. PRFileDesc *client_stack, *service_stack;
  254. PRNetAddr any_address;
  255. const char *server_name = NULL;
  256. const PRIOMethods *stubMethods;
  257. PRThread *client_thread, *server_thread;
  258. PRThreadScope thread_scope = PR_LOCAL_THREAD;
  259. PLOptState *opt = PL_CreateOptState(argc, argv, "dqGC:c:p:");
  260. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  261. {
  262. if (PL_OPT_BAD == os) {
  263. continue;
  264. }
  265. switch (opt->option)
  266. {
  267. case 0:
  268. server_name = opt->value;
  269. break;
  270. case 'd': /* debug mode */
  271. if (verbosity < noisy) {
  272. verbosity = ChangeVerbosity(verbosity, 1);
  273. }
  274. break;
  275. case 'q': /* debug mode */
  276. if (verbosity > silent) {
  277. verbosity = ChangeVerbosity(verbosity, -1);
  278. }
  279. break;
  280. case 'G': /* use global threads */
  281. thread_scope = PR_GLOBAL_THREAD;
  282. break;
  283. case 'C': /* number of threads waiting */
  284. major_iterations = atoi(opt->value);
  285. break;
  286. case 'c': /* number of client threads */
  287. minor_iterations = atoi(opt->value);
  288. break;
  289. case 'p': /* default port */
  290. default_port = atoi(opt->value);
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. PL_DestroyOptState(opt);
  297. PR_STDIO_INIT();
  298. logFile = PR_GetSpecialFD(PR_StandardError);
  299. identity = PR_GetUniqueIdentity("Dummy");
  300. stubMethods = PR_GetDefaultIOMethods();
  301. /*
  302. ** The protocol we're going to implement is one where in order to initiate
  303. ** a send, the sender must first solicit permission. Therefore, every
  304. ** send is really a send - receive - send sequence.
  305. */
  306. myMethods = *stubMethods; /* first get the entire batch */
  307. myMethods.recv = MyRecv; /* then override the ones we care about */
  308. myMethods.send = MySend; /* then override the ones we care about */
  309. if (NULL == server_name)
  310. rv = PR_InitializeNetAddr(
  311. PR_IpAddrLoopback, default_port, &server_address);
  312. else
  313. {
  314. rv = PR_StringToNetAddr(server_name, &server_address);
  315. PR_ASSERT(PR_SUCCESS == rv);
  316. rv = PR_InitializeNetAddr(
  317. PR_IpAddrNull, default_port, &server_address);
  318. }
  319. PR_ASSERT(PR_SUCCESS == rv);
  320. /* one type w/o layering */
  321. mits = minor_iterations;
  322. while (major_iterations-- > 0)
  323. {
  324. if (verbosity > silent) {
  325. PR_fprintf(logFile, "Beginning non-layered test\n");
  326. }
  327. client = PR_NewTCPSocket(); PR_ASSERT(NULL != client);
  328. service = PR_NewTCPSocket(); PR_ASSERT(NULL != service);
  329. rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &any_address);
  330. PR_ASSERT(PR_SUCCESS == rv);
  331. rv = PR_Bind(service, &any_address); PR_ASSERT(PR_SUCCESS == rv);
  332. rv = PR_Listen(service, 10); PR_ASSERT(PR_SUCCESS == rv);
  333. minor_iterations = mits;
  334. server_thread = PR_CreateThread(
  335. PR_USER_THREAD, Server, service,
  336. PR_PRIORITY_HIGH, thread_scope,
  337. PR_JOINABLE_THREAD, 16 * 1024);
  338. PR_ASSERT(NULL != server_thread);
  339. client_thread = PR_CreateThread(
  340. PR_USER_THREAD, Client, client,
  341. PR_PRIORITY_NORMAL, thread_scope,
  342. PR_JOINABLE_THREAD, 16 * 1024);
  343. PR_ASSERT(NULL != client_thread);
  344. rv = PR_JoinThread(client_thread);
  345. PR_ASSERT(PR_SUCCESS == rv);
  346. rv = PR_JoinThread(server_thread);
  347. PR_ASSERT(PR_SUCCESS == rv);
  348. rv = PR_Close(client); PR_ASSERT(PR_SUCCESS == rv);
  349. rv = PR_Close(service); PR_ASSERT(PR_SUCCESS == rv);
  350. if (verbosity > silent) {
  351. PR_fprintf(logFile, "Ending non-layered test\n");
  352. }
  353. /* with layering */
  354. if (verbosity > silent) {
  355. PR_fprintf(logFile, "Beginning layered test\n");
  356. }
  357. client = PR_NewTCPSocket(); PR_ASSERT(NULL != client);
  358. PushLayer(client);
  359. service = PR_NewTCPSocket(); PR_ASSERT(NULL != service);
  360. PushLayer(service);
  361. rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &any_address);
  362. PR_ASSERT(PR_SUCCESS == rv);
  363. rv = PR_Bind(service, &any_address); PR_ASSERT(PR_SUCCESS == rv);
  364. rv = PR_Listen(service, 10); PR_ASSERT(PR_SUCCESS == rv);
  365. minor_iterations = mits;
  366. server_thread = PR_CreateThread(
  367. PR_USER_THREAD, Server, service,
  368. PR_PRIORITY_HIGH, thread_scope,
  369. PR_JOINABLE_THREAD, 16 * 1024);
  370. PR_ASSERT(NULL != server_thread);
  371. client_thread = PR_CreateThread(
  372. PR_USER_THREAD, Client, client,
  373. PR_PRIORITY_NORMAL, thread_scope,
  374. PR_JOINABLE_THREAD, 16 * 1024);
  375. PR_ASSERT(NULL != client_thread);
  376. rv = PR_JoinThread(client_thread);
  377. PR_ASSERT(PR_SUCCESS == rv);
  378. rv = PR_JoinThread(server_thread);
  379. PR_ASSERT(PR_SUCCESS == rv);
  380. rv = PR_Close(client); PR_ASSERT(PR_SUCCESS == rv);
  381. rv = PR_Close(service); PR_ASSERT(PR_SUCCESS == rv);
  382. /* with layering, using new style stack */
  383. if (verbosity > silent)
  384. PR_fprintf(logFile,
  385. "Beginning layered test with new style stack\n");
  386. client = PR_NewTCPSocket(); PR_ASSERT(NULL != client);
  387. client_stack = PR_CreateIOLayer(client);
  388. PushNewLayers(client_stack);
  389. service = PR_NewTCPSocket(); PR_ASSERT(NULL != service);
  390. service_stack = PR_CreateIOLayer(service);
  391. PushNewLayers(service_stack);
  392. rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &any_address);
  393. PR_ASSERT(PR_SUCCESS == rv);
  394. rv = PR_Bind(service, &any_address); PR_ASSERT(PR_SUCCESS == rv);
  395. rv = PR_Listen(service, 10); PR_ASSERT(PR_SUCCESS == rv);
  396. minor_iterations = mits;
  397. server_thread = PR_CreateThread(
  398. PR_USER_THREAD, Server, service_stack,
  399. PR_PRIORITY_HIGH, thread_scope,
  400. PR_JOINABLE_THREAD, 16 * 1024);
  401. PR_ASSERT(NULL != server_thread);
  402. client_thread = PR_CreateThread(
  403. PR_USER_THREAD, Client, client_stack,
  404. PR_PRIORITY_NORMAL, thread_scope,
  405. PR_JOINABLE_THREAD, 16 * 1024);
  406. PR_ASSERT(NULL != client_thread);
  407. rv = PR_JoinThread(client_thread);
  408. PR_ASSERT(PR_SUCCESS == rv);
  409. rv = PR_JoinThread(server_thread);
  410. PR_ASSERT(PR_SUCCESS == rv);
  411. rv = PR_Close(client_stack); PR_ASSERT(PR_SUCCESS == rv);
  412. rv = PR_Close(service_stack); PR_ASSERT(PR_SUCCESS == rv);
  413. if (verbosity > silent) {
  414. PR_fprintf(logFile, "Ending layered test\n");
  415. }
  416. }
  417. return 0;
  418. } /* main */
  419. /* layer.c */