This repository was archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPebbleCubeSDK.mm
372 lines (299 loc) · 9 KB
/
PebbleCubeSDK.mm
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
//
// Connect.mm
// PebbleCubeSDK
//
// Created by Richard Adem on 7/11/10.
// Copyright 2010 PebbleCube. All rights reserved.
//
#define IGNORE_CERT_AUTHENTICATION
#import "PebbleCubeSDK.h"
#import "FileHelper.h"
#import "JSON.h"
#import "consts.h"
@interface PebbleCubeSDK()
@property(nonatomic, retain) NSXMLParser *xmlParser;
@property(nonatomic, retain) NSString* apiSignature;
@property(nonatomic, retain) NSString* apiKey;
@property(nonatomic, retain) NSString* sessionKey;
@property(nonatomic, retain) NSMutableArray* eventArray;
- (void) SendEvent: (NSMutableDictionary*) info;
- (void) FireEvent;
@end
// ------------------------------------------------------
@implementation PebbleCubeSDK
@synthesize xmlParser;
@synthesize sessionKey;
@synthesize apiSignature;
@synthesize apiKey;
@synthesize eventArray = _eventArray;
- (id)initWithSaveToStorage: (BOOL) save
{
self = [super init];
if (self) {
/* class-specific initialization goes here */
saveEventsToStorage = NO;
saveEventsToStorage = save;
eventSendInProgress = NO;
_eventArray = [[NSMutableArray alloc] init];
if (saveEventsToStorage)
{
[FileHelper Load:_eventArray];
}
[NSTimer scheduledTimerWithTimeInterval:4
target:self
selector:@selector(FireEvent)
userInfo:nil
repeats:YES];
[self setApiSignature:@""];
[self setApiKey:@""];
}
return self;
}
- (id) init
{
return [self initWithSaveToStorage: NO];
}
- (void) MakeConnection: (NSString*) api_sig
withKey: (NSString*) api_key
andVersion: (NSString*) version
andTime: (NSString*) time;
{
Log(@"+MakeConnection");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if ([sessionKey length] <= 0)
{
[self setApiSignature:api_sig];
[self setApiKey:api_key];
NSString* urlString = [NSString stringWithFormat:@"https://api.pebblecube.com/sessions/start?api_sig=%@&api_key=%@&version=%@&time=%@"
, api_sig
, api_key
, version
, time
];
@synchronized(response)
{
response = [[NSMutableData data] retain];
}
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Log(@"urlString: %@", urlString);
}
else
{
Log(@"sessionKey length > 0, session started already, cannot start again yet");
}
[pool drain];
Log(@"-MakeConnection");
}
- (void) CloseConnection: (NSString*) api_sig
withKey: (NSString*) api_key
andTime: (NSString*) time
{
Log(@"+CloseConnection");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if ([sessionKey length] > 0)
{
NSString* urlString = [NSString stringWithFormat:@"https://api.pebblecube.com/sessions/stop?api_sig=%@&api_key=%@&session_key=%@&time=%@"
, api_sig
, api_key
, sessionKey
, time
];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// Perform request and get JSON back as a NSData object
NSData *r = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *rString = [[NSString alloc] initWithData:r encoding:NSUTF8StringEncoding];
Log(@"urlString: %@", urlString);
Log(@"rString: %@", rString);
[rString release];
[self setSessionKey: @""];
[self setApiSignature:@""];
[self setApiKey:@""];
}
else
{
Log(@"sessionkey length <= 0, session not started, cannot stop");
}
[pool drain];
Log(@"-CloseConnection");
}
#define IGNORE_CERT_AUTHENTICATION
#if defined(IGNORE_CERT_AUTHENTICATION)
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
//if ([trustedHosts containsObject:challenge.protectionSpace.host])
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
#endif
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)resp
{
@synchronized(response)
{
[response setLength: 0];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
@synchronized(response)
{
[response appendData:data];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
Log(@"ERROR with theConenction: \n%@", [error localizedDescription]);
[connection release];
@synchronized(response)
{
[response release];
}
eventSendInProgress = NO;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
@synchronized(response)
{
Log(@"DONE. Received Bytes: %d", [response length]);
}
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
Log(@"responce json_string: \n%@", json_string);
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSError *error = nil;
NSDictionary *object = [parser objectWithString:json_string error:&error];
Log(@"%@", [error description]);
NSString *key = [object objectForKey:@"k"];
if (key != nil)
{
[self setSessionKey:key];
Log(@"key: %@", key);
}
else
{
Log(@"key not found");
}
[json_string release];
[connection release];
[parser release];
@synchronized(response)
{
[response release];
}
if (eventSendInProgress)
{
@synchronized(_eventArray)
{
[_eventArray removeObjectAtIndex:0];
if (saveEventsToStorage)
{
[FileHelper Save:_eventArray];
}
}
eventSendInProgress = NO;
}
}
- (void) SendEvent: (NSMutableDictionary*) info
{
Log(@"+SendEvent");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Event *event = [[[Event alloc] init] autorelease];
[event setInfo: info];
@synchronized(_eventArray)
{
[_eventArray addObject: event];
if (saveEventsToStorage)
{
[FileHelper Save:_eventArray];
}
}
[pool drain];
Log(@"-SendEvent");
}
- (void) SendEvent: (NSString*) code
value: (NSObject*) value
andTime: (NSString*) time
{
NSMutableDictionary* info = [[[NSMutableDictionary alloc] init] autorelease];
[info setObject:code forKey:@"code"];
[info setObject:value forKey:@"value"];
[info setObject:time forKey:@"time"];
[self SendEvent:info];
}
- (void) FireEvent
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (!eventSendInProgress && [_eventArray count] > 0 && [sessionKey length] > 0)
{
eventSendInProgress = YES;
Log(@"fire event, event count: %d", [_eventArray count]);
Event* event;
NSMutableDictionary* eventInfo;
NSString* api_sig;
NSString* api_key;
@synchronized(_eventArray)
{
event = [[_eventArray objectAtIndex:0] retain];
eventInfo = [event info];
api_sig = [self apiSignature];
api_key = [self apiKey];
}
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSError* error = nil;
NSMutableArray* dictArray;
dictArray = [[NSMutableArray alloc] init];
[dictArray addObject: eventInfo];
NSString* jsonOut = [writer stringWithObject:dictArray error:&error];
jsonOut = [jsonOut stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
jsonOut = [jsonOut stringByReplacingOccurrencesOfString:@"{" withString:@"%7B"];
jsonOut = [jsonOut stringByReplacingOccurrencesOfString:@"}" withString:@"%7D"];
jsonOut = [jsonOut stringByReplacingOccurrencesOfString:@"[" withString:@"%5B"];
jsonOut = [jsonOut stringByReplacingOccurrencesOfString:@"]" withString:@"%5D"];
jsonOut = [jsonOut stringByReplacingOccurrencesOfString:@"\"" withString:@"%22"];
jsonOut = [jsonOut stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
NSString* sKey = [self sessionKey];
Log(@"sKey: %@", sKey);
NSString* urlString;
urlString = [NSString stringWithFormat:@"https://api.pebblecube.com/events/send?api_sig=%@&api_key=%@&session_key=%@&events=%@"
, api_sig
, api_key
, sKey
, jsonOut
];
@synchronized(response)
{
response = [[NSMutableData data] retain];
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlString]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 60.0];
[request setHTTPMethod:@"POST"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[event release];
[writer release];
[dictArray release];
}
[pool drain];
}
- (void)dealloc
{
@synchronized(response)
{
[response release];
}
[xmlParser release];
[sessionKey release];
[apiSignature release];
[apiKey release];
[_eventArray release];
[super dealloc];
}
@end