forked from karelia/MockServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMSServer.m
260 lines (202 loc) · 6.64 KB
/
KMSServer.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// Created by Sam Deane on 06/11/2012.
// Copyright 2012 Karelia Software. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <sys/socket.h>
#import <netinet/in.h> // for IPPROTO_TCP, sockaddr_in
#import "KMSServer.h"
#import "KMSConnection.h"
#import "KMSListener.h"
#import "KMSRegExResponder.h"
#import "KMSSendDataCommand.h"
#import "KMSPauseCommand.h"
#import "KMSCloseCommand.h"
@interface KMSServer()
@property (strong, nonatomic) NSMutableArray* connections;
@property (strong, nonatomic) KMSListener* dataListener;
@property (strong, nonatomic) KMSListener* listener;
@property (strong, nonatomic) NSDateFormatter* rfc1123DateFormatter;
@end
@implementation KMSServer
@synthesize connections = _connections;
@synthesize data = _data;
@synthesize dataListener = _dataListener;
@synthesize listener = _listener;
@synthesize queue = _queue;
@synthesize responder = _responder;
@synthesize rfc1123DateFormatter = _rfc1123DateFormatter;
@synthesize running = _running;
@synthesize transcript = _transcript;
NSString *const CloseCommandToken = @"«close»";
NSString *const DataCommandToken = @"«data»";
NSString *const InitialResponsePattern = @"«initial»";
#pragma mark - Object Lifecycle
+ (KMSServer*)serverWithResponder:(KMSResponder*)responder
{
KMSServer* server = [[KMSServer alloc] initWithPort:0 responder:responder];
return [server autorelease];
}
+ (KMSServer*)serverWithPort:(NSUInteger)port responder:(KMSResponder*)responder
{
KMSServer* server = [[KMSServer alloc] initWithPort:port responder:responder];
return [server autorelease];
}
- (id)initWithPort:(NSUInteger)port responder:(KMSResponder*)responder
{
NSAssert(responder != nil, @"should be given a valid responder");
if ((self = [super init]) != nil)
{
self.queue = dispatch_queue_create("com.karelia.mockserver", 0);
self.responder = responder;
self.connections = [NSMutableArray array];
self.transcript = [NSMutableArray array];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
self.rfc1123DateFormatter = formatter;
self.listener = [KMSListener listenerWithPort:port connectionBlock:^BOOL(int socket) {
KMSAssert(socket != 0);
KMSLogDetail(@"received connection");
dispatch_async(dispatch_get_main_queue(), ^{
KMSConnection* connection = [KMSConnection connectionWithSocket:socket responder:nil server:self];
dispatch_async(self.queue, ^{
[self.connections addObject:connection];
KMSLogDetail(@"connection added %@", connection);
});
});
return YES;
}];
}
return self;
}
- (void)dealloc
{
dispatch_release(_queue);
_queue = nil;
[_connections release];
[_data release];
[_dataListener release];
[_responder release];
[_transcript release];
[super dealloc];
}
#pragma mark - Public API
- (void)start
{
BOOL success = [self.listener start];
if (success)
{
[self makeDataListener];
KMSAssert(self.port != 0);
KMSLog(@"server started on port %ld", self.port);
self.state = KMSRunning;
}
}
- (void)pause
{
KMSLogDetail(@"pause requested");
self.state = KMSPauseRequested;
}
- (void)resume
{
KMSAssert(self.state != KMSPauseRequested);
KMSLogDetail(@"resumed");
self.state = KMSRunning;
}
- (BOOL)isRunning
{
return self.state == KMSRunning;
}
- (void)stop
{
KMSLogDetail(@"stop requested");
[self stopConnections];
}
- (void)stopConnections
{
[self.listener stop:@"stopped externally"];
[self.dataListener stop:@"stopped externally"];
for (KMSConnection* connection in self.connections)
{
[connection cancel];
}
self.state = KMSStopped;
}
- (void)runUntilPaused
{
KMSAssert(self.state != KMSStopped);
KMSLogDetail(@"running until paused");
while (self.state != KMSPauseRequested)
{
@autoreleasepool {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];
}
}
self.state = KMSPaused;
KMSLogDetail(@"now paused");
}
- (NSUInteger)port
{
return self.listener.port;
}
#pragma mark - Substitutions
- (NSDictionary*)standardSubstitutions
{
NSUInteger extraPort = self.dataListener.port;
NSDictionary* substitutions =
@{
@"$address" : @"127.0.0.1",
@"$server" : @"fakeserver 20121107",
@"$size" : [NSString stringWithFormat:@"%ld", (long) [self.data length]],
@"$pasv" : [NSString stringWithFormat:@"127,0,0,1,%ld,%ld", extraPort / 256L, extraPort % 256L],
@"$date" : [self.rfc1123DateFormatter stringFromDate:[NSDate date]],
};
return substitutions;
}
#pragma mark - Data Connection
- (void)makeDataListener
{
dispatch_async(self.queue, ^{
__block KMSServer* server = self;
self.dataListener = [KMSListener listenerWithPort:0 connectionBlock:^BOOL(int socket) {
KMSLogDetail(@"got connection on data listener");
NSData* data = server.data;
if (!data)
{
data = [@"Test data" dataUsingEncoding:NSUTF8StringEncoding];
}
NSArray* responses = @[ @[InitialResponsePattern, [KMSSendDataCommand sendData:data], [KMSPauseCommand pauseFor:0.1], [KMSCloseCommand closeCommand] ] ];
KMSRegExResponder* responder = [KMSRegExResponder responderWithResponses:responses];
KMSConnection* connection = [KMSConnection connectionWithSocket:socket responder:responder server:server];
[self.connections addObject:connection];
return YES;
}];
[self.dataListener start];
});
}
- (void)disposeDataListener
{
}
#pragma mark - Streams
- (void)connectionDidClose:(KMSConnection*)connection
{
NSAssert([self.connections indexOfObject:connection] != NSNotFound, @"connection should be in our list");
dispatch_async(self.queue, ^{
KMSLogDetail(@"connection %@ closed", connection);
[self.connections removeObject:connection];
});
}
#pragma mark - Logging
static KMSLogLevel gLoggingLevel = KMSLoggingOff;
+ (KMSLogLevel)loggingLevel
{
return gLoggingLevel;
}
+ (void)setLoggingLevel:(KMSLogLevel)level
{
gLoggingLevel = level;
}
@end