-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataModel.m
847 lines (841 loc) · 29 KB
/
DataModel.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
//
// DataModel.m
// ADModel
//
// Created by Doug Russell on 9/9/10.
// Copyright Doug Russell 2010. All rights reserved.
//
// 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 "DataModel.h"
#import "HandleXMLFeed.h"
#import "SynthesizeSingleton.h"
const void * MyRetainNoOp(CFAllocatorRef allocator, const void *value) { return value; }
void MyReleaseNoOp(CFAllocatorRef allocator, const void * value) { }
NSMutableArray * CreateNonRetainingArray()
{
CFArrayCallBacks callbacks = kCFTypeArrayCallBacks;
callbacks.retain = MyRetainNoOp;
callbacks.release = MyReleaseNoOp;
return (NSMutableArray*)CFArrayCreateMutable(nil, 0, &callbacks);
}
@implementation DataModel
SYNTHESIZE_SINGLETON_FOR_CLASS(DataModel)
/******************************************************************************/
#pragma mark -
#pragma mark Sample Ops
#pragma mark -
/******************************************************************************/
- (void)podcastFeed
{
//
// Retrieves and parses the RSS Feed for the podcast The Talk Show
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.instanceCode = kPodcastFeedCode;
op.baseURL = @"http://feeds.feedburner.com";
op.URI = @"/thetalkshow";
op.parseType = ParseXML;
op.xPath = @"item";
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)twitterSearchFeed
{
//
// *UNREVISEDCOMMENTS*
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.instanceCode = kTwitterSearchFeedCode;
op.baseURL = @"http://search.twitter.com";
op.URI = @"/search.json?q=+Asynchronous";
op.parseType = ParseJSONDictionary;
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)twitterUserFeed
{
//
// *UNREVISEDCOMMENTS*
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.instanceCode = kTwitterUserFeedCode;
op.baseURL = @"http://twitter.com";
op.URI = @"/statuses/user_timeline/gruber.json";
op.parseType = ParseJSONArray;
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)postPrint
{
//
// *UNREVISEDCOMMENTS*
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.requestType = POST;
op.parseType = ParseXML;
op.xPath = @"root";
op.instanceCode = kPostPrintCode;
op.URI = @"/ESModelAPI/Post/Form/";
op.bodyBufferDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"valueOne", @"keyOne",
@"valueTwo", @"keyTwo", nil];
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)postFilePrint
{
//
// *UNREVISEDCOMMENTS*
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.requestType = MULTI;
op.parseType = ParseXML;
op.xPath = @"root";
op.instanceCode = kPostFilePrintCode;
op.URI = @"/ESModelAPI/Post/Multipart/";
op.bodyBufferDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"valueOne", @"keyOne",
@"valueTwo", @"keyTwo", nil];
NSData * data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"]];
op.bodyDataArray = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"uploadedfile", @"fieldName",
@"file.txt", @"fileName",
@"text/plain", @"contentType",
data, @"data", nil], nil];
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)postMultiFilePrint
{
//
// *UNREVISEDCOMMENTS*
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.requestType = MULTI;
op.parseType = ParseXML;
op.xPath = @"root";
op.instanceCode = kPostMultiFilePrint;
op.URI = @"/ESModelAPI/Post/Multipart/";
op.bodyBufferDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"valueOne", @"keyOne",
@"valueTwo", @"keyTwo", nil];
NSData * txtdata = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"]];
NSData * imgdata = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Image" ofType:@"jpg"]];
op.bodyDataArray = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"uploadedtxtfile", @"fieldName",
@"file.txt", @"fileName",
@"text/plain", @"contentType",
txtdata, @"data", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"uploadedimgfile", @"fieldName",
@"image.jpg", @"fileName",
@"image/jpeg", @"contentType",
imgdata, @"data", nil], nil];
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)timeout
{
//
// Call a URL that will stall for 20 seconds,
// which should always timeout
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.instanceCode = kTimeoutCode;
op.URI = @"/ESModelAPI/Timeout/";
op.parseType = NoParse;
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)forbiddenURL
{
//
// Call a URL that returns a 403
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.instanceCode = kForbiddenURLCode;
op.URI = @"/ESModelAPI/Forbidden/";
op.parseType = NoParse;
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)missingFile
{
//
// Call a URL that returns a 404
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.instanceCode = kBadURLCode;
op.URI = @"/ESModelAPI/DoesntExist/";
op.parseType = NoParse;
//op.connectionID = [self performSelector:@selector(generateConnectionID)];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)badServerURL
{
//
// Call a URL that returns a 500
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.instanceCode = kBadServerURLCode;
op.URI = @"/ESModelAPI/BadResponse/";
op.parseType = NoParse;
op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)unavailableURL
{
//
// Call a URL that returns a 503
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.instanceCode = kUnavailableURLCode;
op.URI = @"/ESModelAPI/Unavailable/";
op.parseType = NoParse;
op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)putCreateURL
{
//
// *UNREVISEDCOMMENTS*
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.requestType = PUT;
op.parseType = NoParse;
op.instanceCode = kPutCreateURL;
op.URI = @"/ESModelAPI/Put/Created/index.php";
// op.headerDict = [NSDictionary dictionaryWithObjectsAndKeys:
// @"Header_Value_One", @"Header_Field_One",
// @"Header_Value_Two", @"Header_Field_Two", nil];
op.bodyDataArray = [NSArray arrayWithObject:[@"Put Test String\n" dataUsingEncoding:NSUTF8StringEncoding]];
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (void)putNoContentURL
{
//
// *UNREVISEDCOMMENTS*
//
NetworkOperation * op = [[NetworkOperation alloc] init];
op.delegate = self;
op.requestType = PUT;
op.parseType = NoParse;
op.instanceCode = kPutNoContentURL;
op.URI = @"/ESModelAPI/Put/NoContent/index.php";
op.headerDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Header_Value_One", @"Header_Field_One",
@"Header_Value_Two", @"Header_Field_Two", nil];
op.bodyDataArray = [NSArray arrayWithObject:[@"Put Test String Encoded As Data\n" dataUsingEncoding:NSUTF8StringEncoding]];
//op.connectionID = [self generateConnectionID];
if (connected)
[operationQueue addOperation:op];
else
[delayedOperations addObject:op];
[op release];
}
- (NSString *)generateConnectionID
{
return [NSString stringWithFormat:@"%f", CFAbsoluteTimeGetCurrent()];
}
- (NSString *)stringForNSURLError:(int)errorCode
{
switch (errorCode) {
case NSURLErrorUnknown:
//Returned when the URL Loading system encounters an error that it cannot interpret.
//This can occur when an error originates from a lower level framework or library.
//Whenever this error code is received, it is a bug, and should be reported to Apple.
return @"NSURLErrorUnknown";
break;
case NSURLErrorCancelled:
//Returned when an asynchronous load is canceled.
//A Web Kit framework delegate will receive this error when it performs a cancel
//operation on a loading resource. Note that an NSURLConnection or NSURLDownload
//delegate will not receive this error if the download is canceled.
return @"NSURLErrorCancelled";
break;
case NSURLErrorBadURL:
//Returned when a URL is sufficiently malformed that a URL request cannot be initiated
return @"NSURLErrorBadURL";
break;
case NSURLErrorTimedOut:
//Returned when an asynchronous operation times out.
//NSURLConnection will send this error to its delegate when the timeoutInterval in
//NSURLRequest expires before a load can complete.
return @"NSURLErrorTimedOut";
break;
case NSURLErrorUnsupportedURL:
//Returned when a properly formed URL cannot be handled by the framework.
//The most likely cause is that there is no available protocol handler for the URL.
return @"NSURLErrorUnsupportedURL";
break;
case NSURLErrorCannotFindHost:
//Returned when the host name for a URL cannot be resolved.
return @"Returned when the host name for a URL cannot be resolved.";
break;
case NSURLErrorCannotConnectToHost:
//Returned when an attempt to connect to a host has failed.
//This can occur when a host name resolves, but the host is down or may not be
//accepting connections on a certain port.
return @"NSURLErrorCannotConnectToHost";
break;
case NSURLErrorDataLengthExceedsMaximum:
//Returned when the length of the resource data exceeds the maximum allowed.
return @"NSURLErrorDataLengthExceedsMaximum";
break;
case NSURLErrorNetworkConnectionLost:
//Returned when a client or server connection is severed in the middle of an in-progress load.
return @"NSURLErrorNetworkConnectionLost";
break;
case NSURLErrorDNSLookupFailed:
//See NSURLErrorCannotFindHost
return @"NSURLErrorDNSLookupFailed";
break;
case NSURLErrorHTTPTooManyRedirects:
//Returned when a redirect loop is detected or when the threshold for number of allowable
//redirects has been exceeded (currently 16.
return @"NSURLErrorHTTPTooManyRedirects";
break;
case NSURLErrorResourceUnavailable:
//Returned when a requested resource cannot be retrieved.
//Examples are “file not found”, and data decoding problems that prevent data from
//being processed correctly.
return @"NSURLErrorResourceUnavailable";
break;
case NSURLErrorNotConnectedToInternet:
//Returned when a network resource was requested, but an internet connection is
//not established and cannot be established automatically, either through a lack
//of connectivity, or by the user's choice not to make a network connection automatically.
return @"NSURLErrorNotConnectedToInternet";
break;
case NSURLErrorRedirectToNonExistentLocation:
//Returned when a redirect is specified by way of server response code, but the
//server does not accompany this code with a redirect URL.
return @"NSURLErrorRedirectToNonExistentLocation";
break;
case NSURLErrorBadServerResponse:
//Returned when the URL Loading system receives bad data from the server.
//This is equivalent to the “500 Server Error” message sent by HTTP servers.
return @"NSURLErrorBadServerResponse";
break;
case NSURLErrorUserCancelledAuthentication:
//Returned when an asynchronous request for authentication is cancelled by the user.
//This is typically incurred by clicking a “Cancel” button in a username/password dialog,
//rather than the user making an attempt to authenticate.
return @"NSURLErrorUserCancelledAuthentication";
break;
case NSURLErrorUserAuthenticationRequired:
//Returned when authentication is required to access a resource.
return @"NSURLErrorUserAuthenticationRequired";
break;
case NSURLErrorZeroByteResource:
//Returned when a server reports that a URL has a non-zero content length, but
//terminates the network connection “gracefully” without sending any data.
return @"NSURLErrorUserAuthenticationRequired";
break;
case NSURLErrorCannotDecodeRawData:
//Returned when content data received during an NSURLConnection request cannot be
//decoded for a known content encoding.
return @"NSURLErrorCannotDecodeRawData";
break;
case NSURLErrorCannotDecodeContentData:
//Returned when content data received during an NSURLConnection request has an unknown
//content encoding.
return @"NSURLErrorCannotDecodeContentData";
break;
case NSURLErrorCannotParseResponse:
//Returned when a response to an NSURLConnection request cannot be parsed.
return @"NSURLErrorCannotParseResponse";
break;
case NSURLErrorInternationalRoamingOff:
//Returned when a connection would require activating a data context while roaming,
//but international roaming is disabled.
return @"NSURLErrorInternationalRoamingOff";
break;
case NSURLErrorCallIsActive:
//Returned when a connection is attempted while a phone call is active on a network
//that does not support simultaneous phone and data communication (EDGE or GPRS.
return @"NSURLErrorCallIsActive";
break;
case NSURLErrorRequestBodyStreamExhausted:
//Returned when a body stream is needed but the client does not provide one.
//This impacts clients on iOS that send a POST request using a body stream but do not
//implement the NSURLConnection delegate method connection:needNewBodyStream.
return @"NSURLErrorRequestBodyStreamExhausted";
break;
case NSURLErrorFileDoesNotExist:
//Returned when a file does not exist.
return @"NSURLErrorFileDoesNotExist";
break;
case NSURLErrorFileIsDirectory:
//Returned when a request for an FTP file results in the server responding that the
//file is not a plain file, but a directory.
return @"NSURLErrorFileIsDirectory";
break;
case NSURLErrorNoPermissionsToReadFile:
//Returned when a resource cannot be read due to insufficient permissions.
return @"NSURLErrorNoPermissionsToReadFile";
break;
case NSURLErrorSecureConnectionFailed:
//Returned when an attempt to establish a secure connection fails for reasons
//which cannot be expressed more specifically.
return @"NSURLErrorSecureConnectionFailed";
break;
case NSURLErrorServerCertificateHasBadDate:
//Returned when a server certificate has a date which indicates it has expired,
//or is not yet valid.
return @"NSURLErrorServerCertificateHasBadDate";
break;
case NSURLErrorServerCertificateUntrusted:
//Returned when a server certificate is signed by a root server which is not trusted.
return @"NSURLErrorServerCertificateUntrusted";
break;
case NSURLErrorServerCertificateHasUnknownRoot:
//Returned when a server certificate is not signed by any root server.
return @"NSURLErrorServerCertificateHasUnknownRoot";
break;
case NSURLErrorServerCertificateNotYetValid:
//Returned when a server certificate is not yet valid.
return @"NSURLErrorServerCertificateNotYetValid";
break;
case NSURLErrorClientCertificateRejected:
//Returned when a server certificate is rejected.
return @"NSURLErrorClientCertificateRejected";
break;
case NSURLErrorClientCertificateRequired:
//Returned when a client certificate is required to authenticate an
//SSL connection during an NSURLConnection request.
return @"NSURLErrorClientCertificateRequired";
break;
case NSURLErrorCannotLoadFromNetwork:
//Returned when a specific request to load an item only from the cache cannot be satisfied.
//This error is sent at the point when the library would go to the network accept for the
//fact that is has been blocked from doing so by the “load only from cache” directive.
return @"NSURLErrorCannotLoadFromNetwork";
break;
case NSURLErrorCannotCreateFile:
//Returned when NSURLDownload object was unable to create the downloaded file on disk due to a I/O failure.
return @"NSURLErrorCannotCreateFile";
break;
case NSURLErrorCannotOpenFile:
//Returned when NSURLDownload was unable to open the downloaded file on disk.
return @"NSURLErrorCannotOpenFile";
break;
case NSURLErrorCannotCloseFile:
//Returned when NSURLDownload was unable to close the downloaded file on disk.
return @"NSURLErrorCannotCloseFile";
break;
case NSURLErrorCannotWriteToFile:
//Returned when NSURLDownload was unable to write to the downloaded file on disk.
return @"NSURLErrorCannotWriteToFile";
case NSURLErrorCannotRemoveFile:
//Returned when NSURLDownload was unable to remove a downloaded file from disk.
return @"NSURLErrorCannotRemoveFile";
case NSURLErrorCannotMoveFile:
//Returned when NSURLDownload was unable to move a downloaded file on disk.
return @"NSURLErrorCannotMoveFile";
case NSURLErrorDownloadDecodingFailedMidStream:
//Returned when NSURLDownload failed to decode an encoded file during the download.
return @"NSURLErrorDownloadDecodingFailedMidStream";
case NSURLErrorDownloadDecodingFailedToComplete:
//Returned when NSURLDownload failed to decode an encoded file after downloading.
return @"NSURLErrorDownloadDecodingFailedToComplete";
default:
break;
}
return nil;
}
/******************************************************************************/
#pragma mark -
#pragma mark Network Operation Delegate
#pragma mark -
/******************************************************************************/
- (void)networkOperationDidComplete:(NetworkOperation *)operation withResult:(id)result
{
id value = nil;
if ([result isKindOfClass:[NSData class]])
value = [[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding] autorelease];
else
value = result;
NSInteger statusCode = 0;
NSDictionary * headers = nil;
if ([operation.response isKindOfClass:[NSHTTPURLResponse class]])
{
statusCode = [(NSHTTPURLResponse *)operation.response statusCode];
headers = [(NSHTTPURLResponse *)operation.response allHeaderFields];
}
NSLog(@"\n\nOperation Complete for:\nInstance Code: %d\nConnectionID: %@\nURL: %@%@\nResponse Status Code: %d\nHeaders: %@\n\n%@\n\n",
operation.instanceCode,
operation.connectionID,
operation.baseURL,
operation.URI,
statusCode,
headers,
value);
NSArray * dlgts;
NSString * resultString;
switch (operation.instanceCode) {
case kPodcastFeedCode:
NSParameterAssert([result isKindOfClass:[NSArray class]]);
NSParameterAssert(([(NSArray *)result count] > 0));
dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(podcastFeed:)])
[delegate podcastFeed:(NSArray *)result];
}
CleanRelease(dlgts);
break;
case kTwitterSearchFeedCode:
NSParameterAssert([result isKindOfClass:[NSDictionary class]]);
// In a real app I would decompose the tweets into
// custom model objects and extract @mentions and #hashtags for linking
// along with other data conditioning like HTML Entity decoding
// Also in a real app I would hold onto the refresh url instead of
// making new queries each time
NSArray * results = [(NSDictionary *)result objectForKey:@"results"];
dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(twitterSearchFeed:)])
[delegate twitterSearchFeed:results];
}
CleanRelease(dlgts);
break;
case kTwitterUserFeedCode:
NSParameterAssert([result isKindOfClass:[NSArray class]]);
// In a real app I would decompose the tweets into
// custom model objects and extract @mentions and #hashtags for linking
// along with other data conditioning like HTML Entity decoding
// Also in a real app I would hold onto the refresh url instead of
// making new queries each time
// Also in a real I would need to check for users set to priavte (result would be nil)
dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(twitterUserFeed:)])
[delegate twitterUserFeed:(NSArray *)result];
}
CleanRelease(dlgts);
break;
case kPostPrintCode:
NSParameterAssert([result isKindOfClass:[NSArray class]]);
resultString = [NSString stringWithFormat:@"%@", result];
NSParameterAssert((resultString != nil));
dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(postPrint:)])
[delegate postPrint:resultString];
}
CleanRelease(dlgts);
break;
case kPostFilePrintCode:
NSParameterAssert([result isKindOfClass:[NSArray class]]);
resultString = [NSString stringWithFormat:@"%@", result];
NSParameterAssert((resultString != nil));
dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(postFile:)])
[delegate postFile:resultString];
}
CleanRelease(dlgts);
break;
case kPostMultiFilePrint:
NSParameterAssert([result isKindOfClass:[NSArray class]]);
resultString = [NSString stringWithFormat:@"%@", result];
NSParameterAssert((resultString != nil));
dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(postMultiFile:)])
[delegate postMultiFile:resultString];
}
CleanRelease(dlgts);
break;
case kPutCreateURL:
NSParameterAssert([result isKindOfClass:[NSData class]]);
NSParameterAssert(([(NSHTTPURLResponse *)operation.response statusCode] == 201));
resultString = [[[NSString alloc] initWithData:(NSData *)result encoding:NSUTF8StringEncoding] autorelease];
NSParameterAssert((resultString != nil));
dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(putCreate:)])
[delegate putCreate:resultString];
}
CleanRelease(dlgts);
break;
case kPutNoContentURL:
NSParameterAssert([result isKindOfClass:[NSData class]]);
NSParameterAssert(([(NSData *)result length] == 0));
NSInteger statusCode = 0;
NSDictionary * headers = nil;
if ([operation.response isKindOfClass:[NSHTTPURLResponse class]])
{
statusCode = [(NSHTTPURLResponse *)operation.response statusCode];
headers = [(NSHTTPURLResponse *)operation.response allHeaderFields];
}
resultString = [NSString stringWithFormat:@"Response Status Code: %d\nHeaders: %@",
statusCode,
headers];
dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(putNoContent:)])
[delegate putNoContent:resultString];
}
CleanRelease(dlgts);
break;
default:
break;
}
}
- (void)networkOperationDidFail:(NetworkOperation *)operation withError:(NSError *)error
{
NSInteger statusCode = 0;
if ([operation.response isKindOfClass:[NSHTTPURLResponse class]])
statusCode = [(NSHTTPURLResponse *)operation.response statusCode];
NSLog(@"\n\nOperation Failed for:\nInstance Code: %d\nConnectionID: %@\nURL: %@%@\nError: %@\nResponse Status Code: %d\n%@\n\n",
operation.instanceCode,
operation.connectionID,
operation.baseURL,
operation.URI,
error,
statusCode,
[self stringForNSURLError:error.code]);
NSArray * dlgts = [[NSArray alloc] initWithArray:delegates];
for (id delegate in dlgts)
{
if ([delegate respondsToSelector:@selector(error:operationCode:)])
[delegate error:error operationCode:operation.instanceCode];
}
CleanRelease(dlgts);
}
/******************************************************************************/
#pragma mark -
#pragma mark Setup/Cleanup
#pragma mark -
/******************************************************************************/
- (id)init
{
if ((self = [super init]))
{
//
//
//
delegates = CreateNonRetainingArray();
//
//
//
operationQueue = [[NetworkOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:10];
//
//
//
delayedOperations = [[NSMutableArray alloc] init];
//
//
//
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(releaseSingleton)
name:UIApplicationWillTerminateNotification
object:nil];
//
// Register for changes in reachability and start reachability object
//
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
hostReach =
[[Reachability reachabilityWithHostName:@"getitdownonpaper.com"] retain];
[hostReach startNotifier];
}
return self;
}
- (void)reachabilityChanged:(NSNotification* )note
{
Reachability *curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateReachability:curReach];
}
- (void)updateReachability:(Reachability*)curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus) {
case NotReachable:
{
//NSLog(@"Model Disconnected");
connected = NO;
connectionType = NotReachable;
break;
}
case ReachableViaWWAN:
{
//NSLog(@"Model Connected");
connected = YES;
connectionType = ReachableViaWWAN;
break;
}
case ReachableViaWiFi:
{
//NSLog(@"Model Connected");
connected = YES;
connectionType = ReachableViaWiFi;
break;
}
}
if (connected)
{
[operationQueue addOperations:delayedOperations];
[delayedOperations removeAllObjects];
}
}
- (void)releaseSingleton
{
[super release];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:kReachabilityChangedNotification
object:nil];
[operationQueue cancelAllOperations];
CleanRelease(operationQueue);
CleanRelease(delegates);
CleanRelease(delayedOperations);
CleanRelease(hostReach);
[super dealloc];
}
/******************************************************************************/
#pragma mark -
#pragma mark Delegates
#pragma mark -
/******************************************************************************/
- (void)addDelegate:(id<DataModelDelegate>)dlgt
{
//
// Adds delegete to delegates array if not already added.
// Delegates array is non retaining to avoid retain loops.
// This might be faster if it used a set instead of an array.
//
// Delegate object must conform to the DataModelDelegate protocol.
//
// This is synchronized to avoid collisions with removeDelegate
// or other addDelegate calls.
//
// Not matching a addDelegate: call with a removeDelegate:
// call may cause released objects to be addressed.
//
@synchronized(self)
{
if (![delegates containsObject:dlgt])
{
[delegates addObject:dlgt];
}
}
}
- (void)removeDelegate:(id<DataModelDelegate>)dlgt
{
//
// Remove delegete from the delegates array.
//
// Delegate object must conform to the DataModelDelegate protocol.
//
// This is synchronized to avoid collisions with removeDelegate
// or other addDelegate calls.
//
// Not matching a addDelegate: call with a removeDelegate:
// call may cause released objects to be addressed.
//
// Multiple removeDelegate calls would be wasteful, but would not
// raise an error or exception.
//
@synchronized(self)
{
[delegates removeObject:dlgt];
}
}
@end