]> 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 5178 2006-02-26 00:24:23Z 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 else if (!strcmp(argv[i], "-l"))
134 {
135 i ++;
136 if (i < argc)
137 lease = atoi(argv[i]);
138 else
139 usage();
140 }
141 else if (!strcmp(argv[i], "-o"))
142 {
143 i ++;
144 if (i < argc)
145 options = argv[i];
146 else
147 usage();
148 }
149 else if (!strcmp(argv[i], "-C"))
150 {
151 i ++;
152 if (i < argc)
153 num_pclasses = atoi(argv[i]);
154 else
155 usage();
156 }
157 else if (!strcmp(argv[i], "-p"))
158 {
159 i ++;
160 if (i < argc)
161 num_printers = atoi(argv[i]);
162 else
163 usage();
164 }
165 else if (!strcmp(argv[i], "-s"))
166 {
167 i ++;
168 if (i < argc)
169 num_servers = atoi(argv[i]);
170 else
171 usage();
172 }
173 else if (!strcmp(argv[i], "-v"))
174 verbose = 1;
175 else if (isdigit(argv[i][0] & 255))
176 {
177 port = atoi(argv[i]);
178 }
179 else
180 usage();
181 }
182
183 if ((num_printers <= 0 && num_pclasses <= 0) || num_servers <= 0 ||
184 interval <= 0 || lease < 1 || port <= 0)
185 usage();
186
187 /*
188 * Open a broadcast socket...
189 */
190
191 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
192 {
193 perror("Unable to open broadcast socket");
194 return (1);
195 }
196
197 /*
198 * Set the "broadcast" flag...
199 */
200
201 val = 1;
202 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
203 {
204 perror("Unable to put socket in broadcast mode");
205
206 close(sock);
207 return (1);
208 }
209
210 /*
211 * Broadcast to 127.0.0.1 (localhost)
212 */
213
214 memset(&addr, 0, sizeof(addr));
215 addr.sin_addr.s_addr = htonl(0x7f000001);
216 addr.sin_family = AF_INET;
217 addr.sin_port = htons(port);
218
219 /*
220 * Send virtual printers continuously until we are stopped.
221 */
222
223 for (;;)
224 {
225 /*
226 * Start a new cycle of N printers...
227 */
228
229 printf("Sending %d printers from %d servers...\n", num_printers,
230 num_servers);
231
232 count = num_servers * (num_printers + num_pclasses) / interval + 1;
233 curtime = time(NULL);
234 curdate = localtime(&curtime);
235 seconds = interval;
236
237 for (i = 0, printer = 0; printer < num_printers; printer ++)
238 {
239 for (server = 0; server < num_servers; server ++, i ++)
240 {
241 if (i == count)
242 {
243 seconds --;
244 i = 0;
245 sleep(1);
246 curtime = time(NULL);
247 curdate = localtime(&curtime);
248 }
249
250 snprintf(packet, sizeof(packet),
251 "%x %x ipp://testserver-%d/printers/%s-%d \"Server Room %d\" "
252 "\"Test Printer %d\" \"Acme Blazer 2000\"%s%s "
253 "lease-duration=%d\n",
254 CUPS_PRINTER_REMOTE, IPP_PRINTER_IDLE, server + 1,
255 names[printer % 26], printer / 26 + 1, server + 1,
256 printer + 1, options ? " ipp-options=" : "",
257 options ? options : "", lease);
258
259 if (verbose)
260 printf("[%02d:%02d:%02d] %s", curdate->tm_hour, curdate->tm_min,
261 curdate->tm_sec, packet);
262
263 if (sendto(sock, packet, strlen(packet), 0,
264 (struct sockaddr *)&addr, sizeof(addr)) < 0)
265 perror("Unabled to send packet");
266 }
267 }
268
269
270 for (i = 0, pclass = 0; pclass < num_pclasses; pclass ++)
271 {
272 for (server = 0; server < num_servers; server ++, i ++)
273 {
274 if (i == count)
275 {
276 seconds --;
277 i = 0;
278 sleep(1);
279 curtime = time(NULL);
280 curdate = localtime(&curtime);
281 }
282
283 snprintf(packet, sizeof(packet),
284 "%x %x ipp://testserver-%d/classes/class-%s-%d \"Server Room %d\" "
285 "\"Test Class %d\" \"Acme Blazer 2000\"%s%s "
286 "lease-duration=%d\n",
287 CUPS_PRINTER_REMOTE | CUPS_PRINTER_CLASS, IPP_PRINTER_IDLE,
288 server + 1, names[pclass % 26], pclass / 26 + 1, server + 1,
289 pclass + 1, options ? " ipp-options=" : "",
290 options ? options : "", lease);
291
292 if (verbose)
293 printf("[%02d:%02d:%02d] %s", curdate->tm_hour, curdate->tm_min,
294 curdate->tm_sec, packet);
295
296 if (sendto(sock, packet, strlen(packet), 0,
297 (struct sockaddr *)&addr, sizeof(addr)) < 0)
298 perror("Unabled to send packet");
299 }
300 }
301
302 if (!continuous)
303 break;
304
305 /*
306 * Sleep for any remaining time...
307 */
308
309 if (seconds > 0)
310 sleep(seconds);
311 }
312
313 return (0);
314 }
315
316
317 /*
318 * 'usage()' - Show program usage...
319 */
320
321 void
322 usage(void)
323 {
324 puts("Usage: testdirsvc [-c] [-i interval] [-l lease-duration] "
325 "[-o ipp-options] [-p printers] "
326 "[-C classes] [-s servers] [-v] port");
327 exit(0);
328 }
329
330
331 /*
332 * End of "$Id: testdirsvc.c 5178 2006-02-26 00:24:23Z mike $".
333 */