Skip to content

Commit

Permalink
Merge branch 'CL-179'
Browse files Browse the repository at this point in the history
  • Loading branch information
geremy cohen committed Oct 3, 2012
2 parents bf9354a + 6876ff0 commit 02d6a26
Show file tree
Hide file tree
Showing 10 changed files with 3,505 additions and 71 deletions.
18 changes: 15 additions & 3 deletions objective-c/3.3/Pubnub.xcodeproj/project.pbxproj
Expand Up @@ -7,8 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
C8C1CDD81611C71100FE66B6 /* JSONKit.m in Sources */ = {isa = PBXBuildFile; fileRef = C8C1CDD71611C71100FE66B6 /* JSONKit.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
C8C1CDDB1611CC3C00FE66B6 /* JSON.m in Sources */ = {isa = PBXBuildFile; fileRef = C8C1CDDA1611CC3C00FE66B6 /* JSON.m */; };
C8FD4D38153D447200D0A418 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8FD4D37153D447200D0A418 /* UIKit.framework */; };
C8FD4D3A153D447200D0A418 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8FD4D39153D447200D0A418 /* Foundation.framework */; };
C8FD4D3A153D447200D0A418 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8FD4D39153D447200D0A418 /* Foundation.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
C8FD4D3C153D447200D0A418 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8FD4D3B153D447200D0A418 /* CoreGraphics.framework */; };
C8FD4D42153D447200D0A418 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = C8FD4D40153D447200D0A418 /* InfoPlist.strings */; };
C8FD4D44153D447200D0A418 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = C8FD4D43153D447200D0A418 /* main.m */; };
Expand All @@ -22,6 +24,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
C8C1CDD61611C71100FE66B6 /* JSONKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONKit.h; sourceTree = "<group>"; };
C8C1CDD71611C71100FE66B6 /* JSONKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONKit.m; sourceTree = "<group>"; };
C8C1CDD91611CC1A00FE66B6 /* JSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSON.h; sourceTree = "<group>"; };
C8C1CDDA1611CC3C00FE66B6 /* JSON.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSON.m; sourceTree = "<group>"; };
C8FD4D33153D447200D0A418 /* Pubnub.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Pubnub.app; sourceTree = BUILT_PRODUCTS_DIR; };
C8FD4D37153D447200D0A418 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
C8FD4D39153D447200D0A418 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -114,6 +120,8 @@
C8FD4D6A153D469D00D0A418 /* PubNub */ = {
isa = PBXGroup;
children = (
C8C1CDD61611C71100FE66B6 /* JSONKit.h */,
C8C1CDD71611C71100FE66B6 /* JSONKit.m */,
C8FD4D6B153D469D00D0A418 /* Base64.h */,
C8FD4D6C153D469D00D0A418 /* Base64.m */,
C8FD4D6D153D469D00D0A418 /* CEPubnub.h */,
Expand All @@ -122,6 +130,8 @@
C8FD4D70153D469D00D0A418 /* Cipher.m */,
C8FD4D71153D469D00D0A418 /* Common.h */,
C8FD4D72153D469D00D0A418 /* Common.m */,
C8C1CDD91611CC1A00FE66B6 /* JSON.h */,
C8C1CDDA1611CC3C00FE66B6 /* JSON.m */,
);
path = PubNub;
sourceTree = "<group>";
Expand Down Expand Up @@ -195,6 +205,8 @@
C8FD4D7A153D469D00D0A418 /* CEPubnub.m in Sources */,
C8FD4D7B153D469D00D0A418 /* Cipher.m in Sources */,
C8FD4D7C153D469D00D0A418 /* Common.m in Sources */,
C8C1CDD81611C71100FE66B6 /* JSONKit.m in Sources */,
C8C1CDDB1611CC3C00FE66B6 /* JSON.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -263,7 +275,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Pubnub/Pubnub-Prefix.pch";
INFOPLIST_FILE = "Pubnub/Pubnub-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 4.0;
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -275,7 +287,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Pubnub/Pubnub-Prefix.pch";
INFOPLIST_FILE = "Pubnub/Pubnub-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 4.0;
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand Down
Binary file not shown.
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges</key>
<true/>
<key>SnapshotAutomaticallyBeforeSignificantChanges</key>
<true/>
</dict>
</plist>
Binary file not shown.
78 changes: 22 additions & 56 deletions objective-c/3.3/Pubnub/PubNub/CEPubnub.m
Expand Up @@ -14,6 +14,7 @@

#import "CEPubnub.h"
#import "Common.h"
#import "JSON.h"

#define kDefaultOrigin @"pubsub.pubnub.com"
#define kMaxHistorySize 100 // From documentation
Expand Down Expand Up @@ -116,19 +117,29 @@ - (void) connectionDidFinishLoading:(NSURLConnection*)connection {
NSString* contentType = [[_response allHeaderFields] objectForKey:@"Content-Type"];
if ([contentType hasPrefix:@"text/javascript"] && [contentType containsString:@"UTF-8"]) { // Should be [text/javascript; charset="UTF-8"] but is sometimes different on 3G
NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
if (error != nil) result = nil;
[_pubNub connection:self didCompleteWithResponse:result];
// NSLog(@"PubNub request returned unexpected content type: %@", contentType);

if ([NSJSONSerialization class]) {
id result= [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
if (error != nil) result = nil;
[_pubNub connection:self didCompleteWithResponse:result];
}else
{
[_pubNub connection:self didCompleteWithResponse:JSONParseData(_data)];
}
} else if ([contentType hasPrefix:@"text/javascript"])
{
if(_command== kCommand_Here_Now)
{

NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
if (error != nil) result = nil;
[_pubNub connection:self didCompleteWithResponse:result];
if ([NSJSONSerialization class]) {
id result = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
if (error != nil) result = nil;
[_pubNub connection:self didCompleteWithResponse:result];
}else
{
[_pubNub connection:self didCompleteWithResponse:JSONParseData(_data)];
}
}
}else {
NSLog(@"PubNub request returned unexpected content type: %@", contentType);
Expand Down Expand Up @@ -494,7 +505,6 @@ - (void) fetchHistory:(NSDictionary * )arg1 {
[_connections addObject:connection];
}


- (void) detailedHistory:(NSDictionary * )arg1 {

NSString* channel;
Expand Down Expand Up @@ -558,8 +568,6 @@ - (void) detailedHistory:(NSDictionary * )arg1 {
[_connections addObject:connection];
}



- (void) getTime {
NSString* url = [NSString stringWithFormat:@"%@/time/0", _host];
PubNubConnection* connection = [[PubNubConnection alloc] initWithPubNub:self
Expand All @@ -569,39 +577,6 @@ - (void) getTime {
[_connections addObject:connection];
}

NSDecimalNumber* time_token = 0;

- (void) getTime1 {
NSString* url = [NSString stringWithFormat:@"%@/time/0", _host];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kConnectionTimeOut];

[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response2,
NSData *data,
NSError *error)
{

if ([data length] >0 && error == nil)
{
NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) result = nil;

if ([result isKindOfClass:[NSArray class]] && ([result count] == 1)) {
time_token = [result objectAtIndex:0];
}
}
else if ([data length] == 0 && error == nil)
{
time_token=0;
}
else if (error != nil){
time_token=0;
}
}];
}

+ (NSString *) getUUID
{
Expand Down Expand Up @@ -716,25 +691,17 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
}
}
}
[self getTime1 ];
if (time_token == 0) {
_tryCount++;

}else
{
_tryCount=0;
if ([_delegate respondsToSelector:@selector(pubnub:Re_ConnectToChannel:)]) {
[_delegate pubnub:self Re_ConnectToChannel:connection.channel];
}
}
_tryCount++;
}
else {
for (ChannelStatus* it in [_subscriptions copy]) {
if ([it.channel isEqualToString:connection.channel])
{
_tryCount=0;
// Connect Callback
if (it.first == NO) {
it.first = YES;
it.connected=YES;
if ([_delegate respondsToSelector:@selector(pubnub:ConnectToChannel:)]) {
// NSLog(@"_Connected to channel %@",connection.channel);
[_delegate pubnub:self ConnectToChannel:connection.channel];
Expand Down Expand Up @@ -809,7 +776,7 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
if(kMinRetry == -1)
{
[self performSelector:@selector(_resubscribeToChannel:) withObject:connection.channel afterDelay:kMinRetryInterval];
}else if(_tryCount < kMinRetry)
}else if(_tryCount <= kMinRetry)
{
[self performSelector:@selector(_resubscribeToChannel:) withObject:connection.channel afterDelay:kMinRetryInterval];
}
Expand Down Expand Up @@ -907,7 +874,6 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
case kCommand_Here_Now: {
if ([response isKindOfClass:[NSDictionary class]] ) {
if ([_delegate respondsToSelector:@selector(pubnub:here_now:onChannel:)]) {

[_delegate pubnub:self here_now:response onChannel:connection.channel];
}
}
Expand Down
28 changes: 16 additions & 12 deletions objective-c/3.3/Pubnub/PubNub/Common.m
Expand Up @@ -9,7 +9,7 @@
#import "Common.h"
#import "Base64.h"
#import "Cipher.h"

#import "JSON.h"


@implementation NSString (Extensions)
Expand Down Expand Up @@ -108,17 +108,21 @@ + (NSString *)AES128DecryptWithKey:(NSString *)key Data:(NSString *)data
+(NSString*)JSONToString:(id)object
{
NSString * jsonString= nil;
if ([object isKindOfClass:[NSString class]]) {
object = [NSString stringWithFormat:@"\"%@\"", object];

return object;
} else {
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:object options:kNilOptions error:&error];
if (error != nil) return nil;
jsonString =[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

}
if ([NSJSONSerialization class]) {
if ([object isKindOfClass:[NSString class]]) {
object = [NSString stringWithFormat:@"\"%@\"", object];

return object;
} else {
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:object options:kNilOptions error:&error];
if (error != nil) return nil;
jsonString =[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

}
}else{
jsonString = JSONWriteString(object);
}
return jsonString;
}

Expand Down
32 changes: 32 additions & 0 deletions objective-c/3.3/Pubnub/PubNub/JSON.h
@@ -0,0 +1,32 @@
// Copyright 2011 Cooliris, 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.

#import <Foundation/Foundation.h>

#ifdef __cplusplus
extern "C" {
#endif

id JSONParseData(NSData* data); // Objects must be NSArray or NSDictionary - Assumes UTF8 encoding
NSData* JSONWriteData(id object); // Objects must be NSArray or NSDictionary - Assumes UTF8 encoding

id JSONParseString(NSString* string); // Objects must be NSArray or NSDictionary
NSString* JSONWriteString(id object); // Objects must be NSArray or NSDictionary

id JSONGetDictionaryValueForKey(NSDictionary* dictionary, NSString* key); // Converts NSNull to nil
id JSONGetArrayValueAtIndex(NSArray* array, NSUInteger index); // Allows out of bounds and converts NSNull to nil

#ifdef __cplusplus
}
#endif
94 changes: 94 additions & 0 deletions objective-c/3.3/Pubnub/PubNub/JSON.m
@@ -0,0 +1,94 @@
// Copyright 2011 Cooliris, 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.

#import "JSON.h"
#import "JSONKit.h"

#if 1
//#import "Logging.h"
#else
#define LOG_ERROR(...) NSLog(__VA_ARGS__)
#define DCHECK(...)
#endif

id JSONParseData(NSData* data) {
NSError* error = nil;
id object = [data objectFromJSONDataWithParseOptions:JKParseOptionNone error:&error];
if (object == nil) {
NSLog(@"JSON deserializing failed: %@", error);
}
return object;
}

NSData* JSONWriteData(id object) {
NSData* data = nil;
NSError* error = nil;
if ([object isKindOfClass:[NSString class]]) {
data = [(NSString*)object JSONDataWithOptions:JKSerializeOptionNone includeQuotes:YES error:&error];
} else if ([object isKindOfClass:[NSArray class]]) {
data = [(NSArray*)object JSONDataWithOptions:JKSerializeOptionNone error:&error];
} else if ([object isKindOfClass:[NSDictionary class]]) {
data = [(NSDictionary*)object JSONDataWithOptions:JKSerializeOptionNone error:&error];
} else {
// NOT_REACHED();
}
if (data == nil) {
NSLog(@"JSON serializing failed: %@", error);
}
return data;
}

id JSONParseString(NSString* string) {
NSError* error = nil;
id object = [string objectFromJSONStringWithParseOptions:JKParseOptionNone error:&error];
if (object == nil) {
NSLog(@"JSON deserializing failed: %@", error);
}
return object;
}

NSString* JSONWriteString(id object) {
NSString* string = nil;
NSError* error = nil;
if ([object isKindOfClass:[NSString class]]) {
string = [(NSString*)object JSONStringWithOptions:JKSerializeOptionNone includeQuotes:YES error:&error];
} else if ([object isKindOfClass:[NSArray class]]) {
string = [(NSArray*)object JSONStringWithOptions:JKSerializeOptionNone error:&error];
} else if ([object isKindOfClass:[NSDictionary class]]) {
string = [(NSDictionary*)object JSONStringWithOptions:JKSerializeOptionNone error:&error];
} else {
// NOT_REACHED();
}
if (string == nil) {
NSLog(@"JSON serializing failed: %@", error);
}
return string;
}

id JSONGetDictionaryValueForKey(NSDictionary* dictionary, NSString* key) {
// DCHECK(!dictionary || [dictionary isKindOfClass:[NSDictionary class]]);
id value = nil;
if(!dictionary || [dictionary isKindOfClass:[NSDictionary class]])
{
value = [dictionary objectForKey:key];

}
return (value == [NSNull null] ? nil : value);
}

id JSONGetArrayValueAtIndex(NSArray* array, NSUInteger index) {
// DCHECK(!array || [array isKindOfClass:[NSArray class]]);
id value = index < array.count ? [array objectAtIndex:index] : nil;
return (value == [NSNull null] ? nil : value);
}

0 comments on commit 02d6a26

Please sign in to comment.