-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHBDemoController.m
152 lines (133 loc) · 4.44 KB
/
HBDemoController.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
//
// HBDemoController.m
// Blackbox
//
// Created by Matt Patenaude on 8/24/10.
// Copyright 2010 Matt Patenaude. All rights reserved.
//
#import "HBDemoController.h"
#import "BBRequest.h"
@implementation HBDemoController
#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;
// create an array to handle open connections
openConnections = [[NSMutableArray alloc] init];
connectionNames = [[NSMutableArray alloc] init];
}
return self;
}
- (void)awakeFromNib
{
// finally, we set our HaleBopp responder to respond to
// the path "/poll"
[server setResponder:haleBoppResponder forPath:@"/poll"];
}
#pragma mark Deallocator
- (void)dealloc
{
[server release];
[openConnections release];
[connectionNames release];
[super dealloc];
}
#pragma mark Application delegate methods
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
return YES;
}
#pragma mark Control methods
- (IBAction)startStopServer:(id)sender
{
if (!serverIsRunning)
{
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;
}
}
- (IBAction)pushStringToSelectedConnection:(id)sender
{
// this pushes the string to the selected connection
NSInteger selection = [connectionTable selectedRow];
if (selection > -1)
[haleBoppResponder pushResponseString:[pushStringField stringValue] toRequestWithIdentifier:[openConnections objectAtIndex:selection]];
}
#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")
// If they try to access "/comet", we'll put up our sample
// HTML file, which calls "/poll" to test the Comet server
if ([[theRequest fullPath] isEqualToString:@"/comet"])
[theRequest respondWithFile:[[NSBundle mainBundle] pathForResource:@"comet" ofType:@"html"]];
else
{
// 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=\"/comet\">the Comet demo</a>?</p>"];
NSLog(@"Request: someone tried to access %@", [theRequest fullPath]);
}
}
#pragma mark HaleBopp delegate methods (HBResponderDelegate)
- (void)responder:(HBResponder *)responder startedLongPollWithRequest:(BBRequest *)theRequest identifier:(NSString *)theIdentifier
{
[openConnections addObject:theIdentifier];
// add a name if one is passed as a param
// otherwise, just use the identifier
NSString *name = [[theRequest GETParameters] objectForKey:@"name"];
[connectionNames addObject:((name) ? name : theIdentifier)];
[connectionTable reloadData];
}
- (void)responder:(HBResponder *)responder requestNoLongerAvailableWithIdentifier:(NSString *)theIdentifier
{
NSInteger idx = [openConnections indexOfObject:theIdentifier];
[openConnections removeObjectAtIndex:idx];
[connectionNames removeObjectAtIndex:idx];
[connectionTable reloadData];
}
#pragma mark Table data source methods
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [openConnections count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return [connectionNames objectAtIndex:rowIndex];
}
@end