Project

General

Profile

Bug #7093 » main.cpp

Marco Kinski, 06/17/2019 11:14 AM

 
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 () = default;
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) wApp->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
         wApp->log("error") << "something is wrong with the url";
39
      }
40

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

    
45
class ServeData : public Wt::WResource {
46
public:
47
   ServeData () = default;
48
   ~ServeData () override { beingDeleted(); }
49

    
50
   void handleRequest (const Wt::Http::Request& /*request*/, Wt::Http::Response& /*response*/) override 
51
   {
52
   }
53
};
54

    
55

    
56

    
57

    
58

    
59

    
60

    
61
int main (int argc, char** argv) 
62
try {
63
   Wt::WServer wserver{argc, argv};
64

    
65
   FetchData fetchdata;
66
   wserver.addResource(&fetchdata, "/FetchData");
67

    
68
   ServeData servedata;
69
   wserver.addResource(&servedata, "/Data");
70

    
71
   wserver.run();
72

    
73
   return 0;
74
}
75
catch (std::exception& e) {
76
   std::cerr << e.what() << std::endl;
77
   return 10;
78
}
79
 
(1-1/6)