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