Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
name: c-core
schema: 1
version: 2.11.1
version: 2.12.0
scm: github.com/pubnub/c-core
changelog:
- version: v2.12.0
date: Nov 15, 2019
changes:
- type: enhancement
text: Support automatic sending of heartbeat messages (only on POSIX, callback, for now).
- version: v2.11.1
date: Oct 25, 2019
changes:
Expand Down
75 changes: 75 additions & 0 deletions core/pbauto_heartbeat_sync.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
#include "pubnub_assert.h"
#include "pubnub_api_types.h"

#include <stddef.h>

struct pubnub_;

/** A pubnub context. An opaque data structure that holds all the data
needed for a context.
*/
typedef struct pubnub_ pubnub_t;

int pubnub_enable_auto_heartbeat(pubnub_t* pb, size_t period_sec)
{
PUBNUB_UNUSED(pb);
return 0;
}

int pubnub_set_heartbeat_period(pubnub_t* pb, size_t period_sec)
{
PUBNUB_UNUSED(pb);
return 0;
}

void pubnub_disable_auto_heartbeat(pubnub_t* pb)
{
PUBNUB_UNUSED(pb);
}

bool pubnub_is_auto_heartbeat_enabled(pubnub_t* pb)
{
PUBNUB_UNUSED(pb);
return false;
}

void pubnub_heartbeat_free_thumpers(void)
{
return;
}

void pbauto_heartbeat_read_channelInfo(pubnub_t const* pb,
char const** channel,
char const** channel_group)
{
PUBNUB_UNUSED(pb);
}

void pbauto_heartbeat_transaction_ongoing(pubnub_t const* pb)
{
PUBNUB_UNUSED(pb);
}

void pbauto_heartbeat_start_timer(pubnub_t const* pb)
{
PUBNUB_UNUSED(pb);
}

void pbauto_heartbeat_free_channelInfo(pubnub_t* pb)
{
PUBNUB_UNUSED(pb);
}

enum pubnub_res pbauto_heartbeat_prepare_channels_and_ch_groups(pubnub_t* pb,
char const** channel,
char const** channel_group)
{
PUBNUB_UNUSED(pb);
return PNR_OK;
}

void pbauto_heartbeat_stop(void)
{
return;
}
1 change: 1 addition & 0 deletions core/pbcc_advanced_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if PUBNUB_USE_ADVANCED_HISTORY
#include "pubnub_memory_block.h"
#include "pubnub_server_limits.h"
#include "pubnub_advanced_history.h"
#include "pubnub_version.h"
#include "pubnub_json_parse.h"
Expand Down
42 changes: 21 additions & 21 deletions core/pubnub_actions_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
@return #PNR_STARTED on success, an error otherwise
*/
enum pubnub_res pubnub_add_message_action(pubnub_t* pb,
char const* channel,
char const* message_timetoken,
enum pubnub_action_type actype,
char const* value);
char const* channel,
char const* message_timetoken,
enum pubnub_action_type actype,
char const* value);


/** Searches the response(if previous transaction on the @p pb context had been
pubnub_add_action and was accomplished successfully) and retrieves timetoken of a message
action was added on.
pubnub_add_message_action and was accomplished successfully) and retrieves timetoken of
a message action was added on.
If key expected is not found, preconditions(about right transaction) were not fulfilled,
or error was encountered,
returned structure has 0 'size' field and NULL 'ptr' field.
Expand All @@ -43,7 +43,7 @@ pubnub_chamebl_t pubnub_get_message_timetoken(pubnub_t* pb);


/** Searches the response(if previous transaction on the @p pb context had been
pubnub_add_action and was accomplished successfully) and retrieves timetoken of a
pubnub_add_message_action and was accomplished successfully) and retrieves timetoken of a
resently added action.
If key expected is not found, preconditions were not fulfilled, or error was encountered,
returned structure has 0 'size' field and NULL 'ptr' field.
Expand All @@ -68,16 +68,16 @@ pubnub_chamebl_t pubnub_get_message_action_timetoken(pubnub_t* pb);
@return #PNR_STARTED on success, an error otherwise
*/
enum pubnub_res pubnub_remove_message_action(pubnub_t* pb,
char const* channel,
pubnub_chamebl_t message_timetoken,
pubnub_chamebl_t action_timetoken);
char const* channel,
pubnub_chamebl_t message_timetoken,
pubnub_chamebl_t action_timetoken);


