CUPS API: subscribe event
Michael Sweet
mike at easysw.com
Thu Jun 1 06:23:40 PDT 2006
Rytis wrote:
> Uh, it's more complicated than i thought. Anyway, i've rethinked what
> i whant to do so.. what i need is to get notification whenever
> printer completes a job. I would also need id of completed job. Is
> that possible? And how can i achieve that?
OK, to be notified via IPP_GET_NOTIFICATIONS for all printers, use:
/* Create a subscription for job-completed events and return the
subscription-id value or 0 on error */
int
create_subscription(void)
{
http_t *http;
ipp_t *request, *response;
ipp_attribute_t *attr;
int subscription_id;
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
request = ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, "ipp://localhost/printers");
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
ippAddString(request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
"notify-pull-method", NULL, "ippget");
ippAddString(request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
"notify-events", NULL, "job-completed");
response = cupsDoRequest(http, request, "/printers");
attr = ippFindAttribute(response, "subscription-id",
IPP_TAG_INTEGER);
if (attr)
subscription_id = attr->values[0].integer;
else
subscription_id = 0;
ippDelete(response);
httpClose(http);
return (subscription_id);
}
You can then gather the events using the IPP_GET_NOTIFICATIONS
operation:
/* Show job-completed events for the given subscription-id and
return the number of events seen */
int
show_events(int subscription_id)
{
int num_events = 0;
http_t *http;
ipp_t *request, *response;
ipp_attribute_t *attr;
int subscription_id;
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
request = ippNewRequest(IPP_GET_NOTIFICATIONS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, "ipp://localhost/printers");
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
"subscription-id", subscription_id);
response = cupsDoRequest(http, request, "/printers");
if (response)
{
/*
* Loop through the response attributes...
*/
for (attr = response->attrs; attr; attr = attr->next)
{
/*
* Skip everything up to the next event notification...
*/
if (attr->group_tag != IPP_TAG_EVENT_NOTIFICATION)
continue;
/*
* Bump the event counter and then look at the attributes
* for this event...
*/
num_events ++;
while (attr && attr->group_tag == IPP_TAG_EVENT_NOTIFICATION)
{
if (!strcmp(attr->name, "job-id") && attr->value_tag ==
IPP_TAG_INTEGER)
printf("Job %d has completed!\n", attr->values[0].integer);
attr = attr->next;
}
if (!attr)
break;
}
ippDelete(response);
}
httpClose(http);
return (num_events);
}
This code is all untested (just typed it into this email), so there
might be mistakes, but hopefully it will show you the basics...
--
______________________________________________________________________
Michael Sweet, Easy Software Products mike at easysw dot com
Internet Printing and Document Software http://www.easysw.com
More information about the cups-devel
mailing list