Using Recursive Public API Requests

Purpose :

To demonstrate how you can create recursive Public API Requests in a Worker base class to send messages through an application's entire Worker call-chain, both down the Worker call-chain (i.e. from Worker to subWorkers) and up the Worker call-chain (i.e. from Worker to Caller).

Introduction

Every Worker, through its Public Properties (i.e. a property node on a Worker class wire), has access to the Public Properties of its Caller and also access to the Public Properties of its subWorkers. This allows you to send messages in the form of a Public API Request to both a Worker's Caller and a Worker's subWorkers. And if you are sending a Public API Request that exists in a Worker base class that is inherited by all Workers in an application, then you able to send a message throughout the entire Worker call-chain by creating a recursive Public API Request in a Worker base class.


Method - Top to Bottom

Send Public Request from Head Worker to every Worker in application

This example will show you how to send a Public Request from an application's head Worker to every Worker in a Worker's application. The Public Request is propagated throughout a Worker's application 'behind the scenes' by sending a Public Request to the same MHL case of a Worker base class in every Worker in the application.

Recursive Public API Request in the MHL case of a Worker base class

The image above shows how to implement a recursive Public API Request in a Worker base class, to send a Public Request to a Worker's subWorkers. The Public Request VI (position 3.) sends a message to the MHL case which is displayed, however the input to the Request VI are the subWorker handles, which need to be cast first to the Worker base class.

Things to note here:

  1. A Worker's Public Properties contain the handles of its subWorkers (both statically linked and dynamically loaded). These can be cast to their specific Worker class (or Worker base class) and then wired to the subWorker's Public Request VIs.

  2. You must then cast the subWorker handles to the Worker base class. This will not result in an error if every Worker in an application inherits from the same Worker base class you are casting to.

  3. You can then use the Worker base class's Public Request VI to send a Public Request to the same MHL case that contains this VI (i.e. a recursive call). The input to the Public Request VI is not of the same Worker, but instead that of the subWorkers.

  4. You must wire the message datatype for this MHL case to the Data in input terminal of the Public Request VI so that you can propagate the same message data to the subWorkers.

Through the use of overriding the MHL case of a Worker base class, you are able to catch the Public API Request in any Worker that overrides the Worker base class. Of course if you override an MHL case of a Worker base class, make sure to call the Worker base class's MHL Cases.vi in the override MHL case so that the recursive call can be propagated down through the rest of the Worker call-chain.

Example Project

An example project is provided which propagates a recursive Public Request from the head Worker down throughout the entire Worker call-chain. Every Worker in the application inherits from the same Worker base class where the Public API Request exists. The example project's Worker call-chain is shown below.

Worker call-chain for the Recursive Public API Request example project

Added to the recursive Public Request's MHL case is an invoke node that will open the front panel of a Worker's Main VI (see screenshot directly below). Therefore, when the recursive Public Request VI is called from the Head Worker, every Worker's Main VI in the application will open it's front panel.

Recursive Public API Request in Worker base class will open the Main VI's front panel of every Worker in the application.

Source code

The LabVIEW source code for the example project described above can be found in the Workers Community Additional Example Projects repository here.

You can use https://download-directory.github.io/ to download individual folders directly from the GitHub repository.



Method - Bottom to Top

Send Public Request from any subWorker to Head Worker

This example will show you how to send a Public Request from any Worker in an application all the way up to the application's head Worker. Usually you want to use Public Reponses to send a message from a Worker to its Caller, but if the Workers in an application are coupled together through the use of a common Worker base class, then it is also possible to use Public Requests to send messages up the Worker call-chain. This is the process we use here to propagate a 'behind the scenes' Public Request up through the Worker call-chain, by sending the Public Request to an MHL case of a Worker base class in every Caller in the application.

Recursive Public API Request in the MHL case of a Worker base class

The image above shows what how to implement a recursive Public API Request in a Worker base class, to send a Public Request up to a Worker's Caller. The Public Request VI (position 3.) sends a message to the MHL case which is displayed, however the input to the Request VI is the Caller's handle, which needs to be cast first to the Worker base class.

Things to note here:

  1. A Worker's Public Properties contain the handles of its subWorkers (both statically linked and dynamically loaded). These can be cast to their specific Worker class (or Worker base class) and then wired to the Caller's Public Request VIs.

  2. You must then cast the Caller's handle to the Worker base class. This will not result in an error if every Worker in an application inherits from the same Worker base class class you are casting to.

  3. You can then use the Worker base class's Public Request VI to send a Public Request to the same MHL case that contains this VI (i.e. a recursive call). The input to the Public Request VI is not of the same Worker, but instead that of the Caller.

  4. You must wire the message datatype for this case to the Data in input terminal of the Public Request VI so that you can propagate the same message data to the Caller.

Through the use of overriding the MHL case of a Worker base class, you are able to catch the Public API Request in any Worker that overrides the Worker base class. Of course if you override an MHL case of a Worker base class, make sure to call the Worker base class's MHL Cases.vi in the override case so that the recursive call can be propagated up through the rest of the Worker call-chain.

Example Project

This method is demonstrated in Method 3 of the article called Sending errors up the Worker call-chain, and the example project for this article demonstrates the use of a recursive Public API Request in a Worker base class to achieve this.