Merge lp:~thomas-voss/unity-scopes-api/leverage-net-cpp-streaming-api into lp:unity-scopes-api

Proposed by Thomas Voß
Status: Rejected
Rejected by: Marcus Tomlinson
Proposed branch: lp:~thomas-voss/unity-scopes-api/leverage-net-cpp-streaming-api
Merge into: lp:unity-scopes-api
Diff against target: 232 lines (+55/-42)
6 files modified
CMakeLists.txt (+1/-1)
debian/control (+1/-1)
include/unity/scopes/internal/smartscopes/HttpClientNetCpp.h (+2/-2)
include/unity/scopes/internal/smartscopes/SmartScopesClient.h (+4/-2)
src/scopes/internal/smartscopes/HttpClientNetCpp.cpp (+9/-11)
src/scopes/internal/smartscopes/SmartScopesClient.cpp (+38/-25)
To merge this branch: bzr merge lp:~thomas-voss/unity-scopes-api/leverage-net-cpp-streaming-api
Reviewer Review Type Date Requested Status
Marcus Tomlinson (community) Disapprove
Michi Henning (community) Approve
PS Jenkins bot (community) continuous-integration Needs Fixing
Review via email: mp+255061@code.launchpad.net

Commit message

Leverage the streaming API introduced in net-cpp 1.2.0.

Description of the change

Leverage the streaming API introduced in net-cpp 1.2.0.

To post a comment you must log in.
325. By Thomas Voß

Clean up commented out code.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Michi Henning (michihenning) wrote :

Looks good to me, thanks! How do we get the missing dependency into CI?

Marcus, could you look this over too please? You know that code much better than me.

review: Approve
Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

In theory this is correct, however the SSS does not currently send chunked data in complete lines (as we are assuming here, and rightfully so). The SSS needs to be fixed to respond in clean chucks, each terminated by "\r\n", before we can go ahead and merge this.

Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

Marking rejected as this branch has been superseded.

Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

Hi Thomas, could you please either delete this branch or change the status under "Branch information" to Abandoned. Thanks

Revision history for this message
Marcus Tomlinson (marcustomlinson) :
review: Disapprove

Unmerged revisions

325. By Thomas Voß

Clean up commented out code.

324. By Thomas Voß

Move responsibility for interpreting chunks of data arriving from the smart scopes server to smart scopes client.
Rename parse_line to handle_line.
Introduce a function handle_chunk that splits up a given chunk line by line and feeds it to handle_line(...).

323. By Thomas Voß

Pass on the unmodified line arriving from the server.

322. By Thomas Voß

Do not hand the entire result to the line handler.

321. By Thomas Voß

