ccmqmsgque 9.0
Loading...
Searching...
No Matches
Example: Identification-Test

Documentation of the Test-Client-Server tool used for example.test.

INTRODUCTION

The Naming-Test-Tool tool is used to test the name-resolution-feature of ccmqmsgque.

To perform the test 2 tools are used, the testclient and the testserver.

testclient

This tool creates multiple parent and child client-context, all using a --postfix argument to identify unique.

testserver This tool listen on the service GTCX and return the unique identification pattern of the server-context in duty.

All context created, 8x client and 8x server, are connected using the ccmqmsgque protocoll and build together a flat structure.

The GOAL for this setup is:

CODE client

/**
 *   @file         example/cc/testclient.cc
 *   @brief        testclient.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 */

#include  <iostream>
#include  <string>
#include  <cstring>
#include  <cstdlib>
#include  <libgen.h>
#include  <limits.h>
#include  <stdio.h>

#include "msgque_config.h"

using namespace std;
using namespace ccmqmsgque;

static string Get( MqContextC& ctx) {
  MkBufferC RET;
  ctx.SendSTART();
  ctx.SendEND_AND_WAIT("GTCX", MK_TIMEOUT_USER);
  RET.Reset();
  RET.AppendC(ctx.ConfigGetName());
  RET.AppendC("+");
  RET.AppendC(ctx.ReadC());
  RET.AppendC(ctx.ReadC());
  RET.AppendC(ctx.ReadC());
  RET.AppendC(ctx.ReadC());
  RET.AppendC(ctx.ReadC());
  RET.AppendC(ctx.ReadC());
  return RET.GetC();
}

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

  // find abs path :-)
  FILE *fp;
  char abs_exe_path[PATH_MAX];

  // build command
  strcpy (abs_exe_path, "/usr/bin/which ");
  strcat (abs_exe_path, argv[0]);

  /* Open the command for reading. */
  fp = popen(abs_exe_path, "r");
  if (fp == NULL) {
    printf("Failed to run command\n" );
    exit(1);
  }

  /* Read the output a line at a time - output it. */
  if (fgets(abs_exe_path, sizeof(abs_exe_path), fp) == NULL) {
    printf("Failed to extract path\n" );
    exit(1);
  }

  /* close */
  pclose(fp);
  MK_STRN tstr = strdup(dirname(abs_exe_path));
  strcpy (abs_exe_path, tstr);
  MkSysFree(tstr);

    // setup the clients
  MkBufferListC t1 =  {argv[0], "--prefix", "c", "--postfix", "1", NULL};

  for (int i=1; i<argc; i++) t1.AppendC(argv[i]);

  static MqContextC c0, c00, c01, c000, c1, c10, c100, c101;

  try {

    // create the "LINK"
    
#if defined(META_IS_POSIX)
    MK_STRN server = strcat(abs_exe_path,"/testserver");
#elif defined(META_IS_WINDOWS)
    MK_STRN server = strcat(abs_exe_path,"/testserver.exe");
#else
    #error "META_IS_POSIX or META_IS_WINDOWS is missing"
#endif

    c0.LinkCreate(MkBufferListC {
      argv[0], "--prefix", "c", "--postfix", "0", "--debug", 
        getenv("TS_DEBUG"), "@", server, "--prefix", "s", NULL
    });

    c00.LinkCreateChild(  c0,   MkBufferListC  {"--postfix",  "00",    NULL});
    c01.LinkCreateChild(  c0,   MkBufferListC  {"--postfix",  "01",    NULL});
    c000.LinkCreateChild( c00,  MkBufferListC  {"--postfix",  "000",   NULL});

    c1.LinkCreate(t1);

    c10.LinkCreateChild(  c1,   MkBufferListC  {"--postfix",  "10",    NULL});
    c100.LinkCreateChild( c10,  MkBufferListC  {"--postfix",  "100",   NULL});
    c101.LinkCreateChild( c10,  MkBufferListC  {"--postfix",  "101",   NULL});

    // do the tests
    cout  <<  Get(c0)	<< endl
	  <<  Get(c00)	<< endl
	  <<  Get(c01)	<< endl
	  <<  Get(c000)	<< endl;

    cout  <<  Get(c1)	<< endl
	  <<  Get(c10)	<< endl
	  <<  Get(c100)	<< endl
	  <<  Get(c101)	<< endl;

  } catch (const std::exception& e) {
    c1.ErrorCatch(e);
  }

  // do the cleanup
  c1.Exit();
}

CODE server

/**
 *   @file         example/cc/testserver.cc
 *   @brief        testserver.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 testserver : public MqContextC, public IServerSetup {
  friend class MqFactoryCT<testserver>;
  private:
    testserver(MK_TYP const typ, MqContextC* tmpl=NULL) : MqContextC(typ, tmpl) {}
  private:
    void GTCX () {
      SendSTART();
      SendI(LinkGetCtxId());
      SendC("+");
      if (LinkIsParent()) {
	SendI(-1);
      } else {
	SendI(LinkGetParent()->LinkGetCtxId());
      }
      SendC("+");
      SendC(ConfigGetName());
      SendC(":");
      SendRETURN();
    }
    void ServerSetup() {
      ServiceCreate("GTCX", CallbackF(&testserver::GTCX));
    }
};

int MK_CDECL main (int argc, MK_STRN argv[])
{
  MqMsgque::Setup();
  testserver *ctx = MqFactoryCT<testserver>::Add("testserver")->New();
  try {
    ctx->LinkCreate(MkBufferListC {argc, argv});
    ctx->ProcessEvent (MQ_WAIT_FOREVER);
  } catch (const std::exception& e) {
    ctx->ErrorCatch(e);
  }
  ctx->Exit ();
}