Skip to content

Commit bafd776

Browse files
author
Tobias Oberstein
committed
sync
1 parent 10c65a6 commit bafd776

6 files changed

Lines changed: 178 additions & 11 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test_asio:
1010

1111
test_asio2:
1212
scons
13-
./build/test/test13
13+
./build/test/test14
1414

1515
test_worker:
1616
scons

‎include/autobahn.hpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ namespace autobahn {
115115
* \param in The input stream to run this session on.
116116
* \param out THe output stream to run this session on.
117117
*/
118-
session(IStream& in, OStream& out);
118+
session(boost::asio::io_service& io, IStream& in, OStream& out);
119119

120120
/*!
121121
* Join a realm with this session.
@@ -312,6 +312,8 @@ namespace autobahn {
312312

313313
bool m_stopped;
314314

315+
boost::asio::io_service& m_io;
316+
315317
/// Input stream this session runs on.
316318
IStream& m_in;
317319

@@ -322,6 +324,7 @@ namespace autobahn {
322324
char m_buffer_msg_len[4];
323325
uint32_t m_msg_len;
324326

327+
boost::promise<boost::any> m_test_promise;
325328

326329

327330
/// MsgPack serialization buffer.

‎include/autobahn.ipp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@
6161
namespace autobahn {
6262

6363
template<typename IStream, typename OStream>
64-
session<IStream, OStream>::session(IStream& in, OStream& out)
64+
session<IStream, OStream>::session(boost::asio::io_service& io, IStream& in, OStream& out)
6565
: m_debug(true),
6666
m_stopped(false),
67+
m_io(io),
6768
m_in(in),
6869
m_out(out),
6970
m_packer(&m_buffer),
@@ -119,7 +120,7 @@ namespace autobahn {
119120

120121
send();
121122

122-
return m_session_join.get_future();
123+
return std::move(m_session_join.get_future());
123124
}
124125

125126

@@ -294,7 +295,10 @@ namespace autobahn {
294295
pack_any(args);
295296
send();
296297

297-
return m_calls[m_request_id].m_res.get_future();
298+
//return m_calls[m_request_id].m_res.get_future();
299+
//return std::move(m_calls[m_request_id].m_res.get_future());
300+
//return m_calls[m_request_id].m_res.get_future();
301+
return m_test_promise.get_future();
298302

299303
} else {
300304
return call(procedure);

‎test/SConscript‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ else:
3333
# tests.append(env.Program('test11.cpp', LIBS = ['boost_thread', 'boost_system', 'msgpack', autobahn]))
3434
# tests.append(env.Program('test_asio.cpp', LIBS = ['boost_thread', 'boost_system', 'msgpack']))
3535
# tests.append(env.Program('publish.cpp', LIBS = ['boost_thread', 'boost_system', 'msgpack', autobahn]))
36-
tests.append(env.Program('test13.cpp', LIBS = ['boost_thread', 'boost_system', 'msgpack']))
36+
# tests.append(env.Program('test13.cpp', LIBS = ['boost_thread', 'boost_system', 'msgpack']))
37+
tests.append(env.Program('test14.cpp', LIBS = ['boost_thread', 'boost_system']))
3738
# tests.append(env.Program('test12.cpp', LIBS = ['boost_thread', 'boost_system', 'msgpack']))
3839
# tests.append(env.Program(['test7.cpp', '#/src/autobahn.cpp'], LIBS = ['boost_thread', 'boost_system', 'msgpack']))
3940
# tests.append(env.Program(['test7.cpp'], LIBS = ['boost_thread', 'boost_system', 'msgpack']))

‎test/test13.cpp‎

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,47 @@
1616
//
1717
///////////////////////////////////////////////////////////////////////////////
1818

19+
#if 1
20+
21+
// http://stackoverflow.com/questions/22597948/using-boostfuture-with-then-continuations/
22+
#define BOOST_THREAD_PROVIDES_FUTURE
23+
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
24+
#define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
25+
#include <boost/thread/future.hpp>
26+
27+
struct Foo {
28+
29+
boost::future<int> start() {
30+
return p.get_future();
31+
}
32+
33+
void finish() {
34+
p.set_value(23);
35+
}
36+
37+
boost::promise<int> p;
38+
};
39+
40+
41+
int main () {
42+
Foo foo;
43+
44+
auto f1 = foo.start();
45+
auto f2 = f1.then([](boost::future<int> f) {
46+
std::cout << "done:" << std::endl;
47+
std::cout << f.get() << std::endl;
48+
});
49+
50+
foo.finish();
51+
//f2.get();
52+
}
53+
54+
#else
55+
1956
#include <iostream>
2057
#include <string>
2158
#include <thread>
59+
#include <chrono>
2260
2361
#include "autobahn.hpp"
2462
@@ -29,6 +67,14 @@ using namespace boost;
2967
3068
using boost::asio::ip::tcp;
3169
70+
template<typename R>
71+
bool is_ready(future<R>& f)
72+
{ return f.wait_for(std::chrono::seconds(0)) == future_status::ready; }
73+
74+
75+
76+
// http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/examples/cpp11_examples.html
77+
3278
3379
int main () {
3480
@@ -50,7 +96,7 @@ int main () {
5096
tcp::socket socket(io);
5197
5298
autobahn::session<tcp::socket,
53-
tcp::socket> session(socket, socket);
99+
tcp::socket> session(io, socket, socket);
54100
55101
tcp::resolver resolver(io);
56102
auto endpoint_iterator = resolver.resolve({"127.0.0.1", "8080"});
@@ -64,16 +110,17 @@ int main () {
64110
cerr << "connected" << endl;
65111
session.start();
66112
67-
auto s = session.join(string("realm1")).then([&session](future<int> s) {
113+
auto s = session.join(string("realm1")).then([&](future<int> s) {
68114
cerr << "session joined" << endl;
69115
session.publish("com.myapp.topic1");
70116
71117
#if 1
72-
auto cf = session.call("com.arguments.add2", {2, 3});
73-
74-
auto c = cf.then([&session](future<any> f) {
118+
auto c = session.call("com.arguments.add2", {2, 3})
119+
.then([&](future<any> f) {
75120
76121
cerr << "call returned" << endl;
122+
//cerr << "future valid: " << f.valid() << endl;
123+
//cerr << "future ready: " << is_ready(f) << endl;
77124
78125
any r = f.get();
79126
@@ -84,6 +131,10 @@ int main () {
84131
cerr << "result: " << res << endl;
85132
});
86133
134+
cerr << "HERE2" << endl;
135+
136+
c.get();
137+
87138
//c.wait();
88139
#endif
89140
});
@@ -111,3 +162,4 @@ int main () {
111162
}
112163
return 0;
113164
}
165+
#endif

‎test/test14.cpp‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright (C) 2014 Tavendo GmbH
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
///////////////////////////////////////////////////////////////////////////////
18+
19+
#include <map>
20+
#include <memory>
21+
#include <iostream>
22+
23+
#define BOOST_THREAD_PROVIDES_FUTURE
24+
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
25+
#define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
26+
#include <boost/thread/future.hpp>
27+
#include <boost/asio.hpp>
28+
29+
using namespace std;
30+
using namespace boost;
31+
32+
33+
struct rpcsvc {
34+
35+
rpcsvc (asio::io_service& io) : m_io(io), m_call_id(0) {
36+
}
37+
38+
struct Call {
39+
std::shared_ptr<asio::deadline_timer> m_timer;
40+
promise<float> m_promise;
41+
};
42+
43+
future<float> slowsquare(float x, int delay) {
44+
45+
m_call_id += 1;
46+
m_calls[m_call_id] = Call();
47+
48+
std::shared_ptr<asio::deadline_timer>
49+
timer(new asio::deadline_timer(m_io, posix_time::seconds(delay)));
50+
51+
m_calls[m_call_id].m_timer = timer;
52+
53+
int call_id = m_call_id;
54+
55+
timer->async_wait(
56+
[=](system::error_code ec) {
57+
if (!ec) {
58+
cout << m_call_id << " - " << call_id << endl;
59+
this->m_calls[call_id].m_promise.set_value(x * x);
60+
this->m_calls.erase(call_id);
61+
} else {
62+
cout << "Error in timer: " << ec << endl;
63+
}
64+
}
65+
);
66+
67+
return m_calls[m_call_id].m_promise.get_future();
68+
}
69+
70+
asio::io_service& m_io;
71+
int m_call_id;
72+
map<int, Call> m_calls;
73+
};
74+
75+
76+
int main () {
77+
78+
try {
79+
asio::io_service io;
80+
81+
rpcsvc rpc(io);
82+
83+
auto f1 = rpc.slowsquare(2, 2).then([](future<float> f) {
84+
cout << "call 1 returned" << endl;
85+
cout << "result 1: " << f.get() << endl;
86+
});
87+
88+
auto f2 = rpc.slowsquare(3, 1).then([](future<float> f) {
89+
cout << "call 2 returned" << endl;
90+
cout << "result 2: " << f.get() << endl;
91+
});
92+
93+
auto f3 = when_all(std::move(f1), std::move(f2));
94+
auto f4 = f3.then([](decltype(f3)) {
95+
cout << "done." << endl;
96+
});
97+
98+
io.run();
99+
100+
f4.get();
101+
}
102+
catch (std::exception& e) {
103+
cerr << e.what() << endl;
104+
return 1;
105+
}
106+
return 0;
107+
}

0 commit comments

Comments
 (0)