-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBBDemoController.m
146 lines (125 loc) · 4.12 KB
/
BBDemoController.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
//
// BBDemoController.m
// Blackbox
//
// Created by Matt Patenaude on 3/15/10.
// Copyright 2010 Matt Patenaude. All rights reserved.
//
#import "BBDemoController.h"
#import "BBRequest.h"
#import "BBDemoLiveResponder.h"
#import <SystemConfiguration/SystemConfiguration.h>
@implementation BBDemoController
#pragma mark Initializers
- (id)init
{
if (self = [super init])
{
// let's create our server
server = [[BBServer alloc] init];
// we'll set ourself as both the delegate,
// and the default handler of HTTP requests
[server setDelegate:self];
[server setDefaultResponder:self];
// we'll also set a default port; if we
// don't do this, a random one will be chosen
// the default port is specified in BBDemoController.h
[server setPort:BBDemoServerDefaultPort];
serverIsRunning = NO;
}
return self;
}
- (void)awakeFromNib
{
// get computer name
NSString *computerName = (NSString *)SCDynamicStoreCopyComputerName(NULL, NULL);
// setup our default (placeholder) values in the UI
[[serverPortField cell] setPlaceholderString:[NSString stringWithFormat:@"%u", BBDemoServerDefaultPort]];
[[bonjourNameField cell] setPlaceholderString:computerName];
CFRelease((CFStringRef)computerName);
// finally, we set our "live responder" to respond to
// the path "/textbox"
[server setResponder:textBoxResponder forPath:@"/textbox"];
// if we're on 10.6, we can also use blocks!
// visit "/blocks" to see an example
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
[server setHandlerForPath:@"/blocks" handler:^(BBRequest *theRequest){
[theRequest setResponseContentType:@"text/html"];
[theRequest setResponseString:@"<h1>Blocks Demo</h1><p>This response was returned using a block.</p>"];
}];
#endif
}
#pragma mark Deallocator
- (void)dealloc
{
[server release];
[super dealloc];
}
#pragma mark Application delegate methods
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
return YES;
}
#pragma mark Control methods
- (IBAction)startStopServer:(id)sender
{
if (!serverIsRunning)
{
// let's configure the server first
// first, the port
[server setPort:BBDemoServerDefaultPort];
NSString *portString = [serverPortField stringValue];
if (![portString isEqualToString:@""])
[server setPort:[portString integerValue]];
// next, the Bonjour service
[server setName:@""];
[server setType:nil];
if ([publishBonjourService state] == NSOnState)
{
NSString *bonjourName = [bonjourNameField stringValue];
NSString *serviceType = [bonjourTypeField stringValue];
if (![bonjourName isEqualToString:@""])
[server setName:bonjourName];
if (![serviceType isEqualToString:@""])
[server setType:serviceType];
else
[server setType:@"_http._tcp."];
}
// now, we start the server
NSError *err;
if ([server start:&err])
{
[statusImageView setImage:[NSImage imageNamed:@"on"]];
[statusField setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Running On Port %u…", nil), [server port]]];
serverIsRunning = YES;
}
else
{
[statusImageView setImage:[NSImage imageNamed:@"off"]];
[statusField setStringValue:NSLocalizedString(@"Could Not Start Server", nil)];
serverIsRunning = NO;
NSLog(@"Error starting server: %@", [err localizedDescription]);
}
}
else
{
[server stop];
[statusImageView setImage:[NSImage imageNamed:@"off"]];
[statusField setStringValue:NSLocalizedString(@"Not Running", nil)];
serverIsRunning = NO;
}
}
#pragma mark Responder methods (BBResponder)
- (void)handleRequest:(BBRequest *)theRequest
{
// We get here if someone tried to access a URL we
// don't have a responder setup for (because this object is
// set as the "default responder")
// We'll return a 404, put up an error message, and
// print out the URL the user tried to access to the Console
[theRequest setResponseStatusCode:404];
[theRequest setResponseContentType:@"text/html"];
[theRequest setResponseString:@"<h1>404 File Not Found</h1><p>Perhaps you were looking for <a href=\"/textbox\">the live textbox demo</a>?</p>"];
NSLog(@"Request: someone tried to access %@", [theRequest fullPath]);
}
@end