SlideShare a Scribd company logo
1 of 40
Download to read offline
Dive into SObjectizer-5.5
SObjectizer Team, Jan 2016
Sixth Part: Synchronous Interaction
(at v.5.5.15)
This is the next part of the series of presentations with deep
introduction into features of SObjectizer-5.5.
This part is dedicated to synchronous interactions between
agents.
SObjectizer Team, Jan 2016
Introduction
SObjectizer Team, Jan 2016
A classical way of interaction between agents is
asynchronous messages.
Asynchronous interaction is simple, flexible and scalable. It
also could prevent some errors like deadlocks (but could
also lead to other kind of errors).
However, sometimes asynchronous interaction makes code
more complex and requires a lot of programmers work...
SObjectizer Team, Jan 2016
For example: sometimes an agent C must send a request to
an agent S and receive back a response with some
important information. The agent C can continue its work
only after arrival of information from agent S.
How we can express that scenario via asynchronous
messages?
SObjectizer Team, Jan 2016
We need a request message which must contain a mbox for
the response (since v.5.5.9 any struct/class which is
MoveConstructible can be used as a message):
struct get_config {
so_5::mbox_t reply_to;
};
We need a response which will be sent back:
struct config {
... // Some fields.
config(...); // Some constructor...
};
SObjectizer Team, Jan 2016
A skeleton for agent S is quite simple:
class config_manager : public so_5::agent_t {
...
public :
virtual void so_define_agent() override {
so_subscribe_self().event( &config_manager::evt_get_config );
... // Other subscriptions...
}
private :
void evt_get_config( const get_config & request ) {
// Sending a reply back.
so_5::send< config >( request.reply_to, ... /* some args */ );
}
};
SObjectizer Team, Jan 2016
But a skeleton of agent C is more complex:
class client : public so_5::agent_t {
// A special state to wait a response.
const so_5::state_t st_wait_config{ this, "wait_config" };
public :
virtual void so_define_agent() override {
// Agent should start its work in a special state.
this >>= st_wait_config;
// Reply with config data will be handled in that state.
st_wait_config.event( &client::evt_config );
...
}
virtual void so_evt_start() override {
// Request a config at start.
so_5::send< get_config >( config_manager_mbox, so_direct_mbox() );
}
private :
void evt_config( const config & cfg ) { ... /* Handling of configuration. */ }
};
SObjectizer Team, Jan 2016
It is a lot of code for such simple operation.
But this code is not robust enough.
For example, there will be no handling of the cases when
get_config request or config response are lost somewhere...
SObjectizer Team, Jan 2016
Synchronous interaction can help here.
An agent C can issue a service request to agent S and
receive the result of that request synchronously.
SObjectizer Team, Jan 2016
Let’s rewrite our small example with service request instead
of asynchronous messages...
SObjectizer Team, Jan 2016
A slight modification for request type. There is no need to
pass reply_to mbox in a request now. So, get_config
becomes a signal:
struct get_config : public so_5::signal_t {};
Response will remain the same:
struct config {
... // Some fields.
config(...); // Some constructors...
};
SObjectizer Team, Jan 2016
Service request processor will return config as a result of
event handler:
class config_manager : public so_5::agent_t {
...
public :
virtual void so_define_agent() override {
so_subscribe_self().event< get_config >( &config_manager::evt_get_config );
... // Other subscriptions...
}
private :
config evt_get_config() {
// Returning a reply back.
return config{ ... /* some args */ };
}
};
SObjectizer Team, Jan 2016
Service request issuer will look like:
class client : public so_5::agent_t {
public :
virtual void so_define_agent() override {
... // No special state, no subscription for config response.
}
virtual void so_evt_start() override {
// Request a config at start.
// Wait response for 1 second.
auto cfg = so_5::request_value< config, get_config >(
config_manager_mbox, std::chrono::seconds(1) );
}
...
};
SObjectizer Team, Jan 2016
That code is not only much simpler ‒ it is also more robust:
● if there is no service request processor behind
config_manager_mbox there will be an exception;
● if there are more than one service request processor behind
config_manager_mbox there will be an exception;
● if config_manager won’t process a request (request is ignored in the
current state of manager) there will be an exception;
● if config_manager can’t process a request, e.g. throw an exception,
that exception will be propagated to service request's issuer;
● if config_manager can’t process a request in the specified time slice
there will be an exception.
SObjectizer Team, Jan 2016
It means that in several cases synchronous interaction via
service requests is more appropriate than asynchronous
interaction.
In such cases service requests allow to write more simple,
clean and robust code…
...but everything has its price.
SObjectizer Team, Jan 2016
The main disadvantage of service request is a possibility of
deadlocks.
Service request’s issuer and processor must work on
different working threads.
It means that issuer and processor must be bound to
different dispatchers. Or, to the different working threads
inside the same dispatcher.
SObjectizer Team, Jan 2016
If an issuer and a processor work on the same working
thread there will be a deadlock.
SObjectizer doesn’t check that. A user is responsible for
binding issuer and processor to the different contexts.
SObjectizer Team, Jan 2016
But the case when an issuer and a processor are working on
the same thread is the simplest case of deadlock.
There could be more complex cases:
● agent A calls agent B;
● agent B calls agent C;
● agent C calls agent D;
● agent D calls agent A.
It is another kind of classical deadlock with the same
consequences.
SObjectizer Team, Jan 2016
Another disadvantage of service request is a blocking of a
working thread for some time.
If a service request issuer shares the working thread with
different agents (for example, all of them are bound to
one_thread dispatcher instance) then all other agents on that
thread will wait until the service request is completed.
It means that synchronous agents interaction is not very
scalable.
SObjectizer Team, Jan 2016
As shown above, the synchronous agents interaction has
significant disadvantages. Based on that, it should be used
with care.
SObjectizer Team, Jan 2016
How Does It Work?
SObjectizer Team, Jan 2016
There is no any magic behind service requests.
Just an ordinary asynchronous messages and some help
from std::promise and std::future...
SObjectizer Team, Jan 2016
When a service request is initiated a special envelope with
service requests params inside is sent as an ordinary
message to service request processor.
This envelope contains not only request params but also a
std::promise object for the response. A value for that
promise is set on processor’s side.
A service request issuer waits on std::future object which is
bound with std::promise from the envelope which was sent.
SObjectizer Team, Jan 2016
It means that so_5::request_value call in form:
auto r = so_5::request_value<Result,Request>(mbox, timeout, params);
It is a shorthand for something like that:
// Envelope will contain Request object and std::promise<Result> object.
auto env__ = std::make_unique<msg_service_request_t<Result,Request>>(params);
// Get the future to wait on it.
std::future<Result> f__ = env__.m_promise.get_future();
// Sending the envelope with request params as async message.
mbox->deliver_message(std::move(env__));
// Waiting and handling the result.
auto wait_result__ = f__.wait_for(timeout);
if(std::future_status::ready != wait_result__)
throw exception_t(...);
auto r = f__.get();
SObjectizer Team, Jan 2016
Every service request handler in the following form
Result event_handler(const Request & svc_req ) { ... }
is automatically transformed to something like this:
void actual_event_handler(msg_service_request_t<Result,Request> & m) {
try {
m.m_promise.set( event_handler(m.query_param()) );
}
catch(...) {
m.set_exception(std::current_exception());
}
}
This transformation is performed during subscription of
event_handler.
SObjectizer Team, Jan 2016
This approach of supporting synchronous interaction means
that service request is handled by SObjectizer just like
dispatching and handling of ordinary message.
As a consequence, a service request handler even doesn’t
know is a message it handles is a part of a service request
or it was sent as a asynchronous message?
Only the service request issuer knows the difference.
SObjectizer Team, Jan 2016
It means that a config_manager from the example above will
work in the same way even if get_config signal is sent via
so_5::send() function like any other async message.
But in this case the return value of config_manager::
evt_get_config will be discarded.
It means that there is no special rules for writing service
request handlers: they are just agents with traditional event
handlers. Except one important aspect...
SObjectizer Team, Jan 2016
This important aspect is exception handling.
If an agent allows an exception to go out from an event
handler then traditional reaction to unhandled exception will
be initiated:
● SObjectizer Environment will call so_exception_reaction() for that
agent (and may be for coop of that agent and so on);
● appropriate action will be taken (for example, an exception could
be ignored or the whole application will be aborted).
SObjectizer Team, Jan 2016
But if an exception is going out from service request handler
then it will be intercepted and returned back to service
request issuer via std::promise/std::future pair.
It means that this exception will be reraised on service
request issuer side during the call to std::future::get()
method. E.g. that exception will be thrown out from
request_value().
SObjectizer Team, Jan 2016
request_value and request_future functions
SObjectizer Team, Jan 2016
There are several ways of initiating service requests.
The simplest one is the usage of so_5::request_value()
template function. It can be used in the form:
Result r = so_5::request_value<Result, Request>(Target, Timeout [,Args]);
Where Timeout is a value which is defined by std::chrono or
so_5::infinite_wait for non-limited waiting of the result.
Type of Request can be any message or signal type. All
Args (if any) will be passed to the constructor of Request.
SObjectizer Team, Jan 2016
Examples of request_value invocations:
// Sending a get_status signal as service request.
auto v = so_5::request_value<engine_status, get_status>(engine,
// Infinite waiting for the response.
so_5::infinite_wait); // No other params because of signal.
// Sending a message convert_value as a service request.
auto v = so_5::request_value<std::string, convert_value>(converter,
// Waiting for 200ms for the response.
std::chrono::milliseconds(200),
"feets", 33000, "metres" ); // Params for the constructor of convert_value.
SObjectizer Team, Jan 2016
There is also so_5::request_future() template function. It
returns std::future<Result> object:
std::future<Result> r = so_5::request_future<Result, Request>(Target [,Args]);
There is no Timeout argument. Waiting on the std::future is
responsibility of a programmer.
As in the case of request_value type of Request can be any
of message or signal type. All Args (if any) will be passed to
the constructor of Request.
SObjectizer Team, Jan 2016
request_future can be used in more complex scenarios than
request_value. For example, it is possible to issue several
service requests, do some actions and only then request
responses:
auto lights = so_5::request_future<light_status, turn_light_on>(light_controller);
auto heating = so_5::request_future<heating_status, turn_heating_on>(heating_controller);
auto accessories = so_5::request_future<accessories_status, update>(accessories_controller);
... // Some other actions.
if(light_status::on != lights.get())
... // Some reaction...
if(heating_status::on != heating.get())
... // Some reaction...
check_accessories(accessories.get());
...
SObjectizer Team, Jan 2016
There could be more complex scenarios:
class accessories_listener : public so_5::agent_t {
public :
...
virtual void so_evt_start() override {
m_status = so_5::request_future< accessories_status, get_status >(accessories_controller());
// Service request initiated, but the response will be used later.
so_5::send_delayed< check_status >(*this, std::chrono::milliseconds(250));
}
private :
std::future<accessories_status> m_status;
...
void evt_check_status() {
auto status = m_status.get(); // Getting the service request response.
... // Processing it.
// Initiate new request again.
m_status = so_5::request_future< accessories_status, get_status >(accessories_controller());
// Response is expected to be ready on the next timer event
so_5::send_delayed< check_status >(*this, std::chrono::milliseconds(250));
}
};
SObjectizer Team, Jan 2016
Service Requests With void As Result Type
SObjectizer Team, Jan 2016
Sometimes there is some sense in initiating a service
request which returns void.
A result of such service request means that processing of a
request is completely finished. Sometimes, is it a very useful
information.
For example, if service request processor does flushing of
some important data, finishing transactions, invalidating
caches and so on...
SObjectizer Team, Jan 2016
An example of service request with void result:
// Type of agent which implements an intermediate buffer with flushing by demand or by timer.
class data_buffer : public so_5::agent_t {
public :
// Signal for flushing the data.
struct flush : public so_5::signal_t {};
...
private :
// Event which is bound to flush signal.
void evt_flush() {
... // Storing the data to the disk/database/cloud...
}
};
// Initiating flush operation as a service request. Return from request_value
// means the completeness of data flushing.
so_5::request_value<void, data_buffer::flush>(buffer, so_5::infinite_wait);
SObjectizer Team, Jan 2016
Additional Information:
Project’s home: http://sourceforge.net/projects/sobjectizer
Documentation: http://sourceforge.net/p/sobjectizer/wiki/
Forum: http://sourceforge.net/p/sobjectizer/discussion/
Google-group: https://groups.google.com/forum/#!forum/sobjectizer
GitHub mirror: https://github.com/masterspline/SObjectizer

