CRecordGetTargetInfo

Fetch information about a registered target.

<record id="f7bdd19a-fd38-4eac-a3a2-c5957196e6d9" name="GET_TARGET_INFO" isService="true">

    <description>
        Get information about one or more targets.
    </description>

    <namespaces>SYSTEM</namespaces>

    <slot key="tid" name="TID" answer="false" mandatory="false" type="ID">
        <description>Optional: request information about a single target ID.</description>
    </slot>

    <slot key="tiDs" name="TIDS" answer="false" mandatory="false" type="ID_ARRAY">
        <description>Optional: request information about some target IDs.</description>
    </slot>

    <slot key="all" name="ALL_TARGETS" answer="false" mandatory="false" type="BOOLEAN">
        <description>True = request information about all registered targets.</description>
    </slot>

    <slot key="tgTs" name="TARGETS" answer="true" mandatory="false" type="RECORD_ARRAY">
        <description>An array of records of type TARGET_INFO</description>
    </slot>

</record>

Example of use of the class CRecordGetTargetInfo (after generating)

There are three ways to fetch information about targets by message. The first option: we get the information for a single target.

private void getTargetInfoForOneTarget(@NotNull final IId aNamespaceId,
                                       @NotNull final IId aTargetId) throws CException
{
    final CEnvelope env = CEnvelope.forLocalNanoService(CWellKnownNID.SYSTEM);

    final CRecord record = CRecordGetTargetInfo.create();
    CRecordGetTargetInfo.setNamespaceId(record,
                                        aNamespaceId);
    CRecordGetTargetInfo.setTid(record,
                                aTargetId);
    sendRequest(env,
                record);
}

The second option: we fetch information about multiple targets.

private void getTargetInfoForMultipleTargets(@NotNull final IId aNamespaceId,
                                             @NotNull final IId[] aTargetIds) throws CException
{
    final CEnvelope env = CEnvelope.forLocalNanoService(CWellKnownNID.SYSTEM);

    final CRecord record = CRecordGetTargetInfo.create();
    CRecordGetTargetInfo.setNamespaceId(record,
                                        aNamespaceId);
    CRecordGetTargetInfo.setTids(record,
                                 aTargetIds);
    sendRequest(env,
                record);
}

The third option: we fetch information about all targets in the namespace.

private void getTargetInfoForAllTargets(@NotNull final IId aNamespaceId) throws CException
{
    final CEnvelope env = CEnvelope.forLocalNanoService(CWellKnownNID.SYSTEM);

    final CRecord record = CRecordGetTargetInfo.create();
    CRecordGetTargetInfo.setNamespaceId(record,
                                        aNamespaceId);
    CRecordGetTargetInfo.setAllTargets(record,
                                       true);
    sendRequest(env,
                record);
}

To catch the reply, we need a message handler. We add it in the constructor of the message handler registry.

// constructor:
addMessageHandler(CRecordGetTargetInfo.ID,
                  this::asyncGetTargetInfo);

Regardless of which option is chosen, the answer is always handled in the same way:

private boolean asyncGetTargetInfo(@NotNull final CEnvelope aEnvelope,
                                   @NotNull final CRecord aRecord)
{
    if (aEnvelope.isAnswer())
    {
        final int rc = aEnvelope.getResultCode();
        if (rc == CResultCode.SUCCESS)
        {
            final CRecord[] targets = CRecordGetTargetInfo.getTargets(aRecord,
                                                                      null);
            for (final CRecord target : targets)
            {
                final IId tid = CRecordDataTargetInfo.getTid(target,
                                                             null);
                final String className = CRecordDataTargetInfo.getClassName(target,
                                                                            "");
                final String classSimpleName = CRecordDataTargetInfo.getClassSimpleName(target,
                                                                                        "");
                final IId qid = CRecordDataTargetInfo.getQid(target,
                                                             null);
                // ...
            }

            // ...
        }
        return true;
    }
    return false;
}

nyssr.net - Innovative Distributed System