/tags/Release_1_4_beta/Supermon/src/common/test/test_fakemon.c

# · C · 158 lines · 122 code · 8 blank · 28 comment · 22 complexity · 3869bc8a7028242abee5d9a8b72c373a MD5 · raw file

  1. /* ACL:license */
  2. /*
  3. This software and ancillary information (herein called "SOFTWARE")
  4. called Supermon is made available under the terms described
  5. here. The SOFTWARE has been approved for release with associated
  6. LA-CC Number LA-CC 99-51.
  7. Unless otherwise indicated, this SOFTWARE has been authored by an
  8. employee or employees of the University of California, operator of the
  9. Los Alamos National Laboratory under Contract No. W-7405-ENG-36 with
  10. the U.S. Department of Energy. The U.S. Government has rights to use,
  11. reproduce, and distribute this SOFTWARE, and to allow others to do so.
  12. The public may copy, distribute, prepare derivative works and publicly
  13. display this SOFTWARE without charge, provided that this Notice and
  14. any statement of authorship are reproduced on all copies. Neither the
  15. Government nor the University makes any warranty, express or implied,
  16. or assumes any liability or responsibility for the use of this
  17. SOFTWARE.
  18. If SOFTWARE is modified to produce derivative works, such modified
  19. SOFTWARE should be clearly marked, so as not to confuse it with the
  20. version available from LANL.
  21. */
  22. /* ACL:license */
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <sys/errno.h>
  26. #include <unistd.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <limits.h>
  31. #include "sexp.h"
  32. #define MAX_SEXPR_SIZE 1024
  33. #define MAX_CHUNK_SIZE 30
  34. #include "monhole.h"
  35. int
  36. main(int argc, char *argv[]) {
  37. pid_t my_pid;
  38. int l, len, the_len, chunk;
  39. char my_buff[MAX_SEXPR_SIZE];
  40. char the_buff[8*MAX_SEXPR_SIZE];
  41. monhole_t mh;
  42. int chunksize = argc > 1 ? atoi(argv[1]) : MAX_CHUNK_SIZE;
  43. int do_sleep = argc > 2 ? atoi(argv[2]) : 1;
  44. unsigned long int i;
  45. unsigned long int iters = argc > 3 ? atol(argv[3]) : ULONG_MAX;
  46. char *port_name = argc > 4 ? argv[4] : NULL;
  47. mh.monhole_port_name = port_name;
  48. #ifndef _USE_EVENTS_
  49. sprintf(my_buff, "(%s ((id (nr 1) (name pid)) (iter (nr 1) (no)) (other (nr 4) (blah bleck poop)) (%s (nr 0) ((blank (nr 1) (e0)) (blank2 (nr 2) (e1 e2))))))",
  50. MONHOLE_POUND, MONHOLE_EVENT);
  51. #else
  52. sprintf(my_buff, "(%s ((id (nr 1) (name pid)) (iter (nr 1) (no)) (other (nr 4) (blah bleck poop))))",
  53. MONHOLE_POUND);
  54. #endif
  55. if (monhole_open(&mh, my_buff) != 0) {
  56. fprintf(stderr, "monhole_open() failed!\n");
  57. fflush(stderr);
  58. exit(-1);
  59. } else {
  60. printf("monhole_open() success!\n"); fflush(stdout);
  61. }
  62. #ifdef _DO_ACK_
  63. monhole_read(&mh, my_buff, 7);
  64. #endif
  65. my_pid = getpid();
  66. srand(0);
  67. i = 0;
  68. the_buff[0] = '\0';
  69. while (i < iters) {
  70. #ifndef _USE_EVENTS_
  71. if (random() % 2) {
  72. #endif
  73. len = sprintf(my_buff, "(%s ((id (name %s) (pid %d)) (iter (no %ld)) (other (blah %p %d %ld \"%ld\") (bleck %p %.2f %.2f \"%.2f\") (poop %p %.2e %.2e \"%.2e\"))))",
  74. MONHOLE_UPDATE,
  75. argv[0], my_pid, i,
  76. argv, my_pid, i, i,
  77. argv, (double) my_pid, (double) i, (double) i,
  78. argv, (double) my_pid, (double) i, (double) i);
  79. printf("sending update: [");
  80. #ifndef _USE_EVENTS_
  81. } else {
  82. if (random() % 2) {
  83. len = sprintf(my_buff,
  84. "(%s ((blank2 (e1 %p %ld) (e2 %d %ld))))",
  85. MONHOLE_EVENT, argv, i, my_pid, i);
  86. } else {
  87. len = sprintf(my_buff,
  88. "(%s ((blank (e0 %p))))",
  89. MONHOLE_EVENT, argv);
  90. }
  91. printf("sending event: [");
  92. }
  93. #endif
  94. if (chunksize > 0) {
  95. for (l = 0; l < len; ) {
  96. chunk = (random()%chunksize)+1;
  97. monhole_write(&mh, my_buff+l, (l+chunk<len) ? chunk : len-l);
  98. l += chunk;
  99. printf("."); fflush(stdout);
  100. if (do_sleep > 0) {
  101. sleep(1);
  102. }
  103. }
  104. } else {
  105. /*** do not chunk, instead concat and maybe break things up ***/
  106. if (strlen(the_buff)+strlen(my_buff) > 8*MAX_SEXPR_SIZE) {
  107. /*** just send what's there and then copy my_buff ***/
  108. monhole_write(&mh, the_buff, strlen(the_buff));
  109. strcpy(the_buff, my_buff);
  110. printf(".");
  111. } else {
  112. strcat(the_buff, my_buff);
  113. if ((random() % 5) == 0) {
  114. the_len = strlen(the_buff);
  115. if (the_len > 2) {
  116. /*** just send half ***/
  117. monhole_write(&mh, the_buff, the_len/2);
  118. strcpy(the_buff, the_buff+the_len/2);
  119. } else {
  120. monhole_write(&mh, the_buff, the_len);
  121. the_buff[0] = '\0';
  122. }
  123. printf(".");
  124. } else {
  125. printf("delayed");
  126. }
  127. }
  128. if (do_sleep > 0) {
  129. sleep(1);
  130. }
  131. }
  132. printf("]\n");
  133. if (strlen(my_buff) != len) {
  134. fprintf(stderr, "ack!!! strlen(my_buff)=%d but len = %d\n",
  135. strlen(my_buff), len);
  136. fprintf(stderr, "i need help!!!!!\n");
  137. exit(-1);
  138. }
  139. /*** printf("sent sexpr: %s, size=%d\n", my_buff, strlen(my_buff)); ***/
  140. #ifdef _DO_ACK_
  141. monhole_read(&mh, my_buff, 7);
  142. my_buff[7] = 0;
  143. printf("blah sez: %s\n", my_buff); fflush(stdout);
  144. #endif
  145. i++;
  146. }
  147. return(monhole_close(&mh));
  148. }