csmqmsgque 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 csmqmsgque.

To perform the test multiple classes are created and connected using the csmqmsgque 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 csmqmsgque protocoll and build together a tree-like structure.

The GOAL for this setup is:

CODE server

/**
 *   @file         example/csharp/MyRouter.cs
 *   @brief        MyRouter.cs - 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>
 */


using System;
using System.Collections.Generic;
using csmsgque.mkkernel;
using csmsgque.mqmsgque;

namespace example {

// F1 ********************************************************
  abstract class Basis : MqContextC, IServerSetup {
    protected static readonly int id1 = 11;
    protected static readonly int id2 = 12;

    public Basis(MqContextC tmpl=null) : base(tmpl) {
    }

    protected void CreateWorker(int master_id, string factory) {
      SlaveWorker(master_id, factory,
        "--prefix", "cl" + factory + "-" + master_id,
          "@",
            "--prefix", "sv" + factory + "-" + master_id
      );
    }

    protected void HLWO() {
      Send("R", "C", ConfigGetName());
    }

    protected void FOID() {
      Send("R", "C", ClassOriginalIdentGet() + "-" + LinkGetCtxId());
    }

    protected void PATH() {
      Send("R", "C", RouteGetPath());
    }

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

    static protected void HLWS(MqContextC ctx) {
      ((Basis)ctx.SlaveGetMaster()).Setup();
      ctx.SendRETURN();
    }

    protected void Setup () {
      ServiceCreate("HLWO", HLWO);
      ServiceCreate("FOID", FOID);
      ServiceCreate("PATH", PATH);
      ServiceCreate("TREE", TREE);
      ServiceCreate("HLWS", HLWS);
    }
    
    public abstract void ServerSetup ();
  }

// ********************************************************

  sealed class WO1 : Basis {

    public WO1(MqContextC tmpl=null) : base(tmpl) {
    }

    public override void ServerSetup () {
      if (LinkIsParent()) {
        CreateWorker (id1, "WO2");
        CreateWorker (id2, "WO2");
      }
      Setup();
    }
  }

// ********************************************************

  sealed class WO2 : Basis {

    public WO2(MqContextC tmpl=null) : base(tmpl) {
    }

    public override void ServerSetup () {
      if (LinkIsParent()) {
        CreateWorker (id1, "WO3");
        CreateWorker (id2, "WO3");
      }
      Setup();
    }
  }

// ********************************************************

  sealed class WO3 : Basis {

    public WO3(MqContextC tmpl=null) : base(tmpl) {
    }

    void FINL () {
      Send("R", "C", "FINL-" + ConfigGetName());
    }

    public override void ServerSetup () {
      Setup();
      ServiceCreate("FINL", FINL);
    }
  }

// Main ******************************************************
  sealed class MyRouter : MqContextC {

    public static void Main(string[] argv) {

      // create buffer-list of the application arguments
      MkBufferListC largv = new MkBufferListC(argv);

      // add factories
      MqFactoryCT<WO1>.Add().Default();
      MqFactoryCT<WO2>.Add();
      MqFactoryCT<WO3>.Add();

      // select factory using the !first! application argument
      // and create a new server
      MqContextC srv = MqFactoryCT<MqContextC>.GetCalled(largv).New();
      try {
        // configure and start the server
	srv.LinkCreate(largv);

        // start event-loop and wait forever
	srv.ProcessEvent(MqWaitOnEventE.FOREVER);
      } catch (Exception ex) {

        // set the libmqmsgque error from the csharp error
        srv.ErrorCatch (ex);
      }

      // exit aplication and cleanup the environment
      srv.Exit();
    }
  }
}