Comran Morshed | c4ce951 | 2015-03-08 11:51:09 +0000 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <memory> |
| 3 | #include <sstream> |
| 4 | #include <string> |
| 5 | #include <thread> |
| 6 | #include <atomic> |
| 7 | #include <vector> |
| 8 | |
Brian Silverman | 9be949c | 2015-05-14 00:15:21 -0400 | [diff] [blame] | 9 | #include "seasocks/PageHandler.h" |
| 10 | #include "seasocks/PrintfLogger.h" |
| 11 | #include "seasocks/StringUtil.h" |
| 12 | #include "seasocks/WebSocket.h" |
| 13 | |
Comran Morshed | c4ce951 | 2015-03-08 11:51:09 +0000 | [diff] [blame] | 14 | #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 Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 19 | namespace y2015 { |
Comran Morshed | c4ce951 | 2015-03-08 11:51:09 +0000 | [diff] [blame] | 20 | namespace 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 | |
| 30 | class 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 Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 58 | ::std::vector<::aos::monotonic_clock::time_point> sample_times_; |
Comran Morshed | c4ce951 | 2015-03-08 11:51:09 +0000 | [diff] [blame] | 59 | |
| 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 | |
| 70 | class 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 | |
| 85 | class 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 | |
| 99 | class 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 Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame] | 106 | } // namespace y2015 |