Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[idea] A possibility to set a custom mbox to be returned via agent_t::so_direct_mbox()? #39

Closed
eao197 opened this issue Jan 4, 2022 · 2 comments
Assignees

Comments

@eao197
Copy link
Member

eao197 commented Jan 4, 2022

This is just an idea. It's fixed here to be investigated and, maybe, implemented in some future version of SObjectizer.

Every agent in SObjectizer has its MPSC mbox called direct mbox. This mbox is automatically created by SOEnv for every new agent. That mbox is available via agent_t::so_direct_mbox() method.

The type of that mbox is hidden from a user. A user only knows that it's an MPSC mbox.

Sometimes it can be necessary to hide the agent direct mbox and to expose some proxy instead (for example, a proxy that filters or reorders messages of some types), but it's impossible to do because anyone who has a reference to an agent can call so_direct_mbox, and so_direct_mbox isn't a virtual method.

I think that SObjectizer should have a way to specify a custom MPSC mbox to be used as the agent's direct mbox. But I don't know how to do that at the moment. It requires further thinking and prototyping.

@eao197 eao197 self-assigned this Jan 4, 2022
@eao197
Copy link
Member Author

eao197 commented Jan 4, 2022

A possible way to do that can look like this:

namespace so_5 {

// Type for a factory for custom direct mboxes.
using direct_mbox_factory_t = std::function<
  mbox_t(agent_context_t & ctx, so_5::mbox_t actual_mbox) >;

class agent_tuning_options_t
{
public:
  ...
  agent_tuning_options_t &
  custom_direct_mbox_factory(direct_mbox_factory_t factory) {...}
  ...
};
...
class agent_t
{
  ...
protected:
  ...
  template<typename Lambda>
  [[nodiscard]]
  static direct_mbox_factory_t
  direct_mbox_factory(Lambda && lambda) { return { std::forward<Lambda>(lambda) }; }
  ...
};

} /* namespace so_5 */

It allows to write code like that:

class my_agent_with_custom_mbox : public so_5::agent_t
{
public:
  my_agent_with_custom_mbox(context_t ctx, ...)
    : so_5::agent_t{ ctx
        + direct_mbox_factory( [](so_5::agent_context_t & ctx, so_5::mbox_t actual_mbox) {
          return make_some_proxy(ctx.environment(), std::move(actual_mbox));
        })
      }
  {}
};

The mbox returned by the custom factory will be stored inside the agent as its direct mbox. That value then will be returned by so_direct_mbox() method (the so_direct_mbox will still be non-virtual method).

eao197 added a commit that referenced this issue Apr 22, 2022
Possibility to specify custom_direct_mbox_factory added.
If custom_direct_mbox_factory is specified it's used for creation of the
direct mbox for an agent.
@eao197
Copy link
Member Author

eao197 commented May 16, 2022

Is available now in v.5.7.4.

@eao197 eao197 closed this as completed May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant