Index: trunk/imgserv/imgserv-client/imgserv.cxx |
— | — | @@ -22,6 +22,7 @@ |
23 | 23 | int width, height; |
24 | 24 | std::string server = "localhost"; |
25 | 25 | int port = 8765; |
| 26 | + int verbose; |
26 | 27 | |
27 | 28 | void usage(); |
28 | 29 | int safe_write(int s, void *data, size_t n); |
— | — | @@ -35,7 +36,7 @@ |
36 | 37 | { |
37 | 38 | int c; |
38 | 39 | prognam = argv[0]; |
39 | | - while ((c = getopt(argc, argv, "i:o:s:p:w:h:")) != -1) { |
| 40 | + while ((c = getopt(argc, argv, "i:o:s:p:w:h:v")) != -1) { |
40 | 41 | switch (c) { |
41 | 42 | case 'i': |
42 | 43 | informat = optarg; |
— | — | @@ -55,6 +56,9 @@ |
56 | 57 | case 'p': |
57 | 58 | port = atoi(optarg); |
58 | 59 | break; |
| 60 | + case 'v': |
| 61 | + verbose++; |
| 62 | + break; |
59 | 63 | default: |
60 | 64 | usage(); |
61 | 65 | return 1; |
— | — | @@ -112,7 +116,7 @@ |
113 | 117 | return 1; |
114 | 118 | } |
115 | 119 | |
116 | | - int sock, i; |
| 120 | + int sock = -1, i; |
117 | 121 | struct addrinfo hints, *res, *r; |
118 | 122 | std::memset(&hints, 0, sizeof(hints)); |
119 | 123 | hints.ai_socktype = SOCK_STREAM; |
— | — | @@ -138,26 +142,35 @@ |
139 | 143 | else |
140 | 144 | canon = hostname; |
141 | 145 | |
142 | | - std::cerr << "% Trying " << canon << " (" << |
143 | | - hostname << ") port " << portname << "... " << std::flush; |
| 146 | + if (verbose) |
| 147 | + std::cerr << "% Trying " << hostname << ":" << portname << "...\n"; |
144 | 148 | if ((sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1) { |
145 | | - std::cerr << "failed: " << std::strerror(errno) << '\n'; |
| 149 | + std::cerr << "% Connection to " << hostname << |
| 150 | + ":" << port << " failed: " << std::strerror(errno) << '\n'; |
| 151 | + sock = -1; |
146 | 152 | continue; |
147 | 153 | } |
148 | 154 | |
149 | 155 | if (connect(sock, r->ai_addr, r->ai_addrlen) == -1) { |
150 | | - std::cerr << "failed: " << std::strerror(errno) << '\n'; |
| 156 | + std::cerr << "% Connection to " << hostname << |
| 157 | + ":" << port << " failed: " << std::strerror(errno) << '\n'; |
151 | 158 | close(sock); |
| 159 | + sock = -1; |
152 | 160 | continue; |
153 | 161 | } |
154 | 162 | |
155 | | - std::cerr << "connected.\n"; |
| 163 | + if (verbose) |
| 164 | + std::cerr << "% Connected.\n"; |
156 | 165 | freeaddrinfo(res); |
157 | 166 | break; |
158 | 167 | } |
159 | 168 | |
160 | | - std::cerr << "% Writing input file to server... " << std::flush; |
| 169 | + if (sock == -1) |
| 170 | + return 1; |
161 | 171 | |
| 172 | + if (verbose) |
| 173 | + std::cerr << "% Writing input file to server.\n"; |
| 174 | + |
162 | 175 | std::ostringstream strm; |
163 | 176 | strm << "INFORMAT " << informat << "\r\n" |
164 | 177 | << "OUTFORMAT " << outformat << "\r\n"; |
— | — | @@ -183,7 +196,7 @@ |
184 | 197 | insize -= infile.gcount(); |
185 | 198 | |
186 | 199 | if (insize < 0) { |
187 | | - std::cerr << "input file was too long!\n"; |
| 200 | + std::cerr << "% Input file was too long!\n"; |
188 | 201 | return 1; |
189 | 202 | } |
190 | 203 | |
— | — | @@ -192,12 +205,14 @@ |
193 | 206 | } |
194 | 207 | |
195 | 208 | if (insize) { |
196 | | - std::cerr << "input file was too short!\n"; |
| 209 | + std::cerr << "% Input file was too short!\n"; |
197 | 210 | return 1; |
198 | 211 | } |
199 | 212 | |
200 | | - std::cerr << "done, " << wr << " bytes\n"; |
201 | | - std::cerr << "% Waiting for reply..."; |
| 213 | + if (verbose) { |
| 214 | + std::cerr << "% Done, bytes written = " << wr << '\n'; |
| 215 | + std::cerr << "% Waiting for reply...\n"; |
| 216 | + } |
202 | 217 | |
203 | 218 | enum { |
204 | 219 | READING_STATUS, |
— | — | @@ -243,12 +258,13 @@ |
244 | 259 | bufpos += len + 2; |
245 | 260 | buflen -= len + 2; |
246 | 261 | if (s == "OK") { |
247 | | - std::cout << "ok.\n"; |
| 262 | + if (verbose) |
| 263 | + std::cout << "% Server reports: success.\n"; |
248 | 264 | } else { |
249 | 265 | if (s.substr(0, 5) == "ERROR") { |
250 | | - std::cout << "error: " << s.substr(6) << '\n'; |
| 266 | + std::cout << "% Server error: " << s.substr(6) << '\n'; |
251 | 267 | } else { |
252 | | - std::cout << "error: unknown status\n"; |
| 268 | + std::cout << "% Server error: unknown status\n"; |
253 | 269 | } |
254 | 270 | return 1; |
255 | 271 | } |
— | — | @@ -258,7 +274,7 @@ |
259 | 275 | continue; |
260 | 276 | } else { |
261 | 277 | if (s.size() + buflen > 8192) { |
262 | | - std::cout << "error: header too long\n"; |
| 278 | + std::cout << "% Protocol error: header too long\n"; |
263 | 279 | return 1; |
264 | 280 | } |
265 | 281 | |
— | — | @@ -312,7 +328,8 @@ |
313 | 329 | } |
314 | 330 | done:; |
315 | 331 | |
316 | | - std::cerr << "% Wrote " << outsize << " bytes to " << argv[1] << '\n'; |
| 332 | + if (verbose) |
| 333 | + std::cerr << "% Wrote " << outsize << " bytes to " << argv[1] << '\n'; |
317 | 334 | } |
318 | 335 | |
319 | 336 | namespace { |
— | — | @@ -320,7 +337,7 @@ |
321 | 338 | void |
322 | 339 | usage() |
323 | 340 | { |
324 | | - std::cerr << "usage: " << prognam << " [-i <informat>] [-o <outformat>] [-p port] [-s server] [-w width] [-h height] <infile> <outfile>\n"; |
| 341 | + std::cerr << "usage: " << prognam << " [-v] [-i <informat>] [-o <outformat>] [-p port] [-s server] [-w width] [-h height] <infile> <outfile>\n"; |
325 | 342 | } |
326 | 343 | |
327 | 344 | int |