SObjectizer-5.7 with support of send_case in select() and so5extra-1.4 relicensed under BSD-license

20 views
Skip to first unread message

Yauheni Akhotnikau

unread,
Jan 23, 2020, 6:16:09 AM1/23/20
to SObjectizer

New versions of SObjectizer and its companion project so5extra are released.


TL;DR:

  • SObjectizer-5.7 now supports the `send_case` in `select()` function. It makes SObjectizer's `select()` much more similar to Golang's `select` statement. But it breaks the compatibility with v.5.6 because old `case_` function is now renamed to `receive_case`;

  • v.5.7 of SObjectizer fixes a flaw in enveloped message delivery mechanism with respect to `transfer_to_state()` and `suppress()` functionality;

  • so5extra is now relicensed and distributed under BSD-3-CLAUSE license. Previous versions of so5extra were distributed under GNU Affero GPL v.3 and commercial licenses;

  • so5extra-1.4 now implements fixed-capacity message chains that capacity is known at the compile-time.


Speaking more lengthy the main new feature of SObjectizer-5 is the support of `send_case` in `select()` function (somewhat similar to Golang's `select` statement with sending an outgoing message in a channel). Now it is possible to do things like that:


using namespace so_5;


struct Greetings {

   std::string msg_;

};


// Try to send messages to the corresponding chains,

// but wait no more than 250ms for all the operations.

select(from_all().handle_n(3).total_time(250ms),

   send_case(chAlice,

      message_holder_t<Greetings>::make("Hello, Alice!"),

      []{ std::cout << "message sent to chAlice" << std::endl; }),

   send_case(chBob,

      message_holder_t<Greetings>::make("Hello, Bob!"),

      []{ std::cout << "message sent to chBob" << std::endl; }),

   send_case(chEve,

      message_holder_t<Greeting>::make("Hello, Eve!"),

      []{ std::cout << "message sent to chEve" << std::endl; }));


`send_case()` can be used with `receive_case()` in the same `select()`. For example, this is SObjectizer's version of calculation of Fibonacci numbers in a separate thread from Golang's tour:


using namespace std;

using namespace std::chrono_literals;

using namespace so_5;


struct quit {};


void fibonacci( mchain_t values_ch, mchain_t quit_ch )

{

   int x = 0, y = 1;

   mchain_select_result_t r;

   do

   {

      r = select(

         from_all().handle_n(1),

         // Sends a new message of type 'int' with value 'x' inside

         // when values_ch is ready for a new outgoing message.

         send_case( values_ch, message_holder_t<int>::make(x),

               [&x, &y] { // This block of code will be called after the send().

                  auto old_x = x;

                  x = y; y = old_x + y;

               } ),

         // Receive a 'quit' message from quit_ch if it is here.

         receive_case( quit_ch, [](quit){} ) );

   }

   // Continue the loop while we send something and receive nothing.

   while( r.was_sent() && !r.was_handled() );

}


int main()

{

   wrapped_env_t sobj;


   thread fibonacci_thr;

   auto thr_joiner = auto_join( fibonacci_thr );


   // The chain for the Fibonacci numbers will have limited capacity.

   auto values_ch = create_mchain( sobj, 1s, 1,

         mchain_props::memory_usage_t::preallocated,

         mchain_props::overflow_reaction_t::abort_app );


   auto quit_ch = create_mchain( sobj );

   auto ch_closer = auto_close_drop_content( values_ch, quit_ch );


   fibonacci_thr = thread{ fibonacci, values_ch, quit_ch };


   // Read the first 10 numbers from values_ch.

   receive( from( values_ch ).handle_n( 10 ),

         // And show every number to the standard output.

         []( int v ) { cout << v << endl; } );


   send< quit >( quit_ch );

}


The full release notes for SObjectizer-5.7.0 can be found here.


The main new feature of so5extra library is the new license. Since v.1.4.0 so5extra is distributed under BSD-3-CLAUSE license and all so5extra's stuff (like Asio's based dispatcher and environment infrastructures, different kinds of message boxes and so on) can now be used for free. Even in closed-source commercial projects.


The only new addition to so5extra is the implementation of the message chain that maximal capacity is known at the compile time. Usage of such message chains can be useful, for example, in request-reply scenarios where just only reply message is expected:


#include <so_5_extra/mchains/fixed_size.hpp>

#include <so_5/all.hpp>

...

using namespace so_5;


// The chain for the reply message.

auto reply_ch = extra::mchains::fixed_size::create_mchain<1>(env,

   mchain_props::overflow_reaction_t::drop_newset);

// Send a request.

send<SomeRequest>(target, ..., reply_ch, ...);

// Wait and handle the response.

receive(so_5::from(reply_ch).handle_n(1), [](const SomeReply & reply) { ... });


The full release notes for so5extra can be found here.


Reply all
Reply to author
Forward
0 new messages