NAP
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
APIService Class Reference

#include <apiservice.h>

Public Member Functions

 APIService (ServiceConfiguration *configuration)
 
 ~APIService () override
 
bool sendFloat (const char *id, float value, utility::ErrorState *error)
 
bool sendString (const char *id, const char *value, utility::ErrorState *error)
 
bool sendInt (const char *id, int value, utility::ErrorState *error)
 
bool sendByte (const char *id, nap::uint8 value, utility::ErrorState *error)
 
bool sendBool (const char *id, bool value, utility::ErrorState *error)
 
bool sendLong (const char *id, int64_t value, utility::ErrorState *error)
 
bool sendChar (const char *id, char value, utility::ErrorState *error)
 
bool send (const char *id, utility::ErrorState *error)
 
bool sendEvent (APIEventPtr apiEvent, utility::ErrorState *error)
 
bool sendMessage (const char *json, utility::ErrorState *error)
 
bool sendIntArray (const char *id, int *array, int length, utility::ErrorState *error)
 
bool sendFloatArray (const char *id, float *array, int length, utility::ErrorState *error)
 
bool sendByteArray (const char *id, uint8_t *array, int length, utility::ErrorState *error)
 
bool sendStringArray (const char *id, const char **array, int length, utility::ErrorState *error)
 
void processEvents ()
 
void dispatchEvent (nap::APIEventPtr apiEvent)
 
- Public Member Functions inherited from Service
UNPREFIXED_MODULE_NAME_INPUTCASE Service (ServiceConfiguration *configuration)
 
virtual void getDependentServices (std::vector< rtti::TypeInfo > &dependencies) override
 
 Service (ServiceConfiguration *configuration)
 
virtual ~Service ()
 
CoregetCore ()
 
const CoregetCore () const
 
std::string getTypeName () const
 
const ModulegetModule () const
 
 Service (Service &)=delete
 
Serviceoperator= (const Service &)=delete
 
 Service (Service &&)=delete
 
Serviceoperator= (Service &&)=delete
 

Public Attributes

nap::Signal< const APIEvent & > eventDispatched
 

Protected Member Functions

virtual bool init (utility::ErrorState &error) override
 
virtual void shutdown () override
 
virtual void update (double deltaTime) override
 
- Protected Member Functions inherited from Service
virtual void registerObjectCreators (rtti::Factory &factory)
 
virtual void getDependentServices (std::vector< rtti::TypeInfo > &dependencies)
 
virtual void created ()
 
virtual void preUpdate (double deltaTime)
 
virtual void postUpdate (double deltaTime)
 
virtual void preShutdown ()
 
virtual void preResourcesLoaded ()
 
virtual void postResourcesLoaded ()
 
template<typename SERVICE_CONFIG >
SERVICE_CONFIG * getConfiguration ()
 
template<typename SERVICE_CONFIG >
const SERVICE_CONFIG * getConfiguration () const
 
std::string getIniFilePath () const
 
std::string getIniFilePath (const std::string &appendix) const
 

Description

Offers a C-Style interface that can be used to send and receive data from a running NAP application. Use this interface to wrap a REST-like interface around your NAP application.

Use the various utility functions, such as sendInt() and sendIntArray() to send data to a running NAP application. All 'send' methods copy the arguments by value, making it hard to leak memory. The copied data is converted (moved) into a nap::APIEvent and forwarded to an interested nap::APIComponent. Install listeners on an api component to provide logic when a function is called through this interface.

Messages with variable number of arguments can be constructed and sent using sendMessage(). This call accepts a JSON formatted string that can contain multiple API messages as a bundle, where each message contains a variable number of arguments. See sendMessage() for more information.

When sending data to an app a unique nap::APIEvent is created. Events aren't processed immediately but stored in a queue. This ensures that the recording and processing of api events is thread-safe, similar to how osc events are processed. To process all the recorded api events call processEvents(). processEvents() is called for you automatically when using an application event handler.

This service can also be used to dispatch events to an external environment, often as a reply to a previously received message. In order for an external environment to receive an event it needs to listen to the messageDispatched signal. Events are dispatched immediately, without being queued.

To dispatch an event from a NAP application, often in reply to a previously received message, use dispatchEvent(). Sending messages to a NAP application is completely thread safe. Dispatching messages from a running NAP application is also completely thread safe. When processing events manually by calling processEvents() it is recommended to do that from the thread that initializes and shut down NAP.

Inheritance diagram for APIService:
[legend]
Collaboration diagram for APIService:
[legend]

Constructor & Destructor Documentation

◆ APIService()

APIService ( ServiceConfiguration configuration)

Default constructor

◆ ~APIService()

~APIService ( )
override

Destructor

Member Function Documentation

◆ dispatchEvent()

void dispatchEvent ( nap::APIEventPtr  apiEvent)

Dispatches an event to an external environment. NAP applications often dispatch events as a reply to a previously received message, after processing. Events are dispatched immediately, there is no dispatch queue. In order for an external environment to receive this message at least one type of EventDispatcher needs to be declared in json. The given event is destroyed after calling this function.

Parameters
apiEventthe event to send to the external environment.

◆ init()

virtual bool init ( utility::ErrorState error)
overrideprotectedvirtual

