2017-04-25 129 views
1

我正在尝试侦听打印机状态更改(例如卡纸,暂停...)以下代码给出了“错误的notify-recipient-uri”响应,然后锁定了ippReadFile,并且在打印机暂停/取消暂停。CUPS状态更改订阅

int main() 
{ 
    http_t *http = httpConnectEncrypt(cupsServer(), ippPort(), 
    cupsEncryption()); 

    ipp_t *request = ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION); 

    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", 
     NULL, "ipp://localhost:631/printers/Generic-text-only"); 
    ippAddString(request, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, "notify-recipient-uri", 
     NULL, "cups_test://"); 
    ippAddString(request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, "notify-events", 
     NULL, "printer-state-changed"); 

    ipp_t *response = cupsDoRequest(http, request, "/"); 

    while (1) 
    { 
     ipp_state_t state;  
     ipp_t *event = ippNew(); 

     while ((state = ippReadFile(0, event)) != IPP_DATA) 
     { 
      printf("%s\n","Got Data"); 
     } 

     printf("%s\n","Repeating"); 
     ippDelete(event); 
    } 
} 

在筛选打印机属性后,我发现notify-schemes-supported属性设置为“dbus”。我无法通过IPP_SET_PRINTER_ATTRIBUTES更改属性。任何想法如何让这个工作?

+0

我对杯子并不熟悉,但我注意到你正在传递文件描述符0作为'ippReadFile(int fd,ipp_t * ipp)'的第一个参数。文件描述符0是标准输入。是否有理由期望在程序的标准输入中出现任何内容?否则,正如你所描述的那样,它会挂起并不奇怪。如果你使用'ippRead(http,event)'? – MassPikeMike

+0

'ippRead(http,event)'不会锁定,所以它只是无限循环。我从https://github.com/apple/cups/blob/master/notifier/testnotify.c和https://github.com/apple/cups/blob/master/test/create- printer-subscription.test。我完全有可能错误地做这件事,虽然我不认为是这样。 –

+0

根据我在杯子上的书,Michael Sweet的CUPS:通用UNIX打印系统,“通知程序为CUPS通知用户或程序关于服务器,打印机或作业的状态变化的方法.ippget通知方案在CUPS服务器内部实现,而所有其他CUPS通告程序是在其标准输入文件上接收事件的外部程序。“ –

回答

0

一个不需要任何C代码的非常基本的示例是使用ipptool针对create-printer-subscription宏来订阅事件的rss URI。这是pyipptool所示的方法。

ipptool通常随附CUPS,但对于现代Ubuntu版本,您可能需要安装cups-ipp-utils

首先,创建一个HTTP套接字监听器可以接收的事件......

python -m SimpleHTTPServer 9876 

其次,发送事件到插座听众。

ipptool -d recipient=rss://localhost:9876 ipp://localhost:631/printers /usr/share/cups/ipptool/create-printer-subscription.test 

最后,触发事件,如禁用打印机。

cupsdisable PDFWriter # or some valid printer name 
cupsenable PDFWriter 

rss:// URI方案将使用针对HTTP套接字服务器PUT命令。由于SimpleHTTPServer没有对PUT命令的内置支持,因此会出现501错误。您将不得不自定义HTTP侦听器来处理这些命令,但您会看到事件触发。

注意,默认create-printer-subscription宏配置为发送事件printer-config-changedprinter-state-changednot printer-queue-order-changed可以通过使宏的副本,并对其进行编辑进行调整。

此外,这将使订阅在默认租约期限内保持活动状态(定义为86400 in the source,这应该是一天)。零的额外参数notify-lease-duration可以为无限期订购指定。