1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/PortMapper/PortMapperTest.m Wed Apr 22 16:45:39 2009 -0700
1.3 @@ -0,0 +1,111 @@
1.4 +// PortMapperTest.m
1.5 +// MYNetwork
1.6 +//
1.7 +// Created by Jens Alfke on 1/4/08.
1.8 +// Copyright 2008 Jens Alfke. All rights reserved.
1.9 +//
1.10 +
1.11 +
1.12 +
1.13 +#import "MYPortMapper.h"
1.14 +#import "IPAddress.h"
1.15 +#import "Test.h"
1.16 +#import "Logging.h"
1.17 +
1.18 +#if DEBUG
1.19 +
1.20 +
1.21 +/** A trivial class that just demonstrates how to create a MYPortMapper and listen for changes. */
1.22 +@interface MYPortMapperTest : NSObject
1.23 +{
1.24 + MYPortMapper *_mapper;
1.25 +}
1.26 +
1.27 +@end
1.28 +
1.29 +
1.30 +@implementation MYPortMapperTest
1.31 +
1.32 +
1.33 +- (id) init
1.34 +{
1.35 + self = [super init];
1.36 + if( self ) {
1.37 + // Create MYPortMapper. This example doesn't open a real socket; just pretend that there's
1.38 + // already a TCP socket listening on port 8080. To experiment, you could turn on Web
1.39 + // Sharing, then change this port number to 80 -- that will make your computer's local
1.40 + // Apache web server reachable from the outside world while this program is running.
1.41 + _mapper = [[MYPortMapper alloc] initWithLocalPort: 80];
1.42 +
1.43 + // Optionally, request a public port number.
1.44 + // The NAT is free to ignore this and return a random number instead.
1.45 + _mapper.desiredPublicPort = 22222;
1.46 +
1.47 + // Now open the mapping (asynchronously):
1.48 + if( [_mapper open] ) {
1.49 + Log(@"Opening port mapping...");
1.50 + // Now listen for notifications to find out when the mapping opens, fails, or changes:
1.51 + [[NSNotificationCenter defaultCenter] addObserver: self
1.52 + selector: @selector(portMappingChanged:)
1.53 + name: MYPortMapperChangedNotification
1.54 + object: _mapper];
1.55 + } else {
1.56 + // MYPortMapper failed -- this is unlikely, but be graceful:
1.57 + Log(@"!! Error: MYPortMapper wouldn't start: %i",_mapper.error);
1.58 + [self release];
1.59 + return nil;
1.60 + }
1.61 + }
1.62 + return self;
1.63 +}
1.64 +
1.65 +
1.66 +- (void) portMappingChanged: (NSNotification*)n
1.67 +{
1.68 + // This is where we get notified that the mapping was created, or that no mapping exists,
1.69 + // or that mapping failed.
1.70 + if( _mapper.error )
1.71 + Log(@"!! MYPortMapper error %i", _mapper.error);
1.72 + else {
1.73 + NSString *message = @"";
1.74 + if( !_mapper.isMapped )
1.75 + message = @" (no address translation!)";
1.76 + Log(@"** Public address:port is %@%@", _mapper.publicAddress, message);
1.77 + Log(@" local address:port is %@", _mapper.localAddress);
1.78 + }
1.79 +}
1.80 +
1.81 +
1.82 +- (void) dealloc
1.83 +{
1.84 + [_mapper close];
1.85 + [_mapper release];
1.86 + [super dealloc];
1.87 +}
1.88 +
1.89 +
1.90 +@end
1.91 +
1.92 +
1.93 +
1.94 +TestCase(MYPortMapper) {
1.95 +
1.96 + EnableLogTo(PortMapper,YES);
1.97 + // Here's how to simply obtain your local and public address(es):
1.98 + IPAddress *addr = [IPAddress localAddress];
1.99 + Log(@"** Local address is %@%@ ...getting public addr...",
1.100 + addr, (addr.isPrivate ?@" (private)" :@""));
1.101 + addr = [MYPortMapper findPublicAddress];
1.102 + Log(@"** Public address is %@", addr);
1.103 +
1.104 + // Start up the test class to create a mapping:
1.105 + MYPortMapperTest *test = [[MYPortMapperTest alloc] init];
1.106 +
1.107 + // Now let the runloop run forever...
1.108 + Log(@"Running the runloop forever...");
1.109 + [[NSRunLoop currentRunLoop] run];
1.110 +
1.111 + [test release];
1.112 +}
1.113 +
1.114 +#endif DEBUG