]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/testdirsvc.c
Merge changes from CUPS 1.6svn-r9968.
[thirdparty/cups.git] / scheduler / testdirsvc.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: testdirsvc.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
71e16022 4 * Browsing test program for CUPS.
ef416fc2 5 *
22c9029b 6 * Copyright 2007-2011 by Apple Inc.
f899b121 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 14 *
15 * Contents:
16 *
17 * main() - Simulate one or more remote printers.
18 * usage() - Show program usage...
19 */
20
21/*
22 * Include necessary headers...
23 */
24
25#include <cups/cups.h>
71e16022 26#include <cups/string-private.h>
ef416fc2 27
28
29/*
30 * Local functions...
31 */
32
85dda01c 33static void usage(void) __attribute__((noreturn));
ef416fc2 34
35
36/*
37 * 'main()' - Simulate one or more remote printers.
38 */
39
40int /* O - Exit status */
41main(int argc, /* I - Number of command-line arguments */
42 char *argv[]) /* I - Command-line arguments */
43{
44 int i, /* Looping var */
45 printer, /* Current printer */
46 num_printers, /* Number of printers */
b423cd4c 47 pclass, /* Current printer class */
48 num_pclasses, /* Number of printer classes */
ef416fc2 49 server, /* Current server */
50 num_servers, /* Number of servers */
51 count, /* Number of printers sent this cycle */
52 interval, /* Browse Interval */
b423cd4c 53 lease, /* Browse lease-duration */
ef416fc2 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 */
66 {
67 "alpha",
68 "bravo",
69 "charlie",
70 "delta",
71 "echo",
72 "foxtrot",
73 "golf",
74 "hotel",
75 "india",
76 "juliet",
77 "kilo",
78 "lima",
79 "mike",
80 "november",
81 "oscar",
82 "papa",
83 "quebec",
84 "romeo",
85 "sierra",
86 "tango",
87 "uniform",
88 "victor",
89 "wiskey",
90 "x-ray",
91 "yankee",
92 "zulu"
93 };
94
95
96 /*
97 * Process command-line arguments...
98 */
99
100 num_printers = 10;
b423cd4c 101 num_pclasses = 5;
ef416fc2 102 num_servers = 1;
103 interval = 30;
b423cd4c 104 lease = 60;
ef416fc2 105 port = 0;
106 verbose = 0;
107 continuous = 0;
108 options = NULL;
109
110 for (i = 1; i < argc; i ++)
111 {
112 if (!strcmp(argv[i], "-c"))
113 continuous = 1;
ef416fc2 114 else if (!strcmp(argv[i], "-i"))
115 {
116 i ++;
117 if (i < argc)
118 interval = atoi(argv[i]);
119 else
120 usage();
e1d6a774 121
122 continuous = 1;
ef416fc2 123 }
b423cd4c 124 else if (!strcmp(argv[i], "-l"))
125 {
126 i ++;
127 if (i < argc)
128 lease = atoi(argv[i]);
129 else
130 usage();
131 }
ef416fc2 132 else if (!strcmp(argv[i], "-o"))
133 {
134 i ++;
135 if (i < argc)
136 options = argv[i];
137 else
138 usage();
139 }
b423cd4c 140 else if (!strcmp(argv[i], "-C"))
141 {
142 i ++;
143 if (i < argc)
144 num_pclasses = atoi(argv[i]);
145 else
146 usage();
147 }
ef416fc2 148 else if (!strcmp(argv[i], "-p"))
149 {
150 i ++;
151 if (i < argc)
152 num_printers = atoi(argv[i]);
153 else
154 usage();
155 }
156 else if (!strcmp(argv[i], "-s"))
157 {
158 i ++;
159 if (i < argc)
160 num_servers = atoi(argv[i]);
161 else
162 usage();
163 }
b423cd4c 164 else if (!strcmp(argv[i], "-v"))
165 verbose = 1;
ef416fc2 166 else if (isdigit(argv[i][0] & 255))
167 {
168 port = atoi(argv[i]);
169 }
170 else
171 usage();
172 }
173
b423cd4c 174 if ((num_printers <= 0 && num_pclasses <= 0) || num_servers <= 0 ||
175 interval <= 0 || lease < 1 || port <= 0)
ef416fc2 176 usage();
177
178 /*
179 * Open a broadcast socket...
180 */
181
182 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
183 {
184 perror("Unable to open broadcast socket");
185 return (1);
186 }
187
188 /*
189 * Set the "broadcast" flag...
190 */
191
192 val = 1;
193 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
194 {
195 perror("Unable to put socket in broadcast mode");
196
197 close(sock);
198 return (1);
199 }
200
201 /*
202 * Broadcast to 127.0.0.1 (localhost)
203 */
204
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);
209
210 /*
211 * Send virtual printers continuously until we are stopped.
212 */
213
214 for (;;)
215 {
216 /*
217 * Start a new cycle of N printers...
218 */
219
220 printf("Sending %d printers from %d servers...\n", num_printers,
221 num_servers);
222
b423cd4c 223 count = num_servers * (num_printers + num_pclasses) / interval + 1;
ef416fc2 224 curtime = time(NULL);
225 curdate = localtime(&curtime);
226 seconds = interval;
227
228 for (i = 0, printer = 0; printer < num_printers; printer ++)
229 {
230 for (server = 0; server < num_servers; server ++, i ++)
231 {
232 if (i == count)
233 {
234 seconds --;
235 i = 0;
236 sleep(1);
237 curtime = time(NULL);
238 curdate = localtime(&curtime);
239 }
240
241 snprintf(packet, sizeof(packet),
242 "%x %x ipp://testserver-%d/printers/%s-%d \"Server Room %d\" "
b423cd4c 243 "\"Test Printer %d\" \"Acme Blazer 2000\"%s%s "
244 "lease-duration=%d\n",
ef416fc2 245 CUPS_PRINTER_REMOTE, IPP_PRINTER_IDLE, server + 1,
246 names[printer % 26], printer / 26 + 1, server + 1,
247 printer + 1, options ? " ipp-options=" : "",
b423cd4c 248 options ? options : "", lease);
249
250 if (verbose)
251 printf("[%02d:%02d:%02d] %s", curdate->tm_hour, curdate->tm_min,
252 curdate->tm_sec, packet);
253
254 if (sendto(sock, packet, strlen(packet), 0,
255 (struct sockaddr *)&addr, sizeof(addr)) < 0)
256 perror("Unabled to send packet");
257 }
258 }
259
260
261 for (i = 0, pclass = 0; pclass < num_pclasses; pclass ++)
262 {
263 for (server = 0; server < num_servers; server ++, i ++)
264 {
265 if (i == count)
266 {
267 seconds --;
268 i = 0;
269 sleep(1);
270 curtime = time(NULL);
271 curdate = localtime(&curtime);
272 }
273
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);
ef416fc2 282
283 if (verbose)
284 printf("[%02d:%02d:%02d] %s", curdate->tm_hour, curdate->tm_min,
285 curdate->tm_sec, packet);
286
287 if (sendto(sock, packet, strlen(packet), 0,
288 (struct sockaddr *)&addr, sizeof(addr)) < 0)
289 perror("Unabled to send packet");
290 }
291 }
292
293 if (!continuous)
294 break;
295
296 /*
297 * Sleep for any remaining time...
298 */
299
22c9029b 300 if (seconds > 0)
ef416fc2 301 sleep(seconds);
302 }
303
304 return (0);
305}
306
307
308/*
309 * 'usage()' - Show program usage...
310 */
311
f899b121 312static void
ef416fc2 313usage(void)
314{
b423cd4c 315 puts("Usage: testdirsvc [-c] [-i interval] [-l lease-duration] "
316 "[-o ipp-options] [-p printers] "
317 "[-C classes] [-s servers] [-v] port");
ef416fc2 318 exit(0);
319}
320
321
322/*
bc44d920 323 * End of "$Id: testdirsvc.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 324 */