Project

General

Profile

Bug #7093 » main.cpp

Marco Kinski, 06/18/2019 06:43 PM

 
1

    
2
#include <Wt/WServer>
3
#include <Wt/WResource>
4
#include <Wt/Http/Client>
5
#include <Wt/Http/Response>
6

    
7
#include <memory>
8
#include <atomic>
9
#include <iostream>
10

    
11
// CMD:
12
// C:> server.exe --http-address 0.0.0.0 --docroot .
13

    
14
// Terminal (in parallel):
15
// $ while [ "$(curl -s 'http://localhost/FetchData')" == "done" ]; do   echo -n '.';   sleep 1; done
16

    
17
class FetchData : public Wt::WResource {
18
public:
19
   FetchData (Wt::WServer& server) : server(server) { }
20
   ~FetchData () override { beingDeleted(); }
21

    
22
   void handleRequest (const Wt::Http::Request& /*request*/, Wt::Http::Response& response) override 
23
   {
24
      std::atomic<bool> done = false;
25
      auto client = std::make_unique<Wt::Http::Client>();
26

    
27
      client->done().connect([&] (boost::system::error_code err, const Wt::Http::Message& /*response*/, ...) {
28
         if (err) server.log("error") << err.message();
29
         done = true;
30
      });
31

    
32
      if (client->get("http://localhost/data")) {
33
         while (!done) {
34
            boost::this_thread::sleep_for(boost::chrono::milliseconds{25});
35
         }
36
      } 
37
      else {
38
         server.log("error") << "something is wrong with the url";
39
      }
40

    
41
      response.out().write("done", 4);
42
   }
43

    
44
private:
45
   Wt::WServer& server;
46
};
47

    
48
class ServeData : public Wt::WResource {
49
public:
50
   ServeData () = default;
51
   ~ServeData () override { beingDeleted(); }
52

    
53
   void handleRequest (const Wt::Http::Request& /*request*/, Wt::Http::Response& /*response*/) override 
54
   {
55
   }
56
};
57

    
58

    
59

    
60

    
61

    
62

    
63

    
64
int main (int argc, char** argv) 
65
try {
66
   Wt::WServer wserver{argc, argv};
67

    
68
   FetchData fetchdata(wserver);
69
   wserver.addResource(&fetchdata, "/FetchData");
70

    
71
   ServeData servedata;
72
   wserver.addResource(&servedata, "/Data");
73

    
74
   wserver.run();
75

    
76
   return 0;
77
}
78
catch (std::exception& e) {
79
   std::cerr << e.what() << std::endl;
80
   return 10;
81
}
82
 
(2-2/6)