source: trunk/macosx/PortChecker.m @ 8227

Last change on this file since 8227 was 7659, checked in by livings124, 13 years ago

update the Mac code's copyright dates

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