theLink 10.0 NHI1 - theKernel - theLink - theConfig - theSq3Lite - theCompiler - theBrain - theGuard
c - tcl - cs - py - rb - jv - cc
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         NHI1/example/cc/MyRouter.cc
 *   @brief        MyRouter.cc - 12 Nov 2024 - aotto1968
 *   @copyright    (C) NHI - #1 - Project - Group
 *                 This software has NO permission to copy,
 *                 please contact AUTHOR for additional information
 *   @version      478c13192af70c38a452c665c8243fd6902efb63
 *   @date         Tue Nov 12 14:49:07 2024 +0100
 *   @author       aotto1968 <aotto1968@t-online.de>
 */

/* LABEL-START */

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

/* LABEL-END */

using namespace ccmqmsgque;

class Basic : public MqContextC, public MqServerSetupIF {

  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.AppendSTR("--prefix");
      argv.AppendV("cl%s-%i", factory, master_id);
      argv.AppendSTR("@");
      argv.AppendSTR("--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", MqServiceICB(&Basic::HLWO));
      ServiceCreate("FOID", MqServiceICB(&Basic::FOID));
      ServiceCreate("PATH", MqServiceICB(&Basic::PATH));
      ServiceCreate("TREE", MqServiceICB(&Basic::TREE));
      ServiceCreate("HLWS", MqServiceICB(&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", MqServiceICB(&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();
}