transmission: update from 2.13 to 2.22
[tomato.git] / release / src / router / transmission / macosx / PortChecker.m
blobb4c217beeac6cefd559095c323e12a994fdf997c
1 /******************************************************************************
2  * $Id: PortChecker.m 11617 2011-01-01 20:42:14Z livings124 $
3  *
4  * Copyright (c) 2006-2011 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  *****************************************************************************/
25 #import "PortChecker.h"
27 #define CHECKER_URL(port) [NSString stringWithFormat: @"http://portcheck.transmissionbt.com/%d", port]
28 #define CHECK_FIRE 3.0
30 @interface PortChecker (Private)
32 - (void) startProbe: (NSTimer *) timer;
34 - (void) callBackWithStatus: (port_status_t) status;
36 @end
38 @implementation PortChecker
40 - (id) initForPort: (NSInteger) portNumber delay: (BOOL) delay withDelegate: (id) delegate
42     if ((self = [super init]))
43     {
44         fDelegate = delegate;
45         
46         fStatus = PORT_STATUS_CHECKING;
47         
48         fTimer = [NSTimer scheduledTimerWithTimeInterval: CHECK_FIRE target: self selector: @selector(startProbe:)
49                     userInfo: [NSNumber numberWithInteger: portNumber] repeats: NO];
50         if (!delay)
51             [fTimer fire];
52     }
53     
54     return self;
57 - (void) dealloc
59     [fTimer invalidate];
60     
61     [fConnection release];
62     [fPortProbeData release];
63     [super dealloc];
66 - (port_status_t) status
68     return fStatus;
71 - (void) cancelProbe
73     [fTimer invalidate];
74     fTimer = nil;
75     
76     [fConnection cancel];
79 - (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
81     [fPortProbeData setLength: 0];
84 - (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
86     [fPortProbeData appendData: data];
89 - (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
91     NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
92     [self callBackWithStatus: PORT_STATUS_ERROR];
95 - (void) connectionDidFinishLoading: (NSURLConnection *) connection
97     NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSUTF8StringEncoding];
98     [fPortProbeData release];
99     fPortProbeData = nil;
100     
101     if (probeString)
102     {
103         if ([probeString isEqualToString: @"1"])
104             [self callBackWithStatus: PORT_STATUS_OPEN];
105         else if ([probeString isEqualToString: @"0"])
106             [self callBackWithStatus: PORT_STATUS_CLOSED];
107         else
108         {
109             NSLog(@"Unable to get port status: invalid response (%@)", probeString);
110             [self callBackWithStatus: PORT_STATUS_ERROR];
111         }
112         [probeString release];
113     }
114     else
115     {
116         NSLog(@"Unable to get port status: invalid data received");
117         [self callBackWithStatus: PORT_STATUS_ERROR];
118     }
121 @end
123 @implementation PortChecker (Private)
125 - (void) startProbe: (NSTimer *) timer
127     fTimer = nil;
128     
129     NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: CHECKER_URL([[timer userInfo] integerValue])]
130                                         cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 15.0];
131     
132     if ((fConnection = [[NSURLConnection alloc] initWithRequest: portProbeRequest delegate: self]))
133         fPortProbeData = [[NSMutableData alloc] init];
134     else
135     {
136         NSLog(@"Unable to get port status: failed to initiate connection");
137         [self callBackWithStatus: PORT_STATUS_ERROR];
138     }
141 - (void) callBackWithStatus: (port_status_t) status
143     fStatus = status;
144     
145     if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
146         [fDelegate performSelectorOnMainThread: @selector(portCheckerDidFinishProbing:) withObject: self waitUntilDone: NO];
149 @end