21
21
#include " Internal.hh"
22
22
#include " CBLCollection_Internal.hh"
23
23
#include " CBLDatabase_Internal.hh"
24
- #include " c4.h "
24
+ #include " c4Listener.hh "
25
25
26
26
#ifdef COUCHBASE_ENTERPRISE
27
27
@@ -35,35 +35,43 @@ struct CBLURLEndpointListener final : public CBLRefCounted {
35
35
public:
36
36
CBLURLEndpointListener (const CBLURLEndpointListenerConfiguration &conf)
37
37
: _conf(conf)
38
- { }
38
+ {
39
+ if (conf.collectionCount == 0 ) {
40
+ C4Error::raise (LiteCoreDomain, kC4ErrorInvalidParameter , " No collections in CBLURLEndpointListenerConfiguration" );
41
+ }
42
+ }
39
43
40
44
const CBLURLEndpointListenerConfiguration* configuration () const { return &_conf; }
41
45
42
46
uint16_t port () const {
43
47
if (_port == 0 && _c4listener) {
44
- _port = c4listener_getPort ( _c4listener);
48
+ _port = _c4listener-> port ( );
45
49
}
46
50
return _port;
47
51
}
48
52
49
53
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);
58
63
}
59
64
60
65
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
+ }
64
72
}
65
73
66
- bool start (CBLError* _cbl_nullable outError ) {
74
+ bool start () {
67
75
if (_c4listener) return true ;
68
76
69
77
Assert (_conf.collectionCount > 0 );
@@ -77,46 +85,43 @@ public:
77
85
c4config.allowPull = !_conf.readOnly ;
78
86
c4config.enableDeltaSync = _conf.enableDeltaSync ;
79
87
80
- _c4listener = c4listener_start (&c4config, internal (outError));
81
- if (!_c4listener)
82
- return false ;
88
+ _c4listener = new C4Listener (c4config);
83
89
84
90
slice dbname;
85
91
CBLDatabase* cblDb = CBLCollection_Database (_conf.collections [0 ]);
86
92
cblDb->c4db ()->useLocked ([&](C4Database* db) {
87
93
dbname = db->getName ();
88
- if (c4listener_shareDB ( _c4listener, dbname, db, internal (outError) )) {
94
+ if (_c4listener-> shareDB ( dbname, db)) {
89
95
_db = db;
90
96
}
91
97
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 ;
103
106
}
104
- }
107
+ } catchAndWarnNoReturn ();
105
108
}
106
109
});
110
+
107
111
if (_collections.size () == _conf.collectionCount ) {
108
112
return true ;
109
113
}
110
114
111
115
// Otherwise unwind
112
116
for (C4Collection* coll: _collections) {
113
117
// These collections have been successfully shared.
114
- (void )c4listener_unshareCollection ( _c4listener, dbname, coll, nullptr );
118
+ (void )_c4listener-> unshareCollection ( dbname, coll);
115
119
}
116
- (void )c4listener_unshareDB ( _c4listener, _db, nullptr );
120
+ (void )_c4listener-> unshareDB ( _db);
117
121
118
- c4listener_free ( _c4listener) ;
122
+ delete _c4listener;
119
123
_c4listener = nullptr ;
124
+
120
125
return false ;
121
126
}
122
127
@@ -125,9 +130,9 @@ public:
125
130
126
131
auto dbname = _db->getName ();
127
132
for (C4Collection* coll: _collections) {
128
- (void )c4listener_unshareCollection ( _c4listener, dbname, coll, nullptr );
133
+ (void )_c4listener-> unshareCollection ( dbname, coll);
129
134
}
130
- (void )c4listener_unshareDB ( _c4listener, _db, nullptr );
135
+ (void )_c4listener-> unshareDB ( _db);
131
136
132
137
c4listener_free (_c4listener);
133
138
_c4listener = nullptr ;
0 commit comments