PortMapper/PortMapperTest.m
author Jens Alfke <jens@mooseyard.com>
Fri Jul 24 14:06:28 2009 -0700 (2009-07-24)
changeset 63 5e4855a592ee
parent 26 cb9cdf247239
permissions -rw-r--r--
* The BLIPConnection receivedRequest: delegate method now returns BOOL. If the method returns NO (or if the method isn't implemented in the delegate), that means it didn't handle the message at all; an error will be returned to the sender.
* If the connection closes unexpectedly due to an error, then the auto-generated responses to pending requests will contain that error. This makes it easier to display a meaningful error message in the handler for the request.
     1 //  PortMapperTest.m
     2 //  MYNetwork
     3 //
     4 //  Created by Jens Alfke on 1/4/08.
     5 //  Copyright 2008 Jens Alfke. All rights reserved.
     6 //
     7 
     8 
     9 
    10 #import "MYPortMapper.h"
    11 #import "IPAddress.h"
    12 #import "Test.h"
    13 #import "Logging.h"
    14 
    15 #if DEBUG
    16 
    17 
    18 /** A trivial class that just demonstrates how to create a MYPortMapper and listen for changes. */
    19 @interface MYPortMapperTest : NSObject
    20 {
    21     MYPortMapper *_mapper;
    22 }
    23 
    24 @end
    25 
    26 
    27 @implementation MYPortMapperTest
    28 
    29 
    30 - (id) init
    31 {
    32     self = [super init];
    33     if( self ) {
    34         // Create MYPortMapper. This example doesn't open a real socket; just pretend that there's
    35         // already a TCP socket listening on port 8080. To experiment, you could turn on Web
    36         // Sharing, then change this port number to 80 -- that will make your computer's local
    37         // Apache web server reachable from the outside world while this program is running.
    38         _mapper = [[MYPortMapper alloc] initWithLocalPort: 80];
    39         
    40         // Optionally, request a public port number.
    41         // The NAT is free to ignore this and return a random number instead.
    42         _mapper.desiredPublicPort = 22222;
    43         
    44         // Now open the mapping (asynchronously):
    45         if( [_mapper start] ) {
    46             Log(@"Opening port mapping...");
    47             // Now listen for notifications to find out when the mapping opens, fails, or changes:
    48             [[NSNotificationCenter defaultCenter] addObserver: self 
    49                                                      selector: @selector(portMappingChanged:) 
    50                                                          name: MYPortMapperChangedNotification 
    51                                                        object: _mapper];
    52         } else {
    53             // MYPortMapper failed -- this is unlikely, but be graceful:
    54             Log(@"!! Error: MYPortMapper wouldn't start: %i",_mapper.error);
    55             [self release];
    56             return nil;
    57         }
    58     }
    59     return self;
    60 }
    61 
    62 
    63 - (void) portMappingChanged: (NSNotification*)n
    64 {
    65     // This is where we get notified that the mapping was created, or that no mapping exists,
    66     // or that mapping failed.
    67     if( _mapper.error )
    68         Log(@"!! MYPortMapper error %i", _mapper.error);
    69     else {
    70         NSString *message = @"";
    71         if( !_mapper.isMapped )
    72             message = @" (no address translation!)";
    73         Log(@"** Public address:port is %@%@", _mapper.publicAddress, message);
    74         Log(@"    local address:port is %@", _mapper.localAddress);
    75     }
    76 }
    77 
    78 
    79 - (void) dealloc
    80 {
    81     [_mapper stop];
    82     [_mapper release];
    83     [super dealloc];
    84 }
    85 
    86 
    87 @end
    88 
    89 
    90 
    91 TestCase(MYPortMapper) {
    92     
    93     EnableLogTo(DNS,YES);
    94     EnableLogTo(PortMapper,YES);
    95     
    96     // Here's how to simply obtain your local and public address(es):
    97     IPAddress *addr = [IPAddress localAddress];
    98     Log(@"** Local address is %@%@ ...getting public addr...", 
    99         addr, (addr.isPrivate ?@" (private)" :@""));
   100     addr = [MYPortMapper findPublicAddress];
   101     Log(@"** Public address is %@", addr);
   102 
   103     // Start up the test class to create a mapping:
   104     MYPortMapperTest *test = [[MYPortMapperTest alloc] init];
   105     
   106     // Now let the runloop run forever...
   107     Log(@"Running the runloop forever...");
   108     [[NSRunLoop currentRunLoop] run];
   109         
   110     [test release];
   111 }
   112 
   113 #endif DEBUG