Core Foundation Sockets  v0.2(28.0)
 All Classes Files Functions Variables Properties
ServerTests.m
Go to the documentation of this file.
1 // CFSockets ServerTests.m
2 //
3 // Copyright © 2012, 2013, Roy Ratcliffe, Pioneering Software, United Kingdom
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the “Software”), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER
16 // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
18 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22 //
23 //------------------------------------------------------------------------------
24 
25 #import "ServerTests.h"
26 
27 #import <CFSockets/CFSockets.h>
28 
29 @interface ServerTests()
30 
31 @property(strong, NS_NONATOMIC_IOSONLY) NSMutableSet *streamPairs;
32 
33 @end
34 
35 @implementation ServerTests
36 
37 @synthesize streamPairs;
38 
39 - (void)setUp
40 {
41  [self setStreamPairs:[NSMutableSet set]];
42 }
43 
45 {
46  NSError *__autoreleasing error = nil;
47 
48  CFSocket *serverSocket = [[CFSocket alloc] initForTCPv6];
49  STAssertNotNil(serverSocket, nil);
50  STAssertTrue([serverSocket setReuseAddressOption:YES], nil);
51  STAssertTrue([serverSocket setAddress:CFSocketAddressDataFromLoopBackIPv6WithPort(54321) error:&error], nil);
52  STAssertNil(error, nil);
53  STAssertEquals([serverSocket addressFamily], AF_INET6, nil);
54  STAssertEquals([serverSocket port], 54321, nil);
55 
56  // Run the server socket in a run-loop for 10 seconds. Make a connection to
57  // the server using "telnet localhost 54321" at the command line. Enter a
58  // line and you will see your message replied with a cryptic prefix. Beware
59  // the Master Control Program!
60  [serverSocket setDelegate:self];
61  [serverSocket addToCurrentRunLoopForCommonModes];
62  do
63  {
64  [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];
65  }
66  while ([[self streamPairs] count]);
67 }
68 
69 - (void)socket:(CFSocket *)socket acceptStreamPair:(CFStreamPair *)streamPair
70 {
71  // Important to retain the stream pair. Otherwise ARC will de-allocate and
72  // reclaim its space.
73  [[self streamPairs] addObject:streamPair];
74  [streamPair setDelegate:self];
75  [streamPair open];
76 }
77 
78 - (void)streamPair:(CFStreamPair *)streamPair hasBytesAvailable:(NSUInteger)bytesAvailable
79 {
80  NSString *line = [streamPair receiveLineUsingEncoding:NSUTF8StringEncoding];
81  if (line)
82  {
83  [streamPair sendBytes:[[NSString stringWithFormat:@"MCP: %@", line] dataUsingEncoding:NSUTF8StringEncoding]];
84  }
85 }
86 
87 - (void)streamPair:(CFStreamPair *)streamPair handleRequestEvent:(NSStreamEvent)eventCode
88 {
89  switch (eventCode)
90  {
91  case NSStreamEventEndEncountered:
92  // Important to un-retain the stream pair after the other end
93  // terminates the connection. No point holding on to it. ARC will
94  // automatically release it, and the stream pair will automatically
95  // close itself.
96  [[self streamPairs] removeObject:streamPair];
97  break;
98  default:
99  ;
100  }
101 }
102 
103 @end