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

Universal JAPI Push Services library. More...

#include <json-c/json.h>
#include <stdbool.h>
#include <pthread.h>
#include "japi.h"
Include dependency graph for japi_pushsrv.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  japi_pushsrv_context
 JAPI push service context. More...
 

Typedefs

typedef void(* japi_pushsrv_routine )(struct __japi_pushsrv_context *psc)
 JAPI push service routine.
 

Functions

japi_pushsrv_contextjapi_pushsrv_register (japi_context *ctx, const char *pushsrv_name)
 Register a JAPI push service. More...
 
int japi_pushsrv_destroy (japi_context *ctx, japi_pushsrv_context *psc)
 Remove push service context from japi context, unsubscribe for all clients and free memory. More...
 
int japi_pushsrv_sendmsg (japi_pushsrv_context *psc, json_object *jmsg)
 Send messages to all subscribed clients. More...
 
int japi_pushsrv_start (japi_pushsrv_context *psc, japi_pushsrv_routine routine)
 Start push service routine. More...
 
int japi_pushsrv_stop (japi_pushsrv_context *psc)
 Stop push service routine. More...
 

Detailed Description

Universal JAPI Push Services library.

Author
Deniz Armagan
Date
2019-03-25
Version
0.1

japi_pushsrv 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_pushsrv_destroy ( japi_context ctx,
japi_pushsrv_context psc 
)

Remove push service context from japi context, unsubscribe for all clients and free memory.

Clean up the push service if no more needed:

  • remove entry from linked list in ctx->push_services
  • unsubscribe all clients and free their memory
  • stop the push service and free the used memory
Parameters
ctxJAPI context
pscJAPI push service context which is to be removed
Returns
On success, 0 is returned. On error, -1 is returned.

References japi_pushsrv_context::clients, japi_pushsrv_remove_client(), japi_pushsrv_stop(), japi_pushsrv_context::lock, japi_pushsrv_context::next, japi_client::next, japi_context::push_services, and japi_client::socket.

Referenced by japi_destroy().

japi_pushsrv_context* japi_pushsrv_register ( japi_context ctx,
const char *  pushsrv_name 
)

Register a JAPI push service.

Register a JAPI push service specified by pushsrv_name.

Parameters
ctxJAPI context
pushsrv_namePush-Service name
Returns
On success, a pointer to the japi_push_service context is returned. On error, NULL is returned

References japi_pushsrv_context::clients, japi_pushsrv_context::enabled, japi_pushsrv_context::lock, japi_pushsrv_context::next, japi_context::push_services, japi_pushsrv_context::pushsrv_name, japi_pushsrv_context::routine, japi_pushsrv_context::thread_id, japi_context::userptr, and japi_pushsrv_context::userptr.

int japi_pushsrv_sendmsg ( japi_pushsrv_context psc,
json_object *  jmsg 
)

Send messages to all subscribed clients.

Send message to all subscribed clients of a push service.

Parameters
pscJAPI push service context
jmsgJSON push message
Returns
On success, number of successfull send messages is returned. 0 is returned, if no client is subscribed. On error, -1 is returned.

References japi_pushsrv_context::clients, japi_get_jobj_as_ndstr(), japi_pushsrv_remove_client(), japi_pushsrv_context::lock, japi_client::next, japi_pushsrv_context::pushsrv_name, japi_client::socket, and write_n().

int japi_pushsrv_start ( japi_pushsrv_context psc,
japi_pushsrv_routine  routine 
)

Start push service routine.

Create thread and start push service routine.

Parameters
pscJAPI push service context
routineThread Push service routine
Returns
On success, 0 is returned. On error, -1 is returned.

References japi_pushsrv_context::enabled, japi_pushsrv_context::routine, and japi_pushsrv_context::thread_id.

int japi_pushsrv_stop ( japi_pushsrv_context psc)

Stop push service routine.

Wait for thread to end and close it.

Parameters
pscJAPI push service context
Returns
On success, 0 is returned. On error, -1 is returned, -2 if thread is not running.

References japi_pushsrv_context::enabled, japi_pushsrv_context::pushsrv_name, and japi_pushsrv_context::thread_id.

Referenced by japi_pushsrv_destroy().