21
21
22
22
#pragma once
23
23
24
+ #include < memory> // for unique_ptr
24
25
#include < utility> // for make_pair
25
26
26
- #include < cloe/handler.hpp> // for Request, Response
27
- #include < cloe/registrar.hpp> // for Registrar
27
+ #include < cloe/handler.hpp> // for Request, Response
28
+ #include < cloe/registrar.hpp> // for Registrar
29
+ #include < cloe/utility/output_serializer_json.hpp> // for JsonFileSerializer
28
30
29
- #include " stack.hpp" // for ServerConf
30
31
#include " oak/server.hpp" // for Server, StaticRegistrar, ...
32
+ #include " stack.hpp" // for ServerConf
31
33
32
34
namespace engine {
33
35
@@ -67,6 +69,11 @@ class Server {
67
69
public:
68
70
bool is_listening () { return server_.is_listening (); }
69
71
72
+ bool is_streaming () { return is_streaming_; }
73
+
74
+ /* *
75
+ * Start the web server.
76
+ */
70
77
void start () {
71
78
assert (!is_listening ());
72
79
@@ -77,13 +84,31 @@ class Server {
77
84
server_.listen ();
78
85
}
79
86
87
+ /* *
88
+ * Open a file for api data streaming. This does not require a running web
89
+ * server.
90
+ */
91
+ void init_stream (const std::string& filename) {
92
+ serializer_ = make_json_file_serializer (cloe::utility::JsonFileType::JSON_GZIP, logger ());
93
+ serializer_->open_file (filename);
94
+ }
95
+
96
+ /* *
97
+ * Stop all server-related procedures.
98
+ */
80
99
void stop () {
81
100
if (is_listening ()) {
82
101
logger ()->info (" Stopping server..." );
83
102
server_.stop ();
84
103
}
104
+ if (serializer_ != nullptr ) {
105
+ serializer_->close_file ();
106
+ }
85
107
}
86
108
109
+ /* *
110
+ * Register a list of all endpoints.
111
+ */
87
112
void enroll (cloe::Registrar& r) {
88
113
r.register_api_handler (
89
114
" /endpoints" , cloe::HandlerType::STATIC,
@@ -107,12 +132,32 @@ class Server {
107
132
}
108
133
109
134
/* *
110
- * Refresh the server buffer.
135
+ * Refresh and/or start streaming api data to a file.
136
+ */
137
+ void refresh_buffer_start_stream () {
138
+ is_streaming_ = serializer_ != nullptr ;
139
+ if (is_listening () || is_streaming ()) {
140
+ buffer_api_registrar_.refresh_buffer ();
141
+ }
142
+ if (is_streaming ()) {
143
+ // Write static endpoints at the beginning of the file.
144
+ write_data_stream (static_api_registrar_.endpoints ());
145
+ write_data_stream (locked_api_registrar_.endpoints ());
146
+ write_data_stream (buffer_api_registrar_.endpoints ());
147
+ }
148
+ }
149
+
150
+ /* *
151
+ * Refresh and/or write api data to a file.
111
152
*/
112
153
void refresh_buffer () {
113
- if (config. listen ) {
154
+ if (is_listening () || is_streaming () ) {
114
155
buffer_api_registrar_.refresh_buffer ();
115
156
}
157
+ if (is_streaming ()) {
158
+ write_data_stream (locked_api_registrar_.endpoints ());
159
+ write_data_stream (buffer_api_registrar_.endpoints ());
160
+ }
116
161
}
117
162
118
163
/* *
@@ -123,12 +168,22 @@ class Server {
123
168
protected:
124
169
cloe::Logger logger () const { return cloe::logger::get (" cloe" ); }
125
170
171
+ private:
172
+ void write_data_stream (const std::vector<std::string>& endpoints) const {
173
+ auto j = server_.endpoints_to_json (endpoints);
174
+ if (!j.empty ()) {
175
+ serializer_->serialize (j);
176
+ }
177
+ };
178
+
126
179
private: // State
127
180
oak::Server server_;
128
181
oak::StaticRegistrar static_registrar_{&server_, config.static_prefix , nullptr };
129
182
oak::StaticRegistrar static_api_registrar_{&server_, config.api_prefix , nullptr };
130
183
oak::LockedRegistrar locked_api_registrar_{&server_, config.api_prefix , nullptr };
131
184
oak::BufferRegistrar buffer_api_registrar_{&server_, config.api_prefix , nullptr };
185
+ bool is_streaming_{false };
186
+ std::unique_ptr<cloe::utility::JsonFileSerializer> serializer_;
132
187
};
133
188
134
189
} // namespace engine
0 commit comments