]> 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 4821 2005-11-04 19:36:39Z 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 server, /* Current server */
59 num_servers, /* Number of servers */
60 count, /* Number of printers sent this cycle */
61 interval, /* Browse Interval */
62 continuous, /* Run continuously? */
63 port, /* Browse port */
64 sock, /* Browse socket */
65 val, /* Socket option value */
66 seconds, /* Seconds until next cycle */
67 verbose; /* Verbose output? */
68 const char *options; /* Options for URIs */
69 time_t curtime; /* Current UNIX time */
70 struct tm *curdate; /* Current date and time */
71 struct sockaddr_in addr; /* Broadcast address */
72 char packet[1540]; /* Data packet */
73 static const char * const names[26] = /* Printer names */
74 {
75 "alpha",
76 "bravo",
77 "charlie",
78 "delta",
79 "echo",
80 "foxtrot",
81 "golf",
82 "hotel",
83 "india",
84 "juliet",
85 "kilo",
86 "lima",
87 "mike",
88 "november",
89 "oscar",
90 "papa",
91 "quebec",
92 "romeo",
93 "sierra",
94 "tango",
95 "uniform",
96 "victor",
97 "wiskey",
98 "x-ray",
99 "yankee",
100 "zulu"
101 };
102
103
104 /*
105 * Process command-line arguments...
106 */
107
108 num_printers = 10;
109 num_servers = 1;
110 interval = 30;
111 port = 0;
112 verbose = 0;
113 continuous = 0;
114 options = NULL;
115
116 for (i = 1; i < argc; i ++)
117 {
118 if (!strcmp(argv[i], "-c"))
119 continuous = 1;
120 if (!strcmp(argv[i], "-v"))
121 verbose = 1;
122 else if (!strcmp(argv[i], "-i"))
123 {
124 i ++;
125 if (i < argc)
126 interval = atoi(argv[i]);
127 else
128 usage();
129 }
130 else if (!strcmp(argv[i], "-o"))
131 {
132 i ++;
133 if (i < argc)
134 options = argv[i];
135 else
136 usage();
137 }
138 else if (!strcmp(argv[i], "-p"))
139 {
140 i ++;
141 if (i < argc)
142 num_printers = atoi(argv[i]);
143 else
144 usage();
145 }
146 else if (!strcmp(argv[i], "-s"))
147 {
148 i ++;
149 if (i < argc)
150 num_servers = atoi(argv[i]);
151 else
152 usage();
153 }
154 else if (isdigit(argv[i][0] & 255))
155 {
156 port = atoi(argv[i]);
157 }
158 else
159 usage();
160 }
161
162 if (num_printers <= 0 || num_servers <= 0 || interval <= 0 || port <= 0)
163 usage();
164
165 /*
166 * Open a broadcast socket...
167 */
168
169 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
170 {
171 perror("Unable to open broadcast socket");
172 return (1);
173 }
174
175 /*
176 * Set the "broadcast" flag...
177 */
178
179 val = 1;
180 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
181 {
182 perror("Unable to put socket in broadcast mode");
183
184 close(sock);
185 return (1);
186 }
187
188 /*
189 * Broadcast to 127.0.0.1 (localhost)
190 */
191
192 memset(&addr, 0, sizeof(addr));
193 addr.sin_addr.s_addr = htonl(0x7f000001);
194 addr.sin_family = AF_INET;
195 addr.sin_port = htons(port);
196
197 /*
198 * Send virtual printers continuously until we are stopped.
199 */
200
201 for (;;)
202 {
203 /*
204 * Start a new cycle of N printers...
205 */
206
207 printf("Sending %d printers from %d servers...\n", num_printers,
208 num_servers);
209
210 count = num_servers * num_printers / interval + 1;
211 curtime = time(NULL);
212 curdate = localtime(&curtime);
213 seconds = interval;
214
215 for (i = 0, printer = 0; printer < num_printers; printer ++)
216 {
217 for (server = 0; server < num_servers; server ++, i ++)
218 {
219 if (i == count)
220 {
221 seconds --;
222 i = 0;
223 sleep(1);
224 curtime = time(NULL);
225 curdate = localtime(&curtime);
226 }
227
228 snprintf(packet, sizeof(packet),
229 "%x %x ipp://testserver-%d/printers/%s-%d \"Server Room %d\" "
230 "\"Test Printer %d\" \"Acme Blazer 2000\"%s%s\n",
231 CUPS_PRINTER_REMOTE, IPP_PRINTER_IDLE, server + 1,
232 names[printer % 26], printer / 26 + 1, server + 1,
233 printer + 1, options ? " ipp-options=" : "",
234 options ? options : "");
235
236 if (verbose)
237 printf("[%02d:%02d:%02d] %s", curdate->tm_hour, curdate->tm_min,
238 curdate->tm_sec, packet);
239
240 if (sendto(sock, packet, strlen(packet), 0,
241 (struct sockaddr *)&addr, sizeof(addr)) < 0)
242 perror("Unabled to send packet");
243 }
244 }
245
246 if (!continuous)
247 break;
248
249 /*
250 * Sleep for any remaining time...
251 */
252
253 if (seconds > 0)
254 sleep(seconds);
255 }
256
257 return (0);
258 }
259
260
261 /*
262 * 'usage()' - Show program usage...
263 */
264
265 void
266 usage(void)
267 {
268 puts("Usage: testdirsvc [-i interval] [-o ipp-options] [-p printers] [-s servers] [-v] port");
269 exit(0);
270 }
271
272
273 /*
274 * End of "$Id: testdirsvc.c 4821 2005-11-04 19:36:39Z mike $".
275 */