0

I need architectural advice. I'm new to c++ and FSM principles. I have pet project that used rdkafka and QT ui. I want to write FSM layer on top of my rdkafka consumer for managing it state. I don't understand where to have my state-transition logic. Below is a very abstract example of what i mean:

namespace t {
// events
struct disconnect {};
struct connect {};
struct connect_succ {};
struct connect_unsucc {};

struct consumer_ : public msm::front::state_machine_def<consumer_> {
  struct Disconnected : public msm::front::state<> {};
  struct Connected : public msm::front::state<> {};
  struct UnConnected : public msm::front::state<> {};

  using initial_state = Disconnected;

  struct connect_a {
    template <class EVT, class FSM, class SourceState, class TargetState>
    void operator()(EVT const &, FSM &fsm, SourceState &, TargetState &) {
      boolean isConnected = fsm->service->connect(); // just an example
      if (isConnected) {
        fsm.process_event(connect_succ());
      } else {
        fsm.process_event(connect_unsucc());
      }
    }
  };

  // clang-format off
  struct transition_table : mpl::vector<
    Row < Disconnected, connect, none, connect_a, none >,
    Row < Disconnected, connect_succ, Connected, none, none >,
    Row < Disconnected, connect_unsucc, UnConnected, none, none >
  >{};
  // clang-format on
};
using consumer = msm::back::state_machine<consumer_>;
}; // namespace t

int main() {
  t::consumer c;

  c.process_event(t::connect());
  return 0;
}

So here is my question - should I choose next state in action or should I write some service layer around my FSM and just keep FSM simple as possible?

Sem Grozovski
  • 404
  • 3
  • 11

0 Answers0