C Messaging API Reference for Oracle Internet of Things Cloud Service Client Software Library. Release 21.1.1.0.0-3. E70344-26
iotcs_messaging.h File Reference

The file contains API for IOT CS Message and Request Dispatchers. More...

#include "iotcs_message.h"

Go to the source code of this file.

Typedefs

typedef void(* iotcs_message_dispatcher_delivery_callback )(iotcs_message *message)
 typedef for message dispatcher send callback.
typedef void(* iotcs_message_dispatcher_error_callback )(iotcs_message *message, iotcs_result result, const char *fail_reason)
 typedef for message dispatcher receive callback.
typedef void(* iotcs_resource_handler )(iotcs_request_message *request, void *arg, iotcs_message *response)
 resource handler callback function After handler invocation request id and url is copied from 'request' and 'request' is freed.

Functions

void iotcs_message_dispatcher_set_delivery_callback (iotcs_message_dispatcher_delivery_callback delivery_cb)
 set delivery callback for IoT CS message dispatcher
void iotcs_message_dispatcher_set_error_callback (iotcs_message_dispatcher_error_callback error_cb)
 set error callback for IoT CS message dispatcher
iotcs_result iotcs_message_dispatcher_queue (iotcs_message *message)
 IoT CS message dispatcher send method.
iotcs_result iotcs_offer (iotcs_message *message)
iotcs_result iotcs_send (iotcs_message messages[], size_t len)
 Send a number of messages to IOT server.
iotcs_request_messageiotcs_receive (int32_t timeout)
 Receive a iotcs_request_message from the server, if any.
void iotcs_request_message_free (iotcs_request_message *message)
 Destroy request message when it is no longer needed.
iotcs_result iotcs_request_dispatcher_init (void)
 Initializes a request dispatcher.
void iotcs_request_dispatcher_finalize (void)
 Finalizes a request dispatcher.
void iotcs_request_dispatcher_dispatch (iotcs_request_message *request_message, iotcs_message *response_message)
 Match the request to a handler and invoke the handler.
iotcs_result iotcs_register_request_handler (const char *endpoint_id, const char *path, iotcs_resource_handler handler, void *arg)
 Register a handler for the given endpoint and path.
iotcs_result iotcs_unregister_request_handler_all (iotcs_resource_handler handler)
 Un-register the given handler for all endpoints and paths for which the handler may be registered.
iotcs_result iotcs_unregister_request_handler (const char *endpoint_id, const char *path)
 Un-register the handler for the given endpoint and path.
iotcs_resource_handler iotcs_get_request_handler (const char *endpoint_id, const char *path)
 Lookup a iotcs_resource_handler for the given endpoint_id and path.

Detailed Description

The file contains API for IOT CS Message and Request Dispatchers.


Typedef Documentation

typedef for message dispatcher send callback.

Note:
The implementation of callback should not call iotcs_message_dispatcher_queue procedure to avoid a possible deadlock on waiting for the message sending status from the calling thread.
Parameters:
messagepointer to iotcs_message

typedef for message dispatcher receive callback.

Note:
The implementation of callback should not call iotcs_message_dispatcher_queue procedure to avoid a possible deadlock on waiting for the message sending status from the calling thread.
Parameters:
messagepointer to iotcs_message
resultiotcs_result
fail_reasonstring with fail reason
typedef void(* iotcs_resource_handler)(iotcs_request_message *request, void *arg, iotcs_message *response)

resource handler callback function After handler invocation request id and url is copied from 'request' and 'request' is freed.

Parameters:
requestrequest received from server
argoptional argument given during registration (iotcs_register_request_handler call)
responseaddress of request to be filled

Function Documentation

iotcs_resource_handler iotcs_get_request_handler ( const char *  endpoint_id,
const char *  path 
)

Lookup a iotcs_resource_handler for the given endpoint_id and path.

The lookup will return the most specific handler for the endpoint_id and path. If there is not a specific match for endpoint_id, the lookup will try to match ("*", path). Failing that, the lookup will return a handler for ("*", "*"), or the default handler if no handler for ("*","*") has been registered. Null or empty parameters are interpreted as a wildcard.

Parameters:
endpoint_idthe endpoint id that has the given the resource path, or a wildcard ("*").
paththe resource path, or a wildcard ("*").
Return values:
iotcs_resource_handlerif success.
NULLotherwise.
iotcs_result iotcs_message_dispatcher_queue ( iotcs_message message)

IoT CS message dispatcher send method.

Parameters:
messagepointer to iotcs_message
Note:
This procedure should not be called from the callback implementation due to the possible deadlock on waiting for the message sending status from the current thread.
Return values:
IOTCS_RESULT_INVALID_ARGUMENTif message is NULL.
IOTCS_RESULT_OKif succeeds.
IOTCS_RESULT_FAILotherwise.
void iotcs_message_dispatcher_set_delivery_callback ( iotcs_message_dispatcher_delivery_callback  delivery_cb)

set delivery callback for IoT CS message dispatcher

