libjapi  0.3.2
Universal JSON API Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
japi.c File Reference

Universal JSON API library. More...

#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include "japi.h"
#include "creadline.h"
#include "japi_intern.h"
#include "japi_pushsrv.h"
#include "japi_pushsrv_intern.h"
#include "japi_utils.h"
#include "networking.h"
#include "prntdbg.h"
#include "rw_n.h"
Include dependency graph for japi.c:

Functions

int japi_process_message (japi_context *ctx, const char *request, char **response, int socket)
 Process the JSON request. More...
 
int japi_shutdown (japi_context *ctx)
 Shutdown the JAPI server. More...
 
int japi_destroy (japi_context *ctx)
 Destroy a JAPI context. More...
 
int japi_register_request (japi_context *ctx, const char *req_name, japi_req_handler req_handler)
 Register a JAPI request handler. More...
 
japi_contextjapi_init (void *userptr)
 Create and initialize a new JAPI context. More...
 
int japi_set_max_allowed_clients (japi_context *ctx, uint16_t num)
 Set the number of allowed clients. More...
 
int japi_include_args_in_response (japi_context *ctx, bool include_args)
 Configure context to include request arguments in response. More...
 
int japi_add_client (japi_context *ctx, int socket)
 Add client. More...
 
int japi_remove_client (japi_context *ctx, int socket)
 Remove client. More...
 
int japi_remove_all_clients (japi_context *ctx)
 Remove all clients. More...
 
int japi_start_server (japi_context *ctx, const char *port)
 Start a JAPI server. More...
 
void japi_cmd_list (japi_context *ctx, json_object *request, json_object *response)
 Provide the names of all registered commands as a JAPI response. More...
 
void japi_request_not_found_handler (japi_context *ctx, json_object *request, json_object *response)
 Default handler for reacting to unknown requests. More...
 

Detailed Description

Universal JSON API library.

Author
Christopher Stender
Date
2018-02-15
Version
0.1

libjapi is a universal JSON API library.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Function Documentation

int japi_add_client ( japi_context ctx,
int  socket 
)

Add client.

Add client to JAPI context client list.

Parameters
ctxJAPI context
socketThe socket to be added
Returns
On success, 0 is returned. On error, -1 if memory allocation failed, is returned.

References japi_context::clients, japi_client::crl_buffer, japi_context::lock, creadline_buf_t::nbytes, japi_client::next, japi_context::num_clients, and japi_client::socket.

Referenced by japi_start_server().

void japi_cmd_list ( japi_context ctx,
json_object *  request,
json_object *  response 
)

Provide the names of all registered commands as a JAPI response.

Provides the names of all registered commands as a JAPI response.

Parameters
ctxJAPI context
requestPointer to JAPI JSON request
responsePointer to JAPI JSON response
Note
Parameter 'request' declared, although not used in function. Function declaration needs to be identical to respective handler.

References japi_request::name, japi_request::next, and japi_context::requests.

Referenced by japi_init().

int japi_destroy ( japi_context ctx)

Destroy a JAPI context.

Destroy the JAPI context pointed to by ctx and release allocated memory.

Parameters
ctxJAPI context
Returns
On succes, zero is returned. On error, -1 is returned.

References japi_pushsrv_destroy(), japi_context::lock, japi_pushsrv_context::next, japi_request::next, japi_context::push_services, and japi_context::requests.

int japi_include_args_in_response ( japi_context ctx,
bool  include_args 
)

Configure context to include request arguments in response.

Configure context to include request arguments in response

Parameters
ctxJAPI context
include_argsInclude request arguments in response.
Returns
On success, zero is returned. On error, -1 for empty JAPI context, is returned.

References japi_context::include_args_in_response.

japi_context* japi_init ( void *  userptr)

Create and initialize a new JAPI context.

Create, initialize and return a new japi_context object.

Parameters
userptrPointer to user data or NULL. If a valid pointer is provided a request handler can access that data later.
Returns
On success, a japi_context object is returned. On error, NULL is returned.

