How to offer a nano service

Service, van Gogh

Nano services are needed for three purposes:

  • Offering notifications.

    For a nano service, interested targets can register an observer. The notification trigger sends messages to the nano service, of which each observer receives a copy.
  • Operation of a tiny services

    The owner registers a nano service and registers himself as an observer. Clients of the service send a message to the nano service, which is forwarded to the owner. Advantage: The client does not need the address of the providing service.
  • Observation

    A silent observer can register an observer for a nano service and get copies of the messages to the service.

Prerequisite for the tutorial

You should have read these tutorials beforehand:
Package initialization
How to start a simple target
How to register a nano service
How to add an observer to a nano service

Preliminary work

As described in the tutorials, first a nano service is registered and then an observer with its own address is added.

Nano service message processing

Another message handler is added in the constructor.

addMessageHandler(CRecordAppRunApplication.ID,
                  this::asyncStartApp);

The method of the message handler is implemented:

  • The data is fetched from the message if necessary.
  • The data will be verified.
  • Something will be done with it.
  • New data is placed in the message (the same message is returned as a response).
  • A result code is set.
  • After leaving the handler, the message is automatically sent back to the sender by the system (if the sender wishes a reply).
private boolean asyncStartApp(@NotNull final CEnvelope aEnvelope,
                              @NotNull final CRecord aRecord) throws CException
{
    // get preset name
    final String presetName = CRecordAppRunApplication.getPresetName(aRecord,
                                                                     null);
    CUtilCheck.checkEmptyString(presetName,
                                "Missing Preset Name.");

    // get remote skin client
    final CNodeAddress remoteSkinClient = CRecordAppRunApplication.getRemoteSkinClient(aRecord,
                                                                                       null);

    // get session token
    final UUID sessionToken = CRecordAppRunApplication.getSessionToken(aRecord,
                                                                       null);
    CUtilCheck.checkNotNull(sessionToken,
                            "Missing Session Token");

    // ...
    // Do something

    aEnvelope.setResult(CResultCode.SUCCESS,
                        "");

    return true;
}

nyssr.net - Innovative Distributed System