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]; danpreston@41: [message release]; 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