More Related Content

What's hot

arataga. SObjectizer and RESTinio in action: a real-world example
arataga. SObjectizer and RESTinio in action: a real-world examplearataga. SObjectizer and RESTinio in action: a real-world example
arataga. SObjectizer and RESTinio in action: a real-world exampleYauheni Akhotnikau
 
Date Processing Attracts Bugs or 77 Defects in Qt 6
Date Processing Attracts Bugs or 77 Defects in Qt 6Date Processing Attracts Bugs or 77 Defects in Qt 6
Date Processing Attracts Bugs or 77 Defects in Qt 6Andrey Karpov
 
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured frameworkTesting RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured frameworkMicha Kops
 
Creating a Java EE 7 Websocket Chat Application
Creating a Java EE 7 Websocket Chat ApplicationCreating a Java EE 7 Websocket Chat Application
Creating a Java EE 7 Websocket Chat ApplicationMicha Kops
 
The Little Unicorn That Could
The Little Unicorn That CouldThe Little Unicorn That Could
The Little Unicorn That CouldPVS-Studio
 
MQTT and Java - Client and Broker Examples
MQTT and Java - Client and Broker ExamplesMQTT and Java - Client and Broker Examples
MQTT and Java - Client and Broker ExamplesMicha Kops
 
PVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd CheckPVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd CheckAndrey Karpov
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility PatternHüseyin Ergin
 
