source: trunk/macosx/PortChecker.m @ 4225

Last change on this file since 4225 was 4225, checked in by livings124, 15 years ago

initial group filtering

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 3.8 KB
Line 
1/******************************************************************************
2 * $Id: PortChecker.m 4225 2007-12-19 20:46:00Z livings124 $
3 *
4 * Copyright (c) 2006-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "PortChecker.h"
26#import "NSApplicationAdditions.h"
27
28#define CHECKER_URL @"http://transmission.m0k.org/PortCheck.php?port=%d"
29
30@implementation PortChecker
31
32- (id) initForPort: (int) portNumber withDelegate: (id) delegate
33{
34    if ((self = [super init]))
35    {
36        fDelegate = delegate;
37       
38        NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
39                [NSString stringWithFormat: CHECKER_URL, portNumber]] cachePolicy:
40                [NSApp isOnLeopardOrBetter] ? NSURLRequestReloadIgnoringLocalCacheData : NSURLRequestReloadIgnoringCacheData
41                timeoutInterval: 15.0];
42       
43        fStatus = PORT_STATUS_CHECKING;
44       
45        if ((fConnection = [[NSURLConnection alloc] initWithRequest: portProbeRequest delegate: self]))
46            fPortProbeData = [[NSMutableData alloc] init];
47        else
48        {
49            NSLog(@"Unable to get port status: failed to initiate connection");
50            [self callBackWithStatus: PORT_STATUS_ERROR];
51        }
52    }
53   
54    return self;
55}
56
57- (void) dealloc
58{
59    [fConnection release];
60    [fPortProbeData release];
61    [super dealloc];
62}
63
64- (port_status_t) status
65{
66    return fStatus;
67}
68
69- (void) endProbe
70{
71    [fConnection cancel];
72}
73
74- (void) callBackWithStatus: (port_status_t) status
75{
76    fStatus = status;
77   
78    if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
79        [fDelegate performSelectorOnMainThread: @selector(portCheckerDidFinishProbing:) withObject: self waitUntilDone: NO];
80}
81
82#pragma mark NSURLConnection delegate methods
83
84- (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
85{
86    [fPortProbeData setLength: 0];
87}
88
89- (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
90{
91    [fPortProbeData appendData: data];
92}
93
94- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
95{
96    NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
97    [self callBackWithStatus: PORT_STATUS_ERROR];
98}
99
100- (void) connectionDidFinishLoading: (NSURLConnection *) connection
101{
102    NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSASCIIStringEncoding];
103   
104    port_status_t status;
105    if ([probeString isEqualToString: @"1"])
106        status = PORT_STATUS_OPEN;
107    else if ([probeString isEqualToString: @"0"])
108        status = PORT_STATUS_CLOSED;
109    else
110        status = PORT_STATUS_ERROR;
111   
112    [self callBackWithStatus: status];
113    [probeString release];
114}
115
116@end
Note: See TracBrowser for help on using the repository browser.