blob: 7e6383f41708ba51db175fd0b96360c0fa9e9a47 [file] [log] [blame]
Comran Morshedc4ce9512015-03-08 11:51:09 +00001#include <iostream>
2#include <memory>
3#include <sstream>
4#include <string>
5#include <thread>
6#include <atomic>
7#include <vector>
8
Brian Silverman9be949c2015-05-14 00:15:21 -04009#include "seasocks/PageHandler.h"
10#include "seasocks/PrintfLogger.h"
11#include "seasocks/StringUtil.h"
12#include "seasocks/WebSocket.h"
13
Comran Morshedc4ce9512015-03-08 11:51:09 +000014#include "aos/linux_code/init.h"
15#include "aos/common/time.h"
16#include "aos/common/util/phased_loop.h"
17#include "aos/common/mutex.h"
18
Austin Schuh88af0852016-12-04 20:31:32 -080019namespace y2015 {
Comran Morshedc4ce9512015-03-08 11:51:09 +000020namespace http_status {
21
22// A class for storing data from DataCollector and packaging it as a custom
23// message for the websocket.
24// Samples are stored in a vector that wraps around at a certain point to avoid
25// clogging up memory. These samples should be already on all clients before
26// they are overwritten. To avoid losing samples, there must be a balance
27// between the rate samples are being recorded at and the speed of the link
28// between the robot and client.
29
30class HTTPStatusMessage {
31 public:
32 HTTPStatusMessage();
33
34 // Stores an individual measurement in the current sample.
35 void AddMeasure(::std::string name, double value);
36
37 // Starts a new sample that contains measurements for all the states at a
38 // timestep, and lock mutex to synchronize measures.
39 void NextSample();
40
41 // Unlock mutex.
42 void EndSample();
43
44 // Method called by the websocket to get a JSON-packaged string containing,
45 // at most, a constant number of samples, starting at "from_sample".
46 // "from_sample" is a specific index for a sample that is not wrapped.
47 ::std::string Fetch(size_t from_sample);
48
49 private:
50 // Returns the vector index of the sample given.
51 // Since the vectors wrap, multiple sample_ids may refer to the same vector
52 // index.
53 int32_t GetIndex(int32_t sample_id);
54
55 // Vectors of vectors to store samples at indexes determined by GetIndex.
56 ::std::vector<::std::string> data_names_;
57 ::std::vector<::std::vector<double>> data_values_;
Austin Schuhf2a50ba2016-12-24 16:16:26 -080058 ::std::vector<::aos::monotonic_clock::time_point> sample_times_;
Comran Morshedc4ce9512015-03-08 11:51:09 +000059
60 int32_t sample_id_; // Last sample id used.
61 int32_t measure_index_; // Last measure index used.
62 const int32_t overflow_id_; // Vector wrapping size.
63 // Number of samples to include in each JSON packet.
64 const int32_t num_samples_per_packet_;
65
66 // Mutex used to synchronize data.
67 ::aos::Mutex mutex_;
68};
69
70class DataCollector {
71 public:
72 DataCollector();
73 void RunIteration();
74 ::std::string GetData(int32_t from);
75
76 void operator()(); // Will be called by ::std::thread internally.
77 void Quit() { run_ = false; }
78
79 private:
80 ::std::string cur_raw_data_;
81 HTTPStatusMessage message_;
82 ::std::atomic<bool> run_{true};
83};
84
85class SocketHandler : public seasocks::WebSocket::Handler {
86 public:
87 SocketHandler();
88 void onConnect(seasocks::WebSocket* connection) override;
89 void onData(seasocks::WebSocket* connection, const char* data) override;
90 void onDisconnect(seasocks::WebSocket* connection) override;
91 void Quit();
92
93 private:
94 ::std::set<seasocks::WebSocket*> connections_;
95 DataCollector data_collector_;
96 ::std::thread data_collector_thread_;
97};
98
99class SeasocksLogger : public seasocks::PrintfLogger {
100 public:
101 SeasocksLogger(Level min_level_to_log);
102 void log(Level level, const char* message) override;
103};
104
105} // namespace http_status
Austin Schuh88af0852016-12-04 20:31:32 -0800106} // namespace y2015