-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncpointClient.m
409 lines (354 loc) · 15 KB
/
SyncpointClient.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
// SyncpointClient.m
// Syncpoint
//
// Created by Jens Alfke on 2/23/12.
// Copyright (c) 2012 Couchbase, Inc. All rights reserved.
//
#import "SyncpointClient.h"
#import "SyncpointModels.h"
#import "SyncpointInternal.h"
#import "CouchCocoa.h"
#import "TDMisc.h"
#import "MYBlockUtils.h"
#define kLocalControlDatabaseName @"sp_control"
#define kLocalChannelDatabaseName @"sp_channel"
@interface SyncpointClient ()
@end
@implementation SyncpointClient
{
@private
NSURL* _remote;
NSString* _appId;
CouchServer* _server;
CouchDatabase* _localControlOrChannelDatabase;
SyncpointSession* _session;
CouchReplication *_syncpointPull;
CouchReplication *_syncpointPush;
BOOL _observingControlPull;
BOOL _singleChannelMode;
}
@synthesize localServer=_server, session=_session, appId=_appId;
- (id) initWithRemoteServer: (NSURL*)remoteServerURL
appId: (NSString*)syncpointAppId
multiChannel: (BOOL) multi
error: (NSError**)outError
{
CouchTouchDBServer* newLocalServer = [CouchTouchDBServer sharedInstance];
return [self initWithLocalServer:newLocalServer remoteServer:remoteServerURL appId:syncpointAppId multiChannel:multi error:outError];
}
- (id) initWithLocalServer: (CouchServer*)localServer
remoteServer: (NSURL*)remoteServerURL
appId: (NSString*)syncpointAppId
multiChannel: (BOOL) multi
error: (NSError**)outError
{
CAssert(localServer);
CAssert(remoteServerURL);
self = [super init];
if (self) {
_server = localServer;
_remote = remoteServerURL;
_appId = syncpointAppId;
if (multi) {
// Create the control database on the first run of the app.
_localControlOrChannelDatabase = [self setupControlDatabaseNamed: kLocalControlDatabaseName error: outError];
} else {
// Create the sync channel database on the first run of the app.
_localControlOrChannelDatabase = [_server databaseNamed: kLocalChannelDatabaseName];
if (![_localControlOrChannelDatabase ensureCreated: outError])
return nil;
}
if (!_localControlOrChannelDatabase) return nil;
_session = [SyncpointSession sessionInDatabase: _localControlOrChannelDatabase];
if (!_session) { // if no session make one
_session = [SyncpointSession makeSessionInDatabase: _localControlOrChannelDatabase
appId: _appId
multiChannel: multi
withRemoteServer: _remote
error: nil]; // TODO: Report error
}
if (nil != _session.error) {
LogTo(Syncpoint, @"Session has error: %@", _session.error.localizedDescription);
}
if (_session.isPaired) {
LogTo(Syncpoint, @"Session is active");
[self connectToControlOrChannelDB];
} else if (_session.isReadyToPair) {
LogTo(Syncpoint, @"Begin pairing with cloud: %@", _remote.absoluteString);
[self beginPairing];
}
}
return self;
}
- (void)dealloc {
[self stopObservingControlPull];
[[NSNotificationCenter defaultCenter] removeObserver: self];
}
- (SyncpointChannel*) myChannelNamed: (NSString*)channelName
error: (NSError**)error {
SyncpointChannel* channel = [_session myChannelWithName:channelName];
if (!channel) {
channel = [_session makeChannelWithName: channelName error:error];
if (!channel) return nil;
}
return channel;
}
- (CouchDatabase*) myDatabase {
_singleChannelMode = YES;
return _localControlOrChannelDatabase;
}
#pragma mark - Views and Queries
- (CouchLiveQuery*) myChannelsQuery {
CouchLiveQuery* query = [[[_localControlOrChannelDatabase designDocumentWithName: @"syncpoint"]
queryViewNamed: @"channels"] asLiveQuery];
id owner;
if (_session.owner_id) {
owner = _session.owner_id;
} else {
owner = @"unpaired";
}
query.descending = YES;
query.keys = $array(owner);
return query;
}
- (CouchDatabase*) setupControlDatabaseNamed: (NSString*)name error: (NSError**)outError {
CouchDatabase* database = [_server databaseNamed: name];
if (![database ensureCreated: outError])
return nil;
// Create a 'view' of known channels by owner:
CouchDesignDocument* design = [database designDocumentWithName: @"syncpoint"];
[design defineViewNamed: @"channels" mapBlock: MAPBLOCK({
NSString* type = $castIf(NSString, [doc objectForKey: @"type"]);
if ([type isEqualToString:@"channel"]) {
emit([doc objectForKey: @"owner_id"], doc);
}
}) version: @"1.1"];
database.tracksChanges = YES;
return database;
}
#pragma mark - Pairing with cloud
- (void) pairSessionWithType: (NSString*)pairingType andToken: (NSString*)pairingToken {
if (_session.isPaired) return;
[_session setValue: pairingType ofProperty: @"pairing_type"];
[_session setValue: pairingToken ofProperty: @"pairing_token"];
[[_session save] wait: nil];
[self beginPairing];
}
- (void) beginPairing {
LogTo(Syncpoint, @"Pairing session...");
if (_session.isReadyToPair) {
Assert(!_session.isPaired);
[_session clearState: nil];
[self savePairingUserToRemote];
}
}
- (void) savePairingUserToRemote {
CouchServer* anonRemote = [[CouchServer alloc] initWithURL: _remote];
RESTResource* remoteSession = [[RESTResource alloc] initWithParent: anonRemote relativePath: @"_session"];
RESTOperation* op = [remoteSession GET];
[op onCompletion: ^{
NSDictionary* resp = $castIf(NSDictionary, op.responseBody.fromJSON);
NSString* userDbName = [[resp objectForKey:@"info"] objectForKey:@"authentication_db"];
CouchDatabase* anonUserDb = [anonRemote databaseNamed:userDbName];
NSDictionary* userProps = [_session pairingUserProperties];
CouchDocument* newUserDoc = [anonUserDb documentWithID:[userProps objectForKey:@"_id"]];
RESTOperation* docPut = [newUserDoc putProperties:userProps];
[docPut onCompletion:^{
NSString* remoteURLString = [[_remote absoluteString]
stringByReplacingOccurrencesOfString:@"://"
withString:$sprintf(@"://%@:%@@",
[_session.pairing_creds objectForKey:@"username"],
[_session.pairing_creds objectForKey:@"password"])];
CouchServer* userRemote = [[CouchServer alloc] initWithURL: [NSURL URLWithString:remoteURLString]];
CouchDatabase* userUserDb = [userRemote databaseNamed:userDbName];
CouchDocument* readUserDoc = [userUserDb documentWithID: [newUserDoc documentID]];
[self waitForPairingToComplete: readUserDoc];
}];
}];
[op start];
}
- (void) waitForPairingToComplete: (CouchDocument*)userDoc {
MYAfterDelay(3.0, ^{
RESTOperation* op = [userDoc GET];
[op onCompletion:^{
NSDictionary* resp = $castIf(NSDictionary, op.responseBody.fromJSON);
NSString* state = [resp objectForKey:@"pairing_state"];
if ([state isEqualToString:@"paired"]) {
[self pairingDidComplete: userDoc];
} else {
[self waitForPairingToComplete: userDoc];
}
}];
[op start];
});
}
- (void) pairingDidComplete: (CouchDocument*)userDoc {
NSMutableDictionary* props = [[userDoc properties] mutableCopy];
[_session setValue:@"paired" forKey:@"state"];
[_session setValue:[props valueForKey:@"owner_id"] ofProperty:@"owner_id"];
[_session setValue:[props valueForKey:@"control_database"] ofProperty:@"control_database"];
[_session setValue:[props valueForKey:@"channel_database"] ofProperty:@"channel_database"];
RESTOperation* op = [_session save];
[op onCompletion:^{
LogTo(Syncpoint, @"Device is now paired");
[props setObject:[NSNumber numberWithBool:YES] forKey:@"_deleted"];
[[userDoc currentRevision] putProperties: props];
[self connectToControlOrChannelDB];
}];
}
#pragma mark - Connect to Control Database
- (CouchReplication*) pullFromSyncpointDatabaseNamed: (NSString*)dbName {
NSURL* url = [NSURL URLWithString: dbName relativeToURL: _remote];
return [_localControlOrChannelDatabase pullFromDatabaseAtURL: url];
}
- (CouchReplication*) pushToSyncpointDatabaseNamed: (NSString*)dbName {
NSURL* url = [NSURL URLWithString: dbName relativeToURL: _remote];
return [_localControlOrChannelDatabase pushToDatabaseAtURL: url];
}
// Start bidirectional sync with the control or channel database.
- (void) connectToControlOrChannelDB {
if (_session.channel_database) {
LogTo(Syncpoint, @"connectToChannelDB %@", (_session.channel_database));
_syncpointPull = [self pullFromSyncpointDatabaseNamed: _session.channel_database];
_syncpointPull.continuous = YES;
_syncpointPush = [self pushToSyncpointDatabaseNamed: _session.channel_database];
_syncpointPush.continuous = YES;
} else {
LogTo(Syncpoint, @"connectToControlDB %@", (_session.control_database));
if (!_session.control_db_synced) {
[self doInitialSyncOfControlDB]; // sync once before we write
} else {
[self didInitialSyncOfControlDB]; // go continuous
}
}
}
- (void) doInitialSyncOfControlDB {
if (!_observingControlPull) {
// During the initial sync, make the pull non-continuous, and observe when it stops.
// That way we know when the control DB has been fully updated from the server.
// Once it has stopped, we can fire the didSyncControlDB event on the session,
// and restart the sync in continuous mode.
LogTo(Syncpoint, @"doInitialSyncOfControlDB");
_syncpointPull = [self pullFromSyncpointDatabaseNamed: _session.control_database];
[_syncpointPull addObserver: self forKeyPath: @"running" options: 0 context: NULL];
_observingControlPull = YES;
}
}
- (void) didInitialSyncOfControlDB {
LogTo(Syncpoint, @"didInitialSyncOfControlDB");
// Now we can sync continuously & push
_syncpointPull = [self pullFromSyncpointDatabaseNamed: _session.control_database];
_syncpointPull.continuous = YES;
_syncpointPush = [self pushToSyncpointDatabaseNamed: _session.control_database];
_syncpointPush.continuous = YES;
[_session didFirstSyncOfControlDB];
MYAfterDelay(1.0, ^{
[self getUpToDateWithSubscriptions];
[self observeControlDatabase];
});
}
// Observes when the initial _syncpointPull stops running, after -doInitialSyncOfControlDB.
- (void) observeValueForKeyPath: (NSString*)keyPath ofObject: (id)object
change: (NSDictionary*)change context: (void*)context
{
if (object == _syncpointPull && !_syncpointPull.running) {
// first Control database sync is done
[self stopObservingControlPull];
[self mergeExistingChannels];
[self didInitialSyncOfControlDB];
}
}
- (void) stopObservingControlPull {
if (_observingControlPull) {
[_syncpointPull removeObserver: self forKeyPath: @"running"];
_observingControlPull = NO;
}
}
#pragma mark - React to Control Database Changes
// Begins observing document changes in the _localControlOrChannelDatabase.
- (void) observeControlDatabase {
Assert(_localControlOrChannelDatabase);
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(controlDatabaseChanged)
name: kCouchDatabaseChangeNotification
object: _localControlOrChannelDatabase];
}
- (void) controlDatabaseChanged {
// if we are done with first ever sync
if (_session.control_db_synced) {
LogTo(Syncpoint, @"Control DB changed");
// collect 1 second of changes before acting
// todo can we make these calls all collapse into one?
MYAfterDelay(1.0, ^{
[self getUpToDateWithSubscriptions];
});
}
}
// Called when the control database changes or is initial pulled from the server.
- (void) getUpToDateWithSubscriptions {
if (_singleChannelMode) {
return;
}
LogTo(Syncpoint, @"getUpToDateWithSubscriptions");
// Make installations for any subscriptions that don't have one:
NSSet* installedSubscriptions = _session.installedSubscriptions;
for (SyncpointSubscription* sub in _session.activeSubscriptions) {
if (![installedSubscriptions containsObject: sub]) {
LogTo(Syncpoint, @"Making installation db for %@", sub);
[sub makeInstallationWithLocalDatabase: nil error: nil]; // TODO: Report error
}
}
// Sync all installations whose channels are ready:
for (SyncpointInstallation* inst in _session.allInstallations)
if (inst.channel.isReady)
[inst sync];
}
#pragma mark - Merge Session Data on Initial Pairing Sync
- (void) mergeExistingChannels {
LogTo(Syncpoint, @"mergeExistingChannels");
NSEnumerator* pairedChannels = _session.myChannels;
BOOL matched;
for (SyncpointChannel* unpaired in _session.unpairedChannels) {
matched = NO;
for (SyncpointChannel* paired in pairedChannels) {
if ([paired.name isEqual:unpaired.name]) {
matched = YES;
[self mergeUnpairedChannel: unpaired intoPairedChannel: paired];
}
}
if (!matched) {
unpaired.state = @"new";
unpaired.owner_id = _session.owner_id;
[[unpaired save] wait];
}
}
}
// if remote subscription exists
// replace local subscription with remote subscription (update local install if exists)
// else
// update local subscription with cloud channel id
// update local installation channel_id of cloud channel
- (void) mergeUnpairedChannel: (SyncpointChannel*) unpaired
intoPairedChannel: (SyncpointChannel*) paired {
SyncpointSubscription* unpairedSub = unpaired.subscription;
SyncpointSubscription* pairedSub = paired.subscription;
SyncpointInstallation* unpairedInst = unpaired.installation;
if (pairedSub) {
if (unpairedInst) {
unpairedInst.subscription = paired.subscription;
}
[[unpairedSub deleteDocument] wait];
} else {
unpairedSub.channel = paired;
unpairedSub.owner_id = paired.owner_id;
[[unpairedSub save] wait];
}
if (unpairedInst) {
unpairedInst.owner_id = paired.owner_id;
unpairedInst.channel = paired;
[[unpairedInst save] wait];
}
[[unpaired deleteDocument] wait];
}
@end