csmqmsgque 9.0
Loading...
Searching...
No Matches
Example: Filter6

Documentation of the Filter6 tool used for trans2.test.

INTRODUCTION

The Filer6 tool is used to test the filter-feature of csmqmsgque.

To run the filter test, a first client, one or more filters and a final server are created. All are connected to the csmqmsgque protocol.

The trans2.test carries out common filter tests and special stress tests. A stress test is performed by exiting one or more filters or servers and observing the response and behavior when reconnecting.

The GOAL for this test is:

CODE filter

/**
 *   @file         example/csharp/Filter6.cs
 *   @brief        Filter6.cs - 25 Dec 2022 - aotto1968
 *   @copyright    (C) NHI - #1 - Project - Group
 *                 This software has NO permissions to copy,
 *                 please contact AUTHOR for additional information
 *   @version      9dfa375425d5e344c853828f6fe0ccf7c03287d5
 *   @date         Sun Dec 25 13:44:16 2022 +0100
 *   @author       aotto1968 <aotto1968@t-online.de>
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using csmsgque.mkkernel;
using csmsgque.mqmsgque;

namespace example {
  sealed class Filter6 : MqContextC, IServerSetup, IServerCleanup, IEvent {

    private Regex rgx = null;
    private MkLogFileC FH = null;

    public Filter6(MqContextC tmpl=null) : base(tmpl) {
      ConfigSetIgnoreExit(true);
      rgx = new Regex("^(?:Filter6-1|Filter6|fs1.*)$");
    }

    void ErrorWrite () {
      var err = ErrorDEFAULT();
      FH.WriteC("ERROR: " + err.GetText());
      err.Reset();
    }

    void LOGF () {
      MqContextC ftr = SlaveGetFilter();
      if (rgx.IsMatch(ftr.ConfigGetName())) {
        FH = MkLogFileC.Open(this,ReadC());
      } else {
	ProxyForward(ftr);
      }
      SendRETURN();
    }

    static void WRIT (MqContextC ctx) {
      var master = (Filter6) ctx.SlaveGetMaster();
      if (master.rgx.IsMatch(master.ConfigGetName())) {
        master.FH.WriteC(ctx.ReadC());
      } else {
        ctx.ProxyForward(master);
      }
      ctx.SendRETURN();
    }

    void EXIT () {
      Exit();
    }

    void SOEX () {
      ErrorDEFAULT().SetEXIT();
    }

    void FilterIn () {
      StorageExport();
      SendRETURN();
    }

    void IEvent.Event () {
      if (StorageCount() == 0L) {
	ErrorDEFAULT().SetCONTINUE();
      } else {
	int Id = 0;
	try  {
	  MqContextC ftr = SlaveGetFilter();
	  Id = StorageImport();
          try {
            ProxyForward(ftr);
	  } catch (MkExceptionC) {
            if (StorageErrCnt(Id) <= 3) {
              StorageDecrRef(Id);
              return;
            } else {
              throw;
            }
          } catch {
            throw;
          }
	} catch (Exception ex) {
	  ErrorCatch (ex);
          ErrorWrite();
	}
	Id = StorageDelete(Id);
      }
    }

    void IServerCleanup.ServerCleanup() {
      if (FH == null) return;
      FH.Dispose();
      FH = null;
    }

    /// [filter_service_example]
    void IServerSetup.ServerSetup() {
      MqContextC ftr = SlaveGetFilter();
      ServiceCreate     ("LOGF", LOGF);
      ServiceCreate     ("EXIT", EXIT);
      ServiceCreate     ("SOEX", SOEX);
      ServiceCreate     ("+ALL", FilterIn);
      ServiceStorage    ("PRNT");
      ServiceStorage    ("PRN2");
      ftr.ServiceCreate ("WRIT", Filter6.WRIT);
      ServiceCreate     ("WRIT", Filter6.WRIT);
      ftr.ServiceProxy  ("WRT2", (int)MqSlaveE.MASTER);
    }
    /// [filter_service_example]

    /// [error_example]
    public static void Main(string[] argv) {
      Filter6 srv = MqFactoryCT<Filter6>.Add().New();
      try {
	srv.LinkCreate(argv);
	srv.ProcessEvent(MqWaitOnEventE.FOREVER);
      } catch (Exception ex) {
        srv.ErrorCatch (ex);
      }
      srv.Exit();
    }
    /// [error_example]
  }
}