Miranda NG Project to Get the "Wild Pointers" Award (Part 1)
Miranda NG Project to Get the "Wild Pointers" Award (Part 1) Miranda NG Project to Get the "Wild Pointers" Award (Part 1)
Miranda NG Project to Get the "Wild Pointers" Award (Part 1) Andrey Karpov
 
Checking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioChecking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioAndrey Karpov
 
Static Analysis of Mozilla Thunderbird's Code by PVS-Studio
Static Analysis of Mozilla Thunderbird's Code by PVS-StudioStatic Analysis of Mozilla Thunderbird's Code by PVS-Studio
Static Analysis of Mozilla Thunderbird's Code by PVS-StudioPVS-Studio
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desaijinaldesailive
 
Analyzing ReactOS One More Time
Analyzing ReactOS One More TimeAnalyzing ReactOS One More Time
Analyzing ReactOS One More TimePVS-Studio
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptKaty Slemon
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptjnewmanux
 

What's hot (20)

What is SObjectizer 5.5
What is SObjectizer 5.5What is SObjectizer 5.5
What is SObjectizer 5.5
 
arataga. SObjectizer and RESTinio in action: a real-world example
arataga. SObjectizer and RESTinio in action: a real-world examplearataga. SObjectizer and RESTinio in action: a real-world example
arataga. SObjectizer and RESTinio in action: a real-world example
 
