]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testdirsvc.c
2 * "$Id: testdirsvc.c 5301 2006-03-17 23:44:33Z mike $"
4 * Browsing test program for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Simulate one or more remote printers.
27 * usage() - Show program usage...
31 * Include necessary headers...
34 #include <cups/cups.h>
35 #include <cups/string.h>
48 * 'main()' - Simulate one or more remote printers.
51 int /* O - Exit status */
52 main(int argc
, /* I - Number of command-line arguments */
53 char *argv
[]) /* I - Command-line arguments */
55 int i
, /* Looping var */
56 printer
, /* Current printer */
57 num_printers
, /* Number of printers */
58 pclass
, /* Current printer class */
59 num_pclasses
, /* Number of printer classes */
60 server
, /* Current server */
61 num_servers
, /* Number of servers */
62 count
, /* Number of printers sent this cycle */
63 interval
, /* Browse Interval */
64 lease
, /* Browse lease-duration */
65 continuous
, /* Run continuously? */
66 port
, /* Browse port */
67 sock
, /* Browse socket */
68 val
, /* Socket option value */
69 seconds
, /* Seconds until next cycle */
70 verbose
; /* Verbose output? */
71 const char *options
; /* Options for URIs */
72 time_t curtime
; /* Current UNIX time */
73 struct tm
*curdate
; /* Current date and time */
74 struct sockaddr_in addr
; /* Broadcast address */
75 char packet
[1540]; /* Data packet */
76 static const char * const names
[26] = /* Printer names */
108 * Process command-line arguments...
121 for (i
= 1; i
< argc
; i
++)
123 if (!strcmp(argv
[i
], "-c"))
125 else if (!strcmp(argv
[i
], "-i"))
129 interval
= atoi(argv
[i
]);
135 else if (!strcmp(argv
[i
], "-l"))
139 lease
= atoi(argv
[i
]);
143 else if (!strcmp(argv
[i
], "-o"))
151 else if (!strcmp(argv
[i
], "-C"))
155 num_pclasses
= atoi(argv
[i
]);
159 else if (!strcmp(argv
[i
], "-p"))
163 num_printers
= atoi(argv
[i
]);
167 else if (!strcmp(argv
[i
], "-s"))
171 num_servers
= atoi(argv
[i
]);
175 else if (!strcmp(argv
[i
], "-v"))
177 else if (isdigit(argv
[i
][0] & 255))
179 port
= atoi(argv
[i
]);
185 if ((num_printers
<= 0 && num_pclasses
<= 0) || num_servers
<= 0 ||
186 interval
<= 0 || lease
< 1 || port
<= 0)
190 * Open a broadcast socket...
193 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
195 perror("Unable to open broadcast socket");
200 * Set the "broadcast" flag...
204 if (setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
206 perror("Unable to put socket in broadcast mode");
213 * Broadcast to 127.0.0.1 (localhost)
216 memset(&addr
, 0, sizeof(addr
));
217 addr
.sin_addr
.s_addr
= htonl(0x7f000001);
218 addr
.sin_family
= AF_INET
;
219 addr
.sin_port
= htons(port
);
222 * Send virtual printers continuously until we are stopped.
228 * Start a new cycle of N printers...
231 printf("Sending %d printers from %d servers...\n", num_printers
,
234 count
= num_servers
* (num_printers
+ num_pclasses
) / interval
+ 1;
235 curtime
= time(NULL
);
236 curdate
= localtime(&curtime
);
239 for (i
= 0, printer
= 0; printer
< num_printers
; printer
++)
241 for (server
= 0; server
< num_servers
; server
++, i
++)
248 curtime
= time(NULL
);
249 curdate
= localtime(&curtime
);
252 snprintf(packet
, sizeof(packet
),
253 "%x %x ipp://testserver-%d/printers/%s-%d \"Server Room %d\" "
254 "\"Test Printer %d\" \"Acme Blazer 2000\"%s%s "
255 "lease-duration=%d\n",
256 CUPS_PRINTER_REMOTE
, IPP_PRINTER_IDLE
, server
+ 1,
257 names
[printer
% 26], printer
/ 26 + 1, server
+ 1,
258 printer
+ 1, options
? " ipp-options=" : "",
259 options
? options
: "", lease
);
262 printf("[%02d:%02d:%02d] %s", curdate
->tm_hour
, curdate
->tm_min
,
263 curdate
->tm_sec
, packet
);
265 if (sendto(sock
, packet
, strlen(packet
), 0,
266 (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
267 perror("Unabled to send packet");
272 for (i
= 0, pclass
= 0; pclass
< num_pclasses
; pclass
++)
274 for (server
= 0; server
< num_servers
; server
++, i
++)
281 curtime
= time(NULL
);
282 curdate
= localtime(&curtime
);
285 snprintf(packet
, sizeof(packet
),
286 "%x %x ipp://testserver-%d/classes/class-%s-%d \"Server Room %d\" "
287 "\"Test Class %d\" \"Acme Blazer 2000\"%s%s "
288 "lease-duration=%d\n",
289 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
290 server
+ 1, names
[pclass
% 26], pclass
/ 26 + 1, server
+ 1,
291 pclass
+ 1, options
? " ipp-options=" : "",
292 options
? options
: "", lease
);
295 printf("[%02d:%02d:%02d] %s", curdate
->tm_hour
, curdate
->tm_min
,
296 curdate
->tm_sec
, packet
);
298 if (sendto(sock
, packet
, strlen(packet
), 0,
299 (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
300 perror("Unabled to send packet");
308 * Sleep for any remaining time...
320 * 'usage()' - Show program usage...
326 puts("Usage: testdirsvc [-c] [-i interval] [-l lease-duration] "
327 "[-o ipp-options] [-p printers] "
328 "[-C classes] [-s servers] [-v] port");
334 * End of "$Id: testdirsvc.c 5301 2006-03-17 23:44:33Z mike $".