source: trunk/macosx/PortChecker.m @ 6363

Last change on this file since 6363 was 6363, checked in by livings124, 14 years ago

use portcheck.transmissionbt.com instead of a third-party site

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 4.5 KB
Line 
1/******************************************************************************
2 * $Id: PortChecker.m 6363 2008-07-19 14:32:11Z livings124 $
3 *
4 * Copyright (c) 2006-2008 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://portcheck.transmissionbt.com/%d"
29#define CHECK_FIRE  3.0
30
31@implementation PortChecker
32
33- (id) initForPort: (int) portNumber withDelegate: (id) delegate
34{
35    if ((self = [super init]))
36    {
37        fDelegate = delegate;
38       
39        fPortNumber = portNumber;
40        fStatus = PORT_STATUS_CHECKING;
41       
42        fTimer = [NSTimer scheduledTimerWithTimeInterval: CHECK_FIRE target: self selector: @selector(startProbe)
43                    userInfo: nil repeats: NO];
44    }
45   
46    return self;
47}
48
49- (void) dealloc
50{
51    [fTimer invalidate];
52   
53    [fConnection release];
54    [fPortProbeData release];
55    [super dealloc];
56}
57
58- (port_status_t) status
59{
60    return fStatus;
61}
62
63- (void) startProbe
64{
65    fTimer = nil;
66   
67    NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
68                [NSString stringWithFormat: CHECKER_URL, fPortNumber]] cachePolicy:
69                [NSApp isOnLeopardOrBetter] ? NSURLRequestReloadIgnoringLocalCacheData : NSURLRequestReloadIgnoringCacheData
70                timeoutInterval: 15.0];
71   
72    if ((fConnection = [[NSURLConnection alloc] initWithRequest: portProbeRequest delegate: self]))
73        fPortProbeData = [[NSMutableData alloc] init];
74    else
75    {
76        NSLog(@"Unable to get port status: failed to initiate connection");
77        [self callBackWithStatus: PORT_STATUS_ERROR];
78    }
79}
80
81- (void) cancelProbe
82{
83    [fTimer invalidate];
84    fTimer = nil;
85   
86    [fConnection cancel];
87}
88
89- (void) callBackWithStatus: (port_status_t) status
90{
91    fStatus = status;
92   
93    if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
94        [fDelegate performSelectorOnMainThread: @selector(portCheckerDidFinishProbing:) withObject: self waitUntilDone: NO];
95}
96
97#pragma mark NSURLConnection delegate methods
98
99- (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
100{
101    [fPortProbeData setLength: 0];
102}
103
104- (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
105{
106    [fPortProbeData appendData: data];
107}
108
109- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
110{
111    NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
112    [self callBackWithStatus: PORT_STATUS_ERROR];
113}
114
115- (void) connectionDidFinishLoading: (NSURLConnection *) connection
116{
117    NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSUTF8StringEncoding];
118    [fPortProbeData release];
119    fPortProbeData = nil;
120   
121    if (probeString)
122    {
123        if ([probeString isEqualToString: @"1"])
124            [self callBackWithStatus: PORT_STATUS_OPEN];
125        else if ([probeString isEqualToString: @"0"])
126            [self callBackWithStatus: PORT_STATUS_CLOSED];
127        else
128        {
129            NSLog(@"Unable to get port status: invalid response");
130            [self callBackWithStatus: PORT_STATUS_ERROR];
131        }
132        [probeString release];
133    }
134    else
135    {
136        NSLog(@"Unable to get port status: invalid data received");
137        [self callBackWithStatus: PORT_STATUS_ERROR];
138    }
139}
140
141@end
Note: See TracBrowser for help on using the repository browser.