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

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Quick hack to dump the current set of subscriptions on an Elvind server
 */

#include <stdio.h>
#include <ctype.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include "log.h"
#include "event.h"

static char	*progname;
static int      counter = 0;
25 26
static char     *ATTRS[] = {"SCHEDULER"};
static int      numattrs = 1;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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

void
usage()
{
	fprintf(stderr,
		"Usage: %s [-s server] [-p port] [-k keyfile]\n", progname);
	exit(-1);
}

void add_cb(elvin_handle_t handle,
            elvin_quench_t quench,
            int is_secure,
            int64_t term_id,
            elvin_ast_t term,
            void *rock,
            elvin_error_t error) {

  char *aststr;

  aststr = elvin_ast_to_string(term, error);

  counter++;
  printf("%s\n", aststr);
  
}

void modify_cb(elvin_handle_t handle,
               elvin_quench_t quench,
               int is_secure,
               int64_t term_id,
               elvin_ast_t term,
               void *rock,
               elvin_error_t error) {

  printf("Got MODIFY quench!\n");
}

void del_cb(elvin_handle_t handle,
            elvin_quench_t quench,
            int64_t term_id,
            void *rock,
            elvin_error_t error) {

  printf("Got DELETE quench!\n");
}

int
main(int argc, char **argv)
{
	event_handle_t handle;
        elvin_error_t elverr;
        elvin_attrlist_t attrlist;
	char *server = NULL;
	char *port = NULL;
	char buf[BUFSIZ];
        int lastcounter = 0;
	int c, i;

	progname = argv[0];
	
	while ((c = getopt(argc, argv, "s:p:")) != -1) {
		switch (c) {
		case 's':
			server = optarg;
			break;
		case 'p':
			port = optarg;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	loginit(0, 0);

	/*
	 * 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;
	}

        /* Register with the event system */
	handle = event_register(server, 0);
	if (handle == NULL) {
		fatal("could not register with event system");
	}

        if ((elverr = elvin_error_alloc()) == NULL) {
          fatal("Cannot allocate elvin error structure!");
        }

        if ((attrlist = elvin_attrlist_alloc(elverr)) == NULL) {
          error("Problem allocating attribute list");
          elvin_error_fprintf(stderr, elverr);
          exit(1);
        }

133 134 135 136 137 138
        for (i = 0; i < numattrs; ++i) {
          if (!elvin_attrlist_add(attrlist, ATTRS[i], elverr)) {
            error("Could not add to attribute list");
            elvin_error_fprintf(stderr, elverr);
            exit(1);
          }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        }

        if (!elvin_sync_add_quench(handle->server, attrlist, NULL, 1,
                                   add_cb, modify_cb, del_cb,
                                   NULL, elverr)) {
          error("Error adding quench to server");
          elvin_error_fprintf(stderr, elverr);
          exit(1);
        }

        /*
	 * Begin the event loop...
         */

        i = 0;
        while(i < 2) {
          int retval = 0;
          retval = event_poll_blocking(handle, 1000);
          if (counter == lastcounter) {
            i++;
          } else {
            i = 0;
          }
          lastcounter = counter;
        }

        return 0;
}