Bredbandskollen CLI  1.2
Asynchronous network task engine
measurementtask.h
1 #pragma once
2 
3 #include "../http/httpclienttask.h"
4 #include "../http/httphost.h"
5 #include "defs.h"
6 #include <clocale>
7 #include <sstream>
8 
10 public:
11  MeasurementTask(const std::string &name, const std::string &ticket,
12  const HttpHost &httpserver,
13  unsigned int no_conn = 1, unsigned int max_conn = 3,
14  double timeout = 25.0) :
15  HttpClientTask(name, httpserver),
16  no_connections(no_conn),
17  active_connections(0),
18  max_connections(max_conn),
19  timeout_sec(timeout),
20  ticket_string(ticket) {
21  setUserAgentString(measurement::appName + " " + measurement::appVersion);
22  }
23 
24  std::string t() {
25  return ticket_string;
26  }
27 
28  // Only use keep-alive within the same measurement:
29  std::string cacheLabel() override {
30  return ticket_string;
31  }
32 
33  // If you override this, you must call checkConnectionCount().
34  // Return value is number of seconds until timerEvent
35  // should be called, <= 0 for never.
36  double start() override {
37  checkConnectionCount();
38  return timeout_sec;
39  }
40 
41  // If you override timerEvent(), you'll have to check for timeout.
42  /*
43  double timerEvent() override {
44  if (timeout())
45  setResult("");
46  return 0;
47  }
48  */
49 
50  // Will be called if checkConnectionCount fails.
51  // Default is to terminate task with an empty result.
52  virtual void connectionLost() {
53  log() << "connectionLost()";
54  setResult("");
55  }
56 
57  // Returns a JSON object with a single attribute-value pair
58  static std::string json_obj(const std::string &attr,
59  const std::string &value);
60 
61  // Return floating point value as string.
62  // We don't want to use to_string since it's locale dependent,
63  // which may break JSON.
64  static std::string fValue(double x) {
65  std::ostringstream s;
66  s.imbue(std::locale("C"));
67  s << x;
68  return s.str();
69  }
70 
71  static std::string calculateLatency(std::vector<double> &samples);
72 protected:
73  // Tries to make sure we have at least no_connections simultaneous
74  // HTTP connections. Will not try more than max_connection times.
75  // Will call connectionLost on fatal failure.
76  void checkConnectionCount();
77 
78  // Will not kill already created connections.
79  void setNoConnections(unsigned int no) {
80  no_connections = no;
81  checkConnectionCount();
82  }
83 
84  unsigned int getNoConnections() const {
85  return no_connections;
86  }
87 
88  unsigned int currentNoConnections() const {
89  return active_connections;
90  }
91 
92  void noMoreConnections() {
93  max_connections = 0;
94  }
95 
96  bool timeout() {
97  if (timeout_sec < 0)
98  return false;
99  if (elapsed() < timeout_sec)
100  return false;
101  log() << "Task timeout.";
102  return true;
103  }
104 
105  double timeout_s() {
106  return timeout_sec;
107  }
108 private:
109  void connAdded(SocketConnection *) final;
110  void connRemoved(SocketConnection *) final;
111 
112  unsigned int no_connections, active_connections, max_connections;
113  double timeout_sec;
114  std::string ticket_string;
115 };
API for HTTP clients.
Definition: httpclienttask.h:11
The host name and port number of a HTTP host.
Definition: httphost.h:17
std::ostream & log() const
Write a line of info log.
Definition: logger.h:328
Definition: measurementtask.h:9
double start() override
Definition: measurementtask.h:36
This class implements low-level socket connection operations. Inherit from it to implement protocols ...
Definition: socketconnection.h:47
void setResult(const std::string &res)
Definition: task.cpp:18
double elapsed() const
Return number of seconds since the task was started.
Definition: task.h:234