ccmqmsgque 9.0
Loading...
Searching...
No Matches
Example: MyRouter

Documentation of the MyRouter tool used for route2.test.

INTRODUCTION

The MyRouter tool is used to test the routing-feature of ccmqmsgque.

To perform the test multiple classes are created and connected using the ccmqmsgque protocol.
The following class-hierarchie is used:

       Base
   |----|----|
  WO1  WO2  WO3       

The routing-test is perfomed by connecting multiple context.

  • The client connect to the WO1-context.
  • On WO1-ServerSetup the WO1-context create two WO2-context using the CreateWorker methode from Base.
  • On WO2-ServerSetup the W02-context create two WO3-context using the CreateWorker methode from Base.
                           |-> WO3#1
                |-> WO2#1 -|-> WO3#2
 client -> WO1 -|
                |-> W02#2 -|-> WO3#1
                           |-> WO3#2

All context created, 1x client and 7x server, are connected using the ccmqmsgque protocoll and build together a tree-like structure.

The GOAL for this setup is:

CODE server

/**
 *   @file         example/cc/MyRouter.cc
 *   @brief        MyRouter.cc - 02 May 2023 - aotto1968
 *   @copyright    (C) NHI - #1 - Project - Group
 *                 This software has NO permissions to copy,
 *                 please contact AUTHOR for additional information
 *   @version      d2cd8f6ec2179f5e5583ae98faed11359996631d
 *   @date         Tue May 2 21:28:46 2023 +0200
 *   @author       aotto1968 <aotto1968@t-online.de>
 */

/* VAR-START */

#include  "debug_mq.h"
#include  "LibMqMsgque_cc.hh"

/* VAR-END */

using namespace ccmqmsgque;

class Basic : public MqContextC, public IServerSetup {

  public:
    Basic(MK_TYP const typ, MqContextC* tmpl=NULL) : MqContextC(typ, tmpl) {};

  protected:
    MqContextC* CreateWorker (
      MK_NUM master_id,
      MK_STRN factory
    )
    {
      MkBufferListC argv(5);
      argv.AppendC("--prefix");
      argv.AppendV("cl%s-%i", factory, master_id);
      argv.AppendC("@");
      argv.AppendC("--prefix");
      argv.AppendV("sv%s-%i", factory, master_id);

      SlaveWorker (master_id, factory, argv);

      return SlaveGet(master_id);
    }

    static const MK_NUM id1 = MQ_SLAVE_USER+1;
    static const MK_NUM id2 = MQ_SLAVE_USER+2;
    static const MK_NUM id3 = MQ_SLAVE_USER+3;
    static const MK_NUM id4 = MQ_SLAVE_USER+4;

  private:
    
    void HLWO () {
      Send("R", "C", ConfigGetName());
    }
    
    void FOID () {
      SendSTART();
      SendV("%s-%i", ClassOriginalIdentGet(), LinkGetCtxId());
      SendRETURN();
    }
    
    void PATH () {
      Send("R", "C", RouteGetPath());
    }

    void TREE () {
      Send("R", "L", RouteGetTree());
    }

    void HLWS () {
      static_cast<Basic*>(SlaveGetMaster())->Setup();
      SendRETURN();
    }
    
  protected:
    void Setup() {
      ServiceCreate("HLWO", CallbackF(&Basic::HLWO));
      ServiceCreate("FOID", CallbackF(&Basic::FOID));
      ServiceCreate("PATH", CallbackF(&Basic::PATH));
      ServiceCreate("TREE", CallbackF(&Basic::TREE));
      ServiceCreate("HLWS", CallbackF(&Basic::HLWS));
    }
};

class WO1 final : public Basic {
  friend class MqFactoryCT<WO1>;

  private:
    WO1(MK_TYP const typ, MqContextC* tmpl=NULL) : Basic(typ, tmpl) {};

  private:
    void ServerSetup() {
      if (LinkIsParent()) {
        CreateWorker (id1, "WO2");
        CreateWorker (id2, "WO2");
      }
      Basic::Setup();
    }
};

class WO2 final : public Basic {
  friend class MqFactoryCT<WO2>;

  private:
    WO2(MK_TYP const typ, MqContextC* tmpl=NULL) : Basic(typ, tmpl) {};

  private:
    void ServerSetup() {
      if (LinkIsParent()) {
        CreateWorker (id1, "WO3");
        CreateWorker (id2, "WO3");
      }
      Basic::Setup();
    }
};

class WO3 final : public Basic {
  friend class MqFactoryCT<WO3>;

  private:
    WO3(MK_TYP const typ, MqContextC* tmpl=NULL) : Basic(typ, tmpl) {};

  private:
    void FINL () {
      SendSTART();
      SendV("FINL-%s", ConfigGetName());
      SendRETURN();
    }

    void ServerSetup() {
      Basic::Setup();
      ServiceCreate("FINL", CallbackF(&WO3::FINL));
    }
};

int MK_CDECL main(int argc, MK_STRN argv[]) {
  MqMsgque::Setup();
  MkBufferListC largs(argc, argv);

  MqFactoryCT<WO1>::Add ("WO1")->Default();
  MqFactoryCT<WO2>::Add ("WO2");
  MqFactoryCT<WO3>::Add ("WO3");

  auto srv = MqFactoryCT<MqContextC>::GetCalled(&largs)->New();
  try {
    srv->LinkCreate(largs);
    srv->ProcessEvent (MQ_WAIT_FOREVER);
  } catch (const std::exception& e) {
    srv->ErrorCatch(e);
  }
  srv->Exit();
}