]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testdirsvc.c
2 * "$Id: testdirsvc.c 6649 2007-07-11 21:46:42Z mike $"
4 * Browsing test program for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Simulate one or more remote printers.
18 * usage() - Show program usage...
22 * Include necessary headers...
25 #include <cups/cups.h>
26 #include <cups/string-private.h>
33 static void usage(void) __attribute__((noreturn
));
37 * 'main()' - Simulate one or more remote printers.
40 int /* O - Exit status */
41 main(int argc
, /* I - Number of command-line arguments */
42 char *argv
[]) /* I - Command-line arguments */
44 int i
, /* Looping var */
45 printer
, /* Current printer */
46 num_printers
, /* Number of printers */
47 pclass
, /* Current printer class */
48 num_pclasses
, /* Number of printer classes */
49 server
, /* Current server */
50 num_servers
, /* Number of servers */
51 count
, /* Number of printers sent this cycle */
52 interval
, /* Browse Interval */
53 lease
, /* Browse lease-duration */
54 continuous
, /* Run continuously? */
55 port
, /* Browse port */
56 sock
, /* Browse socket */
57 val
, /* Socket option value */
58 seconds
, /* Seconds until next cycle */
59 verbose
; /* Verbose output? */
60 const char *options
; /* Options for URIs */
61 time_t curtime
; /* Current UNIX time */
62 struct tm
*curdate
; /* Current date and time */
63 struct sockaddr_in addr
; /* Broadcast address */
64 char packet
[1540]; /* Data packet */
65 static const char * const names
[26] = /* Printer names */
97 * Process command-line arguments...
110 for (i
= 1; i
< argc
; i
++)
112 if (!strcmp(argv
[i
], "-c"))
114 else if (!strcmp(argv
[i
], "-i"))
118 interval
= atoi(argv
[i
]);
124 else if (!strcmp(argv
[i
], "-l"))
128 lease
= atoi(argv
[i
]);
132 else if (!strcmp(argv
[i
], "-o"))
140 else if (!strcmp(argv
[i
], "-C"))
144 num_pclasses
= atoi(argv
[i
]);
148 else if (!strcmp(argv
[i
], "-p"))
152 num_printers
= atoi(argv
[i
]);
156 else if (!strcmp(argv
[i
], "-s"))
160 num_servers
= atoi(argv
[i
]);
164 else if (!strcmp(argv
[i
], "-v"))
166 else if (isdigit(argv
[i
][0] & 255))
168 port
= atoi(argv
[i
]);
174 if ((num_printers
<= 0 && num_pclasses
<= 0) || num_servers
<= 0 ||
175 interval
<= 0 || lease
< 1 || port
<= 0)
179 * Open a broadcast socket...
182 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
184 perror("Unable to open broadcast socket");
189 * Set the "broadcast" flag...
193 if (setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
195 perror("Unable to put socket in broadcast mode");
202 * Broadcast to 127.0.0.1 (localhost)
205 memset(&addr
, 0, sizeof(addr
));
206 addr
.sin_addr
.s_addr
= htonl(0x7f000001);
207 addr
.sin_family
= AF_INET
;
208 addr
.sin_port
= htons(port
);
211 * Send virtual printers continuously until we are stopped.
217 * Start a new cycle of N printers...
220 printf("Sending %d printers from %d servers...\n", num_printers
,
223 count
= num_servers
* (num_printers
+ num_pclasses
) / interval
+ 1;
224 curtime
= time(NULL
);
225 curdate
= localtime(&curtime
);
228 for (i
= 0, printer
= 0; printer
< num_printers
; printer
++)
230 for (server
= 0; server
< num_servers
; server
++, i
++)
237 curtime
= time(NULL
);
238 curdate
= localtime(&curtime
);
241 snprintf(packet
, sizeof(packet
),
242 "%x %x ipp://testserver-%d/printers/%s-%d \"Server Room %d\" "
243 "\"Test Printer %d\" \"Acme Blazer 2000\"%s%s "
244 "lease-duration=%d\n",
245 CUPS_PRINTER_REMOTE
, IPP_PRINTER_IDLE
, server
+ 1,
246 names
[printer
% 26], printer
/ 26 + 1, server
+ 1,
247 printer
+ 1, options
? " ipp-options=" : "",
248 options
? options
: "", lease
);
251 printf("[%02d:%02d:%02d] %s", curdate
->tm_hour
, curdate
->tm_min
,
252 curdate
->tm_sec
, packet
);
254 if (sendto(sock
, packet
, strlen(packet
), 0,
255 (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
256 perror("Unabled to send packet");
261 for (i
= 0, pclass
= 0; pclass
< num_pclasses
; pclass
++)
263 for (server
= 0; server
< num_servers
; server
++, i
++)
270 curtime
= time(NULL
);
271 curdate
= localtime(&curtime
);
274 snprintf(packet
, sizeof(packet
),
275 "%x %x ipp://testserver-%d/classes/class-%s-%d \"Server Room %d\" "
276 "\"Test Class %d\" \"Acme Blazer 2000\"%s%s "
277 "lease-duration=%d\n",
278 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
279 server
+ 1, names
[pclass
% 26], pclass
/ 26 + 1, server
+ 1,
280 pclass
+ 1, options
? " ipp-options=" : "",
281 options
? options
: "", lease
);
284 printf("[%02d:%02d:%02d] %s", curdate
->tm_hour
, curdate
->tm_min
,
285 curdate
->tm_sec
, packet
);
287 if (sendto(sock
, packet
, strlen(packet
), 0,
288 (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
289 perror("Unabled to send packet");
297 * Sleep for any remaining time...
309 * 'usage()' - Show program usage...
315 puts("Usage: testdirsvc [-c] [-i interval] [-l lease-duration] "
316 "[-o ipp-options] [-p printers] "
317 "[-C classes] [-s servers] [-v] port");
323 * End of "$Id: testdirsvc.c 6649 2007-07-11 21:46:42Z mike $".