iPhone/Classes/MyViewController.m
author snej@snej.local
Tue Dec 02 22:42:56 2008 -0800 (2008-12-02)
changeset 25 a4875607a3a0
child 26 cb9cdf247239
permissions -rwxr-xr-x
Added iPhone demo project
     1 /*
     2 
     3 File: MyViewController.m
     4 Abstract: A view controller responsible for managing the Hello World view.
     5 
     6 Version: 1.7
     7 
     8 Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc.
     9 ("Apple") in consideration of your agreement to the following terms, and your
    10 use, installation, modification or redistribution of this Apple software
    11 constitutes acceptance of these terms.  If you do not agree with these terms,
    12 please do not use, install, modify or redistribute this Apple software.
    13 
    14 In consideration of your agreement to abide by the following terms, and subject
    15 to these terms, Apple grants you a personal, non-exclusive license, under
    16 Apple's copyrights in this original Apple software (the "Apple Software"), to
    17 use, reproduce, modify and redistribute the Apple Software, with or without
    18 modifications, in source and/or binary forms; provided that if you redistribute
    19 the Apple Software in its entirety and without modifications, you must retain
    20 this notice and the following text and disclaimers in all such redistributions
    21 of the Apple Software.
    22 Neither the name, trademarks, service marks or logos of Apple Inc. may be used
    23 to endorse or promote products derived from the Apple Software without specific
    24 prior written permission from Apple.  Except as expressly stated in this notice,
    25 no other rights or licenses, express or implied, are granted by Apple herein,
    26 including but not limited to any patent rights that may be infringed by your
    27 derivative works or by other works in which the Apple Software may be
    28 incorporated.
    29 
    30 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
    31 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
    32 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    33 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
    34 COMBINATION WITH YOUR PRODUCTS.
    35 
    36 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
    37 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    38 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    39 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
    40 DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
    41 CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
    42 APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    43 
    44 Copyright (C) 2008 Apple Inc. All Rights Reserved.
    45 
    46 */
    47 
    48 #import "MyViewController.h"
    49 #import "BLIP.h"
    50 
    51 
    52 @implementation MyViewController
    53 
    54 @synthesize textField;
    55 @synthesize label;
    56 @synthesize string;
    57 
    58 - (void)viewDidLoad {
    59     // When the user starts typing, show the clear button in the text field.
    60     textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    61     
    62     label.text = @"Opening listener socket...";
    63 
    64     _listener = [[BLIPListener alloc] initWithPort: 12345];
    65     _listener.delegate = self;
    66     _listener.pickAvailablePort = YES;
    67     _listener.bonjourServiceType = @"_blipecho._tcp";
    68     [_listener open];
    69 }
    70 
    71 
    72 - (void)updateString {
    73 	
    74 	// Store the text of the text field in the 'string' instance variable.
    75 	self.string = textField.text;
    76     // Set the text of the label to the value of the 'string' instance variable.
    77 	label.text = self.string;
    78 }
    79 
    80 
    81 - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    82 	// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    83 	if (theTextField == textField) {
    84 		[textField resignFirstResponder];
    85         // Invoke the method that changes the greeting.
    86         [self updateString];
    87 	}
    88 	return YES;
    89 }
    90 
    91 
    92 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    93 {
    94     // Dismiss the keyboard when the view outside the text field is touched.
    95     [textField resignFirstResponder];
    96     // Revert the text field to the previous value.
    97     [super touchesBegan:touches withEvent:event];
    98 }
    99 
   100 
   101 - (void)dealloc {
   102 	[textField release];
   103 	[label release];
   104     
   105     [_listener close];
   106     [_listener release];
   107 
   108 	[super dealloc];
   109 }
   110 
   111 
   112 #pragma mark BLIP Listener Delegate:
   113 
   114 
   115 - (void) listenerDidOpen: (TCPListener*)listener
   116 {
   117     label.text = [NSString stringWithFormat: @"Listening on port %i",listener.port];
   118 }
   119 
   120 - (void) listener: (TCPListener*)listener failedToOpen: (NSError*)error
   121 {
   122     label.text = [NSString stringWithFormat: @"Failed to open listener on port %i: %@",
   123                   listener.port,error];
   124 }
   125 
   126 - (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection
   127 {
   128     label.text = [NSString stringWithFormat: @"Accepted connection from %@",
   129                   connection.address];
   130     connection.delegate = self;
   131 }
   132 
   133 - (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error
   134 {
   135     label.text = [NSString stringWithFormat: @"Failed to open connection from %@: %@",
   136                   connection.address,error];
   137 }
   138 
   139 - (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request
   140 {
   141     NSString *message = [[NSString alloc] initWithData: request.body encoding: NSUTF8StringEncoding];
   142     label.text = [NSString stringWithFormat: @"Echoed:\nā€œ%@ā€",message];
   143     [request respondWithData: request.body contentType: request.contentType];
   144 }
   145 
   146 - (void) connectionDidClose: (TCPConnection*)connection;
   147 {
   148     label.text = [NSString stringWithFormat: @"Connection closed from %@",
   149                   connection.address];
   150 }
   151 
   152 
   153 @end