Date Processing Attracts Bugs or 77 Defects in Qt 6
Date Processing Attracts Bugs or 77 Defects in Qt 6Date Processing Attracts Bugs or 77 Defects in Qt 6
Date Processing Attracts Bugs or 77 Defects in Qt 6
 
Android session-5-sajib
Android session-5-sajibAndroid session-5-sajib
Android session-5-sajib
 
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured frameworkTesting RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
 
Creating a Java EE 7 Websocket Chat Application
Creating a Java EE 7 Websocket Chat ApplicationCreating a Java EE 7 Websocket Chat Application
Creating a Java EE 7 Websocket Chat Application
 
The Little Unicorn That Could
The Little Unicorn That CouldThe Little Unicorn That Could
The Little Unicorn That Could
 
MQTT and Java - Client and Broker Examples
MQTT and Java - Client and Broker ExamplesMQTT and Java - Client and Broker Examples
MQTT and Java - Client and Broker Examples
 
PVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd CheckPVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd Check
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility Pattern
 
Bot builder v4 HOL
Bot builder v4 HOLBot builder v4 HOL
Bot builder v4 HOL
 
JS
JSJS
JS
 
Miranda NG Project to Get the "Wild Pointers" Award (Part 1)
Miranda NG Project to Get the "Wild Pointers" Award (Part 1) Miranda NG Project to Get the "Wild Pointers" Award (Part 1)
Miranda NG Project to Get the "Wild Pointers" Award (Part 1)
 
Checking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioChecking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-Studio
 
Static Analysis of Mozilla Thunderbird's Code by PVS-Studio
Static Analysis of Mozilla Thunderbird's Code by PVS-StudioStatic Analysis of Mozilla Thunderbird's Code by PVS-Studio
Static Analysis of Mozilla Thunderbird's Code by PVS-Studio
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
Vbs
VbsVbs
Vbs
 
Analyzing ReactOS One More Time
Analyzing ReactOS One More TimeAnalyzing ReactOS One More Time
Analyzing ReactOS One More Time
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescript
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
 

Similar to Dive into SObjectizer-5.5. Sixth part: Synchronous Interaction

What is SObjectizer 5.7 (at v.5.7.0)
What is SObjectizer 5.7 (at v.5.7.0)What is SObjectizer 5.7 (at v.5.7.0)
What is SObjectizer 5.7 (at v.5.7.0)Yauheni Akhotnikau
 
What is SObjectizer 5.6 (at v.5.6.0)
What is SObjectizer 5.6 (at v.5.6.0)What is SObjectizer 5.6 (at v.5.6.0)
What is SObjectizer 5.6 (at v.5.6.0)Yauheni Akhotnikau
 
How to use Salesforce composite request connector in Mule
How to use Salesforce composite request connector in MuleHow to use Salesforce composite request connector in Mule
How to use Salesforce composite request connector in MuleAlexandra N. Martinez
 
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
"Node.js vs workers — A comparison of two JavaScript runtimes", James M SnellFwdays
 
Lightning page optimization &amp; best practices
Lightning page optimization &amp; best practicesLightning page optimization &amp; best practices
Lightning page optimization &amp; best practicesGaurav Jain
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end appsZohar Arad
 
Guidelines to understand durable functions with .net core, c# and stateful se...
Guidelines to understand durable functions with .net core, c# and stateful se...Guidelines to understand durable functions with .net core, c# and stateful se...
Guidelines to understand durable functions with .net core, c# and stateful se...Concetto Labs
 
You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?Surendra kumar
 
Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Christian Heilmann
 
Struts interview-questions-ppt
Struts interview-questions-pptStruts interview-questions-ppt
Struts interview-questions-pptMayank Kumar
 
Patterns&Antipatternsof SOA
Patterns&Antipatternsof SOAPatterns&Antipatternsof SOA
Patterns&Antipatternsof SOAMohamed Samy
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNicole Gomez
 
There are only 3 operations in a web app
There are only 3 operations in a web appThere are only 3 operations in a web app
There are only 3 operations in a web appSimon Smith
 
