2 * Simulated client test program for CUPS.
4 * Copyright 2017 by Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * missing or damaged, see the license at "http://www.cups.org/".
12 * This file is subject to the Apple OS-Developed Software exception.
16 * Include necessary headers...
20 #include "thread-private.h"
28 typedef struct _client_monitor_s
30 const char *uri
, /* Printer URI */
31 *hostname
, /* Hostname */
33 *resource
; /* Resource path */
34 int port
; /* Port number */
35 http_encryption_t encryption
; /* Use encryption? */
36 ipp_pstate_t printer_state
; /* Current printer state */
37 char printer_state_reasons
[1024];
38 /* Current printer-state-reasons */
39 int job_id
; /* Job ID for submitted job */
40 ipp_jstate_t job_state
; /* Current job state */
41 char job_state_reasons
[1024];
42 /* Current job-state-reasons */
50 static void *monitor_printer(_client_monitor_t
*monitor
);
54 * 'main()' - Main entry.
57 int /* O - Exit status */
58 main(int argc
, /* I - Number of command-line arguments */
59 char *argv
[]) /* I - Command-line arguments */
66 * 'monitor_printer()' - Monitor the job and printer states.
69 static void * /* O - Thread exit code */
71 _client_monitor_t
*monitor
) /* I - Monitoring data */
73 http_t
*http
; /* Connection to printer */
74 ipp_t
*request
, /* IPP request */
75 *response
; /* IPP response */
76 ipp_attribute_t
*attr
; /* Attribute in response */
77 ipp_pstate_t printer_state
; /* Printer state */
78 char printer_state_reasons
[1024];
79 /* Printer state reasons */
80 int job_id
; /* Job ID */
81 ipp_jstate_t job_state
; /* Job state */
82 char job_state_reasons
[1024];/* Printer state reasons */
83 static const char * const jattrs
[] = /* Job attributes we want */
88 static const char * const pattrs
[] = /* Printer attributes we want */
91 "printer-state-reasons"
96 * Open a connection to the printer...
99 http
= httpConnect2(monitor
->hostname
, monitor
->port
, NULL
, AF_UNSPEC
,
100 monitor
->encryption
, 1, 0, NULL
);
101 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
104 * Loop until the job is canceled, aborted, or completed.
107 printer_state
= (ipp_pstate_t
)0;
108 printer_state_reasons
[0] = '\0';
110 job_state
= (ipp_jstate_t
)0;
111 job_state_reasons
[0] = '\0';
113 while (monitor
->job_state
< IPP_JOB_CANCELED
)
116 * Reconnect to the printer as needed...
119 if (httpGetFd(http
) < 0)
122 if (httpGetFd(http
) >= 0)
125 * Connected, so check on the printer state...
128 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
129 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, monitor
->uri
);
130 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
131 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
133 response
= cupsDoRequest(http
, request
, monitor
->resource
);
135 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
136 printer_state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
138 if ((attr
= ippFindAttribute(response
, "printer-state-reasons", IPP_TAG_KEYWORD
)) != NULL
)
139 ippAttributeString(attr
, printer_state_reasons
, sizeof(printer_state_reasons
));
141 if (printer_state
!= monitor
->printer_state
|| strcmp(printer_state_reasons
, monitor
->printer_state_reasons
))
143 printf("PRINTER: %s (%s)\n", ippEnumString("printer-state", printer_state
), printer_state_reasons
);
145 monitor
->printer_state
= printer_state
;
146 strlcpy(monitor
->printer_state_reasons
, printer_state_reasons
, sizeof(monitor
->printer_state_reasons
));
151 if (monitor
->job_id
> 0)
154 * Check the status of the job itself...
157 request
= ippNewRequest(IPP_OP_GET_JOB_ATTRIBUTES
);
158 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, monitor
->uri
);
159 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", monitor
->job_id
);
160 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
161 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
163 response
= cupsDoRequest(http
, request
, monitor
->resource
);
165 if ((attr
= ippFindAttribute(response
, "job-state", IPP_TAG_ENUM
)) != NULL
)
166 job_state
= (ipp_jstate_t
)ippGetInteger(attr
, 0);
168 if ((attr
= ippFindAttribute(response
, "job-state-reasons", IPP_TAG_KEYWORD
)) != NULL
)
169 ippAttributeString(attr
, job_state_reasons
, sizeof(job_state_reasons
));
171 if (job_state
!= monitor
->job_state
|| strcmp(job_state_reasons
, monitor
->job_state_reasons
))
173 printf("JOB %d: %s (%s)\n", monitor
->job_id
, ippEnumString("job-state", job_state
), job_state_reasons
);
175 monitor
->job_state
= job_state
;
176 strlcpy(monitor
->job_state_reasons
, job_state_reasons
, sizeof(monitor
->job_state_reasons
));
184 * Sleep for 5 seconds...
191 * Cleanup and return...