iPhone/Classes/MyViewController.m
changeset 25 a4875607a3a0
child 26 cb9cdf247239
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/iPhone/Classes/MyViewController.m	Tue Dec 02 22:42:56 2008 -0800
     1.3 @@ -0,0 +1,153 @@
     1.4 +/*
     1.5 +
     1.6 +File: MyViewController.m
     1.7 +Abstract: A view controller responsible for managing the Hello World view.
     1.8 +
     1.9 +Version: 1.7
    1.10 +
    1.11 +Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc.
    1.12 +("Apple") in consideration of your agreement to the following terms, and your
    1.13 +use, installation, modification or redistribution of this Apple software
    1.14 +constitutes acceptance of these terms.  If you do not agree with these terms,
    1.15 +please do not use, install, modify or redistribute this Apple software.
    1.16 +
    1.17 +In consideration of your agreement to abide by the following terms, and subject
    1.18 +to these terms, Apple grants you a personal, non-exclusive license, under
    1.19 +Apple's copyrights in this original Apple software (the "Apple Software"), to
    1.20 +use, reproduce, modify and redistribute the Apple Software, with or without
    1.21 +modifications, in source and/or binary forms; provided that if you redistribute
    1.22 +the Apple Software in its entirety and without modifications, you must retain
    1.23 +this notice and the following text and disclaimers in all such redistributions
    1.24 +of the Apple Software.
    1.25 +Neither the name, trademarks, service marks or logos of Apple Inc. may be used
    1.26 +to endorse or promote products derived from the Apple Software without specific
    1.27 +prior written permission from Apple.  Except as expressly stated in this notice,
    1.28 +no other rights or licenses, express or implied, are granted by Apple herein,
    1.29 +including but not limited to any patent rights that may be infringed by your
    1.30 +derivative works or by other works in which the Apple Software may be
    1.31 +incorporated.
    1.32 +
    1.33 +The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
    1.34 +WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
    1.35 +WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.36 +PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
    1.37 +COMBINATION WITH YOUR PRODUCTS.
    1.38 +
    1.39 +IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
    1.40 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    1.41 +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.42 +ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
    1.43 +DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
    1.44 +CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
    1.45 +APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.46 +
    1.47 +Copyright (C) 2008 Apple Inc. All Rights Reserved.
    1.48 +
    1.49 +*/
    1.50 +
    1.51 +#import "MyViewController.h"
    1.52 +#import "BLIP.h"
    1.53 +
    1.54 +
    1.55 +@implementation MyViewController
    1.56 +
    1.57 +@synthesize textField;
    1.58 +@synthesize label;
    1.59 +@synthesize string;
    1.60 +
    1.61 +- (void)viewDidLoad {
    1.62 +    // When the user starts typing, show the clear button in the text field.
    1.63 +    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    1.64 +    
    1.65 +    label.text = @"Opening listener socket...";
    1.66 +
    1.67 +    _listener = [[BLIPListener alloc] initWithPort: 12345];
    1.68 +    _listener.delegate = self;
    1.69 +    _listener.pickAvailablePort = YES;
    1.70 +    _listener.bonjourServiceType = @"_blipecho._tcp";
    1.71 +    [_listener open];
    1.72 +}
    1.73 +
    1.74 +
    1.75 +- (void)updateString {
    1.76 +	
    1.77 +	// Store the text of the text field in the 'string' instance variable.
    1.78 +	self.string = textField.text;
    1.79 +    // Set the text of the label to the value of the 'string' instance variable.
    1.80 +	label.text = self.string;
    1.81 +}
    1.82 +
    1.83 +
    1.84 +- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    1.85 +	// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    1.86 +	if (theTextField == textField) {
    1.87 +		[textField resignFirstResponder];
    1.88 +        // Invoke the method that changes the greeting.
    1.89 +        [self updateString];
    1.90 +	}
    1.91 +	return YES;
    1.92 +}
    1.93 +
    1.94 +
    1.95 +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    1.96 +{
    1.97 +    // Dismiss the keyboard when the view outside the text field is touched.
    1.98 +    [textField resignFirstResponder];
    1.99 +    // Revert the text field to the previous value.
   1.100 +    [super touchesBegan:touches withEvent:event];
   1.101 +}
   1.102 +
   1.103 +
   1.104 +- (void)dealloc {
   1.105 +	[textField release];
   1.106 +	[label release];
   1.107 +    
   1.108 +    [_listener close];
   1.109 +    [_listener release];
   1.110 +
   1.111 +	[super dealloc];
   1.112 +}
   1.113 +
   1.114 +
   1.115 +#pragma mark BLIP Listener Delegate:
   1.116 +
   1.117 +
   1.118 +- (void) listenerDidOpen: (TCPListener*)listener
   1.119 +{
   1.120 +    label.text = [NSString stringWithFormat: @"Listening on port %i",listener.port];
   1.121 +}
   1.122 +
   1.123 +- (void) listener: (TCPListener*)listener failedToOpen: (NSError*)error
   1.124 +{
   1.125 +    label.text = [NSString stringWithFormat: @"Failed to open listener on port %i: %@",
   1.126 +                  listener.port,error];
   1.127 +}
   1.128 +
   1.129 +- (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection
   1.130 +{
   1.131 +    label.text = [NSString stringWithFormat: @"Accepted connection from %@",
   1.132 +                  connection.address];
   1.133 +    connection.delegate = self;
   1.134 +}
   1.135 +
   1.136 +- (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error
   1.137 +{
   1.138 +    label.text = [NSString stringWithFormat: @"Failed to open connection from %@: %@",
   1.139 +                  connection.address,error];
   1.140 +}
   1.141 +
   1.142 +- (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request
   1.143 +{
   1.144 +    NSString *message = [[NSString alloc] initWithData: request.body encoding: NSUTF8StringEncoding];
   1.145 +    label.text = [NSString stringWithFormat: @"Echoed:\nā€œ%@ā€",message];
   1.146 +    [request respondWithData: request.body contentType: request.contentType];
   1.147 +}
   1.148 +
   1.149 +- (void) connectionDidClose: (TCPConnection*)connection;
   1.150 +{
   1.151 +    label.text = [NSString stringWithFormat: @"Connection closed from %@",
   1.152 +                  connection.address];
   1.153 +}
   1.154 +
   1.155 +
   1.156 +@end