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

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 libmqmsgque protocoll and build together a flat structure.

The GOAL for this setup is:

CODE client

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

#define NHI1_FILE_NAME "testclient.c"

/* VAR-END */

#include <libgen.h>
#include <limits.h>

#include "common.h"

#define META_CONTEXT_S ctx

static const char * Get (
  MQ_CTX const ctx
)
{
  MkRtSetup_NULL;
  static char str[250];
  MK_STRN buf;

  MqSendSTART_E (ctx);
  MqSendEND_AND_WAIT_E (ctx, "GTCX", MK_TIMEOUT_USER);
  str[0] = '\0';
  strncat(str, MqConfigGetName(ctx), 250-1);
  strncat(str, "+", 250-1);
  MqReadC_E (ctx, &buf); strncat(str, buf, 250-1);
  MqReadC_E (ctx, &buf); strncat(str, buf, 250-1);
  MqReadC_E (ctx, &buf); strncat(str, buf, 250-1);
  MqReadC_E (ctx, &buf); strncat(str, buf, 250-1);
  MqReadC_E (ctx, &buf); strncat(str, buf, 250-1);
  MqReadC_E (ctx, &buf); strncat(str, buf, 250-1);
  return str;

error:
  MqExit_1(ctx);
  return "";
}

#define FIRST

int main (int argc, MK_STRN argv[]) 
{
  MkRtSetup_NULL;
  // 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);
  char * sd = strdup(dirname(abs_exe_path));
  strcpy  (abs_exe_path,  sd);
#ifdef META_IS_POSIX
  strcat  (abs_exe_path,  "/testserver"),
#else
  strcat  (abs_exe_path,  "/testserver.exe"),
#endif
  free(sd);

  // start work
  MK_BFL largv = MkBufferListCreateVC(argc, argv);
  MQ_CTX ctx=NULL; 
#if defined(FIRST)
  MQ_CTX c0=NULL, c00=NULL, c01=NULL, c000=NULL;
#endif
  MQ_CTX c1=NULL, c10=NULL, c100=NULL, c101=NULL;

#if defined(FIRST)
  struct MkBufferListS *bs0 = MkBufferListCreateVA( 
      argv[0], "--prefix", "c", "--debug", getenv("TS_DEBUG"), "@", abs_exe_path, "--prefix", "s",NULL);

  struct MkBufferListS *args;
#endif

#if defined(FIRST)
  ctx = MqContextCreate(NULL,NULL);

  MqConfigSetPostfix(ctx, "0");
  MqLinkCreate_E (ctx, bs0);
  MqCheckForLeftOverArguments_E (ctx,bs0);
  MkBufferListDelete(bs0);

  c0 = ctx;

  ctx = MqContextCreate(NULL,NULL);
  args = MkBufferListCreateVA ("--postfix", "00", NULL);
  MqLinkCreateChild_E (ctx, c0, args);
  MkBufferListDelete(args);
  c00 = ctx;

  ctx = MqContextCreate(NULL,NULL);
  args = MkBufferListCreateVA ("--postfix", "01", NULL);
  MqLinkCreateChild_E (ctx, c0, args);
  MkBufferListDelete(args);
  c01 = ctx;

  ctx = MqContextCreate(NULL,NULL);
  args = MkBufferListCreateVA ("--postfix", "000", NULL);
  MqLinkCreateChild_E (ctx, c00, args);
  MkBufferListDelete(args);
  c000 = ctx;
#endif

  ctx = MqContextCreate(NULL,NULL);
  MqConfigSetPrefix(ctx, "c");
  MqConfigSetPostfix(ctx, "1");
  MqLinkCreate_E (ctx, largv);
  MqCheckForLeftOverArguments_E (ctx, largv);
  MkBufferListDelete(largv);
  c1 = ctx;

  ctx = MqContextCreate(NULL,NULL);
  MqConfigSetPostfix(ctx, "10");
  MqLinkCreateChild_E (ctx, c1, NULL);
  c10 = ctx;

  ctx = MqContextCreate(NULL,NULL);
  MqConfigSetPostfix(ctx, "100");
  MqLinkCreateChild_E (ctx, c10, NULL);
  c100 = ctx;

  ctx = MqContextCreate(NULL,NULL);
  MqConfigSetPostfix(ctx, "101");

  MqLinkCreateChild_E (ctx, c10, NULL);
  c101 = ctx;

  // do the tests
#if defined(FIRST)
  fprintf(stdout,"%s\n", Get(c0));
  fprintf(stdout,"%s\n", Get(c00));
  fprintf(stdout,"%s\n", Get(c01));
  fprintf(stdout,"%s\n", Get(c000));
#endif

  fprintf(stdout,"%s\n", Get(c1));
  fprintf(stdout,"%s\n", Get(c10));
  fprintf(stdout,"%s\n", Get(c100));
  fprintf(stdout,"%s\n", Get(c101));
  fflush(stdout);

  // do the cleanup
#if defined(FIRST)
  MqContextDelete(c0);
  MqContextDelete(c00);
  MqContextDelete(c01);
  MqContextDelete(c000);
#endif


  MqContextDelete(c1);
  MqContextDelete(c10);
  MqContextDelete(c100);
  MqContextDelete(c101);
  return 0;

error:
  MqExit_1(ctx);
}

