2017-04-25 136 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可以爲無限期訂購指定。