-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathREADME
656 lines (478 loc) · 19.5 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
What is SObjectizer?
====================
SObjectizer is one of a few cross-platform and OpenSource "actor frameworks"
for C++. But SObjectizer supports not only Actor Model, but also
Publish-Subscribe Model and CSP-like channels. The goal of SObjectizer is
significant simplification of development of concurrent and multithreaded
applications in C++.
SObjectizer allows the creation of a concurrent app as a set of agent-objects
which interact with each other through asynchronous messages. It handles
message dispatching and provides a working context for message processing. And
allows to tune those things by supplying various ready-to-use dispatchers.
What distinguishes SObjectizer?
===============================
Maturity. SObjectizer is based on ideas that have been put forward in
1995-2000. And SObjectizer itself is being developed since 2002. SObjectizer-5
is continuously evolved since 2010.
Stability. From the very beginning SObjectizer was used for business-critical
applications, and some of them are still being used in production. Breaking
changes in SObjectizer are rare and we approach to them very carefully.
Cross-platform. SObjectizer runs on Windows, Linux, FreeBSD, macOS and Android.
Easy-to-use. SObjectizer provides easy to understand and easy to use API with a
lot of examples in the SObjectizer's distributive and a plenty of information
in the project's Wiki.
Free. SObjectizer is distributed under BSD-3-CLAUSE license, so it can be used
in development of proprietary commercial software for free.
Show me the code!
=================
HelloWorld example
------------------
This is a classical example "Hello, World" expressed by using SObjectizer's
agents:
#include <so_5/all.hpp>
class hello_actor final : public so_5::agent_t {
public:
using so_5::agent_t::agent_t;
void so_evt_start() override {
std::cout << "Hello, World!" << std::endl;
// Finish work of example.
so_deregister_agent_coop_normally();
}
};
int main() {
// Launch SObjectizer.
so_5::launch([](so_5::environment_t & env) {
// Add a hello_actor instance in a new cooperation.
env.introduce_coop([](so_5::coop_t & coop) {
coop.make_agent<hello_actor>();
});
});
return 0;
}
Ping-Pong example
-----------------
Let's look at more interesting example with two agents and message exchange
between them. It is another famous example for actor frameworks, "Ping-Pong":
#include <so_5/all.hpp>
struct ping {
int counter_;
};
struct pong {
int counter_;
};
class pinger final : public so_5::agent_t {
so_5::mbox_t ponger_;
void on_pong(mhood_t<pong> cmd) {
if(cmd->counter_ > 0)
so_5::send<ping>(ponger_, cmd->counter_ - 1);
else
so_deregister_agent_coop_normally();
}
public:
pinger(context_t ctx) : so_5::agent_t{std::move(ctx)} {}
void set_ponger(const so_5::mbox_t mbox) { ponger_ = mbox; }
void so_define_agent() override {
so_subscribe_self().event( &pinger::on_pong );
}
void so_evt_start() override {
so_5::send<ping>(ponger_, 1000);
}
};
class ponger final : public so_5::agent_t {
const so_5::mbox_t pinger_;
int pings_received_{};
public:
ponger(context_t ctx, so_5::mbox_t pinger)
: so_5::agent_t{std::move(ctx)}
, pinger_{std::move(pinger)}
{}
void so_define_agent() override {
so_subscribe_self().event(
[this](mhood_t<ping> cmd) {
++pings_received_;
so_5::send<pong>(pinger_, cmd->counter_);
});
}
void so_evt_finish() override {
std::cout << "pings received: " << pings_received_ << std::endl;
}
};
int main() {
so_5::launch([](so_5::environment_t & env) {
env.introduce_coop([](so_5::coop_t & coop) {
auto pinger_actor = coop.make_agent<pinger>();
auto ponger_actor = coop.make_agent<ponger>(
pinger_actor->so_direct_mbox());
pinger_actor->set_ponger(ponger_actor->so_direct_mbox());
});
});
return 0;
}
All agents in the code above are working on the same work thread. How to bind
them to different work threads?
It is very simple. Just use an appropriate dispatcher:
int main() {
so_5::launch([](so_5::environment_t & env) {
env.introduce_coop(
so_5::disp::active_obj::create_private_disp(env)->binder(),
[](so_5::coop_t & coop) {
auto pinger_actor = coop.make_agent<pinger>();
auto ponger_actor = coop.make_agent<ponger>(
pinger_actor->so_direct_mbox());
pinger_actor->set_ponger(ponger_actor->so_direct_mbox());
});
});
return 0;
}
Pub/Sub example
---------------
SObjectizer supports Pub/Sub model via multi-producer/multi-consumer message
boxes. A message sent to that message box will be received by all subscribers
of that message type:
#include <so_5/all.hpp>
using namespace std::literals;
struct acquired_value {
std::chrono::steady_clock::time_point acquired_at_;
int value_;
};
class producer final : public so_5::agent_t {
const so_5::mbox_t board_;
so_5::timer_id_t timer_;
int counter_{};
struct acquisition_time final : public so_5::signal_t {};
void on_timer(mhood_t<acquisition_time>) {
// Publish the next value for all consumers.
so_5::send<acquired_value>(
board_, std::chrono::steady_clock::now(), ++counter_);
}
public:
producer(context_t ctx, so_5::mbox_t board)
: so_5::agent_t{std::move(ctx)}
, board_{std::move(board)}
{}
void so_define_agent() override {
so_subscribe_self().event(&producer::on_timer);
}
void so_evt_start() override {
// Agent will periodically recive acquisition_time signal
// without initial delay and with period of 750ms.
timer_ = so_5::send_periodic<acquisition_time>(*this, 0ms, 750ms);
}
};
class consumer final : public so_5::agent_t {
const so_5::mbox_t board_;
const std::string name_;
void on_value(mhood_t<acquired_value> cmd) {
std::cout << name_ << ": " << cmd->value_ << std::endl;
}
public:
consumer(context_t ctx, so_5::mbox_t board, std::string name)
: so_5::agent_t{std::move(ctx)}
, board_{std::move(board)}
, name_{std::move(name)}
{}
void so_define_agent() override {
so_subscribe(board_).event(&consumer::on_value);
}
};
int main() {
so_5::launch([](so_5::environment_t & env) {
auto board = env.create_mbox();
env.introduce_coop([board](so_5::coop_t & coop) {
coop.make_agent<producer>(board);
coop.make_agent<consumer>(board, "first"s);
coop.make_agent<consumer>(board, "second"s);
});
std::this_thread::sleep_for(std::chrono::seconds(4));
env.stop();
});
return 0;
}
BlinkingLed example
-------------------
All agents in SObjectizer are finite-state machines. Almost all functionality
of hierarchical finite-states machines (HSM) are supported: child states and
handlers inheritance, on_enter/on_exit handlers, state timeouts, deep- and
shallow state history, except orthogonal states.
This is a very simple example that demonstrates an agent that is HSM:
#include <so_5/all.hpp>
using namespace std::literals;
class blinking_led final : public so_5::agent_t {
state_t off{ this }, blinking{ this },
blink_on{ initial_substate_of{ blinking } },
blink_off{ substate_of{ blinking } };
public :
struct turn_on_off : public so_5::signal_t {};
blinking_led(context_t ctx) : so_5::agent_t{std::move(ctx)} {
this >>= off;
off.just_switch_to<turn_on_off>(blinking);
blinking.just_switch_to<turn_on_off>(off);
blink_on
.on_enter([]{ std::cout << "ON" << std::endl; })
.on_exit([]{ std::cout << "off" << std::endl; })
.time_limit(1250ms, blink_off);
blink_off
.time_limit(750ms, blink_on);
}
};
int main()
{
so_5::launch([](so_5::environment_t & env) {
so_5::mbox_t m;
env.introduce_coop([&](so_5::coop_t & coop) {
auto led = coop.make_agent< blinking_led >();
m = led->so_direct_mbox();
});
const auto pause = [](auto duration) {
std::this_thread::sleep_for(duration);
};
std::cout << "Turn blinking on for 10s" << std::endl;
so_5::send<blinking_led::turn_on_off>(m);
pause(10s);
std::cout << "Turn blinking off for 5s" << std::endl;
so_5::send<blinking_led::turn_on_off>(m);
pause(5s);
std::cout << "Turn blinking on for 5s" << std::endl;
so_5::send<blinking_led::turn_on_off>(m);
pause(5s);
std::cout << "Stopping..." << std::endl;
env.stop();
} );
return 0;
}
CSP-like Ping-Pong example
--------------------------
SObjectizer allows to write concurrent applications even without agents inside.
Only plain threads and CSP-like channels can be used.
This is plain-thread implementation of Ping-Pong example (please note that
main() is not exception-safe):
#include <so_5/all.hpp>
struct ping {
int counter_;
};
struct pong {
int counter_;
};
void pinger_proc(so_5::mchain_t self_ch, so_5::mchain_t ping_ch) {
so_5::send<ping>(ping_ch, 1000);
// Read all message until channel will be closed.
so_5::receive( so_5::from(self_ch),
[&](so_5::mhood_t<pong> cmd) {
if(cmd->counter_ > 0)
so_5::send<ping>(ping_ch, cmd->counter_ - 1);
else {
// Channels have to be closed to break `receive` calls.
so_5::close_drop_content(self_ch);
so_5::close_drop_content(ping_ch);
}
});
}
void ponger_proc(so_5::mchain_t self_ch, so_5::mchain_t pong_ch) {
int pings_received{};
// Read all message until channel will be closed.
so_5::receive( so_5::from(self_ch),
[&](so_5::mhood_t<ping> cmd) {
++pings_received;
so_5::send<pong>(pong_ch, cmd->counter_);
});
std::cout << "pings received: " << pings_received << std::endl;
}
int main() {
so_5::wrapped_env_t sobj;
auto pinger_ch = so_5::create_mchain(sobj);
auto ponger_ch = so_5::create_mchain(sobj);
std::thread pinger{pinger_proc, pinger_ch, ponger_ch};
std::thread ponger{ponger_proc, ponger_ch, pinger_ch};
ponger.join();
pinger.join();
return 0;
}
Want to know more?
------------------
More information about SObjectizer can be found in the corresponding section of
the project's Wiki: https://sourceforge.net/p/sobjectizer/wiki/Basics/
Limitations
===========
SObjectizer is an in-process message dispatching framework. It doesn't support
distributed applications just out of box. But external tools and libraries can
be used in that case. Please take a look at our mosquitto_transport experiment:
https://bitbucket.org/sobjectizerteam/mosquitto_transport-0.6
Obtaining and building
======================
SObjectizer can be downloaded from SourceForge[1] as an archive or
exported/checkedout from Subversion repository.
There are two ways for building SObjectizer. The first one by using Mxx_ru[2]
tool. The second one by using CMake[3].
NOTE. Since v.5.5.15.2 there is a support of Android platform. Building for
Android is possible by CMake only. See the corresponding section below.
SObjectizer can also be installed and used via vcpkg and Conan dependency
managers. See the appropriate sections below.
Building via Mxx_ru
===================
NOTE. This is a standard way for building SObjectizer. This way is used in
SObjectizer development process.
To build SObjectizer it is necessary to use Ruby language and Mxx_ru tool.
Install Ruby and then install Mxx_ru via RubyGems command:
gem install Mxx_ru
If you already have Mxx_ru installed please update to at least version 1.6.11:
gem update Mxx_ru
SObjectizer can be obtained from Subversion repository on SourceForge:
svn export http://svn.code.sf.net/p/sobjectizer/repo/tags/so_5/5.5.24 so-5.5.24
To build SObjectizer:
cd so-5.5.24/dev
ruby build.rb
Static and shared library for SObjectizer will be built. Libraries will be
placed into target/release subdirectory.
If you want to build just shared library:
cd so-5.5.24/dev
ruby so_5/prj.rb
Or if you want to build just static library:
cd so-5.5.24/dev
ruby so_5/prj_s.rb
To build SObjectizer with all tests and samples:
cd so-5.5.24/dev
ruby build_all.rb
Please note that under FreeBSD it could be necessary to define LD_LIBRARY_PATH
environment variable. And the actual build command sequence under FreeBSD could
be as follows:
cd so-5.5.24/dev
export LD_LIBRARY_PATH=target/release
ruby build_all.rb
To build html-format documentation for SObjectizer the Doxygen tool is
necessary. If it is installed then:
cd so-5.5.24/doxygen
doxygen
Generated html-files will be located in so-5.5.24/dev/doc/html.
NOTE. If you do not specify MXX_RU_CPP_TOOLSET by youself then Mxx_ru will
try to detect your C++ toolset automatically. If you want to use C++ compiler
which is not default in your system please define MXX_RU_CPP_TOOLSET
environment variable manually. It could look like:
export MXX_RU_CPP_TOOLSET="clang_linux compiler_name=clang++-3.5 linker_name=clang++-3.5"
More information about tuning Mxx_ru for your needs you can find in the
corresponding documentation [4].
Building via CMake
==================
NOTE. This way of building is not used by SObjectizer developers. But
CMake-related files are in actual state, they maintained by SObjectizer Team
and can be used for building SObjectizer, its samples and tests.
NOTE. It is better to have a rather new version of CMake. The oldest CMake
version which has been tested is 3.2. The version 3.8 or newer is prefered.
To build SObjectizer via CMake it is necessary to have CMake and some knowledge
of how to use it. The following action is just a demonstration. For more
detailed info about cmake build system for SObjectizer see
dev/cmake/CmakeQuickHowto.txt
To get and build SObjectizer under Linux/FreeBSD in command line run:
svn export http://svn.code.sf.net/p/sobjectizer/repo/tags/so_5/5.5.24 so-5.5.24
cd so-5.5.24
mkdir cmake_build
cd cmake_build
cmake -DCMAKE_INSTALL_PREFIX=target -DCMAKE_BUILD_TYPE=Release ../dev
cmake --build . --config Release
cmake --build . --config Release --target install
Those commands will create all necessary Makefile, then build SObjectizer. If
it necessary to build examples and tests too, use
cmake -DBUILD_ALL=ON -DCMAKE_INSTALL_PREFIX=target ../dev
When 'make install' finished './target' will contain two subfolders
'./bin' with samples and './lib' with shared libso.5.x.x.so
CMake build system currently supports this options:
SOBJECTIZER_BUILD_STATIC Enable building SObjectizer as a static library [default: ON]
SOBJECTIZER_BUILD_SHARED Enable building SObjectizer as a shared library [default: ON]
BUILD_ALL Enable building examples and tests [default: OFF]
BUILD_EXAMPLES Enable building examples [default: OFF]
BUILD_TESTS Enable building tests [default: OFF]
Please note that if BUILD_ALL or BUILD_EXAMPLES or BUILD_TESTS is turned ON
then both SOBJECTIZER_BUILD_STATIC and SOBJECTIZER_BUILD_SHARED must be turned
ON. It means that if SOBJECTIZER_BUILD_STATIC or SOBJECTIZER_BUILD_SHARED is
turned OFF then BUILD_ALL/BUILD_EXAMPLES/BUILD_TESTS all must be turned OFF.
To build SObjectizer under Windows by MS Visual Studio 2013 from command line:
cd so-5.5.24
mkdir cmake_build
cd cmake_build
cmake -DCMAKE_INSTALL_PREFIX=target -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 14 2015" ../dev
cmake --build . --config Release
cmake --build . --config Release --target install
If it necessary to build examples too, use BUILD_ALL in cmake invocation:
cmake -DCMAKE_INSTALL_PREFIX=target -DCMAKE_BUILD_TYPE=Release -DBUILD_ALL=ON -G "Visual Studio 14 2015" ../dev
Since v.5.5.24 SObjectizer provides sobjectizer-config.cmake files.
These files are automatically installed into <target>/lib/cmake/sobjectizer
subfolder. It allows to use SObjectizer via CMake's find_package command.
Building for Android
--------------------
Building for Android is possible via a rather fresh Android NDK or CrystaX NDK[5].
Building with Android NDK
~~~~~~~~~~~~~~~~~~~~~~~~~
You need Android SDK and Android NDK installed in your system. As well as an
appropriate version of CMake. You have also need properly set environment
variables ANDROID_HOME, ANDROID_NDK. Then you can issue the following commands:
svn export http://svn.code.sf.net/p/sobjectizer/repo/tags/so_5/5.5.24 so-5.5.24
cd so-5.5.24
mkdir cmake_build
cd cmake_build
cmake -DBUILD_ALL -DCMAKE_INSTALL_PREFIX=target -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-G Ninja \
-DANDROID_ABI=arm64-v8a \
-DANDROID_NDK=${ANDROID_NDK} \
-DANDROID_NATIVE_API_LEVEL=23 \
-DANDROID_TOOLCHAIN=clang \
../dev
cmake --build . --config=Release
cmake --build . --config=Release --target install
Building with CrystaX NDK
~~~~~~~~~~~~~~~~~~~~~~~~~
You need CrystaX NDK v.10.4.0 or higher already installed in your system. CMake
is used for building SObjectizer:
svn export http://svn.code.sf.net/p/sobjectizer/repo/tags/so_5/5.5.24 so-5.5.24
cd so-5.5.24
mkdir cmake_build
cd cmake_build
export NDK=/path/to/the/crystax-ndk
cmake -DBUILD_ALL -DCMAKE_INSTALL_PREFIX=result -DCMAKE_TOOLCHAIN_FILE=$NDK/cmake/toolchain.cmake -DANDROID_ABI=arm64-v8a ../dev
make
make test
make install
Using C++ Dependency Managers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Using via vcpkg
---------------
To use SObjectizer via [vcpkg](https://github.com/Microsoft/vcpkg) it is necessary to do the following steps.
Install `sobjectizer` package:
vcpkg install sobjectizer
Add the following lines into your CMakeLists.txt file:
find_package(sobjectizer CONFIG REQUIRED)
target_link_libraries(your_target sobjectizer::SharedLib) # or sobjectizer::StaticLib
Using via Conan
---------------
Installing SObjectizer And Adding It To conanfile.txt
`````````````````````````````````````````````````````
To use SObjectizer via Conan it is necessary to do the following steps:
Add the corresponding remote to your conan:
conan remote add stiffstream https://api.bintray.com/conan/stiffstream/public
Add SObjectizer to `conanfile.txt` of your project:
[requires]
sobjectizer/5.5.24@stiffstream/testing
It also may be necessary to specify `shared` option for SObjectizer. For example, for build SObjectizer as a static library:
[options]
sobjectizer:shared=False
Install dependencies for your project:
conan install SOME_PATH --build=missing
Adding SObjectizer To Your CMakeLists.txt
`````````````````````````````````````````
Please note that SObjectizer should be added to your `CMakeLists.txt` via `find_package` command:
...
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(sobjectizer CONFIG REQUIRED)
...
target_link_libraries(your_target sobjectizer::SharedLib) # Or sobjectizer::StaticLib
License
=======
SObjectizer is distributed under 3-clause BSD license. For license information
please see LICENSE file.
References
==========
[1] https://sourceforge.net/projects/sobjectizer/files/sobjectizer/SObjectizer%20Core%20v.5.5/
[2] https://sourceforge.net/projects/mxxru/
[3] http://www.cmake.org/
[4] http://sourceforge.net/projects/mxxru/files/Mxx_ru%201.6/mxx_ru-1.6.4-r1.pdf/download
[5] https://www.crystax.net/android/ndk