CODE server

/**
 *   @file         example/c/testserver.c
 *   @brief        testserver.c - 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 */

#define NHI1_FILE_NAME "testserver.c"

/* VAR-END */

#include "common.h"

static enum MkErrorE  GTCX( MQ_CALLBACK_SERVICE_CALL_ARGS ) {
  MqSendSTART_E (mqctx);
  MqSendI_E (mqctx, (MK_INT)MqLinkGetCtxId(mqctx));
  MqSendC_E (mqctx, "+");
  if (MqLinkIsParent(mqctx)) {
    MqSendI_E (mqctx, -1);
  } else {
    MqSendI_E (mqctx, (MK_INT)MqLinkGetCtxId(MqLinkGetParent(mqctx)));
  }
  MqSendC_E (mqctx, "+");
  MqSendC_E (mqctx, MqConfigGetName(mqctx));
  MqSendC_E (mqctx, ":");
error:
  return MqSendRETURN(mqctx);
}

static enum MkErrorE ServerSetup ( MQ_CALLBACK_SERVICE_CALL_ARGS ) {
  return MqServiceCreate(mqctx,"GTCX", GTCX, NULL, NULL, NULL);
}

#if 0

int main (int argc, MK_STRN argv[]) 
{
  //MkSetup();
  struct MkBufferListS * largv = MkBufferListCreateVC(argc, argv);
  MQ_CTX mqctx = MqContextCreate(0, NULL);
  MqConfigSetServerSetup (mqctx, ServerSetup, NULL, NULL, NULL);
  MqLinkCreate_E (mqctx, largv);
  MqProcessEvent_E (mqctx,MQ_WAIT_FOREVER,MK_TIMEOUT_DEFAULT);
error:
  MkBufferListDelete(largv);
  MqExit_1(mqctx);
}

#else

enum MkErrorE
Factory ( MQ_CALLBACK_FACTORY_CTOR_ARGS )
{ 
  MQ_CTX const mqctx = *contextP = MqContextCreate(NULL,tmpl);
  MqConfigSetServerSetup (mqctx, ServerSetup, NULL, NULL, NULL);
  return MK_OK;
}

int main (int argc, MK_STRN argv[]) 
{
  MqRtSetup_NULL;

  // setup commandline arguments for later use
  MK_BFL largv = MkBufferListCreateVC(argc, argv);
  MQ_CTX mqctx = NULL;

  // create "testserver" factory… and make it to the default.
  MqFactoryDefault(
    MqFactoryAdd(MK_ERROR_PANIC, Factory, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "testserver")
  );
  // inspect commandline-argument for the "factory" to choose… and create a object
  MqFactoryNew_E (MqFactoryGetCalledL(largv), NULL, &mqctx);

  // start listen for incoming call's
  MqLinkCreate_E (mqctx, largv);
  MqCheckForLeftOverArguments_E (mqctx, largv);
  MqProcessEvent_E (mqctx,MQ_WAIT_FOREVER,MK_TIMEOUT_DEFAULT);
error:
  MkBufferListDelete(largv);
  MqExit_1(mqctx);
}
#endif