1 | /* |
---|
2 | * Copyright (c) 2006-2007 XenSource, Inc. |
---|
3 | * |
---|
4 | * This library is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU Lesser General Public |
---|
6 | * License as published by the Free Software Foundation; either |
---|
7 | * version 2.1 of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This library is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | * Lesser General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU Lesser General Public |
---|
15 | * License along with this library; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #define _GNU_SOURCE |
---|
20 | #include <assert.h> |
---|
21 | #include <inttypes.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <string.h> |
---|
25 | |
---|
26 | #include <libxml/parser.h> |
---|
27 | #include <curl/curl.h> |
---|
28 | #include <xen/api/xen_event.h> |
---|
29 | |
---|
30 | //#define PRINT_XML |
---|
31 | |
---|
32 | static void usage() |
---|
33 | { |
---|
34 | fprintf(stderr, |
---|
35 | "Usage:\n" |
---|
36 | "\n" |
---|
37 | " test_event_handling <server> <username> <password>\n" |
---|
38 | "\n" |
---|
39 | "where\n" |
---|
40 | " <server> is the server's host and port, e.g. localhost:9363;\n" |
---|
41 | " <username> is the username to use at the server; and\n" |
---|
42 | " <password> is the password.\n"); |
---|
43 | |
---|
44 | exit(EXIT_FAILURE); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | static char *url; |
---|
49 | |
---|
50 | |
---|
51 | typedef struct |
---|
52 | { |
---|
53 | xen_result_func func; |
---|
54 | void *handle; |
---|
55 | } xen_comms; |
---|
56 | |
---|
57 | |
---|
58 | static size_t |
---|
59 | write_func(void *ptr, size_t size, size_t nmemb, xen_comms *comms) |
---|
60 | { |
---|
61 | size_t n = size * nmemb; |
---|
62 | #ifdef PRINT_XML |
---|
63 | printf("\n\n---Result from server -----------------------\n"); |
---|
64 | printf("%s\n",((char*) ptr)); |
---|
65 | fflush(stdout); |
---|
66 | #endif |
---|
67 | return comms->func(ptr, n, comms->handle) ? n : 0; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | static int |
---|
72 | call_func(const void *data, size_t len, void *user_handle, |
---|
73 | void *result_handle, xen_result_func result_func) |
---|
74 | { |
---|
75 | (void)user_handle; |
---|
76 | |
---|
77 | #ifdef PRINT_XML |
---|
78 | printf("\n\n---Data to server: -----------------------\n"); |
---|
79 | printf("%s\n",((char*) data)); |
---|
80 | fflush(stdout); |
---|
81 | #endif |
---|
82 | |
---|
83 | CURL *curl = curl_easy_init(); |
---|
84 | if (!curl) { |
---|
85 | return -1; |
---|
86 | } |
---|
87 | |
---|
88 | xen_comms comms = { |
---|
89 | .func = result_func, |
---|
90 | .handle = result_handle |
---|
91 | }; |
---|
92 | |
---|
93 | curl_easy_setopt(curl, CURLOPT_URL, url); |
---|
94 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); |
---|
95 | curl_easy_setopt(curl, CURLOPT_MUTE, 1); |
---|
96 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_func); |
---|
97 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &comms); |
---|
98 | curl_easy_setopt(curl, CURLOPT_POST, 1); |
---|
99 | curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); |
---|
100 | curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len); |
---|
101 | |
---|
102 | CURLcode result = curl_easy_perform(curl); |
---|
103 | |
---|
104 | curl_easy_cleanup(curl); |
---|
105 | |
---|
106 | return result; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | static void print_error(xen_session *session) |
---|
111 | { |
---|
112 | fprintf(stderr, "Error: %d", session->error_description_count); |
---|
113 | for (int i = 0; i < session->error_description_count; i++) |
---|
114 | { |
---|
115 | fprintf(stderr, "%s ", session->error_description[i]); |
---|
116 | } |
---|
117 | fprintf(stderr, "\n"); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | * Workaround for whinging GCCs, as suggested by strftime(3). |
---|
123 | */ |
---|
124 | static size_t my_strftime(char *s, size_t max, const char *fmt, |
---|
125 | const struct tm *tm) |
---|
126 | { |
---|
127 | return strftime(s, max, fmt, tm); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | int main(int argc, char **argv) |
---|
132 | { |
---|
133 | if (argc != 4) |
---|
134 | { |
---|
135 | usage(); |
---|
136 | } |
---|
137 | |
---|
138 | url = argv[1]; |
---|
139 | char *username = argv[2]; |
---|
140 | char *password = argv[3]; |
---|
141 | |
---|
142 | xmlInitParser(); |
---|
143 | xen_init(); |
---|
144 | curl_global_init(CURL_GLOBAL_ALL); |
---|
145 | |
---|
146 | #define CLEANUP \ |
---|
147 | do { \ |
---|
148 | xen_session_logout(session); \ |
---|
149 | curl_global_cleanup(); \ |
---|
150 | xen_fini(); \ |
---|
151 | xmlCleanupParser(); \ |
---|
152 | } while(0) \ |
---|
153 | |
---|
154 | |
---|
155 | xen_session *session = |
---|
156 | xen_session_login_with_password(call_func, NULL, username, password); |
---|
157 | |
---|
158 | struct xen_string_set *classes = xen_string_set_alloc(0); |
---|
159 | xen_event_register(session, classes); |
---|
160 | xen_string_set_free(classes); |
---|
161 | |
---|
162 | if (!session->ok) |
---|
163 | { |
---|
164 | print_error(session); |
---|
165 | CLEANUP; |
---|
166 | return 1; |
---|
167 | } |
---|
168 | |
---|
169 | while (true) |
---|
170 | { |
---|
171 | struct xen_event_record_set *events; |
---|
172 | if (!xen_event_next(session, &events)) |
---|
173 | { |
---|
174 | print_error(session); |
---|
175 | CLEANUP; |
---|
176 | return 1; |
---|
177 | } |
---|
178 | |
---|
179 | for (size_t i = 0; i < events->size; i++) |
---|
180 | { |
---|
181 | xen_event_record *ev = events->contents[i]; |
---|
182 | char time[256]; |
---|
183 | struct tm *tm = localtime(&ev->timestamp); |
---|
184 | my_strftime(time, 256, "%c, local time", tm); |
---|
185 | printf("Event received: ID = %"PRId64", %s.\n", ev->id, time); |
---|
186 | switch (ev->operation) |
---|
187 | { |
---|
188 | case XEN_EVENT_OPERATION_ADD: |
---|
189 | printf("%s created with UUID %s.\n", ev->class, ev->obj_uuid); |
---|
190 | break; |
---|
191 | |
---|
192 | case XEN_EVENT_OPERATION_DEL: |
---|
193 | printf("%s with UUID %s deleted.\n", ev->class, ev->obj_uuid); |
---|
194 | break; |
---|
195 | |
---|
196 | case XEN_EVENT_OPERATION_MOD: |
---|
197 | printf("%s with UUID %s modified.\n", ev->class, ev->obj_uuid); |
---|
198 | break; |
---|
199 | default: |
---|
200 | assert(false); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | xen_event_record_set_free(events); |
---|
205 | } |
---|
206 | |
---|
207 | CLEANUP; |
---|
208 | |
---|
209 | return 0; |
---|
210 | } |
---|