Parameters:
delivery_cbdelivery callback
void iotcs_message_dispatcher_set_error_callback ( iotcs_message_dispatcher_error_callback  error_cb)

set error callback for IoT CS message dispatcher

Parameters:
error_cberror callback
iotcs_result iotcs_offer ( iotcs_message message)
Return values:
IOTCS_RESULT_INVALID_ARGUMENTif some of messages are NULL or len is negative or zero.
IOTCS_RESULT_CANNOT_AUTHORIZEif there are problems with authorization.
IOTCS_RESULT_OKif succeeds.
IOTCS_RESULT_FAILotherwise.
iotcs_request_message* iotcs_receive ( int32_t  timeout)

Receive a iotcs_request_message from the server, if any.

This call may block if the implementation sends a message to the server to poll for requests. This call may return null.

Parameters:
timeoutmaximum time, in milliseconds, to wait for a response from the server
Return values:
iotcs_request_messagepointer to request message.
NULLif empty or no request from the server received during the timeout.
iotcs_result iotcs_register_request_handler ( const char *  endpoint_id,
const char *  path,
iotcs_resource_handler  handler,
void *  arg 
)

Register a handler for the given endpoint and path.

If endpoint is null, empty, or a wildcard ("*"), then the handler is registered as being used for all endpoints. If path is null, empty, or a wildcard ("*") then the handler is registered as the handler for any requests that do not have a more specific handler.

Parameters:
endpoint_idthe endpoint id that has the given the resource path, or a wildcard ("*") for any endpoint.
paththe resource path, or a wildcard ("*") for any resource.
handlerthe handler to invoke for requests to this endpoint and path
argargument to be passed to handler
Return values:
IOTCS_RESULT_INVALID_ARGUMENTif handler is NULL
IOTCS_RESULT_OUT_OF_MEMORYif no additional memory can be allocated for use
IOTCS_RESULT_OKif success
void iotcs_request_dispatcher_dispatch ( iotcs_request_message request_message,
iotcs_message response_message 
)

Match the request to a handler and invoke the handler.

The function looks up for a request handler based on endpoint_id and url. If no user callback is found then default handler (does nothing if not overridden) will be invoked.

Before the handler invocation the given response_message is filled with zeros, status code is set to 404 and response_message->base is initialized with a pointer to default constant values (type = IOTCS_MESSAGE_RESPONSE, priority = IOTCS_MESSAGE_PRIORITY_HIGH, reliability = IOTCS_MESSAGE_RELIABILITY_BEST_EFFORT).

Next the handler is invoked. A handler is responsible for filling in response_message with required data. If the handler performs no actions, server will get 404 code.

After handler invocation request_message is linked to response_message by setting field response_message->u.response.request. This is required so when response_message is sent, iotcs_request_message_free will be automatically called in order to deallocate request_message resources. For operation without asynchronous message dispatcher it means that if no iotcs_send was called then user must explicitely release request by calling iotcs_request_message_free with request_message as an argument. In asynchronous mode with message dispatcher everything is sent and released automatically.

Parameters:
request_messageThe request message to be dispatched
response_messageThe response message to be filled by handler
iotcs_result iotcs_request_dispatcher_init ( void  )

Initializes a request dispatcher.

Return values:
IOTCS_RESULT_OKif success
IOTCS_RESULT_FAILotherwise
void iotcs_request_message_free ( iotcs_request_message message)

Destroy request message when it is no longer needed.

Parameters:
message
iotcs_result iotcs_send ( iotcs_message  messages[],
size_t  len 
)

Send a number of messages to IOT server.

This method blocks until all messages are sent. All incoming request messages are put into library-maintained request queue and can be retrieved from it by calling receive method.

Parameters:
messagesArray of iotcs_message to be sent.
lenNumber of messages in array to be sent.
Return values:
IOTCS_RESULT_INVALID_ARGUMENTif some of messages are NULL or len is negative or zero.
IOTCS_RESULT_CANNOT_AUTHORIZEif there are problems with authorization.
IOTCS_RESULT_OKif succeeds.
IOTCS_RESULT_FAILotherwise.
iotcs_result iotcs_unregister_request_handler ( const char *  endpoint_id,
const char *  path 
)

Un-register the handler for the given endpoint and path.

Either or both of the parameters may be a wildcard ("*"). Null or empty parameters are interpreted as a wildcard.

Parameters:
endpoint_idthe endpoint id that has the given the resource path.
paththe resource path.
Return values:
IOTCS_RESULT_OKif the handler was unregistered successfully or wasn't found in registered handlers.
IOTCS_RESULT_FAILotherwise.
iotcs_result iotcs_unregister_request_handler_all ( iotcs_resource_handler  handler)

Un-register the given handler for all endpoints and paths for which the handler may be registered.

Parameters:
handlerthe handler to un-register
Return values:
IOTCS_RESULT_INVALID_ARGUMENTif handler is NULL
IOTCS_RESULT_OKif the handler was unregistered successfully or wasn't found in registered handlers
IOTCS_RESULT_FAILotherwise