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