source: trunk/macosx/PortChecker.m @ 4205

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

ensure that a single portchecker can be used for only a single check

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