handleHealthCheck method

void handleHealthCheck (
  1. SamModel sm,
  2. SamReq req
)

Determine health of MissileSite based on signals from Bank.

The Bank signals a change of state to its parent the MissileSite.

We use this to scan the children and ascertain the health of the site. If only one Bank remains with unspent Missiles the BK.ssDepleted state is entered. If no Bankss have unspent Missiles the MissileSite enters the BK.ssDefunct state which is a terminal condition from which there is no escaspe.

Since a Bank can be replenished the MissileSite can leave the Bk.ssDistressed state when this occurs. The logic here handles that case to.

Implementation

void handleHealthCheck (SamModel sm, SamReq req) {
  assert(log("$sm received ${req.signal} signalled from ${req.stepParms['src']}"));
  int goodBanks = 0;
  for(Bank bank in sm.kids) {
    if (!bank.isState(BK.ssDepleted)) goodBanks += 1;
  }
  if (goodBanks == 0) {
    sm.flipState(MS.ssDefunct);
    assert(log("$sm now defunct"));
  } else if (goodBanks == 1) {
    sm.flipState(MS.ssDistressed);
    assert(log("$sm now distressed"));
  } else {
    sm.flipState(MS.ssOperational);
  }
}