CRecordNotifyTargetDeregistered

This nano service is triggered by the TargetRegistry when a Target has been deregistered.

<record id="42686f64-56d9-40d8-9efc-e4f42e9902fd" name="NOTIFY_TARGET_DEREGISTERED"
        isService="true">

    <description>
        SYSTEM nanoservice that will be triggered if a target in a namespace (any namespace) has been deregistered.
    </description>

    <namespaces>SYSTEM</namespaces>

    <slot key="1" name="ADDRESS" answer="false" mandatory="true" type="TARGET_ADDRESS">
        <description>Address of the deregistered target.</description>
    </slot>

    <slot key="2" name="NAME" answer="false" mandatory="true" type="STRING">
        <description>Name of the target, if available via name database.</description>
    </slot>

</record>

Example of use of the class CRecordNotifyTargetDeregistered (after generating)

This record is a notification nanoservice in the SYSTEM namespace. To get the message when targets are deregistered, we need to stick an observer to the nanoservice. We do this best in the handler of CRecordStartTarget, which is called when our target is registered:

// constructor:
addMessageHandler(CRecordStartTarget.ID,
                  this::asyncStartTarget);
private boolean asyncStartTarget(@NotNull final CEnvelope aEnvelope,
                                 @NotNull final CRecord aRecord) throws CException
{
    // fetch namespace
    final INamespace namespace = mDependencies.getNamespaceRegistry()
                                              .getNamespace(CWellKnownNID.SYSTEM);
    assert namespace != null;

    // fetch nanoservice registry
    final INanoServiceRegistry nsr = namespace.getNanoServiceRegistry();

    // add observer
    nsr.addObserver(CRecordNotifyTargetDeregistered.class,
                    getAddress(),
                    false);

    aEnvelope.setResultSuccess();
    return true;
}

Okay, now we need the handler for the CRecordNotifyTargetDeregistered message:

// constructor:
addMessageHandler(CRecordNotifyTargetDeregistered.ID,
                  this::asyncNotifyTargetDeregistered);
private boolean asyncNotifyTargetDeregistered(@NotNull final CEnvelope aEnvelope,
                                              @NotNull final CRecord aRecord)
{
    if (!aEnvelope.isAnswer())
    {
        final CTargetAddress address = CRecordNotifyTargetDeregistered.getAddress(aRecord,
                                                                                  null);
        final String name = CRecordNotifyTargetDeregistered.getName(aRecord,
                                                                    "");

        aEnvelope.setResultSuccess();

        return true;
    }
    return false;
}