transmission/macosx/PortChecker.m

117 lines
3.8 KiB
Mathematica
Raw Normal View History

2007-09-16 01:02:06 +00:00
/******************************************************************************
* $Id$
*
* Copyright (c) 2006-2007 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#import "PortChecker.h"
#import "NSApplicationAdditions.h"
2007-09-16 01:02:06 +00:00
2007-12-19 20:46:00 +00:00
#define CHECKER_URL @"http://transmission.m0k.org/PortCheck.php?port=%d"
2007-09-16 01:02:06 +00:00
@implementation PortChecker
- (id) initForPort: (int) portNumber withDelegate: (id) delegate
2007-09-16 01:02:06 +00:00
{
if ((self = [super init]))
{
2007-12-18 21:13:36 +00:00
fDelegate = delegate;
NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
2007-12-19 20:46:00 +00:00
[NSString stringWithFormat: CHECKER_URL, portNumber]] cachePolicy:
2007-12-18 21:13:36 +00:00
[NSApp isOnLeopardOrBetter] ? NSURLRequestReloadIgnoringLocalCacheData : NSURLRequestReloadIgnoringCacheData
timeoutInterval: 15.0];
fStatus = PORT_STATUS_CHECKING;
if ((fConnection = [[NSURLConnection alloc] initWithRequest: portProbeRequest delegate: self]))
2007-12-18 21:13:36 +00:00
fPortProbeData = [[NSMutableData alloc] init];
else
{
NSLog(@"Unable to get port status: failed to initiate connection");
[self callBackWithStatus: PORT_STATUS_ERROR];
}
2007-09-16 01:02:06 +00:00
}
return self;
}
- (void) dealloc
{
[fConnection release];
[fPortProbeData release];
[super dealloc];
}
2007-09-16 01:02:06 +00:00
- (port_status_t) status
{
return fStatus;
}
- (void) endProbe
{
[fConnection cancel];
}
2007-09-16 01:02:06 +00:00
- (void) callBackWithStatus: (port_status_t) status
{
fStatus = status;
if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
[fDelegate performSelectorOnMainThread: @selector(portCheckerDidFinishProbing:) withObject: self waitUntilDone: NO];
}
#pragma mark NSURLConnection delegate methods
- (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
{
[fPortProbeData setLength: 0];
}
- (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
{
[fPortProbeData appendData: data];
}
- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
{
NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
[self callBackWithStatus: PORT_STATUS_ERROR];
}
- (void) connectionDidFinishLoading: (NSURLConnection *) connection
{
NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSASCIIStringEncoding];
2007-09-16 01:02:06 +00:00
port_status_t status;
2007-12-18 20:05:12 +00:00
if ([probeString isEqualToString: @"1"])
status = PORT_STATUS_OPEN;
2007-12-18 20:05:12 +00:00
else if ([probeString isEqualToString: @"0"])
status = PORT_STATUS_CLOSED;
2007-09-16 01:02:06 +00:00
else
status = PORT_STATUS_ERROR;
[self callBackWithStatus: status];
[probeString release];
2007-09-16 01:02:06 +00:00
}
@end