WvStreams
wvhttpex.cc
1 /*
2  * A WvHttpStream example.
3  *
4  * This program downloads a file via http.
5  * The expected result is:
6  * http<Info>: Now in state 0
7  * http<Info>: Now in state 1
8  * http<Info>: [ 0]
9  * http<Info>: Now in state 2
10  * http<Info>: [ 0]
11  * http<Info>: Now in state 3
12  * http<Info>: [ 0][ 0][ 0][ 0][ 0][ 0][ 0][ 0][ 0]
13  * http<Info>: Now in state 4
14  * http<Info>: [ 751][ 922][ 0]
15  *
16  */
17 #include "wvhttp.h"
18 #include "wvistreamlist.h"
19 #include "wvlog.h"
20 #include "wvfile.h"
21 
22 
23 int main(int argc, char **argv)
24 {
25  WvLog log("http", WvLog::Info);
26  WvURL url("http://www.net-itech.com/");
27  WvHTTPStream http(url);
28  WvFile out("http.out", O_WRONLY | O_TRUNC | O_CREAT);
29  WvHTTPStream::State last_state = WvHTTPStream::Done;
30  static char buf[10240];
31  size_t len;
32 
33  WvIStreamList l;
34  l.add_after(l.tail, &http, false);
35 
36  while (http.isok() && out.isok())
37  {
38  if (last_state != http.state)
39  {
40  log("\nNow in state %s\n", http.state);
41  last_state = http.state;
42  }
43 
44  if (l.select(100))
45  l.callback();
46 
47  if (http.select(0))
48  {
49  len = http.read(buf, sizeof(buf));
50  out.write(buf, len);
51  log("[%6s]", len);
52  }
53  }
54 
55  if (!http.isok() && http.geterr())
56  log("http: %s\n", http.errstr());
57 
58  return 0;
59 }
WvFile
WvFile implements a stream connected to a file or Unix device.
Definition: wvfile.h:28
WvStream::select
bool select(time_t msec_timeout)
Return true if any of the requested features are true on the stream.
Definition: wvstream.h:376
WvStream::callback
virtual void callback()
if the stream has a callback function defined, call it now.
Definition: wvstream.cc:401
WvLog
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition: wvlog.h:56
WvUrl
Definition: wvurl.h:16
WvIStreamList
WvStreamList holds a list of WvStream objects – and its select() and callback() functions know how to...
Definition: wvistreamlist.h:20