This is the Pubnub library for MBed2 ("classic") with the "sync" interface. It is based on the Pubnub C-core library.
Dependencies: Pubnub_c_core_mbed2_pal
Dependents: Pubnub_ATT_IoT_SK_WNC_sync pubnub_sync
Revision 2:4d49720c7200, committed 2016-11-11
- Comitter:
- sveljko
- Date:
- Fri Nov 11 22:46:57 2016 +0000
- Parent:
- 1:9fbec3606b08
- Commit message:
- Added the module/function to srand() using the time returned from Pubnub's `time` operation (which can be started with pubnub_time())
Changed in this revision
diff -r 9fbec3606b08 -r 4d49720c7200 Pubnub_c_core_mbed2_pal.lib --- a/Pubnub_c_core_mbed2_pal.lib Thu Nov 10 22:40:25 2016 +0000 +++ b/Pubnub_c_core_mbed2_pal.lib Fri Nov 11 22:46:57 2016 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/users/sveljko/code/Pubnub_c_core_mbed2_pal/#d29780e2c0ec +https://developer.mbed.org/users/sveljko/code/Pubnub_c_core_mbed2_pal/#9c3ddeede137
diff -r 9fbec3606b08 -r 4d49720c7200 pubnub_sync.h --- a/pubnub_sync.h Thu Nov 10 22:40:25 2016 +0000 +++ b/pubnub_sync.h Fri Nov 11 22:46:57 2016 +0000 @@ -31,7 +31,7 @@ #include "pubnub_assert.h" #include "pubnub_coreapi.h" #include "pubnub_ntf_sync.h" -//#include "pubnub_generate_uuid.h" +#include "pubnub_generate_uuid.h" #include "pubnub_blocking_io.h"
diff -r 9fbec3606b08 -r 4d49720c7200 srand_from_pubnub_time.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/srand_from_pubnub_time.cpp Fri Nov 11 22:46:57 2016 +0000 @@ -0,0 +1,37 @@ +#include "srand_from_pubnub_time.h" + +#include "pubnub_coreapi.h" +#include "pubnub_ntf_sync.h" + +#include <stdlib.h> +#include <string.h> + + +int srand_from_pubnub_time(pubnub_t *pbp) +{ + pubnub_res rslt = pubnub_time(pbp); + if (rslt != PNR_STARTED) { + return -1; + } + rslt = pubnub_await(pbp); + if (rslt != PNR_OK) { + return -1; + } + char const* pbtime = pubnub_get(pbp); + if (0 == pbtime) { + return -1; + } + size_t length_of_time = strlen(pbtime); + if (0 == length_of_time) { + return -1; + } + char const *s = pbtime + length_of_time - 1; + unsigned int val_for_srand = 0; + for (int i = 0; (i < 10) && (s > pbtime); ++i, --s) { + val_for_srand = val_for_srand * 10 + *s - '0'; + } + + srand(val_for_srand); + + return 0; +} \ No newline at end of file
diff -r 9fbec3606b08 -r 4d49720c7200 srand_from_pubnub_time.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/srand_from_pubnub_time.h Fri Nov 11 22:46:57 2016 +0000 @@ -0,0 +1,25 @@ +#if !defined INC_SRAND_FROM_PUBNUB_TIME +#define INC_SRAND_FROM_PUBNUB_TIME + + +#include "pubnub_api_types.h" + + +/** This helper function will call the C standard srand() function with the seed + taken from the time returned from Pubnub's `time` operation (which can + be initiated with pubnub_time()). + + It's useful if you want a high-fidelity time used for srand() and on + embedded system that don't have a Real-Time Clock. + + Keep in mind that this requires a round-trip to Pubnub, so it will take + some time, depending on your network, at least miliseconds. So, it's best + used only once, at the start of your program. + + @param pbp The Pubnub context to use to get time + @return 0: OK, -1: error (srand() was not called) +*/ +int srand_from_pubnub_time(pubnub_t *pbp); + + +#endif /* !defined INC_SRAND_FROM_PUBNUB_TIME */ \ No newline at end of file