Skip to content

Commit

Permalink
7 reds left
Browse files Browse the repository at this point in the history
  • Loading branch information
thevixac committed May 28, 2015
1 parent 0538ca1 commit 29f795f
Show file tree
Hide file tree
Showing 22 changed files with 409 additions and 141 deletions.
1 change: 1 addition & 0 deletions ably-ios/ARTAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ typedef NS_ENUM(NSUInteger, ARTAuthMethod) {
- (void) attemptTokenFetch:(void (^)()) cb;
-(bool) canRequestToken;
+ (void) authWithRest:(ARTRest *) rest options:(ARTAuthOptions *) options cb:(void(^)(ARTAuth * auth)) cb;
+ (ARTSignedTokenRequestCb)defaultSignedTokenRequestCallback:(ARTAuthOptions *)authOptions rest:(ARTRest *)rest;
@end
19 changes: 8 additions & 11 deletions ably-ios/ARTAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ - (void)cancel {

@end


@implementation ARTAuth (Private)

-(ARTAuthCb) getTheAuthCb {
return self.authTokenCb;
}

@end
@implementation ARTAuth


Expand Down Expand Up @@ -284,13 +292,6 @@ -(void) prepConnection:(void (^)()) cb {
ARTAuth * s = weakSelf;
ARTAuthTokenParams * params = nil;
if(s) {

/* if(![s canRequestToken]) {
[ARTLog error:@"ARTAuth cannot request a token because it does not have either an API key, an authCallback or an auth URL"];
authCb(ARTStatus *Error, nil);
return ic;
}
*/
params =[[ ARTAuthTokenParams alloc] initWithId:s.options.keyName
ttl:s.options.ttl
capability:s.options.capability
Expand All @@ -314,7 +315,6 @@ -(void) prepConnection:(void (^)()) cb {
else {
[ARTLog error:@"ARTAuth became nil during token request. Can't assign token"];
}

authCb(status, tokenDetails);
}];
}
Expand Down Expand Up @@ -438,9 +438,6 @@ + (NSString *)random {
}

+ (ARTSignedTokenRequestCb)defaultSignedTokenRequestCallback:(ARTAuthOptions *)authOptions rest:(ARTRest *)rest {



__weak ARTRest *weakRest = rest;
return ^id<ARTCancellable>(ARTAuthTokenParams *params, void(^cb)(ARTAuthTokenParams *)) {
NSString *keySecret = authOptions.keySecret;
Expand Down
17 changes: 1 addition & 16 deletions ably-ios/ARTHttp.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ - (instancetype)initWithBaseUrl:(NSURL *)baseUrl {

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:artRequest.url];
request.HTTPMethod = artRequest.method;
[ARTLog verbose:[NSString stringWithFormat:@"ARTHttp request URL is %@", artRequest.url]];

for (NSString *headerName in artRequest.headers) {
NSString *headerValue = [artRequest.headers objectForKey:headerName];
Expand All @@ -184,14 +185,6 @@ - (instancetype)initWithBaseUrl:(NSURL *)baseUrl {
CFRetain(rl);
NSURLSessionDataTask *task = [self.urlSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;


CFRunLoopPerformBlock(rl, kCFRunLoopDefaultMode, ^{
NSLog(@"WTF THIS WORKS");
CFRunLoopPerformBlock(rl, kCFRunLoopDefaultMode, ^{
NSLog(@"WTF THIS WORKS???");
});
});
[ARTLog verbose:
[NSString stringWithFormat:@"ARTHttp: Got response %@, err %@",
response,error]];
Expand All @@ -201,24 +194,16 @@ - (instancetype)initWithBaseUrl:(NSURL *)baseUrl {
cb([ARTHttpResponse responseWithStatus:500 headers:nil body:nil]);
}
else {
NSLog(@"no error");
if (httpResponse) {
int status = (int)httpResponse.statusCode;
NSLog(@"we have a erpsonse %d", status);;
[ARTLog debug:
[NSString stringWithFormat:@"ARTHttp response status is %d", status]];
[ARTLog verbose:[NSString stringWithFormat:@"ARTHttp received response %@",[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]]];
CFRunLoopPerformBlock(rl, kCFRunLoopDefaultMode, ^{
NSLog(@"WTF THIS ---------");
});

CFRunLoopPerformBlock(rl, kCFRunLoopDefaultMode, ^{
NSLog(@"WELL THEN %d", status);;
cb([ARTHttpResponse responseWithStatus:status headers:httpResponse.allHeaderFields body:data]);
});
} else {
CFRunLoopPerformBlock(rl, kCFRunLoopDefaultMode, ^{
NSLog(@"THE OTHER THING");
cb([ARTHttpResponse response]);
});
}
Expand Down
2 changes: 1 addition & 1 deletion ably-ios/ARTOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ - (instancetype)initWithKey:(NSString *)key {
}

-(NSString *) restHost {
return _environment ?[NSString stringWithFormat:@"%@-%@", _environment, self.restHost] : self.restHost;
return _environment ?[NSString stringWithFormat:@"%@-%@", _environment, _restHost] : _restHost;
}

-(NSString * ) defaultRestHost {
Expand Down
20 changes: 9 additions & 11 deletions ably-ios/ARTPresenceMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
#import <Foundation/Foundation.h>


#import "ARTPresenceMessage.h"
@interface ARTPresenceMap : NSObject
{
@class ARTPresenceMessage;
@interface ARTPresenceMap : NSObject {

}

@property (readwrite, nonatomic, assign) int64_t syncSerial;
- (ARTPresenceMessage *)getClient:(NSString *) clientId;
- (void)put:(ARTPresenceMessage *) message;





- (NSArray *)getClient:(NSString *) clientId; //returns the current presencestate
- (bool)put:(ARTPresenceMessage *) message;
- (NSArray *)values;
- (bool)remove:(ARTPresenceMessage *) message;
//of the form <NSString *, ARTPresenceMessage*> where
// the key is the clientId and the value is the latest relevant ARTPresenceMessage for that clientId.
- (NSDictionary *) members;
- (void)startSync;
- (void)endSync;
- (BOOL)isSyncComplete;
@end
59 changes: 41 additions & 18 deletions ably-ios/ARTPresenceMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,64 @@
//

#import "ARTPresenceMap.h"

#import "ARTPresenceMessage.h"

@interface ARTPresenceMap ()

@property (readwrite, strong, atomic) NSMutableSet * residualMembers;
@property (readwrite, strong, atomic) NSMutableDictionary * members; // of the form <NSString * key, : NSArray * artpresencemessages>

@property (readwrite, strong, atomic) NSMutableDictionary * mostRecentMessageForMember; //<clientId, artPresenceMessage *> message
@property (readonly, nonatomic, assign) bool syncStarted;
@property (readonly, nonatomic, assign) bool syncComplete;
@end

@implementation ARTPresenceMap

- (NSArray *)getClient:(NSString *) clientId {
return nil;
-(id) init {
self = [super init];
if(self) {
self.mostRecentMessageForMember = [NSMutableDictionary dictionary];
_syncStarted = false;
_syncComplete = false;
}
return self;
}

- (bool)put:(ARTPresenceMessage *) message {
return false;
}

- (NSArray *)values {
return nil;
- (ARTPresenceMessage *)getClient:(NSString *) clientId {
NSLog(@"messages is %@", self.mostRecentMessageForMember);
return [self.mostRecentMessageForMember objectForKey:clientId];
}

-(bool)remove:(ARTPresenceMessage *) message {
return false;
- (void)put:(ARTPresenceMessage *) message {
NSLog(@"putting client id %@", message.clientId);
ARTPresenceMessage * latest = [self.mostRecentMessageForMember objectForKey:message.clientId];
if(!latest || latest.timestamp < message.timestamp) {
NSLog(@"indeed");
[self.mostRecentMessageForMember setObject:message forKey:message.clientId];
}
}

- (void)startSync {
NSLog(@"start sync");
self.mostRecentMessageForMember = [NSMutableDictionary dictionary];
_syncStarted = true;

}

- (NSDictionary *) members {
return self.mostRecentMessageForMember;
}
- (void)endSync {

NSLog(@"end sync");
NSArray * keys = [self.mostRecentMessageForMember allKeys];
for(NSString * key in keys) {
ARTPresenceMessage * message = [self.mostRecentMessageForMember objectForKey:key];
if(message.action == ArtPresenceMessageAbsent || message.action == ARTPresenceMessageLeave) {
[self.mostRecentMessageForMember removeObjectForKey:key];
}
}
_syncComplete = true;
}

- (BOOL)isSyncComplete {
return self.syncStarted && self.syncComplete;

}

#pragma mark private

Expand Down
2 changes: 2 additions & 0 deletions ably-ios/ARTProtocolMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ typedef NS_ENUM(NSUInteger, ARTProtocolMessageAction) {
@property (readonly, assign, nonatomic) BOOL ackRequired;
@property (readwrite, assign, nonatomic) int64_t flags;

-(BOOL) syncInOperation;

- (BOOL)mergeFrom:(ARTProtocolMessage *)msg;

@end
3 changes: 3 additions & 0 deletions ably-ios/ARTProtocolMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ - (void) setConnectionSerial:(int64_t)connectionSerial {
- (BOOL)ackRequired {
return self.action == ARTProtocolMessageMessage || self.action == ARTProtocolMessagePresence || self.action == ARTProtocolMessageDetach;
}
-(BOOL) syncInOperation {
return self.flags & 0x1;
}

@end
11 changes: 8 additions & 3 deletions ably-ios/ARTRealtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
#import "ARTPresenceMessage.h"
#import "ARTPaginatedResult.h"


#define ART_WARN_UNUSED_RESULT __attribute__((warn_unused_result))

@class ARTPresenceMap;

typedef NS_ENUM(NSUInteger, ARTRealtimeChannelState) {
ARTRealtimeChannelInitialised,
ARTRealtimeChannelAttaching,
Expand Down Expand Up @@ -59,14 +62,14 @@ typedef NS_ENUM(NSUInteger, ARTRealtimeConnectionState) {
- (void)publishEnterClient:(NSString *) clientId data:(id) data cb:(ARTStatusCallback) cb;
- (void)publishUpdateClient:(NSString *) clientId data:(id) data cb:(ARTStatusCallback) cb;
- (void)publishLeaveClient:(NSString *) clientId data:(id) data cb:(ARTStatusCallback) cb;

- (BOOL)presenceSyncComplete;


- (id<ARTCancellable>)history:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;

-(id<ARTCancellable>)presence:(ARTPaginatedResultCb) cb;
-(id<ARTCancellable>)presenceWithParams:(NSDictionary *) queryParams cb:(ARTPaginatedResultCb) cb;
-(id<ARTCancellable>)presenceGet:(ARTPaginatedResultCb) cb;
-(id<ARTCancellable>)presenceGetWithParams:(NSDictionary *) queryParams cb:(ARTPaginatedResultCb) cb;
- (id<ARTCancellable>)presenceHistory:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)presenceHistoryWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;

Expand All @@ -87,6 +90,7 @@ typedef void (^ARTRealtimeChannelStateCb)(ARTRealtimeChannelState, ARTStatus *);
- (void)detach;
- (void)releaseChannel; //ARC forbids implementation of release
- (ARTRealtimeChannelState)state;
- (ARTPresenceMap *) presenceMap;
@end

@interface ARTRealtime : NSObject
Expand All @@ -109,6 +113,7 @@ typedef void (^ARTRealtimeConstructorCb)(ARTRealtime * realtime );
- (id<ARTCancellable>)time:(void(^)(ARTStatus *status, NSDate *time))cb;



typedef void (^ARTRealtimePingCb)(ARTStatus *);
- (void)ping:(ARTRealtimePingCb) cb;
- (id<ARTCancellable>)stats:(ARTPaginatedResultCb)cb;
Expand Down
Loading

0 comments on commit 29f795f

Please sign in to comment.