/** Initiates transaction that returns all actions added on a given @p channel between @p start
and @p end action timetoken.
The response to this transaction can be partial and than it contains the hyperlink string
value to the rest.
@see pubnub_get_actions_more()
@see pubnub_get_message_actions_more()
If there is no actions data, nor error description in the response it is considered
format error.
@param pb The pubnub context. Can't be NULL
Expand All @@ -92,10 +92,10 @@ enum pubnub_res pubnub_remove_message_action(pubnub_t* pb,
@return #PNR_STARTED on success, an error otherwise
*/
enum pubnub_res pubnub_get_message_actions(pubnub_t* pb,
char const* channel,
char const* start,
char const* end,
size_t limit);
char const* channel,
char const* start,
char const* end,
size_t limit);


/** This function expects previous transaction to be the one for reading the actions and
Expand All @@ -119,7 +119,7 @@ enum pubnub_res pubnub_get_message_actions_more(pubnub_t* pb);
and @p end message timetoken.
The response to this transaction can be partial and than it contains the hyperlink string
value to the rest.
@see pubnub_history_with_actions_more()
@see pubnub_history_with_message_actions_more()
If there is no actions data, nor error description in the response it is considered
format error.
@param pb The pubnub context. Can't be NULL
Expand All @@ -134,10 +134,10 @@ enum pubnub_res pubnub_get_message_actions_more(pubnub_t* pb);
@return #PNR_STARTED on success, an error otherwise
*/
enum pubnub_res pubnub_history_with_message_actions(pubnub_t* pb,
char const* channel,
char const* start,
char const* end,
size_t limit);
char const* channel,
char const* start,
char const* end,
size_t limit);


/** This function expects previous transaction to be the one for reading the history with
Expand All @@ -147,7 +147,7 @@ enum pubnub_res pubnub_history_with_message_actions(pubnub_t* pb,
in the existing response context buffer which it uses for obtaining another
part of the server response. Anotherwords, once the hyperlink is found in the existing
response it is used for initiating new request and function than behaves as
pubnub_history_with_actions().
pubnub_history_with_message_actions().
If there is no hyperlink encountered in the previous transaction response it
returns success: PNR_GOT_ALL_ACTIONS meaning that the answer is complete.
@param pb The pubnub context containing response buffer to be searched. Can't be NULL
Expand Down
2 changes: 2 additions & 0 deletions core/pubnub_alloc_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ int pubnub_free(pubnub_t *pb)
pbnc_stop(pb, PNR_CANCELLED);
if (PBS_IDLE == pb->state) {
PUBNUB_LOG_TRACE("pubnub_free(%p) PBS_IDLE\n", pb);
pubnub_disable_auto_heartbeat(pb);
pbauto_heartbeat_free_channelInfo(pb);
pb->state = PBS_NULL;
#if defined(PUBNUB_CALLBACK_API)
pbntf_requeue_for_processing(pb);
Expand Down
2 changes: 2 additions & 0 deletions core/pubnub_alloc_std.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ int pubnub_free(pubnub_t* pb)
pbnc_stop(pb, PNR_CANCELLED);
if (PBS_IDLE == pb->state) {
PUBNUB_LOG_TRACE("pubnub_free(%p) PBS_IDLE\n", pb);
pubnub_disable_auto_heartbeat(pb);
pbauto_heartbeat_free_channelInfo(pb);
pb->state = PBS_NULL;
#if defined(PUBNUB_CALLBACK_API)
pbntf_requeue_for_processing(pb);
Expand Down
2 changes: 2 additions & 0 deletions core/pubnub_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ enum pubnub_res {
PNR_REPLY_TOO_BIG,
/** An internal error in processing */
PNR_INTERNAL_ERROR,
/** Ran out of dynamic memory */
PNR_OUT_OF_MEMORY,
/** Encryption (and decryption) not supported */
PNR_CRYPTO_NOT_SUPPORTED,
/** Bad compression format or compressed data corrupted */
Expand Down
106 changes: 106 additions & 0 deletions core/pubnub_auto_heartbeat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
#if !defined INC_PBAUTO_HEARTBEAT
#define INC_PBAUTO_HEARTBEAT

#if PUBNUB_USE_AUTO_HEARTBEAT
/** Maximum number of auto heartbeat thumpers that can be used at one time */
#define PUBNUB_MAX_HEARTBEAT_THUMPERS 16
#define UNASSIGNED PUBNUB_MAX_HEARTBEAT_THUMPERS


/** Pubnub context fields for saving subscribed channels and channel groups
*/
#define M_channelInfo() \
struct { \
char* channel; \
char* channel_group; \
} channelInfo;

/** Pubnub context fields for heartbeat info used by the module for keeping presence.
*/
#define M_heartbeatInfo() unsigned thumperIndex;

