This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
/
CouchChangeTracker.m
148 lines (129 loc) · 4.9 KB
/
CouchChangeTracker.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
//
// CouchChangeTracker.m
// CouchCocoa
//
// Created by Jens Alfke on 6/20/11.
// Copyright 2011 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
//
// <http://wiki.apache.org/couchdb/HTTP_database_API#Changes>
#import "CouchChangeTracker.h"
#import "CouchConnectionChangeTracker.h"
#import "CouchSocketChangeTracker.h"
#import "CouchInternal.h"
@implementation CouchChangeTracker
@synthesize lastSequenceNumber=_lastSequenceNumber, databaseURL=_databaseURL, mode=_mode;
- (id)initWithDatabaseURL: (NSURL*)databaseURL
mode: (CouchChangeTrackerMode)mode
lastSequence: (NSUInteger)lastSequence
client: (id<CouchChangeTrackerClient>)client {
NSParameterAssert(databaseURL);
NSParameterAssert(client);
self = [super init];
if (self) {
if ([self class] == [CouchChangeTracker class]) {
[self release];
if (mode == kContinuous && [databaseURL.scheme.lowercaseString hasPrefix: @"http"]) {
return (id) [[CouchSocketChangeTracker alloc] initWithDatabaseURL: databaseURL
mode: mode
lastSequence: lastSequence
client: client];
} else {
return (id) [[CouchConnectionChangeTracker alloc] initWithDatabaseURL: databaseURL
mode: mode
lastSequence: lastSequence
client: client];
}
}
_databaseURL = [databaseURL retain];
_client = client;
_mode = mode;
_lastSequenceNumber = lastSequence;
}
return self;
}
- (NSString*) databaseName {
return _databaseURL.lastPathComponent;
}
- (NSString*) changesFeedPath {
static NSString* const kModeNames[3] = {@"normal", @"longpoll", @"continuous"};
return [NSString stringWithFormat: @"_changes?feed=%@&heartbeat=300000&since=%lu",
kModeNames[_mode],
(unsigned long)_lastSequenceNumber];
}
- (NSURL*) changesFeedURL {
return [NSURL URLWithString: [NSString stringWithFormat: @"%@/%@",
_databaseURL.absoluteString, self.changesFeedPath]];
}
- (NSString*) description {
return [NSString stringWithFormat: @"%@[%@]", [self class], self.databaseName];
}
- (void)dealloc {
[self stop];
[_databaseURL release];
[super dealloc];
}
- (NSURLCredential*) authCredential {
if ([_client respondsToSelector: @selector(authCredential)])
return _client.authCredential;
else
return nil;
}
- (BOOL) start {
return NO;
}
- (void) stop {
[self stopped];
}
- (void) stopped {
if ([_client respondsToSelector: @selector(changeTrackerStopped:)])
[_client changeTrackerStopped: self];
}
- (BOOL) receivedChange: (NSDictionary*)change {
if (![change isKindOfClass: [NSDictionary class]])
return NO;
id seq = [change objectForKey: @"seq"];
if (!seq)
return NO;
[_client changeTrackerReceivedChange: change];
_lastSequenceNumber = [seq intValue];
return YES;
}
- (void) receivedChunk: (NSData*)chunk {
NSString* line = [[[NSString alloc] initWithData: chunk encoding:NSUTF8StringEncoding]
autorelease];
if (!line) {
Warn(@"Couldn't parse UTF-8 from _changes");
return;
}
if (line.length == 0 || [line isEqualToString: @"\n"])
return;
id change = [RESTBody JSONObjectWithString: line];
if (!change)
Warn(@"Received unparseable change line from server: %@", line);
else if (![self receivedChange: change]) {
COUCHLOG(@"%@: Couldn't interpret change line %@", self, line);
}
}
- (BOOL) receivedPollResponse: (NSData*)body {
if (!body)
return NO;
NSDictionary* changeDict = $castIf(NSDictionary,
[RESTBody JSONObjectWithData: body]);
NSArray* changes = $castIf(NSArray, [changeDict objectForKey: @"results"]);
if (!changes)
return NO;
for (NSDictionary* change in changes) {
if (![self receivedChange: change])
return NO;
}
return YES;
}
@end