Bredbandskollen CLI  1.2
Asynchronous network task engine
measurementagent.h
1 // Copyright (c) 2018 IIS (The Internet Foundation in Sweden)
2 // Written by Göran Andersson <initgoran@gmail.com>
3 
4 #pragma once
5 
6 #include <string>
7 #include <map>
8 #include <sstream>
9 
10 #include "../framework/bridgetask.h"
11 #include "../json11/json11.hpp"
12 #include "../http/httphost.h"
13 #include "../http/cookiefile.h"
14 
15 class SpeedTest;
16 
17 class MeasurementAgent : public Task {
18 public:
19  MeasurementAgent(const TaskConfig &config, const HttpHost &webserver);
20  void taskMessage(Task *task) override;
21  void taskFinished(Task *task) override;
22 
23  void handleExecution(Task *sender, const std::string &msg) override;
24  void sendToClient(const std::string &method,
25  const std::string &jsonobj = "{}") {
26  if (bridge)
27  bridge->sendMsgToClient(method, jsonobj);
28  }
29  void sendTaskComplete(const std::string &t, const std::string &res = "");
30  void sendTaskProgress(const std::string &taskname,
31  double speed, double progress);
32  void accumulateLog() {
33  setLogFile(accumulated_log);
34  }
35  void appendLog(const std::string &str) {
36  accumulated_log << "\nAppend " << str.size() << "\n" << str;
37  }
38  void sendLogToServer();
39 private:
40  std::string getDefaultConfig();
41  bool isValidHashkey(const std::string &key);
42  void pollBridge(const std::string &msg);
43  static bool isValidJson(const std::string &s) {
44  std::string err;
45  auto obj = json11::Json::parse(s, err);
46  return err.empty();
47  }
48 
49  void handleMsgFromClient(const std::string &method,
50  const json11::Json &args);
51  void handleConfigurationOption(const std::string &name,
52  const std::string &value);
53  void uploadComplete();
54  void doSaveReport();
55  void resetCurrentTest();
56  BridgeTask *bridge = nullptr;
57  SpeedTest *current_test = nullptr;
58  std::string current_ticket;
59  std::ostringstream accumulated_log;
60 
61  // Initial state is IDLE. When client says "startTest", state becomes
62  // STARTED. When test is done, we send "testComplete global" to client
63  // and set state to FINISHED. When client sends resetTest, state will
64  // be reset to IDLE.
65  // If client sends abortTest in state STARTED, state becomes ABORTED.
66  enum class MeasurementState { IDLE, STARTED, FINISHED, ABORTED };
67  MeasurementState state = MeasurementState::IDLE;
68 
69  // If the client doesn't manage keys, we store them here:
70  CookieManager *key_store;
71 
72  std::string force_key;
73 
74  // The web server and the measurement server:
75  HttpHost wserv, mserv;
76 
77  // Default value, might be modified by the client
78  std::string wserv_contentsurl = "/api/content";
79  std::string wserv_measurementsurl = "/api/measurements";
80  std::string wserv_settingsurl = "/api/servers";
81  std::string settings_result;
82 
83  // Info to be included each time measurement result is sent:
84  std::map<std::string, std::string> report_template;
85 
86  TaskConfig cfgOptions;
87  std::string options_filename;
88 };
Tasks may use a bridge to communicate with an application running outside the event loop.
Definition: bridgetask.h:43
virtual void sendMsgToClient(const std::string &msg)=0
The agent will call this to pass messages to the client.
Definition: cookiemanager.h:14
The host name and port number of a HTTP host.
Definition: httphost.h:17
static void setLogFile(std::ostream &stream)
Set global log destination.
Definition: logger.cpp:166
Definition: measurementagent.h:17
void handleExecution(Task *sender, const std::string &msg) override
Callback to execute code on behalf of another Task.
Definition: measurementagent.cpp:486
void taskMessage(Task *task) override
Definition: measurementagent.cpp:48
void taskFinished(Task *task) override
Definition: measurementagent.cpp:61
Definition: speedtest.h:16
Read configuration from file or string.
Definition: taskconfig.h:44
The purpose of a task is to manage socket connections, and/or to execute timers.
Definition: task.h:39