snej@25: /* snej@25: snej@25: File: MyViewController.m snej@25: Abstract: A view controller responsible for managing the Hello World view. snej@25: snej@25: Version: 1.7 snej@25: snej@25: Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. snej@25: ("Apple") in consideration of your agreement to the following terms, and your snej@25: use, installation, modification or redistribution of this Apple software snej@25: constitutes acceptance of these terms. If you do not agree with these terms, snej@25: please do not use, install, modify or redistribute this Apple software. snej@25: snej@25: In consideration of your agreement to abide by the following terms, and subject snej@25: to these terms, Apple grants you a personal, non-exclusive license, under snej@25: Apple's copyrights in this original Apple software (the "Apple Software"), to snej@25: use, reproduce, modify and redistribute the Apple Software, with or without snej@25: modifications, in source and/or binary forms; provided that if you redistribute snej@25: the Apple Software in its entirety and without modifications, you must retain snej@25: this notice and the following text and disclaimers in all such redistributions snej@25: of the Apple Software. snej@25: Neither the name, trademarks, service marks or logos of Apple Inc. may be used snej@25: to endorse or promote products derived from the Apple Software without specific snej@25: prior written permission from Apple. Except as expressly stated in this notice, snej@25: no other rights or licenses, express or implied, are granted by Apple herein, snej@25: including but not limited to any patent rights that may be infringed by your snej@25: derivative works or by other works in which the Apple Software may be snej@25: incorporated. snej@25: snej@25: The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO snej@25: WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED snej@25: WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR snej@25: PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN snej@25: COMBINATION WITH YOUR PRODUCTS. snej@25: snej@25: IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR snej@25: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE snej@25: GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) snej@25: ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR snej@25: DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF snej@25: CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF snej@25: APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. snej@25: snej@25: Copyright (C) 2008 Apple Inc. All Rights Reserved. snej@25: snej@25: */ snej@25: snej@25: #import "MyViewController.h" snej@25: #import "BLIP.h" snej@25: snej@25: snej@25: @implementation MyViewController snej@25: snej@25: @synthesize textField; snej@25: @synthesize label; snej@25: @synthesize string; snej@25: snej@25: - (void)viewDidLoad { snej@25: // When the user starts typing, show the clear button in the text field. snej@25: textField.clearButtonMode = UITextFieldViewModeWhileEditing; snej@25: snej@25: label.text = @"Opening listener socket..."; snej@25: snej@25: _listener = [[BLIPListener alloc] initWithPort: 12345]; snej@25: _listener.delegate = self; snej@25: _listener.pickAvailablePort = YES; snej@25: _listener.bonjourServiceType = @"_blipecho._tcp"; snej@25: [_listener open]; snej@25: } snej@25: snej@25: snej@25: - (void)updateString { snej@25: snej@25: // Store the text of the text field in the 'string' instance variable. snej@25: self.string = textField.text; snej@25: // Set the text of the label to the value of the 'string' instance variable. snej@25: label.text = self.string; snej@25: } snej@25: snej@25: snej@25: - (BOOL)textFieldShouldReturn:(UITextField *)theTextField { snej@25: // When the user presses return, take focus away from the text field so that the keyboard is dismissed. snej@25: if (theTextField == textField) { snej@25: [textField resignFirstResponder]; snej@25: // Invoke the method that changes the greeting. snej@25: [self updateString]; snej@25: } snej@25: return YES; snej@25: } snej@25: snej@25: snej@25: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event snej@25: { snej@25: // Dismiss the keyboard when the view outside the text field is touched. snej@25: [textField resignFirstResponder]; snej@25: // Revert the text field to the previous value. snej@25: [super touchesBegan:touches withEvent:event]; snej@25: } snej@25: snej@25: snej@25: - (void)dealloc { snej@25: [textField release]; snej@25: [label release]; snej@25: snej@25: [_listener close]; snej@25: [_listener release]; snej@25: snej@25: [super dealloc]; snej@25: } snej@25: snej@25: snej@25: #pragma mark BLIP Listener Delegate: snej@25: snej@25: snej@25: - (void) listenerDidOpen: (TCPListener*)listener snej@25: { snej@25: label.text = [NSString stringWithFormat: @"Listening on port %i",listener.port]; snej@25: } snej@25: snej@25: - (void) listener: (TCPListener*)listener failedToOpen: (NSError*)error snej@25: { snej@25: label.text = [NSString stringWithFormat: @"Failed to open listener on port %i: %@", snej@25: listener.port,error]; snej@25: } snej@25: snej@25: - (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection snej@25: { snej@25: label.text = [NSString stringWithFormat: @"Accepted connection from %@", snej@25: connection.address]; snej@25: connection.delegate = self; snej@25: } snej@25: snej@25: - (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error snej@25: { snej@25: label.text = [NSString stringWithFormat: @"Failed to open connection from %@: %@", snej@25: connection.address,error]; snej@25: } snej@25: snej@25: - (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request snej@25: { snej@25: NSString *message = [[NSString alloc] initWithData: request.body encoding: NSUTF8StringEncoding]; snej@25: label.text = [NSString stringWithFormat: @"Echoed:\nā€œ%@ā€",message]; snej@25: [request respondWithData: request.body contentType: request.contentType]; snej@25: } snej@25: snej@25: - (void) connectionDidClose: (TCPConnection*)connection; snej@25: { snej@25: label.text = [NSString stringWithFormat: @"Connection closed from %@", snej@25: connection.address]; snej@25: } snej@25: snej@25: snej@25: @end