Initialize the API service.

Parameters
errorcontains the error message if initialization fails.
Returns
the if initialization succeeded.

Reimplemented from Service.

◆ processEvents()

void processEvents ( )

Processes all given api commands. This is called automatically on update but can be called manually from an external environment when there is no application loop. All events are recorded before being processed, allowing for thread safe execution of the api events. When calling this manually, call it from the same thread that initializes and shuts down NAP.

◆ send()

bool send ( const char *  id,
utility::ErrorState error 
)

Sends a single signal to a NAP application Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with signal.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendBool()

bool sendBool ( const char *  id,
bool  value,
utility::ErrorState error 
)

Sends a single bool value to a NAP application. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with bool value.
valuethe bool to send.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendByte()

bool sendByte ( const char *  id,
nap::uint8  value,
utility::ErrorState error 
)

Sends a single byte value to a NAP application. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with byte value.
valuethe byte to send.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendByteArray()

bool sendByteArray ( const char *  id,
uint8_t *  array,
int  length,
utility::ErrorState error 
)

Sends an array of bytes to a NAP application, a copy of the data in the array is made. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with byte array.
arraythe array data to send.
lengththe number of elements in the array.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendChar()

bool sendChar ( const char *  id,
char  value,
utility::ErrorState error 
)

Sends a single char value to a NAP application. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with char value.
valuethe char to send.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendEvent()

bool sendEvent ( APIEventPtr  apiEvent,
utility::ErrorState error 
)

Sends a user constructed api event to a NAP application. Processing of the generated event is deferred until processEvents() is called.

Parameters
apiEventthe event to forward to the running application
errorcontains the error if sending fails
Returns
if sending succeeded

◆ sendFloat()

bool sendFloat ( const char *  id,
float  value,
utility::ErrorState error 
)

Send a single float value to a NAP application. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with float value.
valuethe float value to send.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendFloatArray()

bool sendFloatArray ( const char *  id,
float *  array,
int  length,
utility::ErrorState error 
)

Sends an array of floats to a NAP application, a copy of the data in the array is made. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with float array.
arraythe array data to send.
lengththe number of elements in the array.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendInt()

bool sendInt ( const char *  id,
int  value,
utility::ErrorState error 
)

Sends a single int value to a NAP application. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with int value.
valuethe int to send.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendIntArray()

bool sendIntArray ( const char *  id,
int *  array,
int  length,
utility::ErrorState error 
)

Sends an array of ints to a NAP application, a copy of the data in the array is made. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with int array.
arraythe array data to send.
lengththe number of elements in the array.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendLong()

bool sendLong ( const char *  id,
int64_t  value,
utility::ErrorState error 
)

Sends a single long value to a NAP application. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with long value.
valuethe long to send.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendMessage()

bool sendMessage ( const char *  json,
utility::ErrorState error 
)

Interprets the string as JSON and sends the individual messages as separate events to a NAP application. This call extracts nap::Message objects from the json stream. Each message is converted into an api event that is forwarded to the application. Every message holds a custom number of nap::APIValue's and is therefore a powerful way to send custom information to a NAP application.

After extracting all the messages, which translate into separate events, the system checks if the application accepts the data by matching a declared nap::APISignature. For the app to accept an event the message's 'mID' property must match one of the nap::Signature resources inside the application. The amount of nap::APIValue's and the type of those values must also match. This ensures the app always receives correct information.

Processing of the generated event is deferred until processEvents() is called.

Example of a simple API Message in JSON:

{
"Objects":
[
{
"Type": "nap::APIMessage",
"Name": "updateView",
"mID": "0284761",
"Arguments":
[
{
"Type": "nap::APILong",
"mID": "startTime",
"Value": 2000
},
{
"Type": "nap::APILong",
"mID": "endTime",
"Value": 3000
},
{
"Type": "nap::APIInt",
"mID": "samples",
"Value": 30
}
]
}
]
}

Note that the arguments can be of any type as defined in apivalue.h, including arrays. Multiple messages can be combined inside the Objects array. Every api message in that array is converted into an event and given to the nap application.

Parameters
jsonthe json string to parse and extract messages from.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendString()

bool sendString ( const char *  id,
const char *  value,
utility::ErrorState error 
)

Send a single string value to a NAP application. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with string value.
valuethe string to send.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ sendStringArray()

bool sendStringArray ( const char *  id,
const char **  array,
int  length,
utility::ErrorState error 
)

Sends an array of string values to a NAP application, a copy of the data in the array is made. Processing of the generated event is deferred until processEvents() is called.

Parameters
idmethod associated with string array.
arraythe array data to send.
lengththe number of elements in the array.
errorcontains the error if sending fails.
Returns
if sending succeeded

◆ shutdown()

virtual void shutdown ( )
overrideprotectedvirtual

Shuts down the API service

Reimplemented from Service.

◆ update()

virtual void update ( double  deltaTime)
overrideprotectedvirtual

Consumes and pushes all api events to registered api components

Reimplemented from Service.

Member Data Documentation

◆ eventDispatched

nap::Signal<const APIEvent&> eventDispatched

Listen to this signal in your external environment to receive outgoing NAP api events. An event is often dispatched as a reply to a previously received message.