CUPS API: subscribe event
Rytis
zibalas at gmail.com
Mon Jun 5 12:44:14 PDT 2006
Thank you for your response i've made a huge progress thanks to you! There were a several errors in your code but i've fixed them (the error s are listed in the back of this post if anyone is interested) I have few questions though. The first is how do i tell CUPS that i have already received job-completed notification? I don't need CUPS to remind me forever that job 54 has completed. That i do now is after i receive notification i cancel subscription and create a new one. I'm not sure that's the proper way any guidance on that? And for the second one i'll create a new post :)
in create_subscription()
change:
attr = ippFindAttribute(response, "subscription-id", IPP_TAG_INTEGER);
to:
attr = ippFindAttribute(response, "notify-subscription-id", IPP_TAG_INTEGER);
in show_events()
change:
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
"subscription-id", subscription_id);
to:
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
"notify-subscription-ids", subscription_id);
change:
if (!strcmp(attr->name, "job-id") && attr->value_tag == IPP_TAG_INTEGER)
to:
if (!strcmp(attr->name, "notify-job-id") && attr->value_tag == IPP_TAG_INTEGER)
> 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