Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a8ef49e

Browse files
benjchristensenlehecka
authored andcommittedApr 26, 2017
RSocket with Flowable. (#356)
* RSocket with Flowable * Update RSocket Tests to Flowable * Shorten String Capture Code
1 parent 0907520 commit a8ef49e

32 files changed

+307
-483
lines changed
 

‎CMakeLists.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ add_subdirectory(experimental/yarpl)
381381
add_library(
382382
rsocket_experimental
383383
experimental/rsocket/RSocket.h
384+
# TODO remove when ReactiveStreams all synced
385+
experimental/rsocket/OldNewBridge.h
384386
experimental/rsocket-src/RSocket.cpp
385387
experimental/rsocket/RSocketServer.h
386388
experimental/rsocket-src/RSocketServer.cpp
@@ -399,9 +401,10 @@ add_library(
399401
experimental/rsocket-src/transports/TcpConnectionAcceptor.cpp
400402
experimental/rsocket/transports/TcpConnectionFactory.h
401403
experimental/rsocket-src/transports/TcpConnectionFactory.cpp
404+
experimental/rsocket/RSocketRequestHandler.h
402405
)
403406

404-
add_dependencies(rsocket_experimental ReactiveStreams)
407+
add_dependencies(rsocket_experimental ReactiveStreams yarpl)
405408

406409
# include the experimental includes for usage
407410
target_include_directories(rsocket_experimental PUBLIC "${PROJECT_SOURCE_DIR}/experimental")
@@ -416,8 +419,6 @@ add_executable(
416419
experimental/rsocket-test/RSocketClientServerTest.cpp
417420
experimental/rsocket-test/handlers/HelloStreamRequestHandler.h
418421
experimental/rsocket-test/handlers/HelloStreamRequestHandler.cpp
419-
experimental/rsocket-test/handlers/HelloStreamSubscription.h
420-
experimental/rsocket-test/handlers/HelloStreamSubscription.cpp
421422
)
422423

423424
target_link_libraries(
@@ -459,16 +460,14 @@ target_link_libraries(
459460
add_executable(
460461
example_stream-hello-world-server
461462
examples/stream-hello-world/StreamHelloWorld_Server.cpp
462-
examples/stream-hello-world/HelloStreamSubscription.cpp
463-
examples/stream-hello-world/HelloStreamSubscription.h
464-
examples/stream-hello-world/HelloStreamRequestHandler.cpp
465-
examples/stream-hello-world/HelloStreamRequestHandler.h)
463+
)
466464

467465
target_link_libraries(
468466
example_stream-hello-world-server
469467
ReactiveSocket
470468
rsocket_experimental
471469
reactivesocket_examples_util
470+
yarpl
472471
${FOLLY_LIBRARIES}
473472
${GFLAGS_LIBRARY}
474473
${GLOG_LIBRARY}
@@ -484,6 +483,7 @@ target_link_libraries(
484483
ReactiveSocket
485484
rsocket_experimental
486485
reactivesocket_examples_util
486+
yarpl
487487
${FOLLY_LIBRARIES}
488488
${GFLAGS_LIBRARY}
489489
${GLOG_LIBRARY}
@@ -496,15 +496,16 @@ add_executable(
496496
examples/conditional-request-handling/ConditionalRequestHandling_Server.cpp
497497
examples/conditional-request-handling/TextRequestHandler.h
498498
examples/conditional-request-handling/TextRequestHandler.cpp
499-
examples/conditional-request-handling/ConditionalRequestSubscription.h
500-
examples/conditional-request-handling/ConditionalRequestSubscription.cpp
501-
examples/conditional-request-handling/JsonRequestHandler.cpp examples/conditional-request-handling/JsonRequestHandler.h)
499+
examples/conditional-request-handling/JsonRequestHandler.cpp
500+
examples/conditional-request-handling/JsonRequestHandler.h
501+
)
502502

503503
target_link_libraries(
504504
example_conditional-request-handling-server
505505
ReactiveSocket
506506
rsocket_experimental
507507
reactivesocket_examples_util
508+
yarpl
508509
${FOLLY_LIBRARIES}
509510
${GFLAGS_LIBRARY}
510511
${GLOG_LIBRARY}
@@ -520,6 +521,7 @@ target_link_libraries(
520521
ReactiveSocket
521522
rsocket_experimental
522523
reactivesocket_examples_util
524+
yarpl
523525
${FOLLY_LIBRARIES}
524526
${GFLAGS_LIBRARY}
525527
${GLOG_LIBRARY}

‎examples/conditional-request-handling/ConditionalRequestHandling_Client.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
// Copyright 2004-present Facebook. All Rights Reserved.
22

3+
#include <folly/init/Init.h>
34
#include <folly/io/async/ScopedEventBaseThread.h>
45
#include <iostream>
56
#include "examples/util/ExampleSubscriber.h"
67
#include "rsocket/RSocket.h"
78
#include "rsocket/transports/TcpConnectionFactory.h"
9+
#include "yarpl/v/Flowable.h"
10+
#include "yarpl/v/Subscriber.h"
811

912
using namespace ::reactivesocket;
1013
using namespace ::folly;
1114
using namespace ::rsocket_example;
1215
using namespace ::rsocket;
16+
using namespace yarpl;
1317

1418
DEFINE_string(host, "localhost", "host to connect to");
1519
DEFINE_int32(port, 9898, "host:port to connect to");
1620

1721
int main(int argc, char* argv[]) {
1822
FLAGS_logtostderr = true;
1923
FLAGS_minloglevel = 0;
20-
google::ParseCommandLineFlags(&argc, &argv, true);
21-
google::InitGoogleLogging(argv[0]);
22-
google::InstallFailureSignalHandler();
24+
folly::init(&argc, &argv);
2325

2426
auto rsf = RSocket::createClient(
2527
TcpConnectionFactory::create(FLAGS_host, FLAGS_port));
2628
auto rs = rsf->connect().get();
2729

2830
LOG(INFO) << "------------------ Hello Bob!";
29-
auto s1 = std::make_shared<ExampleSubscriber>(5, 6);
30-
rs->requestStream(Payload("Bob"), s1);
31+
auto s1 = yarpl::Reference<ExampleSubscriber>(new ExampleSubscriber(5, 6));
32+
rs->requestStream(Payload("Bob"))
33+
->take(5)
34+
->subscribe(yarpl::Reference<yarpl::Subscriber<Payload>>(s1.get()));
3135
s1->awaitTerminalEvent();
3236

3337
LOG(INFO) << "------------------ Hello Jane!";
34-
auto s2 = std::make_shared<ExampleSubscriber>(5, 6);
35-
rs->requestStream(Payload("Jane"), s2);
38+
auto s2 = yarpl::Reference<ExampleSubscriber>(new ExampleSubscriber(5, 6));
39+
rs->requestStream(Payload("Jane"))
40+
->take(3)
41+
->subscribe(yarpl::Reference<yarpl::Subscriber<Payload>>(s2.get()));
3642
s2->awaitTerminalEvent();
3743

3844
// TODO on shutdown the destruction of

‎examples/conditional-request-handling/ConditionalRequestHandling_Server.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Copyright 2004-present Facebook. All Rights Reserved.
22

33
#include <iostream>
4-
#include "JsonRequestHandler.h"
5-
#include "TextRequestHandler.h"
4+
#include <folly/init/Init.h>
65
#include "rsocket/RSocket.h"
76
#include "rsocket/RSocketErrors.h"
87
#include "rsocket/transports/TcpConnectionAcceptor.h"
8+
#include "JsonRequestHandler.h"
9+
#include "TextRequestHandler.h"
910

1011
using namespace ::reactivesocket;
1112
using namespace ::folly;
@@ -33,7 +34,7 @@ int main(int argc, char* argv[]) {
3334
// start accepting connections
3435
rs->startAndPark(
3536
[textHandler, jsonHandler](std::unique_ptr<ConnectionSetupRequest> r)
36-
-> std::shared_ptr<RequestHandler> {
37+
-> std::shared_ptr<RSocketRequestHandler> {
3738
if (r->getDataMimeType() == "text/plain") {
3839
LOG(INFO) << "Connection Request => text/plain MimeType";
3940
return textHandler;

‎examples/conditional-request-handling/ConditionalRequestSubscription.cpp

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎examples/conditional-request-handling/ConditionalRequestSubscription.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎examples/conditional-request-handling/JsonRequestHandler.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,26 @@
22

33
#include "JsonRequestHandler.h"
44
#include <string>
5-
#include "ConditionalRequestSubscription.h"
5+
#include "yarpl/v/Flowable.h"
6+
#include "yarpl/v/Flowables.h"
67

7-
using namespace ::reactivesocket;
8+
using namespace reactivesocket;
9+
using namespace rsocket;
10+
using namespace yarpl;
811

912
/// Handles a new inbound Stream requested by the other end.
10-
void JsonRequestHandler::handleRequestStream(
11-
Payload request,
12-
StreamId streamId,
13-
const std::shared_ptr<Subscriber<Payload>>& response) noexcept {
13+
yarpl::Reference<yarpl::Flowable<reactivesocket::Payload>>
14+
JsonRequestHandler::handleRequestStream(Payload request, StreamId streamId) {
1415
LOG(INFO) << "JsonRequestHandler.handleRequestStream " << request;
1516

1617
// string from payload data
17-
auto pds = request.moveDataToString();
18-
auto requestString = std::string(pds, request.data->length());
18+
auto requestString = request.moveDataToString();
1919

20-
response->onSubscribe(std::make_shared<ConditionalRequestSubscription>(
21-
response, requestString, 10));
22-
}
23-
24-
std::shared_ptr<StreamState> JsonRequestHandler::handleSetupPayload(
25-
ReactiveSocket& socket,
26-
ConnectionSetupPayload request) noexcept {
27-
LOG(INFO) << "JsonRequestHandler.handleSetupPayload " << request;
28-
// TODO what should this do?
29-
return nullptr;
20+
return Flowables::range(1, 100)->map([name = std::move(requestString)](
21+
int64_t v) {
22+
std::stringstream ss;
23+
ss << "Hello (should be JSON) " << name << " " << v << "!";
24+
std::string s = ss.str();
25+
return Payload(s, "metadata");
26+
});
3027
}

‎examples/conditional-request-handling/JsonRequestHandler.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@
22

33
#pragma once
44

5-
#include <folly/ExceptionWrapper.h>
6-
#include "src/NullRequestHandler.h"
75
#include "src/Payload.h"
8-
#include "src/ReactiveStreamsCompat.h"
9-
#include "src/ReactiveSocket.h"
10-
#include "src/SubscriptionBase.h"
6+
#include "rsocket/RSocket.h"
117

12-
class JsonRequestHandler : public reactivesocket::DefaultRequestHandler {
8+
class JsonRequestHandler : public rsocket::RSocketRequestHandler {
139
public:
1410
/// Handles a new inbound Stream requested by the other end.
15-
void handleRequestStream(
16-
reactivesocket::Payload request,
17-
reactivesocket::StreamId streamId,
18-
const std::shared_ptr<
19-
reactivesocket::Subscriber<reactivesocket::Payload>>&
20-
response) noexcept override;
21-
22-
std::shared_ptr<reactivesocket::StreamState> handleSetupPayload(
23-
reactivesocket::ReactiveSocket&,
24-
reactivesocket::ConnectionSetupPayload request) noexcept override;
11+
yarpl::Reference<yarpl::Flowable<reactivesocket::Payload>>
12+
handleRequestStream(
13+
reactivesocket::Payload request,
14+
reactivesocket::StreamId streamId) override;
2515
};

‎examples/conditional-request-handling/TextRequestHandler.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,26 @@
22

33
#include "TextRequestHandler.h"
44
#include <string>
5-
#include "ConditionalRequestSubscription.h"
5+
#include "yarpl/v/Flowable.h"
6+
#include "yarpl/v/Flowables.h"
67

7-
using namespace ::reactivesocket;
8+
using namespace reactivesocket;
9+
using namespace rsocket;
10+
using namespace yarpl;
811

912
/// Handles a new inbound Stream requested by the other end.
10-
void TextRequestHandler::handleRequestStream(
11-
Payload request,
12-
StreamId streamId,
13-
const std::shared_ptr<Subscriber<Payload>>& response) noexcept {
13+
yarpl::Reference<yarpl::Flowable<reactivesocket::Payload>>
14+
TextRequestHandler::handleRequestStream(Payload request, StreamId streamId) {
1415
LOG(INFO) << "TextRequestHandler.handleRequestStream " << request;
1516

1617
// string from payload data
17-
auto pds = request.moveDataToString();
18-
auto requestString = std::string(pds, request.data->length());
18+
auto requestString = request.moveDataToString();
1919

20-
response->onSubscribe(std::make_shared<ConditionalRequestSubscription>(
21-
response, requestString, 10));
22-
}
23-
24-
std::shared_ptr<StreamState> TextRequestHandler::handleSetupPayload(
25-
ReactiveSocket& socket,
26-
ConnectionSetupPayload request) noexcept {
27-
LOG(INFO) << "TextRequestHandler.handleSetupPayload " << request;
28-
// TODO what should this do?
29-
return nullptr;
20+
return Flowables::range(1, 100)->map([name = std::move(requestString)](
21+
int64_t v) {
22+
std::stringstream ss;
23+
ss << "Hello " << name << " " << v << "!";
24+
std::string s = ss.str();
25+
return Payload(s, "metadata");
26+
});
3027
}

0 commit comments

Comments
 (0)
Please sign in to comment.