main.cc 5.2 KB
Newer Older
1
2
3
4
5
6
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2010 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
25
26
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
133
134
135
136
137
138
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
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
// main.cc

#include <cassert>
#include <cmath>
#include <ctime>

#include <string>
#include <map>
#include <list>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

// For getopt
#include <unistd.h>

#include "event.h"
#include "tbdefs.h"

enum { EVENT_BUFFER_SIZE = 50 };

namespace g
{
  std::string experimentName;
  bool debug = false;
}

void readArgs(int argc, char * argv[]);
void usage(char * name);
// Reads the map file, initializes the pipe and pipeVault data
// structure, and sets up the two subscription strings for events.
void writePidFile(string const & pidFile);
void initEvents(string const & server, string const & port,
                string const & keyFile, string const & subscription);
void subscribe(event_handle_t handle, address_tuple_t eventTuple,
               string const & subscription);
void callback(event_handle_t handle,
              event_notification_t notification, void *data);

int main(int argc, char * argv[])
{
  cerr << "Beginning program" << endl;
  readArgs(argc, argv);
  return 0;
}

void readArgs(int argc, char * argv[])
{
  cerr << "Processing arguments" << endl;
  string server;
  string port;
  string keyFile;
  string subscription;

  // Prevent getopt from printing an error message.
  opterr = 0;

  /* get params from the optstring */
  char const * argstring = "s:p:dE:k:u:";
  int option = getopt(argc, argv, argstring);
  while (option != -1)
  {
    switch (option)
    {
    case 'd':
      g::debug = true;
      break;
    case 's':
      server = optarg;
      break;
    case 'p':
      port = optarg;
      break;
    case 'E':
      g::experimentName = optarg;
      break;
    case 'k':
      keyFile = optarg;
      break;
    case 'u':
      subscription = optarg;
      break;
    case '?':
    default:
      usage(argv[0]);
      break;
    }
    option = getopt(argc, argv, argstring);
  }

  /*Check if all params are specified, otherwise, print usage and exit*/
  if(server == "" || g::experimentName == "")
      usage(argv[0]);


  initEvents(server, port, keyFile, subscription);
}

void usage(char * name)
{
  cerr << "Usage: " << name << " -E proj/exp -s server [-d] [-p port] "
       << "[-i pidFile] [-k keyFile] [-u subscription]" << endl;
  exit(-1);
}

void initEvents(string const & server, string const & port,
                string const & keyFile, string const & subscription)
{
  cerr << "Initializing event system" << endl;
  string serverString = "elvin://" + server;
  event_handle_t handle;
  if (port != "")
  {
    serverString += ":" + port;
  }
  cerr << "Server string: " << serverString << endl;
  if (keyFile != "")
  {
    handle = event_register_withkeyfile(const_cast<char *>(serverString.c_str()), 0,
                                        const_cast<char *>(keyFile.c_str()));
  }
  else
  {
    handle = event_register_withkeyfile(const_cast<char *>(serverString.c_str()), 0, NULL);
  }
  if (handle == NULL)
  {
    cerr << "Could not register with event system" << endl;
    exit(1);
  }

  address_tuple_t eventTuple = address_tuple_alloc();

  subscribe(handle, eventTuple, subscription);

  address_tuple_free(eventTuple);

  event_main(handle);
}

void subscribe(event_handle_t handle, address_tuple_t eventTuple,
               string const & subscription)
{
  cerr << "Link subscription names: " << subscription << endl;
  eventTuple->objname = const_cast<char *>(subscription.c_str());
//  eventTuple->objtype = TBDB_OBJECTTYPE_LINK;
  eventTuple->objtype = ADDRESSTUPLE_ANY;
//  eventTuple->eventtype = TBDB_EVENTTYPE_MODIFY;
  eventTuple->eventtype = ADDRESSTUPLE_ANY;
  eventTuple->expt = const_cast<char *>(g::experimentName.c_str());
  eventTuple->host = ADDRESSTUPLE_ANY;
  eventTuple->site = ADDRESSTUPLE_ANY;
  eventTuple->group = ADDRESSTUPLE_ANY;
  eventTuple->scheduler = 1;
  if (event_subscribe(handle, callback, eventTuple, NULL) == NULL)
  {
    cerr << "Could not subscribe to " << eventTuple->eventtype << " event" << endl;
    exit(1);
  }
}

void callback(event_handle_t handle,
              event_notification_t notification,
              void * data)
{
  char name[EVENT_BUFFER_SIZE];
  char type[EVENT_BUFFER_SIZE];
  char args[EVENT_BUFFER_SIZE];
  time_t basicTime = time(NULL);
  struct tm * structTime = gmtime(&basicTime);
  char timestamp[1024];
  strftime(timestamp, 1024, "%Y-%m-%dT%H:%M:%S", structTime);

  if (event_notification_get_string(handle, notification, "OBJNAME", name, EVENT_BUFFER_SIZE) == 0)
  {
    cerr << timestamp << ": ERROR: Could not get the object name" << endl;
    return;
  }

  if (event_notification_get_string(handle, notification, "EVENTTYPE", type, EVENT_BUFFER_SIZE) == 0)
  {
    cerr << timestamp << ": ERROR: Could not get the event type" << endl;
    return;
  }

  if (event_notification_get_string(handle, notification,
                                    "ARGS", args, EVENT_BUFFER_SIZE) == 0)
  {
    cerr << timestamp << ": ERROR: Could not get event arguments" << endl;
    return;
  }

  istringstream line(args);
  int sequence = 0;
  line >> sequence;

  cerr << timestamp << " name=" << name << " type=" << type
       << " sequence=" << sequence << endl;
}