19
19
#include " objectbox-sync.h"
20
20
#include " objectbox.hpp"
21
21
22
- static_assert (OBX_VERSION_MAJOR == 0 && OBX_VERSION_MINOR == 16 && OBX_VERSION_PATCH == 0 ,
22
+ static_assert (OBX_VERSION_MAJOR == 0 && OBX_VERSION_MINOR == 17 && OBX_VERSION_PATCH == 0 ,
23
23
" Versions of objectbox.h and objectbox-sync.hpp files do not match, please update" );
24
24
25
25
static_assert (sizeof (obx_id) == sizeof(OBX_id_array::ids[0 ]),
@@ -179,8 +179,9 @@ class SyncClient : public Closable {
179
179
public:
180
180
// / Creates a sync client associated with the given store and options.
181
181
// / This does not initiate any connection attempts yet: call start() to do so.
182
- explicit SyncClient (Store& store, const std::string& serverUri, const SyncCredentials& creds) : store_(store) {
183
- cSync_ = checkPtrOrThrow (obx_sync (store.cPtr (), serverUri.c_str ()), " can't initialize sync client" );
182
+ explicit SyncClient (Store& store, const std::string& serverUri, const SyncCredentials& creds)
183
+ : store_(store), cSync_(obx_sync(store.cPtr(), serverUri.c_str())) {
184
+ internal::checkPtrOrThrow (cSync_, " Could not initialize sync client" );
184
185
try {
185
186
setCredentials (creds);
186
187
} catch (...) {
@@ -193,8 +194,8 @@ class SyncClient : public Closable {
193
194
// / This does not initiate any connection attempts yet: call start() to do so.
194
195
// / @param cSync an initialized sync client. You must NOT call obx_sync_close() yourself anymore.
195
196
explicit SyncClient (Store& store, OBX_sync* cSync) : store_(store), cSync_(cSync) {
196
- OBJECTBOX_VERIFY_STATE (obx_has_feature (OBXFeature_Sync));
197
- OBJECTBOX_VERIFY_ARGUMENT (cSync);
197
+ OBX_VERIFY_STATE (obx_has_feature (OBXFeature_Sync));
198
+ OBX_VERIFY_ARGUMENT (cSync);
198
199
}
199
200
200
201
// / Can't be moved due to the atomic cSync_ - use shared_ptr instead of SyncClient instances directly.
@@ -230,8 +231,9 @@ class SyncClient : public Closable {
230
231
// / Configure authentication credentials.
231
232
// / The accepted OBXSyncCredentials type depends on your sync-server configuration.
232
233
void setCredentials (const SyncCredentials& creds) {
233
- checkErrOrThrow (obx_sync_credentials (cPtr (), creds.type_ , creds.data_ .empty () ? nullptr : creds.data_ .data (),
234
- creds.data_ .size ()));
234
+ obx_err err = obx_sync_credentials (cPtr (), creds.type_ , creds.data_ .empty () ? nullptr : creds.data_ .data (),
235
+ creds.data_ .size ());
236
+ internal::checkErrOrThrow (err);
235
237
}
236
238
237
239
// / Sets the interval in which the client sends "heartbeat" messages to the server, keeping the connection alive.
@@ -241,21 +243,21 @@ class SyncClient : public Closable {
241
243
// / @param interval default value is 25 minutes (1 500 000 milliseconds), which is also the allowed maximum.
242
244
// / @throws if value is not in the allowed range, e.g. larger than the maximum (1 500 000).
243
245
void setHeartbeatInterval (std::chrono::milliseconds interval) {
244
- checkErrOrThrow (obx_sync_heartbeat_interval (cPtr (), interval.count ()));
246
+ internal:: checkErrOrThrow (obx_sync_heartbeat_interval (cPtr (), interval.count ()));
245
247
}
246
248
247
249
// / Triggers the heartbeat sending immediately.
248
- void sendHeartbeat () { checkErrOrThrow (obx_sync_send_heartbeat (cPtr ())); }
250
+ void sendHeartbeat () { internal:: checkErrOrThrow (obx_sync_send_heartbeat (cPtr ())); }
249
251
250
252
// / Configures how sync updates are received from the server.
251
253
// / If automatic sync updates are turned off, they will need to be requested manually.
252
254
void setRequestUpdatesMode (OBXRequestUpdatesMode mode) {
253
- checkErrOrThrow (obx_sync_request_updates_mode (cPtr (), mode));
255
+ internal:: checkErrOrThrow (obx_sync_request_updates_mode (cPtr (), mode));
254
256
}
255
257
256
258
// / Configures the maximum number of outgoing TX messages that can be sent without an ACK from the server.
257
259
// / @throws if value is not in the valid range 1-20
258
- void maxMessagesInFlight (int value) { checkErrOrThrow (obx_sync_max_messages_in_flight (cPtr (), value)); }
260
+ void maxMessagesInFlight (int value) { internal:: checkErrOrThrow (obx_sync_max_messages_in_flight (cPtr (), value)); }
259
261
260
262
// / Once the sync client is configured, you can "start" it to initiate synchronization.
261
263
// / This method triggers communication in the background and will return immediately.
@@ -264,21 +266,21 @@ class SyncClient : public Closable {
264
266
// / If the device, network or server is currently offline, connection attempts will be retried later using
265
267
// / increasing backoff intervals.
266
268
// / If you haven't set the credentials in the options during construction, call setCredentials() before start().
267
- void start () { checkErrOrThrow (obx_sync_start (cPtr ())); }
269
+ void start () { internal:: checkErrOrThrow (obx_sync_start (cPtr ())); }
268
270
269
271
// / Stops this sync client. Does nothing if it is already stopped.
270
- void stop () { checkErrOrThrow (obx_sync_stop (cPtr ())); }
272
+ void stop () { internal:: checkErrOrThrow (obx_sync_stop (cPtr ())); }
271
273
272
274
// / Request updates since we last synchronized our database.
273
275
// / @param subscribeForFuturePushes to keep sending us future updates as they come in.
274
276
// / @see updatesCancel() to stop the updates
275
277
bool requestUpdates (bool subscribeForFuturePushes) {
276
- return checkSuccessOrThrow (obx_sync_updates_request (cPtr (), subscribeForFuturePushes));
278
+ return internal:: checkSuccessOrThrow (obx_sync_updates_request (cPtr (), subscribeForFuturePushes));
277
279
}
278
280
279
281
// / Cancel updates from the server so that it will stop sending updates.
280
282
// / @see updatesRequest()
281
- bool cancelUpdates () { return checkSuccessOrThrow (obx_sync_updates_cancel (cPtr ())); }
283
+ bool cancelUpdates () { return internal:: checkSuccessOrThrow (obx_sync_updates_cancel (cPtr ())); }
282
284
283
285
// / Count the number of messages in the outgoing queue, i.e. those waiting to be sent to the server.
284
286
// / Note: This calls uses a (read) transaction internally: 1) it's not just a "cheap" return of a single number.
@@ -287,7 +289,7 @@ class SyncClient : public Closable {
287
289
// / @return the number of messages in the outgoing queue
288
290
uint64_t outgoingMessageCount (uint64_t limit = 0 ) {
289
291
uint64_t result;
290
- checkErrOrThrow (obx_sync_outgoing_message_count (cPtr (), limit, &result));
292
+ internal:: checkErrOrThrow (obx_sync_outgoing_message_count (cPtr (), limit, &result));
291
293
return result;
292
294
}
293
295
@@ -461,7 +463,7 @@ class SyncClient : public Closable {
461
463
protected:
462
464
OBX_sync* cPtr () const {
463
465
OBX_sync* ptr = cSync_;
464
- if (ptr == nullptr ) throw std::runtime_error (" Sync client was already closed" );
466
+ if (ptr == nullptr ) throw IllegalStateException (" Sync client was already closed" );
465
467
return ptr;
466
468
}
467
469
@@ -473,7 +475,7 @@ class SyncClient : public Closable {
473
475
std::lock_guard<std::mutex> lock (store_.syncClientMutex_ );
474
476
store_.syncClient_ .reset ();
475
477
}
476
- checkErrOrThrow (obx_sync_close (ptr));
478
+ internal:: checkErrOrThrow (obx_sync_close (ptr));
477
479
}
478
480
}
479
481
@@ -533,7 +535,7 @@ class Sync {
533
535
static std::shared_ptr<SyncClient> client (Store& store, const std::string& serverUri,
534
536
const SyncCredentials& creds) {
535
537
std::lock_guard<std::mutex> lock (store.syncClientMutex_ );
536
- if (store.syncClient_ ) throw std::runtime_error (" Only one sync client can be active for a store" );
538
+ if (store.syncClient_ ) throw IllegalStateException (" Only one sync client can be active for a store" );
537
539
store.syncClient_ .reset (new SyncClient (store, serverUri, creds));
538
540
return std::static_pointer_cast<SyncClient>(store.syncClient_ );
539
541
}
@@ -542,7 +544,7 @@ class Sync {
542
544
// / @param cSync an initialized sync client. You must NOT call obx_sync_close() yourself anymore.
543
545
static std::shared_ptr<SyncClient> client (Store& store, OBX_sync* cSync) {
544
546
std::lock_guard<std::mutex> lock (store.syncClientMutex_ );
545
- if (store.syncClient_ ) throw std::runtime_error (" Only one sync client can be active for a store" );
547
+ if (store.syncClient_ ) throw IllegalStateException (" Only one sync client can be active for a store" );
546
548
store.syncClient_ .reset (new SyncClient (store, cSync));
547
549
return std::static_pointer_cast<SyncClient>(store.syncClient_ );
548
550
}
@@ -591,10 +593,12 @@ class SyncServer : public Closable {
591
593
// / an arbitrary port that is available. The port can be queried via obx_sync_server_port() once the server
592
594
// / was started. \b Examples: "ws://0.0.0.0:9999" could be used during development (no certificate config
593
595
// / needed), while in a production system, you may want to use wss and a specific IP for security reasons.
594
- explicit SyncServer (Options& storeOptions, const std::string& uri) {
595
- cPtr_ = checkPtrOrThrow (obx_sync_server (storeOptions.release (), uri.c_str ()), " Could not create SyncServer" );
596
+ explicit SyncServer (Options& storeOptions, const std::string& uri)
597
+ : cPtr_(obx_sync_server(storeOptions.release(), uri.c_str())) {
598
+ internal::checkPtrOrThrow (cPtr_, " Could not create SyncServer" );
596
599
try {
597
- OBX_store* cStore = checkPtrOrThrow (obx_sync_server_store (cPtr_), " Can't get SyncServer's store" );
600
+ OBX_store* cStore = obx_sync_server_store (cPtr_);
601
+ internal::checkPtrOrThrow (cStore, " Could not get SyncServer's store" );
598
602
store_.reset (new Store (cStore, false ));
599
603
} catch (...) {
600
604
close ();
@@ -623,7 +627,7 @@ class SyncServer : public Closable {
623
627
624
628
// / The store that is associated with this server.
625
629
Store& store () {
626
- OBJECTBOX_VERIFY_STATE (store_);
630
+ OBX_VERIFY_STATE (store_);
627
631
return *store_;
628
632
}
629
633
@@ -634,7 +638,7 @@ class SyncServer : public Closable {
634
638
cPtr_ = nullptr ;
635
639
store_.reset ();
636
640
if (ptr) {
637
- checkErrOrThrow (obx_sync_server_close (ptr));
641
+ internal:: checkErrOrThrow (obx_sync_server_close (ptr));
638
642
}
639
643
}
640
644
@@ -643,34 +647,36 @@ class SyncServer : public Closable {
643
647
644
648
// / Sets SSL certificate for the server to use. Use before start().
645
649
void setCertificatePath (const std::string& path) {
646
- checkErrOrThrow (obx_sync_server_certificate_path (cPtr (), path.c_str ()));
650
+ internal:: checkErrOrThrow (obx_sync_server_certificate_path (cPtr (), path.c_str ()));
647
651
}
648
652
649
653
// / Sets credentials for the server to accept. Use before start().
650
654
// / @param data may be NULL in combination with OBXSyncCredentialsType_NONE
651
655
void setCredentials (const SyncCredentials& creds) {
652
- checkErrOrThrow (obx_sync_server_credentials (
656
+ internal:: checkErrOrThrow (obx_sync_server_credentials (
653
657
cPtr (), creds.type_ , creds.data_ .empty () ? nullptr : creds.data_ .data (), creds.data_ .size ()));
654
658
}
655
659
656
660
// / Once the sync server is configured, you can "start" it to start accepting client connections.
657
661
// / This method triggers communication in the background and will return immediately.
658
- void start () { checkErrOrThrow (obx_sync_server_start (cPtr ())); }
662
+ void start () { internal:: checkErrOrThrow (obx_sync_server_start (cPtr ())); }
659
663
660
664
// / Stops this sync server. Does nothing if it is already stopped.
661
- void stop () { checkErrOrThrow (obx_sync_server_stop (cPtr ())); }
665
+ void stop () { internal:: checkErrOrThrow (obx_sync_server_stop (cPtr ())); }
662
666
663
667
// / Returns if this sync server is running
664
668
bool isRunning () { return obx_sync_server_running (cPtr ()); }
665
669
666
670
// / Returns a URL this server is listening on, including the bound port (see port().
667
- std::string url () { return checkPtrOrThrow (obx_sync_server_url (cPtr ()), " Can't get SyncServer bound URL" ); }
671
+ std::string url () {
672
+ return internal::checkedPtrOrThrow (obx_sync_server_url (cPtr ()), " Can't get SyncServer bound URL" );
673
+ }
668
674
669
675
// / Returns a port this server listens on. This is especially useful if the bindUri given to the constructor
670
676
// / specified "0" port (i.e. automatic assignment).
671
677
uint16_t port () {
672
678
uint16_t result = obx_sync_server_port (cPtr ());
673
- if (!result) throwLastError ();
679
+ if (!result) internal:: throwLastError ();
674
680
return result;
675
681
}
676
682
@@ -679,8 +685,8 @@ class SyncServer : public Closable {
679
685
680
686
// / Get server runtime statistics.
681
687
std::string statsString (bool includeZeroValues = true ) {
682
- return checkPtrOrThrow ( obx_sync_server_stats_string (cPtr (), includeZeroValues),
683
- " Can't get SyncServer stats string" );
688
+ const char * serverStatsString = obx_sync_server_stats_string (cPtr (), includeZeroValues);
689
+ return internal::checkedPtrOrThrow (serverStatsString, " Can't get SyncServer stats string" );
684
690
}
685
691
686
692
void setChangeListener (std::shared_ptr<SyncChangeListener> listener) {
@@ -723,7 +729,7 @@ class SyncServer : public Closable {
723
729
protected:
724
730
OBX_sync_server* cPtr () const {
725
731
OBX_sync_server* ptr = cPtr_;
726
- if (ptr == nullptr ) throw std::runtime_error (" Sync server was already closed" );
732
+ if (ptr == nullptr ) throw IllegalStateException (" Sync server was already closed" );
727
733
return ptr;
728
734
}
729
735
};
0 commit comments