ReceiveController.m
上传用户:shqiling
上传日期:2009-10-04
资源大小:154k
文件大小:10k
源码类别:

MacOS编程

开发平台:

Objective-C

  1. /*
  2.     File:       ReceiveController.m
  3.     Contains:   Manages the receive tab.
  4.     Written by: DTS
  5.     Copyright:  Copyright (c) 2009 Apple Inc. All Rights Reserved.
  6.     Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  7.                 ("Apple") in consideration of your agreement to the following
  8.                 terms, and your use, installation, modification or
  9.                 redistribution of this Apple software constitutes acceptance of
  10.                 these terms.  If you do not agree with these terms, please do
  11.                 not use, install, modify or redistribute this Apple software.
  12.                 In consideration of your agreement to abide by the following
  13.                 terms, and subject to these terms, Apple grants you a personal,
  14.                 non-exclusive license, under Apple's copyrights in this
  15.                 original Apple software (the "Apple Software"), to use,
  16.                 reproduce, modify and redistribute the Apple Software, with or
  17.                 without modifications, in source and/or binary forms; provided
  18.                 that if you redistribute the Apple Software in its entirety and
  19.                 without modifications, you must retain this notice and the
  20.                 following text and disclaimers in all such redistributions of
  21.                 the Apple Software. Neither the name, trademarks, service marks
  22.                 or logos of Apple Inc. may be used to endorse or promote
  23.                 products derived from the Apple Software without specific prior
  24.                 written permission from Apple.  Except as expressly stated in
  25.                 this notice, no other rights or licenses, express or implied,
  26.                 are granted by Apple herein, including but not limited to any
  27.                 patent rights that may be infringed by your derivative works or
  28.                 by other works in which the Apple Software may be incorporated.
  29.                 The Apple Software is provided by Apple on an "AS IS" basis. 
  30.                 APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  31.                 WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
  32.                 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
  33.                 THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  34.                 COMBINATION WITH YOUR PRODUCTS.
  35.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
  36.                 INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  37.                 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38.                 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY
  39.                 OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  40.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY
  41.                 OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
  42.                 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF
  43.                 SUCH DAMAGE.
  44. */
  45. #import "ReceiveController.h"
  46. #import "AppDelegate.h"
  47. @interface ReceiveController ()
  48. // Properties that don't need to be seen by the outside world.
  49. @property (nonatomic, readonly) BOOL              isReceiving;
  50. @property (nonatomic, retain)   NSNetService *    netService;
  51. @property (nonatomic, retain)   NSInputStream *   networkStream;
  52. @property (nonatomic, copy)     NSString *        filePath;
  53. @property (nonatomic, retain)   NSOutputStream *  fileStream;
  54. @end
  55. @implementation ReceiveController
  56. #pragma mark * Status management
  57. // These methods are used by the core transfer code to update the UI.
  58. - (void)_receiveDidStart
  59. {
  60.     // Clear the current image so that we get a nice visual cue if the receive fails.
  61.     self.imageView.image = [UIImage imageNamed:@"NoImage.png"];
  62.     self.statusLabel.text = @"Receiving";
  63.     [self.receiveOrCancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
  64.     [self.activityIndicator startAnimating];
  65.     [[AppDelegate sharedAppDelegate] didStartNetworking];
  66. }
  67. - (void)_updateStatus:(NSString *)statusString
  68. {
  69.     assert(statusString != nil);
  70.     self.statusLabel.text = statusString;
  71. }
  72. - (void)_receiveDidStopWithStatus:(NSString *)statusString
  73. {
  74.     if (statusString == nil) {
  75.         assert(self.filePath != nil);
  76.         self.imageView.image = [UIImage imageWithContentsOfFile:self.filePath];
  77.         statusString = @"Receive succeeded";
  78.     }
  79.     self.statusLabel.text = statusString;
  80.     [self.receiveOrCancelButton setTitle:@"Receive" forState:UIControlStateNormal];
  81.     [self.activityIndicator stopAnimating];
  82.     [[AppDelegate sharedAppDelegate] didStopNetworking];
  83. }
  84. #pragma mark * Core transfer code
  85. // This is the code that actually does the networking.
  86. @synthesize netService    = _netService;
  87. @synthesize networkStream = _networkStream;
  88. @synthesize filePath      = _filePath;
  89. @synthesize fileStream    = _fileStream;
  90. - (BOOL)isReceiving
  91. {
  92.     return (self.networkStream != nil);
  93. }
  94. - (void)_startReceive
  95. {
  96.     NSInputStream *     input;
  97.     BOOL                success;
  98.     
  99.     assert(self.networkStream == nil);      // don't tap receive twice in a row!
  100.     assert(self.fileStream == nil);         // ditto
  101.     assert(self.filePath == nil);           // ditto
  102.     // Open a stream for the file we're going to send.
  103.     self.filePath = [[AppDelegate sharedAppDelegate] pathForTemporaryFileWithPrefix:@"Receive"];
  104.     assert(self.filePath != nil);
  105.     
  106.     self.fileStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:NO];
  107.     assert(self.fileStream != nil);
  108.     
  109.     [self.fileStream open];
  110.     // Open a stream to the server, finding the server via Bonjour.  Then configure 
  111.     // the stream for async operation.
  112.     self.netService = [[[NSNetService alloc] initWithDomain:@"local." type:@"_x-SNSDownload._tcp." name:@"Test"] autorelease];
  113.     assert(self.netService != nil);
  114.     success = [self.netService getInputStream:&input outputStream:NULL];
  115.     assert(success);
  116.     
  117.     self.networkStream = input;
  118.     
  119.     // -[NSNetService getInputStream:outputStream:] currently returns the stream 
  120.     // with a reference that we have to release (something that's counter to the 
  121.     // standard Cocoa memory management rules <rdar://problem/6868813>).
  122.     
  123.     [input release];
  124.     
  125.     self.networkStream.delegate = self;
  126.     [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  127.     
  128.     [self.networkStream open];
  129.     // Tell the UI we're receiving.
  130.     
  131.     [self _receiveDidStart];
  132. }
  133. - (void)_stopReceiveWithStatus:(NSString *)statusString
  134. {
  135.     self.netService = nil;
  136.     if (self.networkStream != nil) {
  137.         self.networkStream.delegate = nil;
  138.         [self.networkStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  139.         [self.networkStream close];
  140.         self.networkStream = nil;
  141.     }
  142.     if (self.fileStream != nil) {
  143.         [self.fileStream close];
  144.         self.fileStream = nil;
  145.     }
  146.     [self _receiveDidStopWithStatus:statusString];
  147.     self.filePath = nil;
  148. }
  149. - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
  150.     // An NSStream delegate callback that's called when events happen on our 
  151.     // network stream.
  152. {
  153.     #pragma unused(aStream)
  154.     assert(aStream == self.networkStream);
  155.     switch (eventCode) {
  156.         case NSStreamEventOpenCompleted: {
  157.             [self _updateStatus:@"Opened connection"];
  158.         } break;
  159.         case NSStreamEventHasBytesAvailable: {
  160.             NSInteger       bytesRead;
  161.             uint8_t         buffer[32768];
  162.             [self _updateStatus:@"Receiving"];
  163.             
  164.             // Pull some data off the network.
  165.             
  166.             bytesRead = [self.networkStream read:buffer maxLength:sizeof(buffer)];
  167.             if (bytesRead == -1) {
  168.                 [self _stopReceiveWithStatus:@"Network read error"];
  169.             } else if (bytesRead == 0) {
  170.                 [self _stopReceiveWithStatus:nil];
  171.             } else {
  172.                 NSInteger   bytesWritten;
  173.                 NSInteger   bytesWrittenSoFar;
  174.                 
  175.                 // Write to the file.
  176.                 
  177.                 bytesWrittenSoFar = 0;
  178.                 do {
  179.                     bytesWritten = [self.fileStream write:&buffer[bytesWrittenSoFar] maxLength:bytesRead - bytesWrittenSoFar];
  180.                     assert(bytesWritten != 0);
  181.                     if (bytesWritten == -1) {
  182.                         [self _stopReceiveWithStatus:@"File write error"];
  183.                         break;
  184.                     } else {
  185.                         bytesWrittenSoFar += bytesWritten;
  186.                     }
  187.                 } while (bytesWrittenSoFar != bytesRead);
  188.             }
  189.         } break;
  190.         case NSStreamEventHasSpaceAvailable: {
  191.             assert(NO);     // should never happen for the output stream
  192.         } break;
  193.         case NSStreamEventErrorOccurred: {
  194.             [self _stopReceiveWithStatus:@"Stream open error"];
  195.         } break;
  196.         case NSStreamEventEndEncountered: {
  197.             // ignore
  198.         } break;
  199.         default: {
  200.             assert(NO);
  201.         } break;
  202.     }
  203. }
  204. #pragma mark * Actions
  205. - (IBAction)receiveOrCancelAction:(id)sender
  206. {
  207.     #pragma unused(sender)
  208.     if (self.isReceiving) {
  209.         [self _stopReceiveWithStatus:@"Cancelled"];
  210.     } else {
  211.         [self _startReceive];
  212.     }
  213. }
  214. #pragma mark * View controller boilerplate
  215. @synthesize imageView             = _imageView;
  216. @synthesize statusLabel           = _statusLabel;
  217. @synthesize activityIndicator     = _activityIndicator;
  218. @synthesize receiveOrCancelButton = _receiveOrCancelButton;
  219. - (void)dealloc
  220. {
  221.     [self _stopReceiveWithStatus:@"Stopped"];
  222.     [self->_imageView release];
  223.     self->_imageView = nil;
  224.     [self->_statusLabel release];
  225.     self->_statusLabel = nil;
  226.     [self->_activityIndicator release];
  227.     self->_activityIndicator = nil;
  228.     [self->_receiveOrCancelButton release];
  229.     self->_receiveOrCancelButton = nil;
  230.     [super dealloc];
  231. }
  232. - (void)setView:(UIView *)newValue
  233. {
  234.     if (newValue == nil) {
  235.         self.imageView = nil;
  236.         self.statusLabel = nil;
  237.         self.activityIndicator = nil;
  238.         self.receiveOrCancelButton = nil;
  239.     }
  240.     [super setView:newValue];
  241. }
  242. - (void)viewDidLoad
  243. {
  244.     [super viewDidLoad];
  245.     assert(self.imageView != nil);
  246.     assert(self.statusLabel != nil);
  247.     assert(self.activityIndicator != nil);
  248.     assert(self.receiveOrCancelButton != nil);
  249.     
  250.     self.activityIndicator.hidden = YES;
  251.     self.statusLabel.text = @"Tap Receive to start receiving";
  252. }
  253. @end