source: trunk/macosx/PortChecker.m @ 4207

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

fix typo in last commit

  • 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 4207 2007-12-18 21:13:36Z 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@implementation PortChecker
29
30- (id) initForPort: (int) portNumber withDelegate: (id) delegate
31{
32    if ((self = [super init]))
33    {
34        fDelegate = delegate;
35       
36        NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
37                [NSString stringWithFormat: @"http://transmission.m0k.org/PortCheck.php?port=%d", portNumber]] cachePolicy:
38                [NSApp isOnLeopardOrBetter] ? NSURLRequestReloadIgnoringLocalCacheData : NSURLRequestReloadIgnoringCacheData
39                timeoutInterval: 15.0];
40       
41        fStatus = PORT_STATUS_CHECKING;
42       
43        if ((fConnection = [[NSURLConnection alloc] initWithRequest: portProbeRequest delegate: self]))
44            fPortProbeData = [[NSMutableData alloc] init];
45        else
46        {
47            NSLog(@"Unable to get port status: failed to initiate connection");
48            [self callBackWithStatus: PORT_STATUS_ERROR];
49        }
50    }
51   
52    return self;
53}
54
55- (void) dealloc
56{
57    [fConnection release];
58    [fPortProbeData release];
59    [super dealloc];
60}
61
62- (port_status_t) status
63{
64    return fStatus;
65}
66
67- (void) endProbe
68{
69    [fConnection cancel];
70}
71
72- (void) callBackWithStatus: (port_status_t) status
73{
74    fStatus = status;
75   
76    if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
77        [fDelegate performSelectorOnMainThread: @selector(portCheckerDidFinishProbing:) withObject: self waitUntilDone: NO];
78}
79
80#pragma mark NSURLConnection delegate methods
81
82- (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
83{
84    [fPortProbeData setLength: 0];
85}
86
87- (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
88{
89    [fPortProbeData appendData: data];
90}
91
92- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
93{
94    NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
95    [self callBackWithStatus: PORT_STATUS_ERROR];
96}
97
98- (void) connectionDidFinishLoading: (NSURLConnection *) connection
99{
100    NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSASCIIStringEncoding];
101   
102    port_status_t status;
103    if ([probeString isEqualToString: @"1"])
104        status = PORT_STATUS_OPEN;
105    else if ([probeString isEqualToString: @"0"])
106        status = PORT_STATUS_CLOSED;
107    else
108        status = PORT_STATUS_ERROR;
109   
110    [self callBackWithStatus: status];
111    [probeString release];
112}
113
114@end
Note: See TracBrowser for help on using the repository browser.