How to use own redundant state in CTRL scripts?
If a CTRL script should only process data on the active redundant host, the function reduActive (int &iAnswer, string System = "") can be used.
This function is doing a dpGet, so it cost additional time and overhead for the Event manager.
If the redundant state is needed in the callback function of a dpConnect/dpQueryConnectSingle/dpQueryConnectAll, it is a good idea to have the redundant state on a global variable which will be updated on every change of the own redundant state.
The following example shows how to keep the updated redundant state on a global variable and use it inside a function (could be a callback function of a dpConnect):
1) Define a global variable for the updated redundant state:
global int iReduActive=-1; //I am the active or passive host? -1 means not initialized now
2) At the beginning for the main function, connect to the own redundant state and update the variable:
main()
{
if(!isRedundant())
{ //in none redundant projects for testing we are acting as active (anyway, the redundant synchronization makes no sense)
iReduActive = 1;
}
else
{
string sHostSuffix = (initHosts()==2?"_2":"");
string sReduActiveDP = "_ReduManager"+sHostSuffix+".Status.Active";
dpConnect("workUpdateReduActiveState", true, sReduActiveDP); //connect to redundant state of own host
}
// wait for redu state
while(iReduActive==-1) //while not initialized
{
delay(0,100);
}
… //your other code
}
3) update the iReduActive variable on change, inside the callback function
private ctrlSynch_workUpdateReduActiveState(string sDPE, bool bActive)
{
iReduActive = (int)bActive;
}
4) use the redundant state inside your code. e.g.:
yourFunction(...)
{
if(iReduActive!=1) //this host is currently not the active of the redundancy
return;
...//your other code
}