References japi_context::clients, japi_context::include_args_in_response, japi_context::init, japi_cmd_list(), japi_pushsrv_list(), japi_pushsrv_subscribe(), japi_pushsrv_unsubscribe(), japi_register_request(), japi_request_not_found_handler(), japi_context::lock, japi_context::max_clients, japi_context::num_clients, japi_context::push_services, japi_context::requests, japi_context::shutdown, and japi_context::userptr.

int japi_process_message ( japi_context ctx,
const char *  request,
char **  response,
int  socket 
)

Process the JSON request.

Steps performed while processing a JSON request:

  • Convert the received message into a JSON object
  • Extract the request name
  • Search a suitable request handler
  • Call the request handler
  • Prepare the JSON response
  • Free memory
Parameters
ctxJapi context
requestRequest to process
responseFrom request build response
socketNetwork socket
Returns
On success, 0 returned. On error, -1 is returned.

References japi_context::include_args_in_response, japi_get_jobj_as_ndstr(), and japi_get_value_as_str().

Referenced by japi_start_server().

int japi_register_request ( japi_context ctx,
const char *  req_name,
japi_req_handler  req_handler 
)

Register a JAPI request handler.

Register a JAPI request handler provided as a function pointer req_handler for the request specified by req_name.

Parameters
ctxJAPI context
req_nameRequest name
req_handlerFunction pointer
Returns
On success, zero is returned. On error, -1 for empty JAPI context, -2 for empty request name, -3 for empty request handler, -4 for duplicate naming, -5 for failed memory allocation, -6 for bad request name (starting with "japi_") is returned.

References japi_request::func, japi_context::init, japi_request::name, japi_request::next, and japi_context::requests.

Referenced by japi_init().

int japi_remove_all_clients ( japi_context ctx)

Remove all clients.

Remove all clients from JAPI context and pushs service context.

Parameters
ctxJAPI context
Returns
On success, 0 is returned. On error, -1 is returned if a client cannot be removed.

References japi_context::clients, japi_remove_client(), japi_client::next, and japi_client::socket.

Referenced by japi_start_server().

int japi_remove_client ( japi_context ctx,
int  socket 
)

Remove client.

Remove client from JAPI context and pushs service context.

Parameters
ctxJAPI context
socketThe socket to be removed
Returns
On success, 0 is returned. On error, -1 if client is not removed, is returned.

References japi_context::clients, japi_pushsrv_remove_client_from_all_pushsrv(), japi_context::lock, japi_client::next, japi_context::num_clients, and japi_client::socket.

Referenced by japi_remove_all_clients(), and japi_start_server().

void japi_request_not_found_handler ( japi_context ctx,
json_object *  request,
json_object *  response 
)

Default handler for reacting to unknown requests.

Can be overwritten by the user by registering a command called request_not_found_handler. Is called if no suitable registered request is found for the received command. Adds a field named "error" to the response which contains a string describing the issue.

Parameters
ctxJAPI context
requestPointer to JAPI JSON request
responsePointer to JAPI JSON response
Note
Parameter 'ctx' and request' declared, although not used in function. Function declaration needs to be identical to respective handler.

Referenced by japi_init().

int japi_set_max_allowed_clients ( japi_context ctx,
uint16_t  num 
)

Set the number of allowed clients.

Set the maximal number of allowed clients.

Parameters
ctxJAPI context
numNumber of clients to be allowed. 0 stands for unlimited.
Returns
On success, zero is returned. On error, -1 for empty JAPI context, is returned.

References japi_context::max_clients.

int japi_shutdown ( japi_context ctx)

Shutdown the JAPI server.

Shutdown the JAPI server.

Parameters
ctxJAPI context
Returns
On success, zero is returned. On error, -1 for empty JAPI context, is returned.

References japi_context::shutdown.

int japi_start_server ( japi_context ctx,
const char *  port 
)

Start a JAPI server.

Start a JAPI server on the given port.

Parameters
ctxJAPI context
portPort to be used by the JAPI server
Returns
Only returns in case of an error.

References japi_context::clients, creadline_r(), japi_add_client(), japi_process_message(), japi_remove_all_clients(), japi_remove_client(), japi_context::max_clients, japi_context::num_clients, japi_context::shutdown, tcp_start_server(), and write_n().