Class: RequestDispatcher

iotcs.device.util.RequestDispatcher()

new RequestDispatcher()

This object is used for request messages dispatch. You can register handlers to an instance of this object that will handle request messages that come from the cloud and will return a response message associated for that request message.

There can be only one instance of this object (singleton) generated at first use.

Methods

(static) dispatch(requestMessage) → {iotcs.message.Message}

This is main function of the RequestDispatcher that dispatches a request message to the appropriate handler, if one is found and the handler is called so the appropriate response message is returned. If no handler is found, the RequestDispatcher implements a default request message dispatcher that would just return a 404 (Not Found) response message. This method will never return null.
Parameters:
Name Type Description
requestMessage object The request message to dispatch.
Returns:
The response message associated with the request.
Type
iotcs.message.Message

(static) getRequestHandler(endpointId, path) → {function}

Returns a registered request handler, if it is registered, otherwise null.
Parameters:
Name Type Description
endpointId string The endpoint ID that the handler was registered with.
path string The path that the handler was registered with.
Returns:
The actual handler or null.
Type
function

(static) registerRequestHandler(endpointId, path, handler)

This method registers a handler to the RequestDispatcher. The handler is a function that must have the form:
handler = function (requestMessage) { ... return responseMessage};
. Where requestMessage if a JSON representing the exact message received from the cloud that has the type REQUEST and responseMessage is an instance of iotcs.message.Message that has type RESPONSE. If neither of the conditions are satisfied the RequestDispatcher will use the default handler.

It is advisable to use the iotcs.message.Message.buildResponseMessage method for generating response messages.

Parameters:
Name Type Description
endpointId string The endpoint IDthat is the destination of the request message.
path string The path that is the "address" (resource definition) of the request message.
handler function The actual handler to be registered.
See:

(static) unregisterRequestHandler(handler, endpointId, path)

This method removed a handler from the registered handlers list of the RequestDispatcher. If handler is present as parameter, then endpointId and path parameters are ignored.
Parameters:
Name Type Description
handler function The reference to the handler to be removed.
endpointId string The endpoint id that the handler was registered with.
path string The path that the handler was registered with.

Home