What’s new in SObjectizer 5.5.8
What’s new in SObjectizer 5.5.8What’s new in SObjectizer 5.5.8
What’s new in SObjectizer 5.5.8Yauheni Akhotnikau
 
Architecture refactoring - accelerating business success
Architecture refactoring - accelerating business successArchitecture refactoring - accelerating business success
Architecture refactoring - accelerating business successGanesh Samarthyam
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx javaCongTrung Vnit
 
Manual testing interview questions
Manual testing interview questionsManual testing interview questions
Manual testing interview questionsBABAR MANZAR
 

Similar to Dive into SObjectizer-5.5. Sixth part: Synchronous Interaction (20)

What is SObjectizer 5.7 (at v.5.7.0)
What is SObjectizer 5.7 (at v.5.7.0)What is SObjectizer 5.7 (at v.5.7.0)
What is SObjectizer 5.7 (at v.5.7.0)
 
What is SObjectizer 5.6 (at v.5.6.0)
What is SObjectizer 5.6 (at v.5.6.0)What is SObjectizer 5.6 (at v.5.6.0)
What is SObjectizer 5.6 (at v.5.6.0)
 
How to use Salesforce composite request connector in Mule
How to use Salesforce composite request connector in MuleHow to use Salesforce composite request connector in Mule
How to use Salesforce composite request connector in Mule
 
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
 
Lightning page optimization &amp; best practices
Lightning page optimization &amp; best practicesLightning page optimization &amp; best practices
Lightning page optimization &amp; best practices
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end apps
 
Ajax learning tutorial
Ajax learning tutorialAjax learning tutorial
Ajax learning tutorial
 
Guidelines to understand durable functions with .net core, c# and stateful se...
Guidelines to understand durable functions with .net core, c# and stateful se...Guidelines to understand durable functions with .net core, c# and stateful se...
Guidelines to understand durable functions with .net core, c# and stateful se...
 
You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?
 
Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010
 
Grails services
Grails servicesGrails services
Grails services
 
Struts interview-questions-ppt
Struts interview-questions-pptStruts interview-questions-ppt
Struts interview-questions-ppt
 
Asynchronyin net
Asynchronyin netAsynchronyin net
Asynchronyin net
 
Patterns&Antipatternsof SOA
Patterns&Antipatternsof SOAPatterns&Antipatternsof SOA
Patterns&Antipatternsof SOA
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
There are only 3 operations in a web app
There are only 3 operations in a web appThere are only 3 operations in a web app
There are only 3 operations in a web app
 
What’s new in SObjectizer 5.5.8
What’s new in SObjectizer 5.5.8What’s new in SObjectizer 5.5.8
What’s new in SObjectizer 5.5.8
 
Architecture refactoring - accelerating business success
Architecture refactoring - accelerating business successArchitecture refactoring - accelerating business success
Architecture refactoring - accelerating business success
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx java
 
Manual testing interview questions
Manual testing interview questionsManual testing interview questions
Manual testing interview questions
 

More from Yauheni Akhotnikau

Actor Model and C++: what, why and how? (March 2020 Edition)
Actor Model and C++: what, why and how? (March 2020 Edition)Actor Model and C++: what, why and how? (March 2020 Edition)
Actor Model and C++: what, why and how? (March 2020 Edition)Yauheni Akhotnikau
 
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...Yauheni Akhotnikau
 
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...Yauheni Akhotnikau
 
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...Yauheni Akhotnikau
 
Акторы на C++: стоило ли оно того?
Акторы на C++: стоило ли оно того?Акторы на C++: стоило ли оно того?
Акторы на C++: стоило ли оно того?Yauheni Akhotnikau
 
25 Years of C++ History Flashed in Front of My Eyes
25 Years of C++ History Flashed in Front of My Eyes25 Years of C++ History Flashed in Front of My Eyes
25 Years of C++ History Flashed in Front of My EyesYauheni Akhotnikau
 
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allGECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allYauheni Akhotnikau
 
Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?Yauheni Akhotnikau
 
Шишки, набитые за 15 лет использования акторов в C++
Шишки, набитые за 15 лет использования акторов в C++Шишки, набитые за 15 лет использования акторов в C++
Шишки, набитые за 15 лет использования акторов в C++Yauheni Akhotnikau
 
Для чего мы делали свой акторный фреймворк и что из этого вышло?
Для чего мы делали свой акторный фреймворк и что из этого вышло?Для чего мы делали свой акторный фреймворк и что из этого вышло?
Для чего мы делали свой акторный фреймворк и что из этого вышло?Yauheni Akhotnikau
 
Модель акторов и C++ что, зачем и как?
Модель акторов и C++ что, зачем и как?Модель акторов и C++ что, зачем и как?
Модель акторов и C++ что, зачем и как?Yauheni Akhotnikau
 
What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9Yauheni Akhotnikau
 
