Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
1c74c220
Commit
1c74c220
authored
6 years ago
by
Adrien Béraud
Committed by
Sébastien Blin
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
proxy server: cache json StreamBuilder
parent
9319bec7
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/opendht/dht_proxy_server.h
+1
-0
1 addition, 0 deletions
include/opendht/dht_proxy_server.h
src/dht_proxy_server.cpp
+10
-22
10 additions, 22 deletions
src/dht_proxy_server.cpp
with
11 additions
and
22 deletions
include/opendht/dht_proxy_server.h
+
1
−
0
View file @
1c74c220
...
...
@@ -271,6 +271,7 @@ private:
std
::
thread
server_thread
{};
std
::
unique_ptr
<
restbed
::
Service
>
service_
;
std
::
shared_ptr
<
DhtRunner
>
dht_
;
Json
::
StreamWriterBuilder
jsonBuilder_
;
std
::
mutex
schedulerLock_
;
std
::
condition_variable
schedulerCv_
;
...
...
This diff is collapsed.
Click to expand it.
src/dht_proxy_server.cpp
+
10
−
22
View file @
1c74c220
...
...
@@ -67,6 +67,9 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port ,
#endif
}
jsonBuilder_
[
"commentStyle"
]
=
"None"
;
jsonBuilder_
[
"indentation"
]
=
""
;
server_thread
=
std
::
thread
([
this
,
port
]()
{
// Create endpoints
auto
resource
=
std
::
make_shared
<
restbed
::
Resource
>
();
...
...
@@ -222,10 +225,7 @@ DhtProxyServer::getNodeInfo(const Sp<restbed::Session>& session) const
result
=
nodeInfo_
.
toJson
();
}
result
[
"public_ip"
]
=
s
->
get_origin
();
// [ipv6:ipv4]:port or ipv4:port
Json
::
StreamWriterBuilder
wbuilder
;
wbuilder
[
"commentStyle"
]
=
"None"
;
wbuilder
[
"indentation"
]
=
""
;
auto
output
=
Json
::
writeString
(
wbuilder
,
result
)
+
"
\n
"
;
auto
output
=
Json
::
writeString
(
jsonBuilder_
,
result
)
+
"
\n
"
;
s
->
close
(
restbed
::
OK
,
output
);
}
else
...
...
@@ -249,10 +249,7 @@ DhtProxyServer::getStats(const Sp<restbed::Session>& session) const
try
{
if
(
dht_
)
{
#ifdef OPENDHT_JSONCPP
Json
::
StreamWriterBuilder
wbuilder
;
wbuilder
[
"commentStyle"
]
=
"None"
;
wbuilder
[
"indentation"
]
=
""
;
auto
output
=
Json
::
writeString
(
wbuilder
,
stats_
.
toJson
())
+
"
\n
"
;
auto
output
=
Json
::
writeString
(
jsonBuilder_
,
stats_
.
toJson
())
+
"
\n
"
;
s
->
close
(
restbed
::
OK
,
output
);
#else
s
->
close
(
restbed
::
NotFound
,
"{
\"
err
\"
:
\"
JSON not enabled on this instance
\"
}"
);
...
...
@@ -284,13 +281,10 @@ DhtProxyServer::get(const Sp<restbed::Session>& session) const
infoHash
=
InfoHash
::
get
(
hash
);
}
s
->
yield
(
restbed
::
OK
,
""
,
[
=
](
const
Sp
<
restbed
::
Session
>&
)
{});
dht_
->
get
(
infoHash
,
[
s
](
const
Sp
<
Value
>&
value
)
{
dht_
->
get
(
infoHash
,
[
this
,
s
](
const
Sp
<
Value
>&
value
)
{
if
(
s
->
is_closed
())
return
false
;
// Send values as soon as we get them
Json
::
StreamWriterBuilder
wbuilder
;
wbuilder
[
"commentStyle"
]
=
"None"
;
wbuilder
[
"indentation"
]
=
""
;
auto
output
=
Json
::
writeString
(
wbuilder
,
value
->
toJson
())
+
"
\n
"
;
auto
output
=
Json
::
writeString
(
jsonBuilder_
,
value
->
toJson
())
+
"
\n
"
;
s
->
yield
(
output
,
[](
const
Sp
<
restbed
::
Session
>&
/*session*/
){
});
return
true
;
},
[
s
](
bool
/*ok* */
)
{
...
...
@@ -338,19 +332,16 @@ DhtProxyServer::listen(const Sp<restbed::Session>& session)
// cache the session to avoid an incrementation of the shared_ptr's counter
// else, the session->close() will not close the socket.
auto
cacheSession
=
std
::
weak_ptr
<
restbed
::
Session
>
(
s
);
listener
.
token
=
dht_
->
listen
(
infoHash
,
[
cacheSession
](
const
std
::
vector
<
Sp
<
Value
>>&
values
,
bool
expired
)
{
listener
.
token
=
dht_
->
listen
(
infoHash
,
[
this
,
cacheSession
](
const
std
::
vector
<
Sp
<
Value
>>&
values
,
bool
expired
)
{
auto
s
=
cacheSession
.
lock
();
if
(
!
s
)
return
false
;
// Send values as soon as we get them
if
(
!
s
->
is_closed
())
{
Json
::
StreamWriterBuilder
wbuilder
;
wbuilder
[
"commentStyle"
]
=
"None"
;
wbuilder
[
"indentation"
]
=
""
;
for
(
const
auto
&
value
:
values
)
{
auto
val
=
value
->
toJson
();
if
(
expired
)
val
[
"expired"
]
=
true
;
auto
output
=
Json
::
writeString
(
wb
uilder
,
val
)
+
"
\n
"
;
auto
output
=
Json
::
writeString
(
jsonB
uilder
_
,
val
)
+
"
\n
"
;
s
->
yield
(
output
,
[](
const
Sp
<
restbed
::
Session
>&
){
});
}
}
...
...
@@ -440,10 +431,7 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session)
if
(
s
->
is_closed
())
return
false
;
// Send values as soon as we get them
Json
::
StreamWriterBuilder
wbuilder
;
wbuilder
[
"commentStyle"
]
=
"None"
;
wbuilder
[
"indentation"
]
=
""
;
auto
output
=
Json
::
writeString
(
wbuilder
,
value
->
toJson
())
+
"
\n
"
;
auto
output
=
Json
::
writeString
(
jsonBuilder_
,
value
->
toJson
())
+
"
\n
"
;
s
->
yield
(
output
,
[](
const
Sp
<
restbed
::
Session
>
&
/*session*/
)
{});
return
true
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment