Skip to content

Commit 5303435

Browse files
committed
addressing review comments
1 parent 923c704 commit 5303435

4 files changed

+44
-44
lines changed

src/CBLCollection_Internal.hh

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ protected:
279279
friend struct CBLDatabase;
280280
friend struct CBLDocument;
281281
friend struct cbl_internal::ListenerToken<CBLCollectionDocumentChangeListener>;
282+
friend struct CBLURLEndpointListener;
282283

283284
// Called by the database to take an ownership.
284285
// Release the database to avoid the circular reference

src/CBLURLEndpointListener_CAPI.cc

+1-8
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ void CBLListenerAuth_Free(CBLListenerAuthenticator* _cbl_nullable auth) noexcept
3333
}
3434

3535
CBLURLEndpointListener* CBLURLEndpointListener_Create(const CBLURLEndpointListenerConfiguration* conf, CBLError* outError) noexcept {
36-
if (conf->collectionCount == 0) {
37-
if (outError) {
38-
outError->domain = kCBLDomain;
39-
outError->code = kCBLErrorInvalidParameter;
40-
}
41-
return nullptr;
42-
}
4336
try {
4437
return retain(new CBLURLEndpointListener(*conf));
4538
} catchAndBridge(outError);
@@ -72,7 +65,7 @@ CBLConnectionStatus CBLURLEndpointListener_Status(const CBLURLEndpointListener*
7265

7366
bool CBLURLEndpointListener_Start(CBLURLEndpointListener* listener, CBLError* outError) noexcept {
7467
try {
75-
return listener->start(outError);
68+
return listener->start();
7669
} catchAndBridge(outError)
7770
}
7871

src/CBLURLEndpointListener_Internal.hh

+41-36
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "Internal.hh"
2222
#include "CBLCollection_Internal.hh"
2323
#include "CBLDatabase_Internal.hh"
24-
#include "c4.h"
24+
#include "c4Listener.hh"
2525

2626
#ifdef COUCHBASE_ENTERPRISE
2727

@@ -35,35 +35,43 @@ struct CBLURLEndpointListener final : public CBLRefCounted {
3535
public:
3636
CBLURLEndpointListener(const CBLURLEndpointListenerConfiguration &conf)
3737
: _conf(conf)
38-
{ }
38+
{
39+
if (conf.collectionCount == 0) {
40+
C4Error::raise(LiteCoreDomain, kC4ErrorInvalidParameter, "No collections in CBLURLEndpointListenerConfiguration");
41+
}
42+
}
3943

4044
const CBLURLEndpointListenerConfiguration* configuration() const { return &_conf; }
4145

4246
uint16_t port() const {
4347
if (_port == 0 && _c4listener) {
44-
_port = c4listener_getPort(_c4listener);
48+
_port = _c4listener->port();
4549
}
4650
return _port;
4751
}
4852

4953
FLMutableArray _cbl_nullable getUrls() const {
50-
FLMutableArray ret = (FLMutableArray) nullptr;
51-
if (_c4listener) {
52-
CBLDatabase* cblDb = CBLCollection_Database(_conf.collections[0]);
53-
cblDb->c4db()->useLocked([&](C4Database* db) {
54-
ret = c4listener_getURLs(_c4listener, db, kC4SyncAPI, nullptr);
55-
});
56-
}
57-
return ret;
54+
if (!_c4listener) return (FLMutableArray) nullptr;
55+
56+
CBLDatabase* cblDb = CBLCollection_Database(_conf.collections[0]);
57+
auto urls = fleece::MutableArray::newArray();
58+
cblDb->c4db()->useLocked([&](C4Database* db) {
59+
for ( const std::string& url : _c4listener->URLs(db, kC4SyncAPI) )
60+
urls.append(url);
61+
});
62+
return (FLMutableArray)FLValue_Retain(urls);
5863
}
5964

6065
CBLConnectionStatus getConnectionStatus() const {
61-
unsigned int total = 0, active = 0;
62-
if (_c4listener) c4listener_getConnectionStatus(_c4listener, &total, &active);
63-
return {total, active};
66+
if (_c4listener) {
67+
auto [total, active] = _c4listener->connectionStatus();
68+
return {total, active};
69+
} else {
70+
return {0, 0};
71+
}
6472
}
6573

66-
bool start(CBLError* _cbl_nullable outError) {
74+
bool start() {
6775
if (_c4listener) return true;
6876

6977
Assert(_conf.collectionCount > 0);
@@ -77,46 +85,43 @@ public:
7785
c4config.allowPull = !_conf.readOnly;
7886
c4config.enableDeltaSync = _conf.enableDeltaSync;
7987

80-
_c4listener = c4listener_start(&c4config, internal(outError));
81-
if (!_c4listener)
82-
return false;
88+
_c4listener = new C4Listener(c4config);
8389

8490
slice dbname;
8591
CBLDatabase* cblDb = CBLCollection_Database(_conf.collections[0]);
8692
cblDb->c4db()->useLocked([&](C4Database* db) {
8793
dbname = db->getName();
88-
if (c4listener_shareDB(_c4listener, dbname, db, internal(outError))) {
94+
if (_c4listener->shareDB(dbname, db)) {
8995
_db = db;
9096
}
9197
if (_db) {
92-
for (unsigned i = 0; i < _conf.collectionCount; ++i) {
93-
C4Collection* coll = db->getCollection(_conf.collections[i]->spec());
94-
if (!coll) {
95-
if (outError) {
96-
outError->domain = kCBLDomain;
97-
outError->code = kCBLErrorInvalidParameter;
98-
}
99-
break;
100-
}
101-
if (c4listener_shareCollection(_c4listener, dbname, coll, internal(outError))) {
102-
_collections.push_back(coll);
98+
try {
99+
for (unsigned i = 0; i < _conf.collectionCount; ++i) {
100+
_conf.collections[i]->useLocked([&](C4Collection* coll) {
101+
if (_c4listener->shareCollection(dbname, coll)) {
102+
_collections.push_back(coll);
103+
}
104+
});
105+
if (_collections.size() < i + 1) break;
103106
}
104-
}
107+
} catchAndWarnNoReturn();
105108
}
106109
});
110+
107111
if (_collections.size() == _conf.collectionCount) {
108112
return true;
109113
}
110114

111115
// Otherwise unwind
112116
for (C4Collection* coll: _collections) {
113117
// These collections have been successfully shared.
114-
(void)c4listener_unshareCollection(_c4listener, dbname, coll, nullptr);
118+
(void)_c4listener->unshareCollection(dbname, coll);
115119
}
116-
(void)c4listener_unshareDB(_c4listener, _db, nullptr);
120+
(void)_c4listener->unshareDB(_db);
117121

118-
c4listener_free(_c4listener);
122+
delete _c4listener;
119123
_c4listener = nullptr;
124+
120125
return false;
121126
}
122127

@@ -125,9 +130,9 @@ public:
125130

126131
auto dbname = _db->getName();
127132
for (C4Collection* coll: _collections) {
128-
(void)c4listener_unshareCollection(_c4listener, dbname, coll, nullptr);
133+
(void)_c4listener->unshareCollection(dbname, coll);
129134
}
130-
(void)c4listener_unshareDB(_c4listener, _db, nullptr);
135+
(void)_c4listener->unshareDB(_db);
131136

132137
c4listener_free(_c4listener);
133138
_c4listener = nullptr;

test/URLEndpointListenerTest.cc

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ TEST_CASE_METHOD(URLEndpointListenerTest, "Listener Basics", "[URLListener]") {
9999

100100
CBLURLEndpointListener* listener = nullptr;
101101
SECTION("0 Collections") {
102+
ExpectingExceptions x;
102103
listenerConfig.collectionCount = 0;
103104
// Cannot create listener with 0 collections.
104105
listener = CBLURLEndpointListener_Create(&listenerConfig, &error);

0 commit comments

Comments
 (0)