Погружение в SObjectizer 5.5. Вводная часть
Погружение в SObjectizer 5.5. Вводная частьПогружение в SObjectizer 5.5. Вводная часть
Погружение в SObjectizer 5.5. Вводная частьYauheni Akhotnikau
 

More from Yauheni Akhotnikau (14)

Actor Model and C++: what, why and how? (March 2020 Edition)
Actor Model and C++: what, why and how? (March 2020 Edition)Actor Model and C++: what, why and how? (March 2020 Edition)
Actor Model and C++: what, why and how? (March 2020 Edition)
 
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
 
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
 
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
 
Акторы на C++: стоило ли оно того?
Акторы на C++: стоило ли оно того?Акторы на C++: стоило ли оно того?
Акторы на C++: стоило ли оно того?
 
25 Years of C++ History Flashed in Front of My Eyes
25 Years of C++ History Flashed in Front of My Eyes25 Years of C++ History Flashed in Front of My Eyes
25 Years of C++ History Flashed in Front of My Eyes
 
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allGECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
 
Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?
 
Шишки, набитые за 15 лет использования акторов в C++
Шишки, набитые за 15 лет использования акторов в C++Шишки, набитые за 15 лет использования акторов в C++
Шишки, набитые за 15 лет использования акторов в C++
 
Для чего мы делали свой акторный фреймворк и что из этого вышло?
Для чего мы делали свой акторный фреймворк и что из этого вышло?Для чего мы делали свой акторный фреймворк и что из этого вышло?
Для чего мы делали свой акторный фреймворк и что из этого вышло?
 
Модель акторов и C++ что, зачем и как?
Модель акторов и C++ что, зачем и как?Модель акторов и C++ что, зачем и как?
Модель акторов и C++ что, зачем и как?
 
What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9
 
Погружение в SObjectizer 5.5. Вводная часть
Погружение в SObjectizer 5.5. Вводная частьПогружение в SObjectizer 5.5. Вводная часть
Погружение в SObjectizer 5.5. Вводная часть
 
Обзор SObjectizer 5.5
Обзор SObjectizer 5.5Обзор SObjectizer 5.5
Обзор SObjectizer 5.5
 

Recently uploaded

CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 

Recently uploaded (20)

CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 

