tbsend.c 5.39 KB
Newer Older
Mike Hibler's avatar
Mike Hibler committed
1 2
/*
 * EMULAB-COPYRIGHT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
 * Copyright (c) 2006, 2007 University of Utah and the Flux Group.
Mike Hibler's avatar
Mike Hibler committed
4 5 6
 * All rights reserved.
 */

7 8 9 10 11 12 13 14 15 16
/*
 * This is a sample event generator to send TBEXAMPLE events to all nodes
 * in an experiment. Modify as needed of course.
 */

#include <stdio.h>
#include <ctype.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
17
#include <sys/time.h>
18 19 20 21
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
22 23
#include <termios.h>
#include <string.h>
24 25 26 27 28
#include "log.h"

#include "event.h"

static char	*progname;
29 30 31
static struct termios origterm, newterm;

static void restoreterm(void);
32 33 34 35 36

void
usage()
{
	fprintf(stderr,
37 38 39
		"Usage: %s [-s server] [-p port] [-k keyfile] "
                "[-f] [-c] [-l] [-w] [-i idleperiod] [-r retries] "
                "<event> [<expt>] [<host>]\n",
40 41 42 43 44 45 46 47 48 49
		progname);
	exit(-1);
}

int
main(int argc, char **argv)
{
	event_handle_t handle;
	event_notification_t notification;
	address_tuple_t	tuple;
50
	char *server = "localhost";
51
	char *port = NULL;
52
	char *keyfile = NULL;
53 54 55
	char buf[BUFSIZ], *bp;
	struct timeval	now;
	int c;
56 57 58 59 60 61
        int loopit    = 0;
        int scheduler = 0;
        int idleping  = 0;
        int failover  = 0;
        int retries   = 0;
        int wait      = 0;
62 63 64

	progname = argv[0];
	
65
	while ((c = getopt(argc, argv, "s:p:k:clwi:fr:")) != -1) {
66 67 68 69 70 71 72
		switch (c) {
		case 's':
			server = optarg;
			break;
		case 'p':
			port = optarg;
			break;
73 74 75
		case 'k':
			keyfile = optarg;
			break;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                case 'c':
                        scheduler = 1;
                        break;
                case 'l':
                        loopit = 1;
                        break;
                case 'w':
                        wait = 1;
                        break;
                case 'i':
                        idleping = atoi(optarg);
                        break;
                case 'f':
                        failover = 1;
                        break;
                case 'r':
                        retries = atoi(optarg);
                        break;
94 95 96 97 98 99 100
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

101
	if (argc < 1)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		usage();
	
	loginit(0, 0);

	/*
	 * Uppercase event tags for now. Should be wired in list instead.
	 */
	bp = argv[0];
	while (*bp) {
		*bp = toupper(*bp);
		bp++;
	}

	/*
	 * Convert server/port to elvin thing.
	 *
	 * XXX This elvin string stuff should be moved down a layer. 
	 */
	if (server) {
		snprintf(buf, sizeof(buf), "elvin://%s%s%s",
			 server,
			 (port ? ":"  : ""),
			 (port ? port : ""));
		server = buf;
	}

	/*
	 * Construct an address tuple for generating the event.
	 */
	tuple = address_tuple_alloc();
	if (tuple == NULL) {
		fatal("could not allocate an address tuple");
	}
	tuple->objtype  = "TBEXAMPLE";
	tuple->eventtype= argv[0];
137 138 139 140 141 142 143 144 145 146 147 148
        if (argc > 1) {
          tuple->expt   = argv[1];
        }
        if (argc > 2) {
          tuple->host   = argv[2];
        } else {
          tuple->host	= ADDRESSTUPLE_ALL;
        }

        if (scheduler) {
          tuple->scheduler = 1;
        }
149 150

	/* Register with the event system: */
151 152 153 154
	handle = event_register_withkeyfile_withretry(server, 
                                                      0, 
                                                      keyfile,
                                                      retries);
155 156 157 158
	if (handle == NULL) {
		fatal("could not register with event system");
	}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        /* setup idle ping if requested */
        if (idleping) {
          event_set_idle_period(handle, idleping);
        }

        /* setup failover if requested */
        if (failover) {
          event_set_failover(handle, 1);
        }

        /* Allocate the event */
        notification = event_notification_alloc(handle, tuple);
        if (notification == NULL) {
          fatal("could not allocate notification");
        }

        /* 
         *  Make the terminal raw if in wait mode so we can capture
         *  individual keystrokes w/o waiting for an EOL.
         */
        if (wait) {
          if (tcgetattr(STDIN_FILENO, &origterm) != 0) {
            errorc("Can't get STDIN terminal settings.");
            exit(1);
          }
          memcpy(&newterm, &origterm, sizeof(struct termios));
          newterm.c_lflag &= ~(ICANON|ECHO);
          if (tcsetattr(STDIN_FILENO, TCSANOW, &newterm) != 0) {
            errorc("Can't set new STDIN terminal settings.");
            exit(1);
          }
        }

        /* Send in a loop if specified, or just once otherwise */
        do {
          gettimeofday(&now, NULL);

          if (event_notify(handle, notification) == 0) {
            error("could not send test event notification at: %lu:%d\n",
                  now.tv_sec, now.tv_usec);
          } else {
            info("Sent at time: %lu:%d\n", now.tv_sec, now.tv_usec);
          }

          if (wait) {
            int inchar = getchar();
            if (tolower(inchar) == 'q') {
              restoreterm();
              exit(0);
            }
          } else {
            sleep(1);
          }

        } while (loopit || wait);

        event_notification_free(handle, notification);
        
        /* Unregister with the event system: */
        if (event_unregister(handle) == 0) {
          fatal("could not unregister with event system");
        }
221 222 223
	
	return 0;
}
224 225 226 227 228 229

void restoreterm() {
  if (tcsetattr(STDIN_FILENO, TCSANOW, &origterm) != 0) {
    errorc("Error restoring STDIN's original terminal settings");
  }
}