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