snej@25
|
1 |
|
snej@25
|
2 |
#import "MyViewController.h"
|
snej@25
|
3 |
#import "BLIP.h"
|
snej@25
|
4 |
|
snej@25
|
5 |
|
snej@25
|
6 |
@implementation MyViewController
|
snej@25
|
7 |
|
snej@25
|
8 |
@synthesize textField;
|
snej@25
|
9 |
@synthesize label;
|
snej@25
|
10 |
@synthesize string;
|
snej@25
|
11 |
|
snej@25
|
12 |
- (void)viewDidLoad {
|
snej@25
|
13 |
// When the user starts typing, show the clear button in the text field.
|
snej@25
|
14 |
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
snej@25
|
15 |
|
snej@25
|
16 |
label.text = @"Opening listener socket...";
|
snej@25
|
17 |
|
snej@25
|
18 |
_listener = [[BLIPListener alloc] initWithPort: 12345];
|
snej@25
|
19 |
_listener.delegate = self;
|
snej@25
|
20 |
_listener.pickAvailablePort = YES;
|
snej@25
|
21 |
_listener.bonjourServiceType = @"_blipecho._tcp";
|
snej@25
|
22 |
[_listener open];
|
snej@25
|
23 |
}
|
snej@25
|
24 |
|
snej@25
|
25 |
|
snej@25
|
26 |
- (void)updateString {
|
snej@25
|
27 |
|
snej@25
|
28 |
// Store the text of the text field in the 'string' instance variable.
|
snej@25
|
29 |
self.string = textField.text;
|
snej@25
|
30 |
// Set the text of the label to the value of the 'string' instance variable.
|
snej@25
|
31 |
label.text = self.string;
|
snej@25
|
32 |
}
|
snej@25
|
33 |
|
snej@25
|
34 |
|
snej@25
|
35 |
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
|
snej@25
|
36 |
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
|
snej@25
|
37 |
if (theTextField == textField) {
|
snej@25
|
38 |
[textField resignFirstResponder];
|
snej@25
|
39 |
// Invoke the method that changes the greeting.
|
snej@25
|
40 |
[self updateString];
|
snej@25
|
41 |
}
|
snej@25
|
42 |
return YES;
|
snej@25
|
43 |
}
|
snej@25
|
44 |
|
snej@25
|
45 |
|
snej@25
|
46 |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
|
snej@25
|
47 |
{
|
snej@25
|
48 |
// Dismiss the keyboard when the view outside the text field is touched.
|
snej@25
|
49 |
[textField resignFirstResponder];
|
snej@25
|
50 |
// Revert the text field to the previous value.
|
snej@25
|
51 |
[super touchesBegan:touches withEvent:event];
|
snej@25
|
52 |
}
|
snej@25
|
53 |
|
snej@25
|
54 |
|
snej@25
|
55 |
- (void)dealloc {
|
snej@25
|
56 |
[textField release];
|
snej@25
|
57 |
[label release];
|
snej@25
|
58 |
|
snej@25
|
59 |
[_listener close];
|
snej@25
|
60 |
[_listener release];
|
snej@25
|
61 |
|
snej@25
|
62 |
[super dealloc];
|
snej@25
|
63 |
}
|
snej@25
|
64 |
|
snej@25
|
65 |
|
snej@25
|
66 |
#pragma mark BLIP Listener Delegate:
|
snej@25
|
67 |
|
snej@25
|
68 |
|
snej@25
|
69 |
- (void) listenerDidOpen: (TCPListener*)listener
|
snej@25
|
70 |
{
|
snej@25
|
71 |
label.text = [NSString stringWithFormat: @"Listening on port %i",listener.port];
|
snej@25
|
72 |
}
|
snej@25
|
73 |
|
snej@25
|
74 |
- (void) listener: (TCPListener*)listener failedToOpen: (NSError*)error
|
snej@25
|
75 |
{
|
snej@25
|
76 |
label.text = [NSString stringWithFormat: @"Failed to open listener on port %i: %@",
|
snej@25
|
77 |
listener.port,error];
|
snej@25
|
78 |
}
|
snej@25
|
79 |
|
snej@25
|
80 |
- (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection
|
snej@25
|
81 |
{
|
snej@25
|
82 |
label.text = [NSString stringWithFormat: @"Accepted connection from %@",
|
snej@25
|
83 |
connection.address];
|
snej@25
|
84 |
connection.delegate = self;
|
snej@25
|
85 |
}
|
snej@25
|
86 |
|
snej@25
|
87 |
- (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error
|
snej@25
|
88 |
{
|
snej@25
|
89 |
label.text = [NSString stringWithFormat: @"Failed to open connection from %@: %@",
|
snej@25
|
90 |
connection.address,error];
|
snej@25
|
91 |
}
|
snej@25
|
92 |
|
snej@25
|
93 |
- (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request
|
snej@25
|
94 |
{
|
snej@25
|
95 |
NSString *message = [[NSString alloc] initWithData: request.body encoding: NSUTF8StringEncoding];
|
snej@25
|
96 |
label.text = [NSString stringWithFormat: @"Echoed:\nā%@ā",message];
|
snej@25
|
97 |
[request respondWithData: request.body contentType: request.contentType];
|
danpreston@41
|
98 |
[message release];
|
snej@25
|
99 |
}
|
snej@25
|
100 |
|
snej@25
|
101 |
- (void) connectionDidClose: (TCPConnection*)connection;
|
snej@25
|
102 |
{
|
snej@25
|
103 |
label.text = [NSString stringWithFormat: @"Connection closed from %@",
|
snej@25
|
104 |
connection.address];
|
snej@25
|
105 |
}
|
snej@25
|
106 |
|
snej@25
|
107 |
|
snej@25
|
108 |
@end
|