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