/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
- /* ACL:license */
- /*
- This software and ancillary information (herein called "SOFTWARE")
- called Supermon is made available under the terms described
- here. The SOFTWARE has been approved for release with associated
- LA-CC Number LA-CC 99-51.
- Unless otherwise indicated, this SOFTWARE has been authored by an
- employee or employees of the University of California, operator of the
- Los Alamos National Laboratory under Contract No. W-7405-ENG-36 with
- the U.S. Department of Energy. The U.S. Government has rights to use,
- reproduce, and distribute this SOFTWARE, and to allow others to do so.
- The public may copy, distribute, prepare derivative works and publicly
- display this SOFTWARE without charge, provided that this Notice and
- any statement of authorship are reproduced on all copies. Neither the
- Government nor the University makes any warranty, express or implied,
- or assumes any liability or responsibility for the use of this
- SOFTWARE.
- If SOFTWARE is modified to produce derivative works, such modified
- SOFTWARE should be clearly marked, so as not to confuse it with the
- version available from LANL.
- */
- /* ACL:license */
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/errno.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include "sexp.h"
- #define MAX_SEXPR_SIZE 1024
- #define MAX_CHUNK_SIZE 30
- #include "monhole.h"
- int
- main(int argc, char *argv[]) {
- pid_t my_pid;
- int l, len, the_len, chunk;
- char my_buff[MAX_SEXPR_SIZE];
- char the_buff[8*MAX_SEXPR_SIZE];
- monhole_t mh;
- int chunksize = argc > 1 ? atoi(argv[1]) : MAX_CHUNK_SIZE;
- int do_sleep = argc > 2 ? atoi(argv[2]) : 1;
- unsigned long int i;
- unsigned long int iters = argc > 3 ? atol(argv[3]) : ULONG_MAX;
- char *port_name = argc > 4 ? argv[4] : NULL;
- mh.monhole_port_name = port_name;
- #ifndef _USE_EVENTS_
- 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))))))",
- MONHOLE_POUND, MONHOLE_EVENT);
- #else
- sprintf(my_buff, "(%s ((id (nr 1) (name pid)) (iter (nr 1) (no)) (other (nr 4) (blah bleck poop))))",
- MONHOLE_POUND);
- #endif
- if (monhole_open(&mh, my_buff) != 0) {
- fprintf(stderr, "monhole_open() failed!\n");
- fflush(stderr);
- exit(-1);
- } else {
- printf("monhole_open() success!\n"); fflush(stdout);
- }
- #ifdef _DO_ACK_
- monhole_read(&mh, my_buff, 7);
- #endif
- my_pid = getpid();
- srand(0);
- i = 0;
- the_buff[0] = '\0';
- while (i < iters) {
- #ifndef _USE_EVENTS_
- if (random() % 2) {
- #endif
- 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\"))))",
- MONHOLE_UPDATE,
- argv[0], my_pid, i,
- argv, my_pid, i, i,
- argv, (double) my_pid, (double) i, (double) i,
- argv, (double) my_pid, (double) i, (double) i);
- printf("sending update: [");
- #ifndef _USE_EVENTS_
- } else {
- if (random() % 2) {
- len = sprintf(my_buff,
- "(%s ((blank2 (e1 %p %ld) (e2 %d %ld))))",
- MONHOLE_EVENT, argv, i, my_pid, i);
- } else {
- len = sprintf(my_buff,
- "(%s ((blank (e0 %p))))",
- MONHOLE_EVENT, argv);
- }
- printf("sending event: [");
- }
- #endif
- if (chunksize > 0) {
- for (l = 0; l < len; ) {
- chunk = (random()%chunksize)+1;
- monhole_write(&mh, my_buff+l, (l+chunk<len) ? chunk : len-l);
- l += chunk;
- printf("."); fflush(stdout);
- if (do_sleep > 0) {
- sleep(1);
- }
- }
- } else {
- /*** do not chunk, instead concat and maybe break things up ***/
- if (strlen(the_buff)+strlen(my_buff) > 8*MAX_SEXPR_SIZE) {
- /*** just send what's there and then copy my_buff ***/
- monhole_write(&mh, the_buff, strlen(the_buff));
- strcpy(the_buff, my_buff);
- printf(".");
- } else {
- strcat(the_buff, my_buff);
- if ((random() % 5) == 0) {
- the_len = strlen(the_buff);
- if (the_len > 2) {
- /*** just send half ***/
- monhole_write(&mh, the_buff, the_len/2);
- strcpy(the_buff, the_buff+the_len/2);
- } else {
- monhole_write(&mh, the_buff, the_len);
- the_buff[0] = '\0';
- }
- printf(".");
- } else {
- printf("delayed");
- }
- }
- if (do_sleep > 0) {
- sleep(1);
- }
- }
- printf("]\n");
- if (strlen(my_buff) != len) {
- fprintf(stderr, "ack!!! strlen(my_buff)=%d but len = %d\n",
- strlen(my_buff), len);
- fprintf(stderr, "i need help!!!!!\n");
- exit(-1);
- }
- /*** printf("sent sexpr: %s, size=%d\n", my_buff, strlen(my_buff)); ***/
- #ifdef _DO_ACK_
- monhole_read(&mh, my_buff, 7);
- my_buff[7] = 0;
- printf("blah sez: %s\n", my_buff); fflush(stdout);
- #endif
- i++;
- }
- return(monhole_close(&mh));
- }