Dive into SObjectizer-5.5. Sixth part: Synchronous Interaction

  • 1. Dive into SObjectizer-5.5 SObjectizer Team, Jan 2016 Sixth Part: Synchronous Interaction (at v.5.5.15)
  • 2. This is the next part of the series of presentations with deep introduction into features of SObjectizer-5.5. This part is dedicated to synchronous interactions between agents. SObjectizer Team, Jan 2016
  • 4. A classical way of interaction between agents is asynchronous messages. Asynchronous interaction is simple, flexible and scalable. It also could prevent some errors like deadlocks (but could also lead to other kind of errors). However, sometimes asynchronous interaction makes code more complex and requires a lot of programmers work... SObjectizer Team, Jan 2016
  • 5. For example: sometimes an agent C must send a request to an agent S and receive back a response with some important information. The agent C can continue its work only after arrival of information from agent S. How we can express that scenario via asynchronous messages? SObjectizer Team, Jan 2016
  • 6. We need a request message which must contain a mbox for the response (since v.5.5.9 any struct/class which is MoveConstructible can be used as a message): struct get_config { so_5::mbox_t reply_to; }; We need a response which will be sent back: struct config { ... // Some fields. config(...); // Some constructor... }; SObjectizer Team, Jan 2016
  • 7. A skeleton for agent S is quite simple: class config_manager : public so_5::agent_t { ... public : virtual void so_define_agent() override { so_subscribe_self().event( &config_manager::evt_get_config ); ... // Other subscriptions... } private : void evt_get_config( const get_config & request ) { // Sending a reply back. so_5::send< config >( request.reply_to, ... /* some args */ ); } }; SObjectizer Team, Jan 2016
  • 8. But a skeleton of agent C is more complex: class client : public so_5::agent_t { // A special state to wait a response. const so_5::state_t st_wait_config{ this, "wait_config" }; public : virtual void so_define_agent() override { // Agent should start its work in a special state. this >>= st_wait_config; // Reply with config data will be handled in that state. st_wait_config.event( &client::evt_config ); ... } virtual void so_evt_start() override { // Request a config at start. so_5::send< get_config >( config_manager_mbox, so_direct_mbox() ); } private : void evt_config( const config & cfg ) { ... /* Handling of configuration. */ } }; SObjectizer Team, Jan 2016
  • 9. It is a lot of code for such simple operation. But this code is not robust enough. For example, there will be no handling of the cases when get_config request or config response are lost somewhere... SObjectizer Team, Jan 2016
  • 10. Synchronous interaction can help here. An agent C can issue a service request to agent S and receive the result of that request synchronously. SObjectizer Team, Jan 2016
  • 11. Let’s rewrite our small example with service request instead of asynchronous messages... SObjectizer Team, Jan 2016
  • 12. A slight modification for request type. There is no need to pass reply_to mbox in a request now. So, get_config becomes a signal: struct get_config : public so_5::signal_t {}; Response will remain the same: struct config { ... // Some fields. config(...); // Some constructors... }; SObjectizer Team, Jan 2016
  • 13. Service request processor will return config as a result of event handler: class config_manager : public so_5::agent_t { ... public : virtual void so_define_agent() override { so_subscribe_self().event< get_config >( &config_manager::evt_get_config ); ... // Other subscriptions... } private : config evt_get_config() { // Returning a reply back. return config{ ... /* some args */ }; } }; SObjectizer Team, Jan 2016
  • 14. Service request issuer will look like: class client : public so_5::agent_t { public : virtual void so_define_agent() override { ... // No special state, no subscription for config response. } virtual void so_evt_start() override { // Request a config at start. // Wait response for 1 second. auto cfg = so_5::request_value< config, get_config >( config_manager_mbox, std::chrono::seconds(1) ); } ... }; SObjectizer Team, Jan 2016
  • 15. That code is not only much simpler ‒ it is also more robust: ● if there is no service request processor behind config_manager_mbox there will be an exception; ● if there are more than one service request processor behind config_manager_mbox there will be an exception; ● if config_manager won’t process a request (request is ignored in the current state of manager) there will be an exception; ● if config_manager can’t process a request, e.g. throw an exception, that exception will be propagated to service request's issuer; ● if config_manager can’t process a request in the specified time slice there will be an exception. SObjectizer Team, Jan 2016
  • 16. It means that in several cases synchronous interaction via service requests is more appropriate than asynchronous interaction. In such cases service requests allow to write more simple, clean and robust code… ...but everything has its price. SObjectizer Team, Jan 2016
  • 17. The main disadvantage of service request is a possibility of deadlocks. Service request’s issuer and processor must work on different working threads. It means that issuer and processor must be bound to different dispatchers. Or, to the different working threads inside the same dispatcher. SObjectizer Team, Jan 2016
  • 18. If an issuer and a processor work on the same working thread there will be a deadlock. SObjectizer doesn’t check that. A user is responsible for binding issuer and processor to the different contexts. SObjectizer Team, Jan 2016
  • 19. But the case when an issuer and a processor are working on the same thread is the simplest case of deadlock. There could be more complex cases: ● agent A calls agent B; ● agent B calls agent C; ● agent C calls agent D; ● agent D calls agent A. It is another kind of classical deadlock with the same consequences. SObjectizer Team, Jan 2016
  • 20. Another disadvantage of service request is a blocking of a working thread for some time. If a service request issuer shares the working thread with different agents (for example, all of them are bound to one_thread dispatcher instance) then all other agents on that thread will wait until the service request is completed. It means that synchronous agents interaction is not very scalable. SObjectizer Team, Jan 2016
  • 21. As shown above, the synchronous agents interaction has significant disadvantages. Based on that, it should be used with care. SObjectizer Team, Jan 2016
  • 22. How Does It Work? SObjectizer Team, Jan 2016
  • 23. There is no any magic behind service requests. Just an ordinary asynchronous messages and some help from std::promise and std::future... SObjectizer Team, Jan 2016
  • 24. When a service request is initiated a special envelope with service requests params inside is sent as an ordinary message to service request processor. This envelope contains not only request params but also a std::promise object for the response. A value for that promise is set on processor’s side. A service request issuer waits on std::future object which is bound with std::promise from the envelope which was sent. SObjectizer Team, Jan 2016
  • 25. It means that so_5::request_value call in form: auto r = so_5::request_value<Result,Request>(mbox, timeout, params); It is a shorthand for something like that: // Envelope will contain Request object and std::promise<Result> object. auto env__ = std::make_unique<msg_service_request_t<Result,Request>>(params); // Get the future to wait on it. std::future<Result> f__ = env__.m_promise.get_future(); // Sending the envelope with request params as async message. mbox->deliver_message(std::move(env__)); // Waiting and handling the result. auto wait_result__ = f__.wait_for(timeout); if(std::future_status::ready != wait_result__) throw exception_t(...); auto r = f__.get(); SObjectizer Team, Jan 2016
  • 26. Every service request handler in the following form Result event_handler(const Request & svc_req ) { ... } is automatically transformed to something like this: void actual_event_handler(msg_service_request_t<Result,Request> & m) { try { m.m_promise.set( event_handler(m.query_param()) ); } catch(...) { m.set_exception(std::current_exception()); } } This transformation is performed during subscription of event_handler. SObjectizer Team, Jan 2016
  • 27. This approach of supporting synchronous interaction means that service request is handled by SObjectizer just like dispatching and handling of ordinary message. As a consequence, a service request handler even doesn’t know is a message it handles is a part of a service request or it was sent as a asynchronous message? Only the service request issuer knows the difference. SObjectizer Team, Jan 2016
  • 28. It means that a config_manager from the example above will work in the same way even if get_config signal is sent via so_5::send() function like any other async message. But in this case the return value of config_manager:: evt_get_config will be discarded. It means that there is no special rules for writing service request handlers: they are just agents with traditional event handlers. Except one important aspect... SObjectizer Team, Jan 2016
  • 29. This important aspect is exception handling. If an agent allows an exception to go out from an event handler then traditional reaction to unhandled exception will be initiated: ● SObjectizer Environment will call so_exception_reaction() for that agent (and may be for coop of that agent and so on); ● appropriate action will be taken (for example, an exception could be ignored or the whole application will be aborted). SObjectizer Team, Jan 2016
  • 30. But if an exception is going out from service request handler then it will be intercepted and returned back to service request issuer via std::promise/std::future pair. It means that this exception will be reraised on service request issuer side during the call to std::future::get() method. E.g. that exception will be thrown out from request_value(). SObjectizer Team, Jan 2016
  • 31. request_value and request_future functions SObjectizer Team, Jan 2016
  • 32. There are several ways of initiating service requests. The simplest one is the usage of so_5::request_value() template function. It can be used in the form: Result r = so_5::request_value<Result, Request>(Target, Timeout [,Args]); Where Timeout is a value which is defined by std::chrono or so_5::infinite_wait for non-limited waiting of the result. Type of Request can be any message or signal type. All Args (if any) will be passed to the constructor of Request. SObjectizer Team, Jan 2016
  • 33. Examples of request_value invocations: // Sending a get_status signal as service request. auto v = so_5::request_value<engine_status, get_status>(engine, // Infinite waiting for the response. so_5::infinite_wait); // No other params because of signal. // Sending a message convert_value as a service request. auto v = so_5::request_value<std::string, convert_value>(converter, // Waiting for 200ms for the response. std::chrono::milliseconds(200), "feets", 33000, "metres" ); // Params for the constructor of convert_value. SObjectizer Team, Jan 2016
  • 34. There is also so_5::request_future() template function. It returns std::future<Result> object: std::future<Result> r = so_5::request_future<Result, Request>(Target [,Args]); There is no Timeout argument. Waiting on the std::future is responsibility of a programmer. As in the case of request_value type of Request can be any of message or signal type. All Args (if any) will be passed to the constructor of Request. SObjectizer Team, Jan 2016
  • 35. request_future can be used in more complex scenarios than request_value. For example, it is possible to issue several service requests, do some actions and only then request responses: auto lights = so_5::request_future<light_status, turn_light_on>(light_controller); auto heating = so_5::request_future<heating_status, turn_heating_on>(heating_controller); auto accessories = so_5::request_future<accessories_status, update>(accessories_controller); ... // Some other actions. if(light_status::on != lights.get()) ... // Some reaction... if(heating_status::on != heating.get()) ... // Some reaction... check_accessories(accessories.get()); ... SObjectizer Team, Jan 2016
  • 36. There could be more complex scenarios: class accessories_listener : public so_5::agent_t { public : ... virtual void so_evt_start() override { m_status = so_5::request_future< accessories_status, get_status >(accessories_controller()); // Service request initiated, but the response will be used later. so_5::send_delayed< check_status >(*this, std::chrono::milliseconds(250)); } private : std::future<accessories_status> m_status; ... void evt_check_status() { auto status = m_status.get(); // Getting the service request response. ... // Processing it. // Initiate new request again. m_status = so_5::request_future< accessories_status, get_status >(accessories_controller()); // Response is expected to be ready on the next timer event so_5::send_delayed< check_status >(*this, std::chrono::milliseconds(250)); } }; SObjectizer Team, Jan 2016
  • 37. Service Requests With void As Result Type SObjectizer Team, Jan 2016
  • 38. Sometimes there is some sense in initiating a service request which returns void. A result of such service request means that processing of a request is completely finished. Sometimes, is it a very useful information. For example, if service request processor does flushing of some important data, finishing transactions, invalidating caches and so on... SObjectizer Team, Jan 2016
  • 39. An example of service request with void result: // Type of agent which implements an intermediate buffer with flushing by demand or by timer. class data_buffer : public so_5::agent_t { public : // Signal for flushing the data. struct flush : public so_5::signal_t {}; ... private : // Event which is bound to flush signal. void evt_flush() { ... // Storing the data to the disk/database/cloud... } }; // Initiating flush operation as a service request. Return from request_value // means the completeness of data flushing. so_5::request_value<void, data_buffer::flush>(buffer, so_5::infinite_wait); SObjectizer Team, Jan 2016
  • 40. Additional Information: Project’s home: http://sourceforge.net/projects/sobjectizer Documentation: http://sourceforge.net/p/sobjectizer/wiki/ Forum: http://sourceforge.net/p/sobjectizer/discussion/ Google-group: https://groups.google.com/forum/#!forum/sobjectizer GitHub mirror: https://github.com/masterspline/SObjectizer