/** Enables periodical heartbeats that keep presence on subscribed channels and channel
groups for uuid provided in @p pb context and sets chosen heartbeat period.
Initially auto heartbeat on @p pb context is disabled.

This module keeps presence by performing pubnub_heartbeat() transaction periodicaly
with uuid given whenever subscription on @p pb context is not in progress and
auto heartbeat is enabled. This process is independent from anything user
may be doing with the context when its not subscribing(, or heartbeating).

If the uuid(or any other relevant data, like dns server, or proxy) is changed at
some point, the module will update it automatically in its heartbeats.
The same goes if the uuid leaves some of the channels, or channel groups.

@param pb The pubnub context. Can't be NULL
@param period_sec Auto heartbeat thumping period in seconds
@return 0 on success, -1 otherwise
*/
int pubnub_enable_auto_heartbeat(pubnub_t* pb, size_t period_sec);

/** Changes auto heartbeat thumping period. If auto heartbeat is desabled on
the @p pb context the period wan't be changed and function returns error.
@param pb The pubnub context. Can't be NULL
@param period_sec Auto heartbeat thumping period in seconds
@return 0 on success, -1 otherwise
*/
int pubnub_set_heartbeat_period(pubnub_t* pb, size_t period_sec);

/** Disables auto heartbeat on the @p pb context.
*/
void pubnub_disable_auto_heartbeat(pubnub_t* pb);

/** Returns if auto heartbeat is enabled on the @p pb context.
*/
bool pubnub_is_auto_heartbeat_enabled(pubnub_t* pb);

/** Releases all allocated heartbeat thumpers.
*/
void pubnub_heartbeat_free_thumpers(void);

/** Reads channel and channel groups saved(subscribed on)
*/
void pbauto_heartbeat_read_channelInfo(pubnub_t const* pb,
char const** channel,
char const** channel_group);

/** Gives notice to auto heartbeat module that subscribe, or heartbeat transaction has begun */
void pbauto_heartbeat_transaction_ongoing(pubnub_t const* pb);

/** Starts auto heartbeat timer, if auto heartbeat is enabled, when subscribe transaction
is finished.
*/
void pbauto_heartbeat_start_timer(pubnub_t const* pb);

/** Releases allocated strings for subscribed channels and channel groups
*/
void pbauto_heartbeat_free_channelInfo(pubnub_t* pb);

/** Preparess channels and channel groups, to be used in request url.
*/
enum pubnub_res pbauto_heartbeat_prepare_channels_and_ch_groups(pubnub_t* pb,
char const** channel,
char const** channel_group);

/** Stops auto heartbeat thread */
void pbauto_heartbeat_stop(void);

#else
#define M_channelInfo()
#define M_heartbeatInfo()
#define pubnub_enable_auto_heartbeat(pb, period_sec) -1
#define pubnub_set_heartbeat_period(pb, period_sec) -1
#define pubnub_disable_auto_heartbeat(pb)
#define pubnub_is_auto_heartbeat_enabled(pb) false
#define pubnub_heartbeat_free_thumpers()
#define pbauto_heartbeat_read_channelInfo(pb, channel, channel_group)
#define pbauto_heartbeat_transaction_ongoing(pb)
#define pbauto_heartbeat_start_timer(pb)
#define pbauto_heartbeat_free_channelInfo(pb)
#define pbauto_heartbeat_prepare_channels_and_ch_groups(pb, addr_channel, addr_channel_group) PNR_OK
#define pbauto_heartbeat_stop()
#endif /* PUBNUB_USE_AUTO_HEARTBEAT */

#endif /* !defined INC_PBAUTO_HEARTBEAT */

12 changes: 12 additions & 0 deletions core/pubnub_ccore_limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
#if !defined INC_PUBNUB_CCORE_LIMITS
#define INC_PUBNUB_CCORE_LIMITS

/** Minimal duration of the transaction timer, in milliseconds. You
* can't set less than this.
*/
#if !defined PUBNUB_MIN_TRANSACTION_TIMER
#define PUBNUB_MIN_TRANSACTION_TIMER 10000
#endif

#endif /* !defined INC_PUBNUB_CCORE_LIMITS */
1 change: 1 addition & 0 deletions core/pubnub_core_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "cgreen/mocks.h"

#include "pubnub_internal.h"
#include "pubnub_server_limits.h"
#include "pubnub_pubsubapi.h"
#include "pubnub_coreapi.h"
#if PUBNUB_USE_ADVANCED_HISTORY
Expand Down
Loading