]> 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 5301 2006-03-17 23:44:33Z mike $"
3 *
4 * Browsing test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Simulate one or more remote printers.
27 * usage() - Show program usage...
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include <cups/cups.h>
35 #include <cups/string.h>
36 #include <stdlib.h>
37 #include <errno.h>
38
39
40 /*
41 * Local functions...
42 */
43
44 void usage(void);
45
46
47 /*
48 * 'main()' - Simulate one or more remote printers.
49 */
50
51 int /* O - Exit status */
52 main(int argc, /* I - Number of command-line arguments */
53 char *argv[]) /* I - Command-line arguments */
54 {
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 */
77 {
78 "alpha",
79 "bravo",
80 "charlie",
81 "delta",
82 "echo",
83 "foxtrot",
84 "golf",
85 "hotel",
86 "india",
87 "juliet",
88 "kilo",
89 "lima",
90 "mike",
91 "november",
92 "oscar",
93 "papa",
94 "quebec",
95 "romeo",
96 "sierra",
97 "tango",
98 "uniform",
99 "victor",
100 "wiskey",
101 "x-ray",
102 "yankee",
103 "zulu"
104 };
105
106
107 /*
108 * Process command-line arguments...
109 */
110
111 num_printers = 10;
112 num_pclasses = 5;
113 num_servers = 1;
114 interval = 30;
115 lease = 60;
116 port = 0;
117 verbose = 0;
118 continuous = 0;
119 options = NULL;
120
121 for (i = 1; i < argc; i ++)
122 {
123 if (!strcmp(argv[i], "-c"))
124 continuous = 1;
125 else if (!strcmp(argv[i], "-i"))
126 {
127 i ++;
128 if (i < argc)
129 interval = atoi(argv[i]);
130 else
131 usage();
132
133 continuous = 1;
134 }
135 else if (!strcmp(argv[i], "-l"))
136 {
137 i ++;
138 if (i < argc)
139 lease = atoi(argv[i]);
140 else
141 usage();
142 }
143 else if (!strcmp(argv[i], "-o"))
144 {
145 i ++;
146 if (i < argc)
147 options = argv[i];
148 else
149 usage();
150 }
151 else if (!strcmp(argv[i], "-C"))
152 {
153 i ++;
154 if (i < argc)
155 num_pclasses = atoi(argv[i]);
156 else
157 usage();
158 }
159 else if (!strcmp(argv[i], "-p"))
160 {
161 i ++;
162 if (i < argc)
163 num_printers = atoi(argv[i]);
164 else
165 usage();
166 }
167 else if (!strcmp(argv[i], "-s"))
168 {
169 i ++;
170 if (i < argc)
171 num_servers = atoi(argv[i]);
172 else
173 usage();
174 }
175 else if (!strcmp(argv[i], "-v"))
176 verbose = 1;
177 else if (isdigit(argv[i][0] & 255))
178 {
179 port = atoi(argv[i]);
180 }
181 else
182 usage();
183 }
184
185 if ((num_printers <= 0 && num_pclasses <= 0) || num_servers <= 0 ||
186 interval <= 0 || lease < 1 || port <= 0)
187 usage();
188
189 /*
190 * Open a broadcast socket...
191 */
192
193 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
194 {
195 perror("Unable to open broadcast socket");
196 return (1);
197 }
198
199 /*
200 * Set the "broadcast" flag...
201 */
202
203 val = 1;
204 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
205 {
206 perror("Unable to put socket in broadcast mode");
207
208 close(sock);
209 return (1);
210 }
211
212 /*
213 * Broadcast to 127.0.0.1 (localhost)
214 */
215
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);
220
221 /*
222 * Send virtual printers continuously until we are stopped.
223 */
224
225 for (;;)
226 {
227 /*
228 * Start a new cycle of N printers...
229 */
230
231 printf("Sending %d printers from %d servers...\n", num_printers,
232 num_servers);
233
234 count = num_servers * (num_printers + num_pclasses) / interval + 1;
235 curtime = time(NULL);
236 curdate = localtime(&curtime);
237 seconds = interval;
238
239 for (i = 0, printer = 0; printer < num_printers; printer ++)
240 {
241 for (server = 0; server < num_servers; server ++, i ++)
242 {
243 if (i == count)
244 {
245 seconds --;
246 i = 0;
247 sleep(1);
248 curtime = time(NULL);
249 curdate = localtime(&curtime);
250 }
251
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);
260
261 if (verbose)
262 printf("[%02d:%02d:%02d] %s", curdate->tm_hour, curdate->tm_min,
263 curdate->tm_sec, packet);
264
265 if (sendto(sock, packet, strlen(packet), 0,
266 (struct sockaddr *)&addr, sizeof(addr)) < 0)
267 perror("Unabled to send packet");
268 }
269 }
270
271
272 for (i = 0, pclass = 0; pclass < num_pclasses; pclass ++)
273 {
274 for (server = 0; server < num_servers; server ++, i ++)
275 {
276 if (i == count)
277 {
278 seconds --;
279 i = 0;
280 sleep(1);
281 curtime = time(NULL);
282 curdate = localtime(&curtime);
283 }
284
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);
293
294 if (verbose)
295 printf("[%02d:%02d:%02d] %s", curdate->tm_hour, curdate->tm_min,
296 curdate->tm_sec, packet);
297
298 if (sendto(sock, packet, strlen(packet), 0,
299 (struct sockaddr *)&addr, sizeof(addr)) < 0)
300 perror("Unabled to send packet");
301 }
302 }
303
304 if (!continuous)
305 break;
306
307 /*
308 * Sleep for any remaining time...
309 */
310
311 if (seconds > 0)
312 sleep(seconds);
313 }
314
315 return (0);
316 }
317
318
319 /*
320 * 'usage()' - Show program usage...
321 */
322
323 void
324 usage(void)
325 {
326 puts("Usage: testdirsvc [-c] [-i interval] [-l lease-duration] "
327 "[-o ipp-options] [-p printers] "
328 "[-C classes] [-s servers] [-v] port");
329 exit(0);
330 }
331
332
333 /*
334 * End of "$Id: testdirsvc.c 5301 2006-03-17 23:44:33Z mike $".
335 */