-
Notifications
You must be signed in to change notification settings - Fork 5
/
RTConnection.m
180 lines (159 loc) · 4.62 KB
/
RTConnection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/******************************************************************************
* Nativa - MacOS X UI for rtorrent
* http://www.aramzamzam.net
*
* Copyright Solomenchuk V. 2010.
* Solomenchuk Vladimir <[email protected]>
*
* Licensed under the GPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#import "RTConnection.h"
#import "AMSession.h"
static NSString* ProxyConnectedContext = @"ProxyConnectedContext";
@implementation RTConnection
@synthesize connected = _connected;
@synthesize connecting = _connecting;
@dynamic error;
- (id)initWithHostPort:(NSString *)initHost port:(int)initPort proxy:(AMSession*) proxy;
{
hostName = [initHost retain];
port = initPort;
_connecting = NO;
_connected = NO;
_proxy = [proxy retain];
return self;
}
- (BOOL) openStreams:(NSInputStream **)iStream oStream:(NSOutputStream **) oStream delegate:(id) delegate error:(NSString **) connectionError
{
if (!_connected)
{
*connectionError = [NSString stringWithString:@"Not connected"];
return NO;
}
NSHost *host = [NSHost hostWithAddress:hostName];
if (host != nil)
{
[NSStream getStreamsToHost:host
port:(_proxy==nil?port:[_proxy localPort])
inputStream:iStream
outputStream:oStream];
[(*iStream) scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
(*iStream).delegate = delegate;
[(*oStream) scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
(*oStream).delegate = delegate;
[(*oStream) open];
[(*iStream) open];
return YES;
}
*connectionError = [NSString stringWithFormat:@"Unable to resolve host: %@",hostName];
return NO;
}
-(void) closeConnection
{
[self willChangeValueForKey:@"connecting"];
[self willChangeValueForKey:@"connected"];
_connected = NO;
_connecting = NO;
[self didChangeValueForKey:@"connecting"];
[self didChangeValueForKey:@"connected"];
if (_proxy != nil)
{
@try {
[_proxy removeObserver:self forKeyPath:@"connected"];
}
@catch (NSException *exception) {
//ignore objserver removal exception
}
[_proxy closeTunnel];
}
[self setError:nil];
}
-(void) openConnection:(void (^)(RTConnection *sender))handler
{
if (hostName == nil || [hostName isEqualToString:@""])
{
[self setError:@"SCGI host cannot be empty"];
if (handler)
handler(self);
return;
}
if (_proxy == nil)
{
[self willChangeValueForKey:@"connecting"];
[self willChangeValueForKey:@"connected"];
_connected = YES;
_connecting = NO;
[self didChangeValueForKey:@"connecting"];
[self didChangeValueForKey:@"connected"];
if (handler != nil)
handler(self);
}
else
{
[_proxy addObserver:self
forKeyPath:@"connected"
options:0
context:&ProxyConnectedContext];
[_proxy openTunnel:^(AMSession *sender){
if (handler != nil)
handler(self);
}];
}
}
-(void)dealloc;
{
[hostName release];
[_proxy release];
[self setError:nil];
[super dealloc];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (context == &ProxyConnectedContext)
{
[self willChangeValueForKey:@"connecting"];
[self willChangeValueForKey:@"connected"];
_connected = _proxy.connected;
_connecting =_proxy.connecting;
[self didChangeValueForKey:@"connected"];
[self didChangeValueForKey:@"connecting"];
}
else
{
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
-(void) setError:(NSString *) err
{
if (err == error)
return;
[err release];
error = [err retain];
}
-(NSString*)error
{
if (error != nil)
return error;
if (_proxy != nil)
return _proxy.error;
return nil;
}
@end