Line data Source code
1 : /**
2 : * @file test_rest_app.cxx Test application for using the
3 : * restCommandFacility with a RestCommandedObject.
4 : * Showcases error handling of bad URIs and proper cleanup
5 : * of resources via the killswitch.
6 : *
7 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
8 : * Licensing/copyright details are in the COPYING file that you should have
9 : * received with this code.
10 : */
11 : #include "cmdlib/CommandFacility.hpp"
12 : #include "cmdlib/CommandedObject.hpp"
13 : #include "rest_commanded_object.hpp"
14 :
15 : #include "logging/Logging.hpp"
16 :
17 : #include <chrono>
18 : #include <memory>
19 : #include <string>
20 :
21 : using namespace dunedaq::cmdlib;
22 :
23 : // Expects the created CommandFacilities to have problems.
24 : std::shared_ptr<CommandFacility>
25 0 : create_facility(const std::string& uri)
26 : {
27 0 : try {
28 0 : return make_command_facility(uri);
29 0 : } catch (const std::exception& ex) {
30 0 : TLOG() << "Something is wrong -> " << ex.what();
31 0 : }
32 0 : return nullptr;
33 : }
34 :
35 : int
36 0 : main(int /*argc*/, char** /*argv[]*/)
37 : {
38 : // Run marker
39 0 : std::atomic<bool> marker{ true };
40 :
41 : // Killswitch that flips the run marker
42 0 : auto killswitch = std::thread([&]() {
43 0 : TLOG() << "Application will terminate in 20s...";
44 0 : std::this_thread::sleep_for(std::chrono::seconds(20));
45 0 : marker.store(false);
46 0 : });
47 :
48 : // Commanded object
49 0 : RestCommandedObject obj(marker);
50 :
51 : // Exercise bad URIs.
52 0 : auto fac = create_facility(std::string("rest://"));
53 0 : fac = create_facility(std::string("rest://localhost"));
54 0 : fac = create_facility(std::string("rest://localhost:"));
55 0 : fac = create_facility(std::string("rest://localhost:-1"));
56 0 : fac = create_facility(std::string("rest://localhost:9999999999"));
57 :
58 : // Create good facility
59 0 : fac = create_facility(std::string("rest://localhost:12345"));
60 :
61 : // Add commanded object to facility
62 0 : fac->set_commanded(obj, "pippo");
63 :
64 : // Run until marked
65 0 : fac->run(marker);
66 :
67 : // Join local threads
68 0 : if (killswitch.joinable()) {
69 0 : killswitch.join();
70 : }
71 :
72 : // Exit
73 0 : TLOG() << "Exiting.";
74 0 : return 0;
75 0 : }
|