2 * "$Id: testsub.c 6649 2007-07-11 21:46:42Z mike $"
4 * Scheduler notification tester for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Subscribe to the .
18 * print_attributes() - Print the attributes in a request...
19 * sigterm_handler() - Flag when the user hits CTRL-C...
20 * usage() - Show program usage...
24 * Include necessary headers...
27 #include <cups/cups.h>
28 #include <cups/debug.h>
29 #include <cups/string.h>
37 static int terminate
= 0;
44 static void print_attributes(ipp_t
*ipp
, int indent
);
45 static void sigterm_handler(int sig
);
46 static void usage(void);
50 * 'main()' - Subscribe to the .
54 main(int argc
, /* I - Number of command-line arguments */
55 char *argv
[]) /* I - Command-line arguments */
57 int i
; /* Looping var */
58 const char *uri
; /* URI to use */
59 int num_events
; /* Number of events */
60 const char *events
[100]; /* Events */
61 int subscription_id
, /* notify-subscription-id */
62 sequence_number
, /* notify-sequence-number */
63 interval
; /* Interval between polls */
64 http_t
*http
; /* HTTP connection */
65 ipp_t
*request
, /* IPP request */
66 *response
; /* IPP response */
67 ipp_attribute_t
*attr
; /* Current attribute */
68 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
69 struct sigaction action
; /* Actions for POSIX signals */
70 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
74 * Parse command-line...
80 for (i
= 1; i
< argc
; i
++)
81 if (!strcmp(argv
[i
], "-E"))
82 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
83 else if (!strcmp(argv
[i
], "-e"))
86 if (i
>= argc
|| num_events
>= 100)
89 events
[num_events
] = argv
[i
];
92 else if (!strcmp(argv
[i
], "-h"))
98 cupsSetServer(argv
[i
]);
100 else if (uri
|| strncmp(argv
[i
], "ipp://", 6))
115 * Connect to the server...
118 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
119 cupsEncryption())) == NULL
)
121 perror(cupsServer());
126 * Catch CTRL-C and SIGTERM...
129 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
130 sigset(SIGINT
, sigterm_handler
);
131 sigset(SIGTERM
, sigterm_handler
);
132 #elif defined(HAVE_SIGACTION)
133 memset(&action
, 0, sizeof(action
));
135 sigemptyset(&action
.sa_mask
);
136 action
.sa_handler
= sigterm_handler
;
137 sigaction(SIGINT
, &action
, NULL
);
138 sigaction(SIGTERM
, &action
, NULL
);
140 signal(SIGINT
, sigterm_handler
);
141 signal(SIGTERM
, sigterm_handler
);
142 #endif /* HAVE_SIGSET */
145 * Create the subscription...
148 if (strstr(uri
, "/jobs/"))
150 request
= ippNewRequest(IPP_CREATE_JOB_SUBSCRIPTION
);
151 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
155 request
= ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION
);
156 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
160 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
163 ippAddStrings(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
, "notify-events",
164 num_events
, NULL
, events
);
165 ippAddString(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
166 "notify-pull-method", NULL
, "ippget");
168 response
= cupsDoRequest(http
, request
, uri
);
169 if (cupsLastError() >= IPP_BAD_REQUEST
)
171 fprintf(stderr
, "Create-%s-Subscription: %s\n",
172 strstr(uri
, "/jobs") ? "Job" : "Printer", cupsLastErrorString());
178 if ((attr
= ippFindAttribute(response
, "notify-subscription-id",
179 IPP_TAG_INTEGER
)) == NULL
)
181 fputs("ERROR: No notify-subscription-id in response!\n", stderr
);
187 subscription_id
= attr
->values
[0].integer
;
189 printf("Create-%s-Subscription: notify-subscription-id=%d\n",
190 strstr(uri
, "/jobs/") ? "Job" : "Printer", subscription_id
);
195 * Monitor for events...
203 * Get the current events...
206 printf("\nGet-Notifications(%d,%d):", subscription_id
, sequence_number
);
209 request
= ippNewRequest(IPP_GET_NOTIFICATIONS
);
211 if (strstr(uri
, "/jobs/"))
212 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
214 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
217 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
218 "requesting-user-name", NULL
, cupsUser());
220 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
221 "notify-subscription-ids", subscription_id
);
223 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
224 "notify-sequence-numbers", sequence_number
+ 1);
226 response
= cupsDoRequest(http
, request
, uri
);
228 printf(" %s\n", ippErrorString(cupsLastError()));
230 if (cupsLastError() >= IPP_BAD_REQUEST
)
231 fprintf(stderr
, "Get-Notifications: %s\n", cupsLastErrorString());
234 print_attributes(response
, 0);
236 for (attr
= ippFindAttribute(response
, "notify-sequence-number",
239 attr
= ippFindNextAttribute(response
, "notify-sequence-number",
241 if (attr
->values
[0].integer
> sequence_number
)
242 sequence_number
= attr
->values
[0].integer
;
245 if ((attr
= ippFindAttribute(response
, "notify-get-interval",
246 IPP_TAG_INTEGER
)) != NULL
&&
247 attr
->values
[0].integer
> 0)
248 interval
= attr
->values
[0].integer
;
257 * Cancel the subscription...
260 printf("\nCancel-Subscription:");
263 request
= ippNewRequest(IPP_CANCEL_SUBSCRIPTION
);
265 if (strstr(uri
, "/jobs/"))
266 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
268 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
271 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
274 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
275 "notify-subscription-id", subscription_id
);
277 ippDelete(cupsDoRequest(http
, request
, uri
));
279 printf(" %s\n", ippErrorString(cupsLastError()));
281 if (cupsLastError() >= IPP_BAD_REQUEST
)
282 fprintf(stderr
, "Cancel-Subscription: %s\n", cupsLastErrorString());
285 * Close the connection and return...
295 * 'print_attributes()' - Print the attributes in a request...
299 print_attributes(ipp_t
*ipp
, /* I - IPP request */
300 int indent
) /* I - Indentation */
302 int i
; /* Looping var */
303 ipp_tag_t group
; /* Current group */
304 ipp_attribute_t
*attr
; /* Current attribute */
305 ipp_value_t
*val
; /* Current value */
306 static const char * const tags
[] = /* Value/group tag strings */
309 "operation-attributes-tag",
310 "job-attributes-tag",
311 "end-of-attributes-tag",
312 "printer-attributes-tag",
313 "unsupported-attributes-tag",
314 "subscription-attributes-tag",
315 "event-attributes-tag",
373 "textWithoutLanguage",
374 "nameWithoutLanguage",
386 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
388 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
390 group
= IPP_TAG_ZERO
;
395 if (group
!= attr
->group_tag
)
397 group
= attr
->group_tag
;
400 for (i
= 4; i
< indent
; i
++)
403 printf("%s:\n\n", tags
[group
]);
406 for (i
= 0; i
< indent
; i
++)
409 printf("%s (", attr
->name
);
410 if (attr
->num_values
> 1)
412 printf("%s):", tags
[attr
->value_tag
]);
414 switch (attr
->value_tag
)
417 case IPP_TAG_INTEGER
:
418 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
419 printf(" %d", val
->integer
);
423 case IPP_TAG_BOOLEAN
:
424 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
425 printf(" %s", val
->boolean
? "true" : "false");
430 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
431 printf(" %d-%d", val
->range
.lower
, val
->range
.upper
);
437 time_t vtime
; /* Date/Time value */
438 struct tm
*vdate
; /* Date info */
439 char vstring
[256]; /* Formatted time */
441 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
443 vtime
= ippDateToTime(val
->date
);
444 vdate
= localtime(&vtime
);
445 strftime(vstring
, sizeof(vstring
), "%c", vdate
);
446 printf(" (%s)", vstring
);
452 case IPP_TAG_RESOLUTION
:
453 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
454 printf(" %dx%d%s", val
->resolution
.xres
, val
->resolution
.yres
,
455 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpc");
459 case IPP_TAG_STRING
:
460 case IPP_TAG_TEXTLANG
:
461 case IPP_TAG_NAMELANG
:
464 case IPP_TAG_KEYWORD
:
466 case IPP_TAG_URISCHEME
:
467 case IPP_TAG_CHARSET
:
468 case IPP_TAG_LANGUAGE
:
469 case IPP_TAG_MIMETYPE
:
470 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
471 printf(" \"%s\"", val
->string
.text
);
475 case IPP_TAG_BEGIN_COLLECTION
:
478 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
482 print_attributes(val
->collection
, indent
+ 4);
487 printf("UNKNOWN (%d values)\n", attr
->num_values
);
495 * 'sigterm_handler()' - Flag when the user hits CTRL-C...
499 sigterm_handler(int sig
) /* I - Signal number (unused) */
508 * 'usage()' - Show program usage...
514 puts("Usage: testsub [-E] [-e event ... -e eventN] [-h hostname] URI");
521 * End of "$Id: testsub.c 6649 2007-07-11 21:46:42Z mike $".