Leverage the streaming API introduced in net-cpp 1.2.0.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2015-02-13 09:20:20 +0000
3+++ CMakeLists.txt 2015-04-02 10:40:26 +0000
4@@ -70,7 +70,7 @@
5 pkg_check_modules(LIBACCOUNTS libaccounts-glib REQUIRED)
6 pkg_check_modules(LIBSIGNON libsignon-glib REQUIRED)
7 pkg_check_modules(ZMQLIB libzmq REQUIRED)
8-pkg_check_modules(NET_CPP net-cpp REQUIRED)
9+pkg_check_modules(NET_CPP net-cpp>=1.2.0 REQUIRED)
10
11 find_library(ZMQPPLIB zmqpp)
12 if(NOT ZMQPPLIB)
13
14=== modified file 'debian/control'
15--- debian/control 2015-02-05 14:57:01 +0000
16+++ debian/control 2015-04-02 10:40:26 +0000
17@@ -25,7 +25,7 @@
18 libdbustest1-dev,
19 libjsoncpp-dev,
20 liblttng-ust-dev,
21- libnet-cpp-dev,
22+ libnet-cpp-dev (>= 1.2.0),
23 libprocess-cpp-dev (>= 1.0.1),
24 libsignon-glib-dev,
25 libunity-api-dev (>= 7.80.7~),
26
27=== modified file 'include/unity/scopes/internal/smartscopes/HttpClientNetCpp.h'
28--- include/unity/scopes/internal/smartscopes/HttpClientNetCpp.h 2015-01-16 06:13:57 +0000
29+++ include/unity/scopes/internal/smartscopes/HttpClientNetCpp.h 2015-04-02 10:40:26 +0000
30@@ -30,7 +30,7 @@
31 {
32 namespace http
33 {
34-class Client;
35+class StreamingClient;
36 }
37 }
38 }
39@@ -63,7 +63,7 @@
40 void cancel_get(unsigned int session_id) override;
41
42 unsigned int no_reply_timeout;
43- std::shared_ptr<core::net::http::Client> client;
44+ std::shared_ptr<core::net::http::StreamingClient> client;
45 std::thread worker;
46 };
47
48
49=== modified file 'include/unity/scopes/internal/smartscopes/SmartScopesClient.h'
50--- include/unity/scopes/internal/smartscopes/SmartScopesClient.h 2015-01-28 09:03:59 +0000
51+++ include/unity/scopes/internal/smartscopes/SmartScopesClient.h 2015-04-02 10:40:26 +0000
52@@ -215,8 +215,10 @@
53 std::shared_ptr<DepartmentInfo> parse_departments(JsonNodeInterface::SPtr node);
54 Filters parse_filters(JsonNodeInterface::SPtr node);
55 FilterState parse_filter_state(JsonNodeInterface::SPtr node);
56- void parse_line(std::string const& json, SearchReplyHandler const& handler);
57- void parse_line(std::string const& json, PreviewReplyHandler const& handler);
58+
59+ void handle_chunk(const std::string& chunk, std::function<void(const std::string&)> line_handler);
60+ void handle_line(std::string const& json, SearchReplyHandler const& handler);
61+ void handle_line(std::string const& json, PreviewReplyHandler const& handler);
62
63 std::vector<std::string> extract_json_stream(std::string const& json_stream);
64
65
66=== modified file 'src/scopes/internal/smartscopes/HttpClientNetCpp.cpp'
67--- src/scopes/internal/smartscopes/HttpClientNetCpp.cpp 2015-01-19 09:15:06 +0000
68+++ src/scopes/internal/smartscopes/HttpClientNetCpp.cpp 2015-04-02 10:40:26 +0000
69@@ -20,8 +20,8 @@
70
71 #include <unity/UnityExceptions.h>
72
73-#include <core/net/http/client.h>
74-#include <core/net/http/request.h>
75+#include <core/net/http/streaming_client.h>
76+#include <core/net/http/streaming_request.h>
77 #include <core/net/http/response.h>
78 #include <core/net/http/status.h>
79
80@@ -102,7 +102,7 @@
81
82 HttpClientNetCpp::HttpClientNetCpp(unsigned int no_reply_timeout)
83 : no_reply_timeout{no_reply_timeout},
84- client{http::make_client()},
85+ client{http::make_streaming_client()},
86 worker([this]() { client->run(); })
87 {
88 }
89@@ -129,7 +129,7 @@
90 }
91 http_config.header = http_header;
92
93- auto request = client->get(http_config);
94+ auto request = client->streaming_get(http_config);
95 request->set_timeout(std::chrono::milliseconds{no_reply_timeout});
96
97 auto promise = std::make_shared<std::promise<void>>();
98@@ -157,12 +157,6 @@
99 }
100 else
101 {
102- std::istringstream in(response.body);
103- std::string line;
104- while (std::getline(in, line))
105- {
106- line_data(line);
107- }
108 promise->set_value();
109 }
110 })
111@@ -170,7 +164,11 @@
112 {
113 unity::ResourceException re(e.what());
114 promise->set_exception(std::make_exception_ptr(re));
115- }));
116+ }),
117+ [line_data](const std::string& const_data)
118+ {
119+ line_data(const_data);
120+ });
121
122 return std::make_shared<HttpResponseHandle>(
123 shared_from_this(),
124
125=== modified file 'src/scopes/internal/smartscopes/SmartScopesClient.cpp'
126--- src/scopes/internal/smartscopes/SmartScopesClient.cpp 2015-01-28 09:03:59 +0000
127+++ src/scopes/internal/smartscopes/SmartScopesClient.cpp 2015-04-02 10:40:26 +0000
128@@ -472,18 +472,12 @@
129 headers.push_back(std::make_pair("User-Agent", user_agent_hdr));
130 }
131
132- auto reponse_mutex = std::make_shared<std::mutex>();
133- query_results_[search_id] = http_client_->get(search_uri.str(), [this, handler, reponse_mutex](std::string const& line_data)
134+ query_results_[search_id] = http_client_->get(search_uri.str(), [this, handler](std::string const& chunk)
135 {
136- std::lock_guard<std::mutex> lock(*reponse_mutex);
137- try
138- {
139- parse_line(line_data, handler);
140- }
141- catch (std::exception const &e)
142- {
143- BOOST_LOG_SEV(logger_, Logger::Error) << "SmartScopesClient.search(): Failed to parse: " << e.what();
144- }
145+ handle_chunk(chunk, [this, handler](const std::string& line)
146+ {
147+ handle_line(line, handler);
148+ });
149 }, headers);
150
151 return SearchHandle::UPtr(new SearchHandle(search_id, shared_from_this()));
152@@ -540,24 +534,42 @@
153
154 BOOST_LOG_SEV(logger_, Logger::Info) << "SmartScopesClient.preview(): GET " << preview_uri.str();
155
156- auto reponse_mutex = std::make_shared<std::mutex>();
157- query_results_[preview_id] = http_client_->get(preview_uri.str(), [this, handler, reponse_mutex](std::string const& line_data)
158+ query_results_[preview_id] = http_client_->get(preview_uri.str(), [this, handler](std::string const& chunk)
159 {
160- std::lock_guard<std::mutex> lock(*reponse_mutex);
161- try
162- {
163- parse_line(line_data, handler);
164- }
165- catch (std::exception const &e)
166- {
167- BOOST_LOG_SEV(logger_, Logger::Error) << "SmartScopesClient.preview(): Failed to parse: " << e.what();
168- }
169+ handle_chunk(chunk, [this, handler](const std::string& line)
170+ {
171+ handle_line(line, handler);
172+ });
173 }, headers);
174
175 return PreviewHandle::UPtr(new PreviewHandle(preview_id, shared_from_this()));
176 }
177
178-void SmartScopesClient::parse_line(std::string const& json, PreviewReplyHandler const& handler)
179+void SmartScopesClient::handle_chunk(const std::string& chunk, std::function<void(const std::string&)> line_handler)
180+{
181+ // According to the docs, we expect:
182+ // The response will have Content-Type
183+ // application/json, it will be a chunked response, in practice a series of
184+ // “\r\n” delimited lines, each containing one JSON object, with the
185+ // possible forms, matching what currently can be pushed into a reply in the
186+ // new scopes API
187+ static constexpr const char separator{'\n'};
188+
189+ std::istringstream ss{chunk}; std::string line;
190+ while (std::getline(ss, line, separator))
191+ {
192+ try
193+ {
194+ line_handler(line);
195+ }
196+ catch (std::exception const &e)
197+ {
198+ BOOST_LOG_SEV(logger_, Logger::Error) << "SmartScopesClient.handle_chunk(): Failed to parse line: " << e.what();
199+ }
200+ }
201+}
202+
203+void SmartScopesClient::handle_line(std::string const& json, PreviewReplyHandler const& handler)
204 {
205 JsonNodeInterface::SPtr root_node;
206 JsonNodeInterface::SPtr child_node;
207@@ -605,7 +617,7 @@
208 }
209 }
210
211-void SmartScopesClient::parse_line(std::string const& json, SearchReplyHandler const& handler)
212+void SmartScopesClient::handle_line(std::string const& json, SearchReplyHandler const& handler)
213 {
214 JsonNodeInterface::SPtr root_node;
215 JsonNodeInterface::SPtr child_node;
216@@ -667,7 +679,7 @@
217 }
218 }
219 handler.result_handler(result);
220- }
221+ }
222 else if (root_node->has_node("departments"))
223 {
224 auto departments = parse_departments(root_node->get_node("departments"));
225@@ -720,6 +732,7 @@
226 std::shared_ptr<DepartmentInfo> SmartScopesClient::parse_departments(JsonNodeInterface::SPtr node)
227 {
228 static std::array<std::string, 2> const mandatory = { { "label", "canned_query" } };
229+
230 for (auto const& field : mandatory)
231 {
232 if (!node->has_node(field))

Subscribers

People subscribed via source and target branches

to all changes: